UNPKG

5.01 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 */
75 sameSite?: boolean | 'lax' | 'strict';
76 /**
77 * Specifies the boolean value for the `Secure` `Set-Cookie` attribute. When
78 * truthy, the `Secure` attribute is set, otherwise it is not. By default,
79 * the `Secure` attribute is not set.
80 *
81 * *Note* be careful when setting this to `true`, as compliant clients will
82 * not send the cookie back to the server in the future if the browser does
83 * not have an HTTPS connection.
84 */
85 secure?: boolean;
86}
87
88export interface CookieParseOptions {
89 /**
90 * Specifies a function that will be used to decode a cookie's value. Since
91 * the value of a cookie has a limited character set (and must be a simple
92 * string), this function can be used to decode a previously-encoded cookie
93 * value into a JavaScript string or other object.
94 *
95 * The default function is the global `decodeURIComponent`, which will decode
96 * any URL-encoded sequences into their byte representations.
97 *
98 * *Note* if an error is thrown from this function, the original, non-decoded
99 * cookie value will be returned as the cookie's value.
100 */
101 decode?(val: string): string;
102}
103
104/**
105 * Parse an HTTP Cookie header string and returning an object of all cookie
106 * name-value pairs.
107 *
108 * @param str the string representing a `Cookie` header value
109 * @param options object containing parsing options
110 */
111export function parse(str: string, options?: CookieParseOptions): { [key: string]: string };
112
113/**
114 * Serialize a cookie name-value pair into a `Set-Cookie` header string.
115 *
116 * @param name the name for the cookie
117 * @param val value to set the cookie to
118 * @param options object containing serialization options
119 */
120export function serialize(name: string, val: string, options?: CookieSerializeOptions): string;