-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.ts
220 lines (211 loc) · 5.24 KB
/
types.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* CorsOptions
*
* An Object that describes how CORS middleware should behave. The default configuration is the equivalent of:
*
* ```ts
* {
* "origin": "*",
* "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
* "preflightContinue": false,
* "optionsSuccessStatus": 204,
* }
* ```
*
* @link https://github.com/tajpouria/cors#configuration-options
*/
export type CorsOptions = {
/**
* Configures the Access-Control-Allow-Origin CORS header.
*
* Examples:
*
* - Boolean
*
* set origin to true to reflect the request origin, as defined by req.header('Origin'):
*
* ```ts
* app.use(cors({ origin: true }));
*
* ```
* or set it to false to disable CORS:
*
* ```ts
* app.use(cors({ origin: false }));
*
* ```
*
* - String
*
* set origin to a specific origin. For example if you set it to "http://example.com" only requests from "http://example.com" will be allowed:
*
* ```ts
* app.use(cors({ origin: 'http://example.com' }));
*
* ```
*
* - RegExp
*
* set origin to a regular expression pattern which will be used to test the request origin. If it's a match, the request origin will be reflected. For example the pattern /example\.com$/ will reflect any request that is coming from an origin ending with "example.com":
*
* ```ts
* app.use(cors({ origin: /example\.com$/ }));
*
* ```
*
* - Array
*
* set origin to an array of valid origins. Each origin can be a String or a RegExp. For example ["http://example1.com", /\.example2\.com$/] will accept any request from "http://example1.com" or from a subdomain of "example2.com":
*
* ```ts
* app.use(cors({ origin: ["http://example1.com", /\.example2\.com$/] }));
*
* ```
*
* - Function
*
* set origin to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (called as callback(err, origin), where origin is a non-function value of the origin option) as the second:
*
* ```ts
* app.use(cors({
* origin: async (requestOrigin) => {
* await loadOriginsFromDataBase(); // Simulate asynchronous task
* return ["http://example1.com", /\.example2\.com$/];
* },
* }));
*
* ```
*/
origin?: boolean | string | RegExp | (string | RegExp)[] | OriginDelegate;
/**
* Configures the Access-Control-Allow-Methods CORS header.
*
* Examples:
*
* - String
*
* Expects a comma-delimited string (ex: 'GET,PUT,POST'):
*
* ```ts
* app.use(cors({ methods: 'GET,PUT,POST' }));
*
* ```
*
* - Array
*
* or an array (ex: ['GET', 'PUT', 'POST'])
*
* ```ts
* app.use(cors({ methods: ['GET', 'PUT', 'POST'] }));
*
* ```
*/
methods?: string | string[];
/**
* Configures the Access-Control-Allow-Headers CORS header. If not specified, defaults to reflecting the headers specified in the request's Access-Control-Request-Headers header.
*
* Examples:
*
* - String
*
* Expects a comma-delimited string (ex: 'Content-Type,Authorization'):
*
* ```ts
* app.use(cors({ allowedHeaders: 'Content-Type,Authorization' }));
*
* ```
*
* - Array
*
* or an array (ex: ['Content-Type', 'Authorization']):
*
* ```ts
* app.use(cors({ allowedHeaders: ['Content-Type', 'Authorization'] }));
*
* ```
*/
allowedHeaders?: string | string[];
/**
* Configures the Access-Control-Expose-Headers CORS header. If not specified, no custom headers are exposed.
*
* Examples:
*
* - String
*
* Expects a comma-delimited string (ex: 'Content-Range,X-Content-Range'):
*
* ```ts
* app.use(cors({ exposedHeaders: 'Content-Range,X-Content-Range' }));
*
* ```
*/
exposedHeaders?: string | string[];
/**
* Configures the Access-Control-Allow-Credentials CORS header. It is omitted by default.
*
* Examples:
*
* - Boolean
*
* Set to true to pass the header:
*
* ```ts
* app.use(cors({ credentials: true }));
*
* ```
*/
credentials?: boolean;
/**
* Configures the Access-Control-Max-Age CORS header. It is omitted by default.
*
* Examples:
*
* - Number
*
* Set to an integer to pass the header:
*
* ```ts
* app.use(cors({ maxAge: 1 }));
*
* ```
*/
maxAge?: number;
/**
* Pass the CORS preflight response to the next handler:
*
* Examples:
*
* - Boolean
*
* ```ts
* app.use(cors({ preflightContinue: true }));
*
* ```
*/
preflightContinue?: boolean;
/**
* Provides a status code to use for successful OPTIONS requests, since some legacy browsers (IE11, various SmartTVs) choke on 204.
*
* Examples:
*
* - Number
*
* ```ts
* app.use(cors({ optionsSuccessStatus: 200 }));
*
* ```
*/
optionsSuccessStatus?: number;
};
/**
* @link https://github.com/tajpouria/cors#configuring-cors-w-dynamic-origin
*/
export type OriginDelegate = (
requestOrigin: string | undefined | null,
) => CorsOptions["origin"] | void | Promise<CorsOptions["origin"] | void>;
/**
* @link https://github.com/tajpouria/cors#configuring-cors-asynchronously
*/
export type CorsOptionsDelegate<RequestT = any> = (
request: RequestT,
) => CorsOptions | void | Promise<CorsOptions | void>;