UNPKG

4.25 kBTypeScriptView Raw
1// Type definitions for js-cookie 3.0
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// Piotr Błażejewicz <https://github.com/peterblazejewicz>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
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 readonly attributes: CookieAttributes;
53 readonly converter: Required<Converter<T>>;
54 /**
55 * Create a cookie
56 */
57 set(name: string, value: string | T, options?: CookieAttributes): string | undefined;
58
59 /**
60 * Read cookie
61 */
62 get(name: string): string | undefined;
63
64 /**
65 * Read all available cookies
66 */
67 get(): { [key: string]: string };
68
69 /**
70 * Delete cookie
71 */
72 remove(name: string, options?: CookieAttributes): void;
73
74 /**
75 * Cookie attribute defaults can be set globally by creating an
76 * instance of the api via withAttributes(), or individually for
77 * each call to Cookies.set(...) by passing a plain object as the
78 * last argument. Per-call attributes override the default attributes.
79 */
80 withAttributes(attributes: CookieAttributes): CookiesStatic<T>;
81
82 /**
83 * Create a new instance of the api that overrides the default
84 * decoding implementation. All methods that rely in a proper
85 * decoding to work, such as Cookies.remove() and Cookies.get(),
86 * will run the converter first for each cookie. The returned
87 * string will be used as the cookie value.
88 */
89 withConverter<TConv extends object>(converter: Converter<TConv>): CookiesStatic<TConv>;
90 }
91
92 interface Converter<TConv extends object> {
93 write?: CookieWriteConverter<TConv> | undefined;
94 read?: CookieReadConverter | undefined;
95 }
96
97 type CookieWriteConverter<T extends object> = (value: string | T, name: string) => string;
98 type CookieReadConverter = (value: string, name: string) => string;
99}
100
101declare const Cookies: Cookies.CookiesStatic & {
102 /**
103 * If there is any danger of a conflict with the namespace Cookies,
104 * the noConflict method will allow you to define a new namespace
105 * and preserve the original one. This is especially useful when
106 * running the script on third party sites e.g. as part of a widget
107 * or SDK. Note: The noConflict method is not necessary when using
108 * AMD or CommonJS, thus it is not exposed in those environments.
109 */
110 noConflict?(): Cookies.CookiesStatic;
111};
112
113export = Cookies;
114export as namespace Cookies;