UNPKG

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