UNPKG

4.15 kBTypeScriptView Raw
1// Type definitions for js-cookie 2.2
2// Project: https://github.com/js-cookie/js-cookie
3// Definitions by: Theodore Brown <https://github.com/theodorejb>
4// BendingBender <https://github.com/BendingBender>
5// Antoine Lépée <https://github.com/alepee>
6// Yuto Doi <https://github.com/yutod>
7// Nicolas Reynis <https://github.com/nreynis>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.3
10
11declare namespace Cookies {
12 interface CookieAttributes {
13 /**
14 * Define when the cookie will be removed. Value can be a Number
15 * which will be interpreted as days from time of creation or a
16 * Date instance. If omitted, the cookie becomes a session cookie.
17 */
18 expires?: number | Date | undefined;
19
20 /**
21 * Define the path where the cookie is available. Defaults to '/'
22 */
23 path?: string | undefined;
24
25 /**
26 * Define the domain where the cookie is available. Defaults to
27 * the domain of the page where the cookie was created.
28 */
29 domain?: string | undefined;
30
31 /**
32 * A Boolean indicating if the cookie transmission requires a
33 * secure protocol (https). Defaults to false.
34 */
35 secure?: boolean | undefined;
36
37 /**
38 * Asserts that a cookie must not be sent with cross-origin requests,
39 * providing some protection against cross-site request forgery
40 * attacks (CSRF)
41 */
42 sameSite?: 'strict' | 'Strict' | 'lax' | 'Lax' | 'none' | 'None' | undefined;
43
44 /**
45 * An attribute which will be serialized, conformably to RFC 6265
46 * section 5.2.
47 */
48 [property: string]: any;
49 }
50
51 interface CookiesStatic<T extends object = object> {
52 /**
53 * Allows default cookie attributes to be accessed, changed, or reset
54 */
55 defaults: CookieAttributes;
56
57 /**
58 * Create a cookie
59 */
60 set(name: string, value: string | T, options?: CookieAttributes): string | undefined;
61
62 /**
63 * Read cookie
64 */
65 get(name: string): string | undefined;
66
67 /**
68 * Read all available cookies
69 */
70 get(): {[key: string]: string};
71
72 /**
73 * Returns the parsed representation of the string
74 * stored in the cookie according to JSON.parse
75 */
76 getJSON(name: string): any;
77
78 /**
79 * Returns the parsed representation of
80 * all cookies according to JSON.parse
81 */
82 getJSON(): {[key: string]: any};
83
84 /**
85 * Delete cookie
86 */
87 remove(name: string, options?: CookieAttributes): void;
88
89 /**
90 * If there is any danger of a conflict with the namespace Cookies,
91 * the noConflict method will allow you to define a new namespace
92 * and preserve the original one. This is especially useful when
93 * running the script on third party sites e.g. as part of a widget
94 * or SDK. Note: The noConflict method is not necessary when using
95 * AMD or CommonJS, thus it is not exposed in those environments.
96 */
97 noConflict?(): CookiesStatic<T>;
98
99 /**
100 * Create a new instance of the api that overrides the default
101 * decoding implementation. All methods that rely in a proper
102 * decoding to work, such as Cookies.remove() and Cookies.get(),
103 * will run the converter first for each cookie. The returned
104 * string will be used as the cookie value.
105 */
106 withConverter<TConv extends object>(converter: CookieReadConverter | { write?: CookieWriteConverter<TConv> | undefined; read?: CookieReadConverter | undefined; }): CookiesStatic<TConv>;
107 }
108
109 type CookieWriteConverter<T extends object> = (value: string | T, name: string) => string;
110 type CookieReadConverter = (value: string, name: string) => string;
111}
112
113declare const Cookies: Cookies.CookiesStatic;
114
115export = Cookies;
116export as namespace Cookies;