@eggjs/koa 3.1.0-beta.2 → 3.1.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +917 -5
- package/dist/index.js +1363 -4
- package/package.json +10 -9
- package/dist/application.d.ts +0 -131
- package/dist/application.js +0 -233
- package/dist/context.d.ts +0 -230
- package/dist/context.js +0 -304
- package/dist/request.d.ts +0 -343
- package/dist/request.js +0 -459
- package/dist/response.d.ts +0 -231
- package/dist/response.js +0 -387
- package/dist/types.d.ts +0 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,918 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
1
|
+
import util from "node:util";
|
|
2
|
+
import Emitter from "node:events";
|
|
3
|
+
import Stream from "node:stream";
|
|
4
|
+
import http, { IncomingMessage, ServerResponse } from "node:http";
|
|
5
|
+
import * as http_errors0 from "http-errors";
|
|
6
|
+
import Cookies from "cookies";
|
|
7
|
+
import { Socket } from "node:net";
|
|
8
|
+
import { ParsedUrlQuery } from "node:querystring";
|
|
9
|
+
import { Accepts } from "accepts";
|
|
10
|
+
import { Options } from "content-disposition";
|
|
11
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
12
|
+
import * as http6 from "http";
|
|
13
|
+
import * as net0 from "net";
|
|
14
|
+
|
|
15
|
+
//#region src/response.d.ts
|
|
16
|
+
declare class Response {
|
|
17
|
+
[key: symbol]: unknown;
|
|
18
|
+
app: Application;
|
|
19
|
+
req: IncomingMessage;
|
|
20
|
+
res: ServerResponse;
|
|
21
|
+
ctx: Context;
|
|
22
|
+
request: Request;
|
|
23
|
+
constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse);
|
|
24
|
+
/**
|
|
25
|
+
* Return the request socket.
|
|
26
|
+
*/
|
|
27
|
+
get socket(): net0.Socket | null;
|
|
28
|
+
/**
|
|
29
|
+
* Return response header.
|
|
30
|
+
*/
|
|
31
|
+
get header(): http6.OutgoingHttpHeaders;
|
|
32
|
+
/**
|
|
33
|
+
* Return response header, alias as response.header
|
|
34
|
+
*/
|
|
35
|
+
get headers(): http6.OutgoingHttpHeaders;
|
|
36
|
+
_explicitStatus: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Get response status code.
|
|
39
|
+
*/
|
|
40
|
+
get status(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Set response status code.
|
|
43
|
+
*/
|
|
44
|
+
set status(code: number);
|
|
45
|
+
/**
|
|
46
|
+
* Get response status message
|
|
47
|
+
*/
|
|
48
|
+
get message(): string;
|
|
49
|
+
/**
|
|
50
|
+
* Set response status message
|
|
51
|
+
*/
|
|
52
|
+
set message(msg: string);
|
|
53
|
+
_body: any;
|
|
54
|
+
_explicitNullBody: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Get response body.
|
|
57
|
+
*/
|
|
58
|
+
get body(): string | Buffer | object | Stream | null | undefined | boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Set response body.
|
|
61
|
+
*/
|
|
62
|
+
set body(val: string | Buffer | object | Stream | null | undefined | boolean);
|
|
63
|
+
/**
|
|
64
|
+
* Set Content-Length field to `n`.
|
|
65
|
+
*/
|
|
66
|
+
set length(n: number | string | undefined);
|
|
67
|
+
/**
|
|
68
|
+
* Return parsed response Content-Length when present.
|
|
69
|
+
*
|
|
70
|
+
* When Content-Length is not defined it will return `undefined`.
|
|
71
|
+
*/
|
|
72
|
+
get length(): number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Check if a header has been written to the socket.
|
|
75
|
+
*/
|
|
76
|
+
get headerSent(): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Vary on `field`.
|
|
79
|
+
*/
|
|
80
|
+
vary(field: string): void;
|
|
81
|
+
_getBackReferrer(): string | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Perform a 302 redirect to `url`.
|
|
84
|
+
*
|
|
85
|
+
* The string "back" is special-cased
|
|
86
|
+
* to provide Referrer support, when Referrer
|
|
87
|
+
* is not present `alt` or "/" is used.
|
|
88
|
+
*
|
|
89
|
+
* Examples:
|
|
90
|
+
*
|
|
91
|
+
* this.redirect('back');
|
|
92
|
+
* this.redirect('back', '/index.html');
|
|
93
|
+
* this.redirect('/login');
|
|
94
|
+
* this.redirect('http://google.com'); // will format to 'http://google.com/'
|
|
95
|
+
*/
|
|
96
|
+
redirect(url: string, alt?: string): void;
|
|
97
|
+
/**
|
|
98
|
+
* Set Content-Disposition header to "attachment" with optional `filename`.
|
|
99
|
+
*/
|
|
100
|
+
attachment(filename?: string, options?: Options): void;
|
|
101
|
+
/**
|
|
102
|
+
* Set Content-Type response header with `type` through `mime.lookup()`
|
|
103
|
+
* when it does not contain a charset.
|
|
104
|
+
*
|
|
105
|
+
* Examples:
|
|
106
|
+
*
|
|
107
|
+
* this.type = '.html';
|
|
108
|
+
* this.type = 'html';
|
|
109
|
+
* this.type = 'json';
|
|
110
|
+
* this.type = 'application/json';
|
|
111
|
+
* this.type = 'png';
|
|
112
|
+
*/
|
|
113
|
+
set type(type: string | null | undefined);
|
|
114
|
+
/**
|
|
115
|
+
* Return the response mime type void of
|
|
116
|
+
* parameters such as "charset".
|
|
117
|
+
*/
|
|
118
|
+
get type(): string;
|
|
119
|
+
/**
|
|
120
|
+
* Check whether the response is one of the listed types.
|
|
121
|
+
* Pretty much the same as `this.request.is()`.
|
|
122
|
+
*
|
|
123
|
+
* this.response.is('html')
|
|
124
|
+
* this.response.is('html', 'json')
|
|
125
|
+
*/
|
|
126
|
+
is(type?: string | string[], ...types: string[]): string | false;
|
|
127
|
+
/**
|
|
128
|
+
* Set the Last-Modified date using a string or a Date.
|
|
129
|
+
*
|
|
130
|
+
* this.response.lastModified = new Date();
|
|
131
|
+
* this.response.lastModified = '2013-09-13';
|
|
132
|
+
*/
|
|
133
|
+
set lastModified(val: string | Date | undefined);
|
|
134
|
+
/**
|
|
135
|
+
* Get the Last-Modified date in Date form, if it exists.
|
|
136
|
+
*/
|
|
137
|
+
get lastModified(): Date | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Set the ETag of a response.
|
|
140
|
+
* This will normalize the quotes if necessary.
|
|
141
|
+
*
|
|
142
|
+
* this.response.etag = 'md5-hash-sum';
|
|
143
|
+
* this.response.etag = '"md5-hash-sum"';
|
|
144
|
+
* this.response.etag = 'W/"123456789"';
|
|
145
|
+
*/
|
|
146
|
+
set etag(val: string);
|
|
147
|
+
/**
|
|
148
|
+
* Get the ETag of a response.
|
|
149
|
+
*/
|
|
150
|
+
get etag(): string;
|
|
151
|
+
/**
|
|
152
|
+
* Return response header.
|
|
153
|
+
*
|
|
154
|
+
* Examples:
|
|
155
|
+
*
|
|
156
|
+
* this.get('Content-Type');
|
|
157
|
+
* // => "text/plain"
|
|
158
|
+
*
|
|
159
|
+
* this.get('content-type');
|
|
160
|
+
* // => "text/plain"
|
|
161
|
+
*/
|
|
162
|
+
get<T = string | string[] | number>(field: string): T;
|
|
163
|
+
/**
|
|
164
|
+
* Returns true if the header identified by name is currently set in the outgoing headers.
|
|
165
|
+
* The header name matching is case-insensitive.
|
|
166
|
+
*
|
|
167
|
+
* Examples:
|
|
168
|
+
*
|
|
169
|
+
* this.has('Content-Type');
|
|
170
|
+
* // => true
|
|
171
|
+
*
|
|
172
|
+
* this.get('content-type');
|
|
173
|
+
* // => true
|
|
174
|
+
*/
|
|
175
|
+
has(field: string): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Set header `field` to `val` or pass
|
|
178
|
+
* an object of header fields.
|
|
179
|
+
*
|
|
180
|
+
* Examples:
|
|
181
|
+
*
|
|
182
|
+
* this.set('Foo', ['bar', 'baz']);
|
|
183
|
+
* this.set('Accept', 'application/json');
|
|
184
|
+
* this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
185
|
+
*/
|
|
186
|
+
set(field: string | Record<string, string>, val?: string | number | unknown[]): void;
|
|
187
|
+
/**
|
|
188
|
+
* Append additional header `field` with value `val`.
|
|
189
|
+
*
|
|
190
|
+
* Examples:
|
|
191
|
+
*
|
|
192
|
+
* ```
|
|
193
|
+
* this.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
|
|
194
|
+
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
|
|
195
|
+
* this.append('Warning', '199 Miscellaneous warning');
|
|
196
|
+
*/
|
|
197
|
+
append(field: string, val: string | string[]): void;
|
|
198
|
+
/**
|
|
199
|
+
* Remove header `field`.
|
|
200
|
+
*/
|
|
201
|
+
remove(field: string): void;
|
|
202
|
+
/**
|
|
203
|
+
* Checks if the request is writable.
|
|
204
|
+
* Tests for the existence of the socket
|
|
205
|
+
* as node sometimes does not set it.
|
|
206
|
+
*/
|
|
207
|
+
get writable(): boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Inspect implementation.
|
|
210
|
+
*/
|
|
211
|
+
inspect(): {
|
|
212
|
+
status: number;
|
|
213
|
+
message: string;
|
|
214
|
+
header: http6.OutgoingHttpHeaders;
|
|
215
|
+
} | undefined;
|
|
216
|
+
[util.inspect.custom](): {
|
|
217
|
+
status: number;
|
|
218
|
+
message: string;
|
|
219
|
+
header: http6.OutgoingHttpHeaders;
|
|
220
|
+
} | undefined;
|
|
221
|
+
/**
|
|
222
|
+
* Return JSON representation.
|
|
223
|
+
*/
|
|
224
|
+
toJSON(): {
|
|
225
|
+
status: number;
|
|
226
|
+
message: string;
|
|
227
|
+
header: http6.OutgoingHttpHeaders;
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Flush any set headers and begin the body
|
|
231
|
+
*/
|
|
232
|
+
flushHeaders(): void;
|
|
233
|
+
}
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/request.d.ts
|
|
236
|
+
interface RequestSocket extends Socket {
|
|
237
|
+
encrypted: boolean;
|
|
238
|
+
}
|
|
239
|
+
declare class Request {
|
|
240
|
+
[key: symbol]: unknown;
|
|
241
|
+
app: Application;
|
|
242
|
+
req: IncomingMessage;
|
|
243
|
+
res: ServerResponse;
|
|
244
|
+
ctx: Context;
|
|
245
|
+
response: Response;
|
|
246
|
+
originalUrl: string;
|
|
247
|
+
constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse);
|
|
248
|
+
/**
|
|
249
|
+
* Return request header.
|
|
250
|
+
*/
|
|
251
|
+
get header(): http6.IncomingHttpHeaders;
|
|
252
|
+
/**
|
|
253
|
+
* Set request header.
|
|
254
|
+
*/
|
|
255
|
+
set header(val: http6.IncomingHttpHeaders);
|
|
256
|
+
/**
|
|
257
|
+
* Return request header, alias as request.header
|
|
258
|
+
*/
|
|
259
|
+
get headers(): http6.IncomingHttpHeaders;
|
|
260
|
+
/**
|
|
261
|
+
* Set request header, alias as request.header
|
|
262
|
+
*/
|
|
263
|
+
set headers(val: http6.IncomingHttpHeaders);
|
|
264
|
+
/**
|
|
265
|
+
* Get request URL.
|
|
266
|
+
*/
|
|
267
|
+
get url(): string;
|
|
268
|
+
/**
|
|
269
|
+
* Set request URL.
|
|
270
|
+
*/
|
|
271
|
+
set url(val: string);
|
|
272
|
+
/**
|
|
273
|
+
* Get origin of URL.
|
|
274
|
+
*/
|
|
275
|
+
get origin(): string;
|
|
276
|
+
/**
|
|
277
|
+
* Get full request URL.
|
|
278
|
+
*/
|
|
279
|
+
get href(): string;
|
|
280
|
+
/**
|
|
281
|
+
* Get request method.
|
|
282
|
+
*/
|
|
283
|
+
get method(): string;
|
|
284
|
+
/**
|
|
285
|
+
* Set request method.
|
|
286
|
+
*/
|
|
287
|
+
set method(val: string);
|
|
288
|
+
/**
|
|
289
|
+
* Get request pathname.
|
|
290
|
+
*/
|
|
291
|
+
get path(): string;
|
|
292
|
+
/**
|
|
293
|
+
* Set pathname, retaining the query string when present.
|
|
294
|
+
*/
|
|
295
|
+
set path(pathname: string);
|
|
296
|
+
protected _parsedUrlQueryCache: Record<string, ParsedUrlQuery> | undefined;
|
|
297
|
+
/**
|
|
298
|
+
* Get parsed query string.
|
|
299
|
+
*/
|
|
300
|
+
get query(): ParsedUrlQuery;
|
|
301
|
+
/**
|
|
302
|
+
* Set query string as an object.
|
|
303
|
+
*/
|
|
304
|
+
set query(obj: ParsedUrlQuery);
|
|
305
|
+
/**
|
|
306
|
+
* Get query string.
|
|
307
|
+
*/
|
|
308
|
+
get querystring(): string;
|
|
309
|
+
/**
|
|
310
|
+
* Set query string.
|
|
311
|
+
*/
|
|
312
|
+
set querystring(str: string);
|
|
313
|
+
/**
|
|
314
|
+
* Get the search string. Same as the query string
|
|
315
|
+
* except it includes the leading ?.
|
|
316
|
+
*/
|
|
317
|
+
get search(): string;
|
|
318
|
+
/**
|
|
319
|
+
* Set the search string. Same as
|
|
320
|
+
* request.querystring= but included for ubiquity.
|
|
321
|
+
*/
|
|
322
|
+
set search(str: string);
|
|
323
|
+
/**
|
|
324
|
+
* Parse the "Host" header field host
|
|
325
|
+
* and support X-Forwarded-Host when a
|
|
326
|
+
* proxy is enabled.
|
|
327
|
+
* return `hostname:port` format
|
|
328
|
+
*/
|
|
329
|
+
get host(): string;
|
|
330
|
+
/**
|
|
331
|
+
* Parse the "Host" header field hostname
|
|
332
|
+
* and support X-Forwarded-Host when a
|
|
333
|
+
* proxy is enabled.
|
|
334
|
+
*/
|
|
335
|
+
get hostname(): string;
|
|
336
|
+
protected _memoizedURL: URL | undefined;
|
|
337
|
+
/**
|
|
338
|
+
* Get WHATWG parsed URL.
|
|
339
|
+
* Lazily memoized.
|
|
340
|
+
*/
|
|
341
|
+
get URL(): URL;
|
|
342
|
+
/**
|
|
343
|
+
* Check if the request is fresh, aka
|
|
344
|
+
* Last-Modified and/or the ETag
|
|
345
|
+
* still match.
|
|
346
|
+
*/
|
|
347
|
+
get fresh(): boolean;
|
|
348
|
+
/**
|
|
349
|
+
* Check if the request is stale, aka
|
|
350
|
+
* "Last-Modified" and / or the "ETag" for the
|
|
351
|
+
* resource has changed.
|
|
352
|
+
*/
|
|
353
|
+
get stale(): boolean;
|
|
354
|
+
/**
|
|
355
|
+
* Check if the request is idempotent.
|
|
356
|
+
*/
|
|
357
|
+
get idempotent(): boolean;
|
|
358
|
+
/**
|
|
359
|
+
* Return the request socket.
|
|
360
|
+
*/
|
|
361
|
+
get socket(): RequestSocket;
|
|
362
|
+
/**
|
|
363
|
+
* Get the charset when present or undefined.
|
|
364
|
+
*/
|
|
365
|
+
get charset(): string;
|
|
366
|
+
/**
|
|
367
|
+
* Return parsed Content-Length when present.
|
|
368
|
+
*/
|
|
369
|
+
get length(): number | undefined;
|
|
370
|
+
/**
|
|
371
|
+
* Return the protocol string "http" or "https"
|
|
372
|
+
* when requested with TLS. When the proxy setting
|
|
373
|
+
* is enabled the "X-Forwarded-Proto" header
|
|
374
|
+
* field will be trusted. If you're running behind
|
|
375
|
+
* a reverse proxy that supplies https for you this
|
|
376
|
+
* may be enabled.
|
|
377
|
+
*/
|
|
378
|
+
get protocol(): string;
|
|
379
|
+
/**
|
|
380
|
+
* Shorthand for:
|
|
381
|
+
*
|
|
382
|
+
* this.protocol == 'https'
|
|
383
|
+
*/
|
|
384
|
+
get secure(): boolean;
|
|
385
|
+
/**
|
|
386
|
+
* When `app.proxy` is `true`, parse
|
|
387
|
+
* the "X-Forwarded-For" ip address list.
|
|
388
|
+
*
|
|
389
|
+
* For example if the value was "client, proxy1, proxy2"
|
|
390
|
+
* you would receive the array `["client", "proxy1", "proxy2"]`
|
|
391
|
+
* where "proxy2" is the furthest down-stream.
|
|
392
|
+
*/
|
|
393
|
+
get ips(): string[];
|
|
394
|
+
protected _ip: string;
|
|
395
|
+
/**
|
|
396
|
+
* Return request's remote address
|
|
397
|
+
* When `app.proxy` is `true`, parse
|
|
398
|
+
* the "X-Forwarded-For" ip address list and return the first one
|
|
399
|
+
*/
|
|
400
|
+
get ip(): string;
|
|
401
|
+
set ip(ip: string);
|
|
402
|
+
/**
|
|
403
|
+
* Return subdomains as an array.
|
|
404
|
+
*
|
|
405
|
+
* Subdomains are the dot-separated parts of the host before the main domain
|
|
406
|
+
* of the app. By default, the domain of the app is assumed to be the last two
|
|
407
|
+
* parts of the host. This can be changed by setting `app.subdomainOffset`.
|
|
408
|
+
*
|
|
409
|
+
* For example, if the domain is "tobi.ferrets.example.com":
|
|
410
|
+
* If `app.subdomainOffset` is not set, this.subdomains is
|
|
411
|
+
* `["ferrets", "tobi"]`.
|
|
412
|
+
* If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`.
|
|
413
|
+
*/
|
|
414
|
+
get subdomains(): string[];
|
|
415
|
+
protected _accept: Accepts;
|
|
416
|
+
/**
|
|
417
|
+
* Get accept object.
|
|
418
|
+
* Lazily memoized.
|
|
419
|
+
*/
|
|
420
|
+
get accept(): Accepts;
|
|
421
|
+
/**
|
|
422
|
+
* Set accept object.
|
|
423
|
+
*/
|
|
424
|
+
set accept(obj: Accepts);
|
|
425
|
+
/**
|
|
426
|
+
* Check if the given `type(s)` is acceptable, returning
|
|
427
|
+
* the best match when true, otherwise `false`, in which
|
|
428
|
+
* case you should respond with 406 "Not Acceptable".
|
|
429
|
+
*
|
|
430
|
+
* The `type` value may be a single mime type string
|
|
431
|
+
* such as "application/json", the extension name
|
|
432
|
+
* such as "json" or an array `["json", "html", "text/plain"]`. When a list
|
|
433
|
+
* or array is given the _best_ match, if any is returned.
|
|
434
|
+
*
|
|
435
|
+
* Examples:
|
|
436
|
+
*
|
|
437
|
+
* // Accept: text/html
|
|
438
|
+
* this.accepts('html');
|
|
439
|
+
* // => "html"
|
|
440
|
+
*
|
|
441
|
+
* // Accept: text/*, application/json
|
|
442
|
+
* this.accepts('html');
|
|
443
|
+
* // => "html"
|
|
444
|
+
* this.accepts('text/html');
|
|
445
|
+
* // => "text/html"
|
|
446
|
+
* this.accepts('json', 'text');
|
|
447
|
+
* // => "json"
|
|
448
|
+
* this.accepts('application/json');
|
|
449
|
+
* // => "application/json"
|
|
450
|
+
*
|
|
451
|
+
* // Accept: text/*, application/json
|
|
452
|
+
* this.accepts('image/png');
|
|
453
|
+
* this.accepts('png');
|
|
454
|
+
* // => false
|
|
455
|
+
*
|
|
456
|
+
* // Accept: text/*;q=.5, application/json
|
|
457
|
+
* this.accepts(['html', 'json']);
|
|
458
|
+
* this.accepts('html', 'json');
|
|
459
|
+
* // => "json"
|
|
460
|
+
*/
|
|
461
|
+
accepts(args: string[]): string | string[] | false;
|
|
462
|
+
accepts(...args: string[]): string | string[] | false;
|
|
463
|
+
/**
|
|
464
|
+
* Return accepted encodings or best fit based on `encodings`.
|
|
465
|
+
*
|
|
466
|
+
* Given `Accept-Encoding: gzip, deflate`
|
|
467
|
+
* an array sorted by quality is returned:
|
|
468
|
+
*
|
|
469
|
+
* ['gzip', 'deflate']
|
|
470
|
+
*/
|
|
471
|
+
acceptsEncodings(): string[];
|
|
472
|
+
acceptsEncodings(encodings: string[]): string | false;
|
|
473
|
+
acceptsEncodings(...encodings: string[]): string | false;
|
|
474
|
+
/**
|
|
475
|
+
* Return accepted charsets or best fit based on `charsets`.
|
|
476
|
+
*
|
|
477
|
+
* Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
|
|
478
|
+
* an array sorted by quality is returned:
|
|
479
|
+
*
|
|
480
|
+
* ['utf-8', 'utf-7', 'iso-8859-1']
|
|
481
|
+
*/
|
|
482
|
+
acceptsCharsets(): string[];
|
|
483
|
+
acceptsCharsets(charsets: string[]): string | false;
|
|
484
|
+
acceptsCharsets(...charsets: string[]): string | false;
|
|
485
|
+
/**
|
|
486
|
+
* Return accepted languages or best fit based on `langs`.
|
|
487
|
+
*
|
|
488
|
+
* Given `Accept-Language: en;q=0.8, es, pt`
|
|
489
|
+
* an array sorted by quality is returned:
|
|
490
|
+
*
|
|
491
|
+
* ['es', 'pt', 'en']
|
|
492
|
+
*/
|
|
493
|
+
acceptsLanguages(): string[];
|
|
494
|
+
acceptsLanguages(languages: string[]): string | false;
|
|
495
|
+
acceptsLanguages(...languages: string[]): string | false;
|
|
496
|
+
/**
|
|
497
|
+
* Check if the incoming request contains the "Content-Type"
|
|
498
|
+
* header field and if it contains any of the given mime `type`s.
|
|
499
|
+
* If there is no request body, `null` is returned.
|
|
500
|
+
* If there is no content type, `false` is returned.
|
|
501
|
+
* Otherwise, it returns the first `type` that matches.
|
|
502
|
+
*
|
|
503
|
+
* Examples:
|
|
504
|
+
*
|
|
505
|
+
* // With Content-Type: text/html; charset=utf-8
|
|
506
|
+
* this.is('html'); // => 'html'
|
|
507
|
+
* this.is('text/html'); // => 'text/html'
|
|
508
|
+
* this.is('text/*', 'application/json'); // => 'text/html'
|
|
509
|
+
*
|
|
510
|
+
* // When Content-Type is application/json
|
|
511
|
+
* this.is('json', 'urlencoded'); // => 'json'
|
|
512
|
+
* this.is('application/json'); // => 'application/json'
|
|
513
|
+
* this.is('html', 'application/*'); // => 'application/json'
|
|
514
|
+
*
|
|
515
|
+
* this.is('html'); // => false
|
|
516
|
+
*/
|
|
517
|
+
is(type?: string | string[], ...types: string[]): string | false | null;
|
|
518
|
+
/**
|
|
519
|
+
* Return the request mime type void of
|
|
520
|
+
* parameters such as "charset".
|
|
521
|
+
*/
|
|
522
|
+
get type(): string;
|
|
523
|
+
/**
|
|
524
|
+
* Return request header.
|
|
525
|
+
*
|
|
526
|
+
* The `Referrer` header field is special-cased,
|
|
527
|
+
* both `Referrer` and `Referer` are interchangeable.
|
|
528
|
+
*
|
|
529
|
+
* Examples:
|
|
530
|
+
*
|
|
531
|
+
* this.get('Content-Type');
|
|
532
|
+
* // => "text/plain"
|
|
533
|
+
*
|
|
534
|
+
* this.get('content-type');
|
|
535
|
+
* // => "text/plain"
|
|
536
|
+
*
|
|
537
|
+
* this.get('Something');
|
|
538
|
+
* // => ''
|
|
539
|
+
*/
|
|
540
|
+
get<T = string | string[]>(field: string): T;
|
|
541
|
+
/**
|
|
542
|
+
* Inspect implementation.
|
|
543
|
+
*/
|
|
544
|
+
inspect(): {
|
|
545
|
+
method: string;
|
|
546
|
+
url: string;
|
|
547
|
+
header: http6.IncomingHttpHeaders;
|
|
548
|
+
} | undefined;
|
|
549
|
+
/**
|
|
550
|
+
* Custom inspection implementation for newer Node.js versions.
|
|
551
|
+
*/
|
|
552
|
+
[util.inspect.custom](): {
|
|
553
|
+
method: string;
|
|
554
|
+
url: string;
|
|
555
|
+
header: http6.IncomingHttpHeaders;
|
|
556
|
+
} | undefined;
|
|
557
|
+
/**
|
|
558
|
+
* Return JSON representation.
|
|
559
|
+
*/
|
|
560
|
+
toJSON(): {
|
|
561
|
+
method: string;
|
|
562
|
+
url: string;
|
|
563
|
+
header: http6.IncomingHttpHeaders;
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
//#endregion
|
|
567
|
+
//#region src/types.d.ts
|
|
568
|
+
type CustomError = Error & {
|
|
569
|
+
headers?: Record<string, string>;
|
|
570
|
+
status?: number;
|
|
571
|
+
statusCode?: number;
|
|
572
|
+
code?: string;
|
|
573
|
+
expose?: boolean;
|
|
574
|
+
headerSent?: boolean;
|
|
575
|
+
};
|
|
576
|
+
interface AnyProto {
|
|
577
|
+
[key: string | symbol]: any;
|
|
578
|
+
}
|
|
579
|
+
//#endregion
|
|
580
|
+
//#region src/context.d.ts
|
|
581
|
+
declare class Context {
|
|
582
|
+
#private;
|
|
583
|
+
[key: symbol | string]: unknown;
|
|
584
|
+
app: Application;
|
|
585
|
+
req: IncomingMessage;
|
|
586
|
+
res: ServerResponse;
|
|
587
|
+
request: Request & AnyProto;
|
|
588
|
+
response: Response & AnyProto;
|
|
589
|
+
originalUrl: string;
|
|
590
|
+
respond?: boolean;
|
|
591
|
+
constructor(app: Application, req: IncomingMessage, res: ServerResponse);
|
|
592
|
+
/**
|
|
593
|
+
* util.inspect() implementation, which
|
|
594
|
+
* just returns the JSON output.
|
|
595
|
+
*/
|
|
596
|
+
inspect(): {
|
|
597
|
+
request: {
|
|
598
|
+
method: string;
|
|
599
|
+
url: string;
|
|
600
|
+
header: http6.IncomingHttpHeaders;
|
|
601
|
+
};
|
|
602
|
+
response: {
|
|
603
|
+
status: number;
|
|
604
|
+
message: string;
|
|
605
|
+
header: http6.OutgoingHttpHeaders;
|
|
606
|
+
};
|
|
607
|
+
app: {
|
|
608
|
+
subdomainOffset: number;
|
|
609
|
+
proxy: boolean;
|
|
610
|
+
env: string;
|
|
611
|
+
};
|
|
612
|
+
originalUrl: string;
|
|
613
|
+
req: string;
|
|
614
|
+
res: string;
|
|
615
|
+
socket: string;
|
|
616
|
+
};
|
|
617
|
+
/**
|
|
618
|
+
* Custom inspection implementation for newer Node.js versions.
|
|
619
|
+
*/
|
|
620
|
+
[util.inspect.custom](): {
|
|
621
|
+
request: {
|
|
622
|
+
method: string;
|
|
623
|
+
url: string;
|
|
624
|
+
header: http6.IncomingHttpHeaders;
|
|
625
|
+
};
|
|
626
|
+
response: {
|
|
627
|
+
status: number;
|
|
628
|
+
message: string;
|
|
629
|
+
header: http6.OutgoingHttpHeaders;
|
|
630
|
+
};
|
|
631
|
+
app: {
|
|
632
|
+
subdomainOffset: number;
|
|
633
|
+
proxy: boolean;
|
|
634
|
+
env: string;
|
|
635
|
+
};
|
|
636
|
+
originalUrl: string;
|
|
637
|
+
req: string;
|
|
638
|
+
res: string;
|
|
639
|
+
socket: string;
|
|
640
|
+
};
|
|
641
|
+
/**
|
|
642
|
+
* Return JSON representation.
|
|
643
|
+
*
|
|
644
|
+
* Here we explicitly invoke .toJSON() on each
|
|
645
|
+
* object, as iteration will otherwise fail due
|
|
646
|
+
* to the getters and cause utilities such as
|
|
647
|
+
* clone() to fail.
|
|
648
|
+
*/
|
|
649
|
+
toJSON(): {
|
|
650
|
+
request: {
|
|
651
|
+
method: string;
|
|
652
|
+
url: string;
|
|
653
|
+
header: http6.IncomingHttpHeaders;
|
|
654
|
+
};
|
|
655
|
+
response: {
|
|
656
|
+
status: number;
|
|
657
|
+
message: string;
|
|
658
|
+
header: http6.OutgoingHttpHeaders;
|
|
659
|
+
};
|
|
660
|
+
app: {
|
|
661
|
+
subdomainOffset: number;
|
|
662
|
+
proxy: boolean;
|
|
663
|
+
env: string;
|
|
664
|
+
};
|
|
665
|
+
originalUrl: string;
|
|
666
|
+
req: string;
|
|
667
|
+
res: string;
|
|
668
|
+
socket: string;
|
|
669
|
+
};
|
|
670
|
+
/**
|
|
671
|
+
* Similar to .throw(), adds assertion.
|
|
672
|
+
*
|
|
673
|
+
* ```ts
|
|
674
|
+
* this.assert(this.user, 401, 'Please login!');
|
|
675
|
+
* ```
|
|
676
|
+
*/
|
|
677
|
+
assert(value: unknown, status?: number, errorProps?: Record<string, unknown>): void;
|
|
678
|
+
assert(value: unknown, status?: number, errorMessage?: string, errorProps?: Record<string, unknown>): void;
|
|
679
|
+
/**
|
|
680
|
+
* Throw an error with `status` (default 500) and
|
|
681
|
+
* `msg`. Note that these are user-level
|
|
682
|
+
* errors, and the message may be exposed to the client.
|
|
683
|
+
*
|
|
684
|
+
* this.throw(403)
|
|
685
|
+
* this.throw(400, 'name required')
|
|
686
|
+
* this.throw('something exploded')
|
|
687
|
+
* this.throw(new Error('invalid'))
|
|
688
|
+
* this.throw(400, new Error('invalid'))
|
|
689
|
+
* this.throw(400, new Error('invalid'), { foo: 'bar' })
|
|
690
|
+
* this.throw(new Error('invalid'), { foo: 'bar' })
|
|
691
|
+
*
|
|
692
|
+
* See: https://github.com/jshttp/http-errors
|
|
693
|
+
*
|
|
694
|
+
* Note: `status` should only be passed as the first parameter.
|
|
695
|
+
*
|
|
696
|
+
* @param {String|Number|Error} status error, msg or status
|
|
697
|
+
* @param {String|Number|Error|Object} [error] error, msg, status or errorProps
|
|
698
|
+
* @param {Object} [errorProps] error object properties
|
|
699
|
+
*/
|
|
700
|
+
throw(status: number): void;
|
|
701
|
+
throw(status: number, errorProps: object): void;
|
|
702
|
+
throw(status: number, errorMessage: string): void;
|
|
703
|
+
throw(status: number, errorMessage: string, errorProps: object): void;
|
|
704
|
+
throw(status: number, error: Error): void;
|
|
705
|
+
throw(status: number, error: Error, errorProps: object): void;
|
|
706
|
+
throw(errorMessage: string): void;
|
|
707
|
+
throw(errorMessage: string, errorProps: object): void;
|
|
708
|
+
throw(errorMessage: string, status: number): void;
|
|
709
|
+
throw(errorMessage: string, status: number, errorProps: object): void;
|
|
710
|
+
throw(error: Error): void;
|
|
711
|
+
throw(error: Error, errorProps: object): void;
|
|
712
|
+
throw(error: Error, status: number): void;
|
|
713
|
+
throw(error: Error, status: number, errorProps: object): void;
|
|
714
|
+
/**
|
|
715
|
+
* Default error handling.
|
|
716
|
+
* @private
|
|
717
|
+
*/
|
|
718
|
+
onerror(err: CustomError): void;
|
|
719
|
+
protected _cookies: Cookies | undefined;
|
|
720
|
+
get cookies(): Cookies;
|
|
721
|
+
set cookies(cookies: Cookies);
|
|
722
|
+
get state(): Record<string, any>;
|
|
723
|
+
/**
|
|
724
|
+
* Request delegation.
|
|
725
|
+
*/
|
|
726
|
+
acceptsLanguages(): string[];
|
|
727
|
+
acceptsLanguages(languages: string[]): string | false;
|
|
728
|
+
acceptsLanguages(...languages: string[]): string | false;
|
|
729
|
+
acceptsEncodings(): string[];
|
|
730
|
+
acceptsEncodings(encodings: string[]): string | false;
|
|
731
|
+
acceptsEncodings(...encodings: string[]): string | false;
|
|
732
|
+
acceptsCharsets(): string[];
|
|
733
|
+
acceptsCharsets(charsets: string[]): string | false;
|
|
734
|
+
acceptsCharsets(...charsets: string[]): string | false;
|
|
735
|
+
accepts(args: string[]): string | string[] | false;
|
|
736
|
+
accepts(...args: string[]): string | string[] | false;
|
|
737
|
+
get<T = string | string[]>(field: string): T;
|
|
738
|
+
is(type?: string | string[], ...types: string[]): string | false | null;
|
|
739
|
+
get querystring(): string;
|
|
740
|
+
set querystring(str: string);
|
|
741
|
+
get idempotent(): boolean;
|
|
742
|
+
get socket(): RequestSocket;
|
|
743
|
+
get search(): string;
|
|
744
|
+
set search(str: string);
|
|
745
|
+
get method(): string;
|
|
746
|
+
set method(method: string);
|
|
747
|
+
get query(): ParsedUrlQuery;
|
|
748
|
+
set query(obj: ParsedUrlQuery);
|
|
749
|
+
get path(): string;
|
|
750
|
+
set path(path: string);
|
|
751
|
+
get url(): string;
|
|
752
|
+
set url(url: string);
|
|
753
|
+
get accept(): Accepts;
|
|
754
|
+
set accept(accept: Accepts);
|
|
755
|
+
get origin(): string;
|
|
756
|
+
get href(): string;
|
|
757
|
+
get subdomains(): string[];
|
|
758
|
+
get protocol(): string;
|
|
759
|
+
get host(): string;
|
|
760
|
+
get hostname(): string;
|
|
761
|
+
get URL(): URL;
|
|
762
|
+
get header(): http6.IncomingHttpHeaders;
|
|
763
|
+
get headers(): http6.IncomingHttpHeaders;
|
|
764
|
+
get secure(): boolean;
|
|
765
|
+
get stale(): boolean;
|
|
766
|
+
get fresh(): boolean;
|
|
767
|
+
get ips(): string[];
|
|
768
|
+
get ip(): string;
|
|
769
|
+
/**
|
|
770
|
+
* Response delegation.
|
|
771
|
+
*/
|
|
772
|
+
attachment(...args: Parameters<Response['attachment']>): void;
|
|
773
|
+
redirect(...args: Parameters<Response['redirect']>): void;
|
|
774
|
+
remove(...args: Parameters<Response['remove']>): void;
|
|
775
|
+
vary(...args: Parameters<Response['vary']>): void;
|
|
776
|
+
has(...args: Parameters<Response['has']>): boolean;
|
|
777
|
+
set(...args: Parameters<Response['set']>): void;
|
|
778
|
+
append(...args: Parameters<Response['append']>): void;
|
|
779
|
+
flushHeaders(...args: Parameters<Response['flushHeaders']>): void;
|
|
780
|
+
get status(): number;
|
|
781
|
+
set status(status: number);
|
|
782
|
+
get message(): string;
|
|
783
|
+
set message(msg: string);
|
|
784
|
+
get body(): any;
|
|
785
|
+
set body(val: any);
|
|
786
|
+
get length(): number | undefined;
|
|
787
|
+
set length(n: number | string | undefined);
|
|
788
|
+
get type(): string;
|
|
789
|
+
set type(type: string | null | undefined);
|
|
790
|
+
get lastModified(): string | Date | undefined;
|
|
791
|
+
set lastModified(val: string | Date | undefined);
|
|
792
|
+
get etag(): string;
|
|
793
|
+
set etag(val: string);
|
|
794
|
+
get headerSent(): boolean;
|
|
795
|
+
get writable(): boolean;
|
|
796
|
+
}
|
|
797
|
+
//#endregion
|
|
798
|
+
//#region src/application.d.ts
|
|
799
|
+
type ProtoImplClass<T = object> = new (...args: any[]) => T;
|
|
800
|
+
type Next = () => Promise<void>;
|
|
801
|
+
type _MiddlewareFunc<T> = (ctx: T, next: Next) => Promise<void> | void;
|
|
802
|
+
type MiddlewareFunc<T extends Context = Context> = _MiddlewareFunc<T> & {
|
|
803
|
+
_name?: string;
|
|
804
|
+
};
|
|
805
|
+
/**
|
|
806
|
+
* Expose `Application` class.
|
|
807
|
+
* Inherits from `Emitter.prototype`.
|
|
808
|
+
*/
|
|
809
|
+
declare class Application extends Emitter {
|
|
810
|
+
[key: symbol]: unknown;
|
|
811
|
+
/**
|
|
812
|
+
* Make HttpError available to consumers of the library so that consumers don't
|
|
813
|
+
* have a direct dependency upon `http-errors`
|
|
814
|
+
*/
|
|
815
|
+
static HttpError: http_errors0.HttpErrorConstructor<number>;
|
|
816
|
+
protected _proxy: boolean;
|
|
817
|
+
protected _env: string;
|
|
818
|
+
subdomainOffset: number;
|
|
819
|
+
proxyIpHeader: string;
|
|
820
|
+
maxIpsCount: number;
|
|
821
|
+
protected _keys?: string[];
|
|
822
|
+
middleware: MiddlewareFunc<Context>[];
|
|
823
|
+
ctxStorage: AsyncLocalStorage<Context>;
|
|
824
|
+
silent: boolean;
|
|
825
|
+
ContextClass: ProtoImplClass<Context>;
|
|
826
|
+
context: AnyProto;
|
|
827
|
+
RequestClass: ProtoImplClass<Request>;
|
|
828
|
+
request: AnyProto;
|
|
829
|
+
ResponseClass: ProtoImplClass<Response>;
|
|
830
|
+
response: AnyProto;
|
|
831
|
+
/**
|
|
832
|
+
* Initialize a new `Application`.
|
|
833
|
+
*
|
|
834
|
+
* @param {object} [options] Application options
|
|
835
|
+
* @param {string} [options.env] Environment, default is `development`
|
|
836
|
+
* @param {string[]} [options.keys] Signed cookie keys
|
|
837
|
+
* @param {boolean} [options.proxy] Trust proxy headers
|
|
838
|
+
* @param {number} [options.subdomainOffset] Subdomain offset
|
|
839
|
+
* @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
|
|
840
|
+
* @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
|
|
841
|
+
*/
|
|
842
|
+
constructor(options?: {
|
|
843
|
+
proxy?: boolean;
|
|
844
|
+
subdomainOffset?: number;
|
|
845
|
+
proxyIpHeader?: string;
|
|
846
|
+
maxIpsCount?: number;
|
|
847
|
+
env?: string;
|
|
848
|
+
keys?: string[];
|
|
849
|
+
});
|
|
850
|
+
get keys(): string[] | undefined;
|
|
851
|
+
set keys(value: string[] | undefined);
|
|
852
|
+
get env(): string;
|
|
853
|
+
set env(value: string);
|
|
854
|
+
get proxy(): boolean;
|
|
855
|
+
set proxy(value: boolean);
|
|
856
|
+
/**
|
|
857
|
+
* Shorthand for:
|
|
858
|
+
*
|
|
859
|
+
* http.createServer(app.callback()).listen(...)
|
|
860
|
+
*/
|
|
861
|
+
listen(...args: any[]): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
862
|
+
/**
|
|
863
|
+
* Return JSON representation.
|
|
864
|
+
* We only bother showing settings.
|
|
865
|
+
*/
|
|
866
|
+
toJSON(): {
|
|
867
|
+
subdomainOffset: number;
|
|
868
|
+
proxy: boolean;
|
|
869
|
+
env: string;
|
|
870
|
+
};
|
|
871
|
+
/**
|
|
872
|
+
* Inspect implementation.
|
|
873
|
+
*/
|
|
874
|
+
inspect(): {
|
|
875
|
+
subdomainOffset: number;
|
|
876
|
+
proxy: boolean;
|
|
877
|
+
env: string;
|
|
878
|
+
};
|
|
879
|
+
[util.inspect.custom](): {
|
|
880
|
+
subdomainOffset: number;
|
|
881
|
+
proxy: boolean;
|
|
882
|
+
env: string;
|
|
883
|
+
};
|
|
884
|
+
/**
|
|
885
|
+
* Use the given middleware `fn`.
|
|
886
|
+
*/
|
|
887
|
+
use<T extends Context = Context>(fn: MiddlewareFunc<T>): this;
|
|
888
|
+
/**
|
|
889
|
+
* Return a request handler callback
|
|
890
|
+
* for node's native http server.
|
|
891
|
+
*/
|
|
892
|
+
callback(): (req: IncomingMessage, res: ServerResponse) => Promise<void | http.ServerResponse<http.IncomingMessage>>;
|
|
893
|
+
/**
|
|
894
|
+
* return current context from async local storage
|
|
895
|
+
*/
|
|
896
|
+
get currentContext(): Context | undefined;
|
|
897
|
+
/**
|
|
898
|
+
* Handle request in callback.
|
|
899
|
+
* @private
|
|
900
|
+
*/
|
|
901
|
+
protected handleRequest(ctx: Context, fnMiddleware: (ctx: Context) => Promise<void>): Promise<void | http.ServerResponse<http.IncomingMessage>>;
|
|
902
|
+
/**
|
|
903
|
+
* Initialize a new context.
|
|
904
|
+
* @private
|
|
905
|
+
*/
|
|
906
|
+
createContext(req: IncomingMessage, res: ServerResponse): Context;
|
|
907
|
+
/**
|
|
908
|
+
* Default error handler.
|
|
909
|
+
* @private
|
|
910
|
+
*/
|
|
911
|
+
protected onerror(err: CustomError): void;
|
|
912
|
+
/**
|
|
913
|
+
* Response helper.
|
|
914
|
+
*/
|
|
915
|
+
protected _respond(ctx: Context): http.ServerResponse<http.IncomingMessage> | undefined;
|
|
916
|
+
}
|
|
917
|
+
//#endregion
|
|
6
918
|
export { type AnyProto, Application, Context, type CustomError, MiddlewareFunc, Next, ProtoImplClass, Request, RequestSocket, Response, Application as default };
|