UNPKG

5.87 kBTypeScriptView Raw
1// Type definitions for cookies 0.7
2// Project: https://github.com/pillarjs/cookies
3// Definitions by: Wang Zishi <https://github.com/WangZishi>
4// jKey Lu <https://github.com/jkeylu>
5// BendingBender <https://github.com/BendingBender>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.3
8
9/// <reference types="node" />
10import { IncomingMessage, ServerResponse } from 'http';
11import * as Keygrip from 'keygrip';
12import * as express from 'express';
13import * as connect from 'connect';
14
15interface Cookies {
16 secure: boolean;
17 request: IncomingMessage;
18 response: ServerResponse;
19
20 /**
21 * This extracts the cookie with the given name from the
22 * Cookie header in the request. If such a cookie exists,
23 * its value is returned. Otherwise, nothing is returned.
24 */
25 get(name: string, opts?: Cookies.GetOption): string | undefined;
26
27 /**
28 * This sets the given cookie in the response and returns
29 * the current context to allow chaining.If the value is omitted,
30 * an outbound header with an expired date is used to delete the cookie.
31 */
32 set(name: string, value?: string | null, opts?: Cookies.SetOption): this;
33}
34
35declare namespace Cookies {
36 /**
37 * for backward-compatibility
38 */
39 type ICookies = Cookies;
40 /**
41 * for backward-compatibility
42 */
43 type IOptions = SetOption;
44
45 interface Option {
46 keys?: string[] | Keygrip | undefined;
47 secure?: boolean | undefined;
48 }
49
50 interface GetOption {
51 signed: boolean;
52 }
53
54 interface SetOption {
55 /**
56 * a number representing the milliseconds from Date.now() for expiry
57 */
58 maxAge?: number | undefined;
59 /**
60 * a Date object indicating the cookie's expiration
61 * date (expires at the end of session by default).
62 */
63 expires?: Date | undefined;
64 /**
65 * a string indicating the path of the cookie (/ by default).
66 */
67 path?: string | undefined;
68 /**
69 * a string indicating the domain of the cookie (no default).
70 */
71 domain?: string | undefined;
72 /**
73 * a boolean indicating whether the cookie is only to be sent
74 * over HTTPS (false by default for HTTP, true by default for HTTPS).
75 */
76 secure?: boolean | undefined;
77 /**
78 * "secureProxy" option is deprecated; use "secure" option, provide "secure" to constructor if needed
79 */
80 secureProxy?: boolean | undefined;
81 /**
82 * a boolean indicating whether the cookie is only to be sent over HTTP(S),
83 * and not made available to client JavaScript (true by default).
84 */
85 httpOnly?: boolean | undefined;
86 /**
87 * a boolean or string indicating whether the cookie is a "same site" cookie (false by default).
88 * This can be set to 'strict', 'lax', or true (which maps to 'strict').
89 */
90 sameSite?: 'strict' | 'lax' | 'none' | boolean | undefined;
91 /**
92 * a boolean indicating whether the cookie is to be signed (false by default).
93 * If this is true, another cookie of the same name with the .sig suffix
94 * appended will also be sent, with a 27-byte url-safe base64 SHA1 value
95 * representing the hash of cookie-name=cookie-value against the first Keygrip key.
96 * This signature key is used to detect tampering the next time a cookie is received.
97 */
98 signed?: boolean | undefined;
99 /**
100 * a boolean indicating whether to overwrite previously set
101 * cookies of the same name (false by default). If this is true,
102 * all cookies set during the same request with the same
103 * name (regardless of path or domain) are filtered out of
104 * the Set-Cookie header when setting this cookie.
105 */
106 overwrite?: boolean | undefined;
107 }
108
109 type CookieAttr = SetOption;
110
111 interface Cookie {
112 name: string;
113 value: string;
114 /**
115 * "maxage" is deprecated, use "maxAge" instead
116 */
117 maxage: number;
118 maxAge: number;
119 expires: Date;
120 path: string;
121 domain: string;
122 secure: boolean;
123 httpOnly: boolean;
124 sameSite: boolean;
125 overwrite: boolean;
126
127 toString(): string;
128 toHeader(): string;
129 }
130}
131
132interface CookiesFunction {
133 (request: IncomingMessage, response: ServerResponse, options?: Cookies.Option): Cookies;
134 /**
135 * "options" array of key strings is deprecated, provide using options {"keys": keygrip}
136 */
137 (request: IncomingMessage, response: ServerResponse, options: string[]): Cookies;
138 /**
139 * "options" instance of Keygrip is deprecated, provide using options {"keys": keygrip}
140 */
141 // tslint:disable-next-line:unified-signatures
142 (request: IncomingMessage, response: ServerResponse, options: Keygrip): Cookies;
143
144 new (request: IncomingMessage, response: ServerResponse, options?: Cookies.Option): Cookies;
145 /**
146 * "options" array of key strings is deprecated, provide using options {"keys": keygrip}
147 */
148 new (request: IncomingMessage, response: ServerResponse, options: string[]): Cookies;
149 /**
150 * "options" instance of Keygrip is deprecated, provide using options {"keys": keygrip}
151 */
152 // tslint:disable-next-line:unified-signatures
153 new (request: IncomingMessage, response: ServerResponse, options: Keygrip): Cookies;
154
155 Cookie: {
156 new (name: string, value?: string, attrs?: Cookies.CookieAttr): Cookies.Cookie;
157 };
158
159 express(keys: string[] | Keygrip): express.Handler;
160 connect(keys: string[] | Keygrip): connect.NextHandleFunction;
161}
162
163declare const Cookies: CookiesFunction;
164
165export = Cookies;