-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
plugin.d.ts
196 lines (167 loc) · 6.04 KB
/
plugin.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/// <reference types='node' />
import { FastifyPluginCallback } from "fastify";
declare module "fastify" {
interface FastifyInstance extends SignerMethods {
/**
* Serialize a cookie name-value pair into a Set-Cookie header string
* @param name Cookie name
* @param value Cookie value
* @param opts Options
* @throws {TypeError} When maxAge option is invalid
*/
serializeCookie(name: string, value: string, opts?: fastifyCookie.SerializeOptions): string;
/**
* Manual cookie parsing method
* @docs https://github.com/fastify/fastify-cookie#manual-cookie-parsing
* @param cookieHeader Raw cookie header value
*/
parseCookie(cookieHeader: string): {
[key: string]: string;
};
}
interface FastifyRequest extends SignerMethods {
/**
* Request cookies
*/
cookies: { [cookieName: string]: string | undefined };
}
interface FastifyReply extends SignerMethods {
/**
* Request cookies
*/
cookies: { [cookieName: string]: string | undefined };
}
interface SignerMethods {
/**
* Signs the specified cookie using the secret/signer provided.
* @param value cookie value
*/
signCookie(value: string): string;
/**
* Unsigns the specified cookie using the secret/signer provided.
* @param value Cookie value
*/
unsignCookie(value: string): fastifyCookie.UnsignResult;
}
export type setCookieWrapper = (
name: string,
value: string,
options?: fastifyCookie.CookieSerializeOptions
) => FastifyReply;
interface FastifyReply {
/**
* Set response cookie
* @name setCookie
* @param name Cookie name
* @param value Cookie value
* @param options Serialize options
*/
setCookie: setCookieWrapper;
/**
* @alias setCookie
*/
cookie(
name: string,
value: string,
options?: fastifyCookie.CookieSerializeOptions
): this;
/**
* clear response cookie
* @param name Cookie name
* @param options Serialize options
*/
clearCookie(
name: string,
options?: fastifyCookie.CookieSerializeOptions
): this;
/**
* Unsigns the specified cookie using the secret provided.
* @param value Cookie value
*/
unsignCookie(value: string): fastifyCookie.UnsignResult;
}
}
type FastifyCookiePlugin = FastifyPluginCallback<
NonNullable<fastifyCookie.FastifyCookieOptions>
>;
declare namespace fastifyCookie {
interface SignerBase {
sign: (value: string) => string;
unsign: (input: string) => UnsignResult;
}
export class Signer implements SignerBase {
constructor (secrets: string | Array<string> | Buffer | Array<Buffer>, algorithm?: string)
sign: (value: string) => string;
unsign: (input: string) => UnsignResult;
}
export interface SerializeOptions {
/** The `Domain` attribute. */
domain?: string;
/** Specifies a function that will be used to encode a cookie's value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value. */
encode?(val: string): string;
/** The expiration `date` used for the `Expires` attribute. */
expires?: Date;
/** Add the `HttpOnly` attribute. Defaults to `false`. */
httpOnly?: boolean;
/** A `number` in seconds that specifies the `Max-Age` attribute. */
maxAge?: number;
/** A `boolean` indicating whether the cookie is tied to the top-level site where it's initially set and cannot be accessed from elsewhere. */
partitioned?: boolean;
/** The `Path` attribute. */
path?: string;
/** A `boolean` or one of the `SameSite` string attributes. E.g.: `lax`, `none` or `strict`. */
sameSite?: 'lax' | 'none' | 'strict' | boolean;
/** One of the `Priority` string attributes (`low`, `medium` or `high`) specifying a retention priority for HTTP cookies that will be respected by user agents during cookie eviction. */
priority?: 'low' | 'medium' | 'high';
/** Add the `Secure` attribute. Defaults to `false`. */
secure?: boolean;
}
export interface CookieSerializeOptions extends Omit<SerializeOptions, 'secure'> {
/** Add the `Secure` attribute. Value can be set to `"auto"`; in this case the `Secure` attribute will only be added for HTTPS requests. Defaults to `false`. */
secure?: boolean | 'auto';
signed?: boolean;
}
export interface ParseOptions {
decode?: (encodedURIComponent: string) => string;
}
type HookType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization';
export interface FastifyCookieOptions {
secret?: string | string[] | Buffer | Buffer[] | Signer;
hook?: HookType | false;
parseOptions?: fastifyCookie.CookieSerializeOptions;
}
export type Sign = (value: string, secret: string | Buffer, algorithm?: string) => string;
export type Unsign = (input: string, secret: string | Buffer, algorithm?: string) => UnsignResult;
export type SignerFactory = (secrets: string | string[] | Buffer | Buffer[], algorithm?: string) => SignerBase;
export type UnsignResult = {
valid: true;
renew: boolean;
value: string;
} | {
valid: false;
renew: false;
value: null;
}
export const signerFactory: SignerFactory;
export const sign: Sign;
export const unsign: Unsign;
export interface FastifyCookie extends FastifyCookiePlugin {
parse: (cookieHeader: string, opts?: ParseOptions) => { [key: string]: string };
serialize: (name: string, value: string, opts?: SerializeOptions) => string;
signerFactory: SignerFactory;
Signer: Signer;
sign: Sign;
unsign: Unsign;
}
export const fastifyCookie: FastifyCookie;
export interface FastifyCookieOptions {
secret?: string | string[] | Buffer | Buffer[] | SignerBase;
algorithm?: string;
parseOptions?: CookieSerializeOptions;
}
export { fastifyCookie as default };
}
declare function fastifyCookie(
...params: Parameters<FastifyCookiePlugin>
): ReturnType<FastifyCookiePlugin>;
export = fastifyCookie;