UNPKG

6.01 kBTypeScriptView Raw
1/**
2 * Parse options.
3 */
4export interface ParseOptions {
5 /**
6 * Specifies a function that will be used to decode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
7 * Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode
8 * a previously-encoded cookie value into a JavaScript string.
9 *
10 * The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error
11 * is thrown it will return the cookie's original value. If you provide your own encode/decode
12 * scheme you must ensure errors are appropriately handled.
13 *
14 * @default decode
15 */
16 decode?: (str: string) => string | undefined;
17}
18/**
19 * Parse a cookie header.
20 *
21 * Parse the given cookie header string into an object
22 * The object has the various cookies as keys(names) => values
23 */
24export declare function parse(str: string, options?: ParseOptions): Record<string, string | undefined>;
25/**
26 * Serialize options.
27 */
28export interface SerializeOptions {
29 /**
30 * Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
31 * Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
32 * a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
33 *
34 * @default encodeURIComponent
35 */
36 encode?: (str: string) => string;
37 /**
38 * Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
39 *
40 * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
41 * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
42 * so if both are set, they should point to the same date and time.
43 */
44 maxAge?: number;
45 /**
46 * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
47 * When no expiration is set clients consider this a "non-persistent cookie" and delete it the current session is over.
48 *
49 * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
50 * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
51 * so if both are set, they should point to the same date and time.
52 */
53 expires?: Date;
54 /**
55 * Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
56 * When no domain is set clients consider the cookie to apply to the current domain only.
57 */
58 domain?: string;
59 /**
60 * Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
61 * When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
62 */
63 path?: string;
64 /**
65 * Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
66 * When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
67 */
68 httpOnly?: boolean;
69 /**
70 * Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
71 * When enabled, clients will only send the cookie back if the browser has a HTTPS connection.
72 */
73 secure?: boolean;
74 /**
75 * Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
76 * When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
77 *
78 * This is an attribute that has not yet been fully standardized, and may change in the future.
79 * This also means clients may ignore this attribute until they understand it. More information
80 * about can be found in [the proposal](https://github.com/privacycg/CHIPS).
81 */
82 partitioned?: boolean;
83 /**
84 * Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
85 *
86 * - `'low'` will set the `Priority` attribute to `Low`.
87 * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
88 * - `'high'` will set the `Priority` attribute to `High`.
89 *
90 * More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
91 */
92 priority?: "low" | "medium" | "high";
93 /**
94 * Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
95 *
96 * - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
97 * - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
98 * - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
99 * - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
100 *
101 * More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
102 */
103 sameSite?: boolean | "lax" | "strict" | "none";
104}
105/**
106 * Serialize data into a cookie header.
107 *
108 * Serialize a name value pair into a cookie string suitable for
109 * http headers. An optional options object specifies cookie parameters.
110 *
111 * serialize('foo', 'bar', { httpOnly: true })
112 * => "foo=bar; httpOnly"
113 */
114export declare function serialize(name: string, val: string, options?: SerializeOptions): string;