UNPKG

7.13 kBTypeScriptView Raw
1// Type definitions for cookie 0.5
2// Project: https://github.com/jshttp/cookie
3// Definitions by: Pine Mizune <https://github.com/pine>
4// Piotr Błażejewicz <https://github.com/peterblazejewicz>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
7/**
8 * Basic HTTP cookie parser and serializer for HTTP servers.
9 */
10
11/**
12 * Additional serialization options
13 */
14export interface CookieSerializeOptions {
15 /**
16 * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
17 * domain is set, and most clients will consider the cookie to apply to only
18 * the current domain.
19 */
20 domain?: string | undefined;
21
22 /**
23 * Specifies a function that will be used to encode a cookie's value. Since
24 * value of a cookie has a limited character set (and must be a simple
25 * string), this function can be used to encode a value into a string suited
26 * for a cookie's value.
27 *
28 * The default function is the global `encodeURIComponent`, which will
29 * encode a JavaScript string into UTF-8 byte sequences and then URL-encode
30 * any that fall outside of the cookie range.
31 */
32 encode?(value: string): string;
33
34 /**
35 * Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
36 * no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
37 * it on a condition like exiting a web browser application.
38 *
39 * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
40 * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
41 * possible not all clients by obey this, so if both are set, they should
42 * point to the same date and time.
43 */
44 expires?: Date | undefined;
45 /**
46 * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
47 * When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
48 * default, the `HttpOnly` attribute is not set.
49 *
50 * *Note* be careful when setting this to true, as compliant clients will
51 * not allow client-side JavaScript to see the cookie in `document.cookie`.
52 */
53 httpOnly?: boolean | undefined;
54 /**
55 * Specifies the number (in seconds) to be the value for the `Max-Age`
56 * `Set-Cookie` attribute. The given number will be converted to an integer
57 * by rounding down. By default, no maximum age is set.
58 *
59 * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
60 * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
61 * possible not all clients by obey this, so if both are set, they should
62 * point to the same date and time.
63 */
64 maxAge?: number | undefined;
65 /**
66 * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
67 * By default, the path is considered the "default path".
68 */
69 path?: string | undefined;
70 /**
71 * Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].
72 *
73 * - `'low'` will set the `Priority` attribute to `Low`.
74 * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
75 * - `'high'` will set the `Priority` attribute to `High`.
76 *
77 * More information about the different priority levels can be found in
78 * [the specification][rfc-west-cookie-priority-00-4.1].
79 *
80 * **note** This is an attribute that has not yet been fully standardized, and may change in the future.
81 * This also means many clients may ignore this attribute until they understand it.
82 */
83 priority?: 'low' | 'medium' | 'high' | undefined;
84 /**
85 * Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
86 *
87 * - `true` will set the `SameSite` attribute to `Strict` for strict same
88 * site enforcement.
89 * - `false` will not set the `SameSite` attribute.
90 * - `'lax'` will set the `SameSite` attribute to Lax for lax same site
91 * enforcement.
92 * - `'strict'` will set the `SameSite` attribute to Strict for strict same
93 * site enforcement.
94 * - `'none'` will set the SameSite attribute to None for an explicit
95 * cross-site cookie.
96 *
97 * More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
98 *
99 * *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
100 */
101 sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined;
102 /**
103 * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
104 * `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
105 *
106 * *Note* be careful when setting this to `true`, as compliant clients will
107 * not send the cookie back to the server in the future if the browser does
108 * not have an HTTPS connection.
109 */
110 secure?: boolean | undefined;
111}
112
113/**
114 * Additional parsing options
115 */
116export interface CookieParseOptions {
117 /**
118 * Specifies a function that will be used to decode a cookie's value. Since
119 * the value of a cookie has a limited character set (and must be a simple
120 * string), this function can be used to decode a previously-encoded cookie
121 * value into a JavaScript string or other object.
122 *
123 * The default function is the global `decodeURIComponent`, which will decode
124 * any URL-encoded sequences into their byte representations.
125 *
126 * *Note* if an error is thrown from this function, the original, non-decoded
127 * cookie value will be returned as the cookie's value.
128 */
129 decode?(value: string): string;
130}
131
132/**
133 * Parse an HTTP Cookie header string and returning an object of all cookie
134 * name-value pairs.
135 *
136 * @param str the string representing a `Cookie` header value
137 * @param [options] object containing parsing options
138 */
139export function parse(str: string, options?: CookieParseOptions): Record<string, string>;
140
141/**
142 * Serialize a cookie name-value pair into a `Set-Cookie` header string.
143 *
144 * @param name the name for the cookie
145 * @param value value to set the cookie to
146 * @param [options] object containing serialization options
147 * @throws {TypeError} when `maxAge` options is invalid
148 */
149export function serialize(name: string, value: string, options?: CookieSerializeOptions): string;