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