Merge pull request #28120 from tomi/master

Add typings for universal-cookie
This commit is contained in:
Benjamin Lichtman
2018-08-14 15:54:31 -07:00
committed by GitHub
4 changed files with 139 additions and 0 deletions
+102
View File
@@ -0,0 +1,102 @@
// Type definitions for universal-cookie 2.2
// Project: https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie#readme
// Definitions by: Tomi Turtiainen <https://github.com/tomi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
export interface CookiesByName {
[cookieName: string]: string;
}
export interface GetOpts {
/**
* do not convert the cookie into an object no matter what
*/
doNotParse: boolean;
}
export interface CookieOpts {
/**
* cookie path, use / as the path if you want your cookie to be accessible on all pages
*/
path?: string;
/**
* absolute expiration date for the cookie
*/
expires?: Date;
/**
* relative max age of the cookie from when the client receives it in second
*/
maxAge?: number;
/**
* domain for the cookie (sub.domain.com or .allsubdomains.com)
*/
domain?: string;
/**
* Is only accessible through HTTPS?
*/
secure?: boolean;
/**
* Is only the server can access the cookie?
*/
httpOnly?: boolean;
}
export type ChangeListenerCallback = (
name: string,
value: string | object,
options: CookieOpts
) => void;
export default class Cookies {
constructor(cookieHeader?: string | CookiesByName);
/**
* Get a cookie value
*
* @param name cookie name
* @param options.doNotParse do not convert the cookie into an object no matter what
*/
get(name: string, options?: GetOpts): string;
/**
* Get all cookies
*
* @param options.doNotParse do not convert the cookie into an object no matter what
*/
getAll(options?: GetOpts): CookiesByName;
/**
* Set a cookie value
*
* @param name cookie name
* @param value save the value and stringify the object if needed
* @param options Support all the cookie options from RFC 6265
*/
set(name: string, value: string | object, options?: CookieOpts): void;
/**
* Set a cookie value
*
* @param name cookie name
* @param options Support all the cookie options from RFC 6265
*/
remove(name: string, options?: CookieOpts): void;
/**
* Add a listener to when a cookie is set or removed.
*
* @param callback Call that will be called with the first argument containing name, value and options of the changed cookie.
*/
addChangeListener(callback: ChangeListenerCallback): void;
/**
* Remove a listener from the change callback.
*/
removeChangeListener(callback: ChangeListenerCallback): void;
}
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "universal-cookie-tests.ts"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
@@ -0,0 +1,20 @@
import Cookies from "universal-cookie";
const cookies = new Cookies();
new Cookies("My=Cookie");
new Cookies({});
cookies.set("my", "cookie");
cookies.set("object", { some: "value" });
cookies.set("with", "options", {
path: "/"
});
cookies.get("my");
cookies.getAll();
cookies.remove("object");
cookies.remove("object", { path: "/" });
cookies.addChangeListener((name, value) => {});
cookies.removeChangeListener((name, value) => {});