From 887a2752a3622d147f501ab7f2689fa47969263a Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen Date: Tue, 14 Aug 2018 22:26:18 +0300 Subject: [PATCH] Add typings for universal-cookie --- types/universal-cookie/index.d.ts | 102 ++++++++++++++++++ types/universal-cookie/tsconfig.json | 16 +++ types/universal-cookie/tslint.json | 1 + .../universal-cookie-tests.ts | 20 ++++ 4 files changed, 139 insertions(+) create mode 100644 types/universal-cookie/index.d.ts create mode 100644 types/universal-cookie/tsconfig.json create mode 100644 types/universal-cookie/tslint.json create mode 100644 types/universal-cookie/universal-cookie-tests.ts diff --git a/types/universal-cookie/index.d.ts b/types/universal-cookie/index.d.ts new file mode 100644 index 0000000000..4bba4adee6 --- /dev/null +++ b/types/universal-cookie/index.d.ts @@ -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 +// 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; +} diff --git a/types/universal-cookie/tsconfig.json b/types/universal-cookie/tsconfig.json new file mode 100644 index 0000000000..1c324ed8c6 --- /dev/null +++ b/types/universal-cookie/tsconfig.json @@ -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"] +} diff --git a/types/universal-cookie/tslint.json b/types/universal-cookie/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/universal-cookie/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/universal-cookie/universal-cookie-tests.ts b/types/universal-cookie/universal-cookie-tests.ts new file mode 100644 index 0000000000..9591fa3c5b --- /dev/null +++ b/types/universal-cookie/universal-cookie-tests.ts @@ -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) => {});