UNPKG

5.12 kBTypeScriptView Raw
1// Type definitions for cookie v0.3.0
2// Project: https://github.com/jshttp/cookie
3// Definitions by: Pine Mizune <https://github.com/pine613>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6interface CookieSerializeOptions {
7 /**
8 * Specifies the value for the Domain Set-Cookie attribute. By default, no
9 * domain is set, and most clients will consider the cookie to apply to only
10 * the current domain.
11 */
12 domain?: string;
13 /**
14 * Specifies a function that will be used to encode a cookie's value. Since
15 * value of a cookie has a limited character set (and must be a simple
16 * string), this function can be used to encode a value into a string suited
17 * for a cookie's value.
18 *
19 * The default function is the global `encodeURIComponent`, which will
20 * encode a JavaScript string into UTF-8 byte sequences and then URL-encode
21 * any that fall outside of the cookie range.
22 */
23 encode?: (val: string) => string;
24 /**
25 * Specifies the `Date` object to be the value for the `Expires`
26 * `Set-Cookie` attribute. By default, no expiration is set, and most
27 * clients will consider this a "non-persistent cookie" and will delete it
28 * on a condition like exiting a web browser application.
29 *
30 * *Note* the cookie storage model specification states that if both
31 * `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
32 * possible not all clients by obey this, so if both are set, they should
33 * point to the same date and time.
34 */
35 expires?: Date;
36 /**
37 * Specifies the boolean value for the `HttpOnly` `Set-Cookie` attribute.
38 * When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
39 * default, the `HttpOnly` attribute is not set.
40 *
41 * *Note* be careful when setting this to true, as compliant clients will
42 * not allow client-side JavaScript to see the cookie in `document.cookie`.
43 */
44 httpOnly?: boolean;
45 /**
46 * Specifies the number (in seconds) to be the value for the `Max-Age`
47 * `Set-Cookie` attribute. The given number will be converted to an integer
48 * by rounding down. By default, no maximum age is set.
49 *
50 * *Note* the cookie storage model specification states that if both
51 * `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
52 * possible not all clients by obey this, so if both are set, they should
53 * point to the same date and time.
54 */
55 maxAge?: number;
56 /**
57 * Specifies the value for the `Path` `Set-Cookie` attribute. By default,
58 * the path is considered the "default path".
59 */
60 path?: string;
61 /**
62 * Specifies the boolean or string to be the value for the `SameSite`
63 * `Set-Cookie` attribute.
64 *
65 * - `true` will set the `SameSite` attribute to `Strict` for strict same
66 * site enforcement.
67 * - `false` will not set the `SameSite` attribute.
68 * - `'lax'` will set the `SameSite` attribute to Lax for lax same site
69 * enforcement.
70 * - `'strict'` will set the `SameSite` attribute to Strict for strict same
71 * site enforcement.
72 */
73 sameSite?: boolean | 'lax' | 'strict';
74 /**
75 * Specifies the boolean value for the `Secure` `Set-Cookie` attribute. When
76 * truthy, the `Secure` attribute is set, otherwise it is not. By default,
77 * the `Secure` attribute is not set.
78 *
79 * *Note* be careful when setting this to `true`, as compliant clients will
80 * not send the cookie back to the server in the future if the browser does
81 * not have an HTTPS connection.
82 */
83 secure?: boolean;
84}
85
86interface CookieParseOptions {
87 /**
88 * Specifies a function that will be used to decode a cookie's value. Since
89 * the value of a cookie has a limited character set (and must be a simple
90 * string), this function can be used to decode a previously-encoded cookie
91 * value into a JavaScript string or other object.
92 *
93 * The default function is the global `decodeURIComponent`, which will decode
94 * any URL-encoded sequences into their byte representations.
95 *
96 * *Note* if an error is thrown from this function, the original, non-decoded
97 * cookie value will be returned as the cookie's value.
98 */
99 decode?: (val: string) => string;
100}
101
102/**
103 * Parse an HTTP Cookie header string and returning an object of all cookie
104 * name-value pairs.
105 *
106 * @param str the string representing a `Cookie` header value
107 * @param options object containing parsing options
108 */
109export function parse(str: string, options?: CookieParseOptions): { [key: string]: string };
110
111/**
112 * Serialize a cookie name-value pair into a `Set-Cookie` header string.
113 *
114 * @param name the name for the cookie
115 * @param val value to set the cookie to
116 * @param options object containing serialization options
117 */
118export function serialize(name: string, val: string, options?: CookieSerializeOptions): string;