expediate 1.0.5 → 1.0.6
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/CHANGELOG.md +138 -0
- package/CONTRIBUTING.md +150 -0
- package/README.md +278 -779
- package/dist/apis.d.ts +372 -12
- package/dist/apis.d.ts.map +1 -1
- package/dist/apis.js +483 -65
- package/dist/apis.js.map +1 -1
- package/dist/cjs/index.js +2290 -807
- package/dist/git.d.ts +1 -1
- package/dist/git.d.ts.map +1 -1
- package/dist/git.js +5 -5
- package/dist/git.js.map +1 -1
- package/dist/http-objects.d.ts +26 -0
- package/dist/http-objects.d.ts.map +1 -0
- package/dist/http-objects.js +588 -0
- package/dist/http-objects.js.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/jwt-auth.d.ts +11 -0
- package/dist/jwt-auth.d.ts.map +1 -1
- package/dist/jwt-auth.js +9 -9
- package/dist/jwt-auth.js.map +1 -1
- package/dist/middleware.js +2 -2
- package/dist/middleware.js.map +1 -1
- package/dist/mimetypes.json +882 -1
- package/dist/misc.d.ts +161 -25
- package/dist/misc.d.ts.map +1 -1
- package/dist/misc.js +228 -80
- package/dist/misc.js.map +1 -1
- package/dist/openapi.d.ts +156 -13
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +214 -71
- package/dist/openapi.js.map +1 -1
- package/dist/router-types.d.ts +760 -0
- package/dist/router-types.d.ts.map +1 -0
- package/dist/router-types.js +23 -0
- package/dist/router-types.js.map +1 -0
- package/dist/router.d.ts +7 -530
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +128 -375
- package/dist/router.js.map +1 -1
- package/dist/static.d.ts +2 -2
- package/dist/static.d.ts.map +1 -1
- package/dist/static.js +77 -22
- package/dist/static.js.map +1 -1
- package/docs/THREAT_MODEL.md +52 -0
- package/docs/api-builder-v2-design.md +644 -0
- package/docs/api-builder-v3-design.md +397 -0
- package/docs/api-builder.md +454 -0
- package/docs/benchmark.md +27 -0
- package/docs/body-parsing.md +223 -0
- package/docs/errors.md +359 -0
- package/docs/expediate.png +0 -0
- package/docs/git.md +139 -0
- package/docs/jwt-auth.md +251 -0
- package/docs/logo.svg +12 -0
- package/docs/middleware.md +264 -0
- package/docs/openapi.md +180 -0
- package/docs/router.md +356 -0
- package/docs/static.md +128 -0
- package/docs/wiki.json +123 -0
- package/package.json +30 -8
- package/dist/cjs/apis.js +0 -327
- package/dist/cjs/git.js +0 -293
- package/dist/cjs/jwt-auth.js +0 -532
- package/dist/cjs/middleware.js +0 -511
- package/dist/cjs/mimetypes.json +0 -1
- package/dist/cjs/misc.js +0 -787
- package/dist/cjs/openapi.js +0 -485
- package/dist/cjs/router.js +0 -898
- package/dist/cjs/static.js +0 -669
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router-types.d.ts","sourceRoot":"","sources":["../src/router-types.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,KAAK,IAAI,MAAO,MAAM,CAAC;AACnC,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEvD,kDAAkD;AAClD,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,aAAc,SAAQ,IAAI,CAAC,eAAe;IACzD,iEAA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 @@
|
|
|
1
|
+
/* Copyright 2021 Fabien Bavent
|
|
2
|
+
*
|
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
4
|
+
* copy of this software and associated documentation files (the "Software"),
|
|
5
|
+
* to deal in the Software without restriction, including without limitation
|
|
6
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
7
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
|
8
|
+
* Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
*
|
|
10
|
+
* The above copyright notice and this permission notice shall be included
|
|
11
|
+
* in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
14
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
16
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19
|
+
* DEALINGS IN THE SOFTWARE.
|
|
20
|
+
*/
|
|
21
|
+
'use strict';
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=router-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router-types.js","sourceRoot":"","sources":["../src/router-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,YAAY,CAAC"}
|
package/dist/router.d.ts
CHANGED
|
@@ -1,531 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as https from 'https';
|
|
3
|
-
import * as http2 from 'http2';
|
|
4
|
-
import { BodyOptions, FormPart } from './misc.js';
|
|
5
|
-
/** A key-value map of arbitrary string values. */
|
|
6
|
-
type StringMap = Record<string, string>;
|
|
7
|
-
/**
|
|
8
|
-
* Options accepted by `createRouter()`.
|
|
9
|
-
*/
|
|
10
|
-
interface RouterOptions {
|
|
11
|
-
/**
|
|
12
|
-
* Secret string used to sign and verify signed cookies.
|
|
13
|
-
*
|
|
14
|
-
* Required when any route calls `res.cookie(name, val, { signed: true })`.
|
|
15
|
-
* When absent, attempting to set a signed cookie throws at runtime.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const app = createRouter({ secret: process.env.COOKIE_SECRET });
|
|
20
|
-
* app.get('/set', (_req, res) =>
|
|
21
|
-
* res.cookie('session', 'user-id', { signed: true }).send('ok'));
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
secret?: string;
|
|
25
|
-
/**
|
|
26
|
-
* Global request timeout in milliseconds.
|
|
27
|
-
*
|
|
28
|
-
* When a request handler does not start writing a response within this
|
|
29
|
-
* period, the socket is marked idle and a **408 Request Timeout** response
|
|
30
|
-
* is sent automatically. The timeout is reset on every response write.
|
|
31
|
-
*
|
|
32
|
-
* Set to `0` or omit to disable the timeout entirely.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* const app = createRouter({ timeout: 30_000 }); // 30 s
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
timeout?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Trust the `X-Forwarded-For` header when resolving `req.ip`.
|
|
42
|
-
*
|
|
43
|
-
* When `true`, `req.ip` is set to the **first** (leftmost) value in the
|
|
44
|
-
* `X-Forwarded-For` header, which is the IP address reported by the
|
|
45
|
-
* outermost client. This is the correct setting when the server sits behind
|
|
46
|
-
* a reverse proxy (e.g. nginx, AWS ALB) that injects this header.
|
|
47
|
-
*
|
|
48
|
-
* When `false` (default), `req.ip` contains the raw socket remote address
|
|
49
|
-
* and the `X-Forwarded-For` header is ignored. Use this mode when the
|
|
50
|
-
* server is directly internet-facing to prevent IP spoofing.
|
|
51
|
-
*
|
|
52
|
-
* @default false
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```ts
|
|
56
|
-
* // Behind a trusted reverse proxy
|
|
57
|
-
* const app = createRouter({ trustProxy: true });
|
|
58
|
-
* app.get('/me', (req, res) => res.send(req.ip));
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
trustProxy?: boolean;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Extended HTTP incoming message that carries parsed routing metadata.
|
|
65
|
-
* Augments the standard `http.IncomingMessage` with fields populated by
|
|
66
|
-
* the router before any middleware is invoked.
|
|
67
|
-
*/
|
|
68
|
-
interface RouterRequest extends http.IncomingMessage {
|
|
69
|
-
/** The original, unmodified URL string from the HTTP request. */
|
|
70
|
-
originalUrl: string;
|
|
71
|
-
/**
|
|
72
|
-
* The current path being matched. Prefix-style layers (`use`)
|
|
73
|
-
* rewrite this field after matching so that nested routers only see the
|
|
74
|
-
* remaining suffix. Exact-method layers (`all`, `get`, `post`, etc.) leave it
|
|
75
|
-
* untouched so that chained middlewares sharing the same path each match.
|
|
76
|
-
*/
|
|
77
|
-
path: string;
|
|
78
|
-
/**
|
|
79
|
-
* Aggregated parameters from all sources.
|
|
80
|
-
* URL query parameters are merged first; named route parameters (from both
|
|
81
|
-
* plain-string and RegExp patterns) override them when a route matches.
|
|
82
|
-
*/
|
|
83
|
-
params: StringMap;
|
|
84
|
-
/**
|
|
85
|
-
* Structured query buckets:
|
|
86
|
-
* - `url` — parameters parsed from the query string. Repeated keys
|
|
87
|
-
* (e.g. `?tag=a&tag=b`) produce an array value.
|
|
88
|
-
* - `route` — named parameters captured from the route pattern.
|
|
89
|
-
*/
|
|
90
|
-
queries: {
|
|
91
|
-
url?: Record<string, string | string[]>;
|
|
92
|
-
route?: StringMap;
|
|
93
|
-
};
|
|
94
|
-
/**
|
|
95
|
-
* Parsed cookies sent with the request.
|
|
96
|
-
*
|
|
97
|
-
* Values are decoded automatically:
|
|
98
|
-
* - `j:` prefixed values are JSON-parsed and returned as their JS type.
|
|
99
|
-
* - `s:` prefixed values are HMAC-verified using the router secret; if
|
|
100
|
-
* valid, the inner value (decoded in turn) is returned. Cookies that
|
|
101
|
-
* fail verification are **not** included in this map.
|
|
102
|
-
* - Plain string values are returned unchanged.
|
|
103
|
-
*/
|
|
104
|
-
cookies: Record<string, unknown>;
|
|
105
|
-
/**
|
|
106
|
-
*
|
|
107
|
-
*/
|
|
108
|
-
body?: any;
|
|
109
|
-
/**
|
|
110
|
-
* Read and parse the request body as JSON.
|
|
111
|
-
*
|
|
112
|
-
* Returns the parsed value, or `null` when the request has no body.
|
|
113
|
-
* Rejects with `{ status, message }` on parse or transport errors.
|
|
114
|
-
*/
|
|
115
|
-
json(opts?: BodyOptions): Promise<unknown | null>;
|
|
116
|
-
/**
|
|
117
|
-
* The IP address of the remote client.
|
|
118
|
-
*
|
|
119
|
-
* - When the router is created with `{ trustProxy: true }`, this is the
|
|
120
|
-
* **first** value from the `X-Forwarded-For` header (the originating
|
|
121
|
-
* client address as reported by the proxy chain).
|
|
122
|
-
* - Otherwise this is the raw TCP socket remote address
|
|
123
|
-
* (`req.socket?.remoteAddress`), which cannot be spoofed by the client.
|
|
124
|
-
*
|
|
125
|
-
* Always an empty string when neither source is available.
|
|
126
|
-
*/
|
|
127
|
-
ip: string;
|
|
128
|
-
/**
|
|
129
|
-
* Read and decode the request body as plain text.
|
|
130
|
-
*
|
|
131
|
-
* Returns the body string (decoded using the charset in `Content-Type`,
|
|
132
|
-
* defaulting to UTF-8), or `null` when the request has no body.
|
|
133
|
-
* Rejects with `{ status, message }` on transport errors.
|
|
134
|
-
*/
|
|
135
|
-
text(opts?: BodyOptions): Promise<string | null>;
|
|
136
|
-
/**
|
|
137
|
-
* Read and parse the request body as `multipart/form-data`.
|
|
138
|
-
*
|
|
139
|
-
* Returns an array of {@link FormPart} objects, or `null` when the request
|
|
140
|
-
* has no body. Rejects with `{ status, message }` on parse or transport
|
|
141
|
-
* errors.
|
|
142
|
-
*/
|
|
143
|
-
formData(opts?: BodyOptions): Promise<FormPart[] | null>;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Extended HTTP server response with convenience helpers.
|
|
147
|
-
* Augments the standard `http.ServerResponse`.
|
|
148
|
-
*/
|
|
149
|
-
interface RouterResponse extends http.ServerResponse {
|
|
150
|
-
/**
|
|
151
|
-
* Write `data` (if provided) and end the response.
|
|
152
|
-
* Equivalent to `res.write(data); res.end()`.
|
|
153
|
-
*/
|
|
154
|
-
send(data?: string): void;
|
|
155
|
-
/** Serialise `data` as JSON, set `Content-Type: application/json`, and end. */
|
|
156
|
-
json(data: unknown): void;
|
|
157
|
-
/**
|
|
158
|
-
* Set the HTTP status code and optional response headers, then return
|
|
159
|
-
* `this` so calls can be chained (e.g. `res.status(404).end(...)`).
|
|
160
|
-
*/
|
|
161
|
-
status(code: number, headers?: StringMap): this;
|
|
162
|
-
/** Redirect the client to `url` with a 302 Found response. */
|
|
163
|
-
redirect(url: string): void;
|
|
164
|
-
/**
|
|
165
|
-
* Append a `Set-Cookie` header for the given `name`/`value` pair.
|
|
166
|
-
* Returns `this` to allow chaining.
|
|
167
|
-
*/
|
|
168
|
-
cookie(name: string, value: string | object, options?: CookieOptions): this;
|
|
169
|
-
/**
|
|
170
|
-
* Trigger a file download in the client's browser.
|
|
171
|
-
*
|
|
172
|
-
* Sets the `Content-Disposition: attachment` header (which prompts a
|
|
173
|
-
* "Save As" dialog in browsers), then streams the file at `filepath` using
|
|
174
|
-
* {@link sendFile}.
|
|
175
|
-
*
|
|
176
|
-
* @param filepath - Absolute or relative filesystem path to the file to send.
|
|
177
|
-
* @param filename - Override the file name advertised to the browser.
|
|
178
|
-
* Defaults to `path.basename(filepath)`.
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```ts
|
|
182
|
-
* app.get('/invoice', (_req, res) =>
|
|
183
|
-
* res.download('/var/reports/2024-Q1.pdf', 'invoice-2024-Q1.pdf'));
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
download(filepath: string, filename?: string): void;
|
|
187
|
-
/**
|
|
188
|
-
* Set the `Content-Type` response header and return `this` for chaining.
|
|
189
|
-
*
|
|
190
|
-
* The value is set verbatim — include the charset when needed
|
|
191
|
-
* (e.g. `'text/html; charset=utf-8'`).
|
|
192
|
-
*
|
|
193
|
-
* @param mime - The MIME type string to set.
|
|
194
|
-
* @returns `this` for chaining.
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* res.type('text/csv').send(csvData);
|
|
199
|
-
* res.type('application/octet-stream').send(binaryData);
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
type(mime: string): this;
|
|
203
|
-
/**
|
|
204
|
-
* Set the `ETag` response header.
|
|
205
|
-
*
|
|
206
|
-
* By default a **weak** ETag is produced (`W/"value"`), which indicates that
|
|
207
|
-
* the two representations are semantically equivalent but not byte-for-byte
|
|
208
|
-
* identical. Pass `true` as the second argument for a **strong** ETag
|
|
209
|
-
* (`"value"`), which implies byte-level equivalence and is required when
|
|
210
|
-
* byte-range requests must be validated.
|
|
211
|
-
*
|
|
212
|
-
* Returns `this` so calls can be chained before `res.send()` or `res.json()`.
|
|
213
|
-
*
|
|
214
|
-
* @param value - The opaque ETag value (without quotes or `W/` prefix).
|
|
215
|
-
* @param strong - When `true`, produce a strong ETag. Defaults to `false`.
|
|
216
|
-
* @returns `this` for chaining.
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* ```ts
|
|
220
|
-
* // Weak ETag (default) — most appropriate for dynamic responses
|
|
221
|
-
* res.etag(user.updatedAt.toISOString()).json(user);
|
|
222
|
-
*
|
|
223
|
-
* // Strong ETag — use when the response body is content-addressed
|
|
224
|
-
* res.etag(sha256hex, true).send(fileContent);
|
|
225
|
-
* ```
|
|
226
|
-
*/
|
|
227
|
-
etag(value: string, strong?: boolean): this;
|
|
228
|
-
}
|
|
229
|
-
/** Options accepted by `res.cookie()`. */
|
|
230
|
-
interface CookieOptions {
|
|
231
|
-
/**
|
|
232
|
-
* Sign the cookie value with HMAC-SHA256 using the router's `secret`.
|
|
233
|
-
* Requires `secret` to be passed to `createRouter()`.
|
|
234
|
-
*/
|
|
235
|
-
signed?: boolean;
|
|
236
|
-
/** Expiry date for the cookie. */
|
|
237
|
-
expires?: Date;
|
|
238
|
-
/** Max age in milliseconds; converted to seconds in the `Set-Cookie` header. */
|
|
239
|
-
maxAge?: number;
|
|
240
|
-
/** Cookie path (defaults to `'/'`). */
|
|
241
|
-
path?: string;
|
|
242
|
-
/** Marks the cookie as `HttpOnly` (not accessible via `document.cookie`). */
|
|
243
|
-
httpOnly?: boolean;
|
|
244
|
-
/** Marks the cookie as `Secure` (only sent over HTTPS). */
|
|
245
|
-
secure?: boolean;
|
|
246
|
-
/** `SameSite` attribute value (`'Strict'`, `'Lax'`, or `'None'`). */
|
|
247
|
-
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Options for HTTPS / HTTP/2 servers passed to `router.listen()`.
|
|
251
|
-
*
|
|
252
|
-
* When both `key` and `cert` are present, `listen()` creates a TLS server.
|
|
253
|
-
* Setting `http2: true` additionally upgrades to HTTP/2 (`http2.createSecureServer`).
|
|
254
|
-
*/
|
|
255
|
-
interface TlsOptions {
|
|
256
|
-
key: string | Buffer;
|
|
257
|
-
cert: string | Buffer;
|
|
258
|
-
/**
|
|
259
|
-
* When `true`, an HTTP/2 secure server (`http2.createSecureServer`) is
|
|
260
|
-
* created instead of an HTTPS/1.1 server. Requires `key` and `cert`.
|
|
261
|
-
* The existing middleware API is compatible with HTTP/2 request/response
|
|
262
|
-
* objects at runtime.
|
|
263
|
-
*/
|
|
264
|
-
http2?: boolean;
|
|
265
|
-
[key: string]: unknown;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* A middleware function. Receives the current request and response objects
|
|
269
|
-
* and a `next` callback to hand control to the next registered middleware.
|
|
270
|
-
*/
|
|
271
|
-
type Middleware = (req: RouterRequest, res: RouterResponse, next: NextFunction) => void;
|
|
272
|
-
/**
|
|
273
|
-
* Callback used to pass control to the next matching middleware.
|
|
274
|
-
*
|
|
275
|
-
* When called **without** arguments (or with `undefined`), the router
|
|
276
|
-
* continues to the next matching layer as usual.
|
|
277
|
-
*
|
|
278
|
-
* When called **with** a non-null argument, the argument is treated as an
|
|
279
|
-
* error: remaining middleware and routes are skipped and the handler
|
|
280
|
-
* registered via {@link Router.onError} is invoked. If no error handler
|
|
281
|
-
* has been registered, a plain **500** response is sent.
|
|
282
|
-
*
|
|
283
|
-
* @example
|
|
284
|
-
* ```ts
|
|
285
|
-
* router.use('/protected', (req, _res, next) => {
|
|
286
|
-
* if (!req.headers.authorization) return next(new Error('Unauthorized'));
|
|
287
|
-
* next(); // proceed normally
|
|
288
|
-
* });
|
|
289
|
-
* ```
|
|
290
|
-
*/
|
|
291
|
-
type NextFunction = (err?: unknown) => void;
|
|
292
|
-
/**
|
|
293
|
-
* Handler invoked when a middleware throws, an async middleware rejects,
|
|
294
|
-
* or `next(err)` is called with a non-null argument.
|
|
295
|
-
*
|
|
296
|
-
* Register it with {@link Router.onError}.
|
|
297
|
-
*
|
|
298
|
-
* @param err - The thrown value or the argument passed to `next()`.
|
|
299
|
-
* @param req - The current request.
|
|
300
|
-
* @param res - The current response (not yet ended — the handler must end it).
|
|
301
|
-
*/
|
|
302
|
-
type ErrorHandler = (err: unknown, req: RouterRequest, res: RouterResponse) => void;
|
|
303
|
-
/**
|
|
304
|
-
* A value that can be registered as a route handler: a single `Middleware`
|
|
305
|
-
* function, a `Router` instance (whose `listener` will be used), or an array
|
|
306
|
-
* of either. Arrays may not be nested.
|
|
307
|
-
*/
|
|
308
|
-
type MiddlewareArg = Middleware | Router | (Middleware | Router)[];
|
|
309
|
-
/**
|
|
310
|
-
* Sanitised description of a single registered route, as returned by
|
|
311
|
-
* {@link Router.routes}.
|
|
312
|
-
*/
|
|
313
|
-
interface RouteInfo {
|
|
314
|
-
/** HTTP method this layer is restricted to, or `null` for any method. */
|
|
315
|
-
method: string | null;
|
|
316
|
-
/** The original path pattern as supplied by the caller. */
|
|
317
|
-
path: string | RegExp;
|
|
318
|
-
/**
|
|
319
|
-
* `true` for prefix-style (`use`) registrations where the matched prefix
|
|
320
|
-
* is stripped from `req.path`; `false` for exact-method routes.
|
|
321
|
-
*/
|
|
322
|
-
stripPath: boolean;
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* Internal representation of a single registered route.
|
|
326
|
-
* One layer is created per middleware function for every `router.get(...)` /
|
|
327
|
-
* `router.use(...)` etc. call, and stored in the route table.
|
|
328
|
-
*/
|
|
329
|
-
interface Layer {
|
|
330
|
-
/** HTTP method this layer is restricted to, or `null` for any method. */
|
|
331
|
-
method: string | null;
|
|
332
|
-
/** The original path pattern supplied by the caller. */
|
|
333
|
-
path: string | RegExp;
|
|
334
|
-
/**
|
|
335
|
-
* The compiled `RegExp` used for matching, regardless of whether the
|
|
336
|
-
* original `path` was a plain string, a glob, or already a `RegExp`.
|
|
337
|
-
*
|
|
338
|
-
* - Plain strings (e.g. `'/users/:id'`) are compiled with named capture
|
|
339
|
-
* groups so that `:id` becomes `(?<id>[^/]+)`.
|
|
340
|
-
* - Glob strings (e.g. `'/**\/*.php'`) are compiled following `.gitignore`
|
|
341
|
-
* wildcard rules.
|
|
342
|
-
* - `RegExp` values are stored as-is; named groups are surfaced directly
|
|
343
|
-
* as route parameters.
|
|
344
|
-
*/
|
|
345
|
-
regex: RegExp;
|
|
346
|
-
/**
|
|
347
|
-
* When `true`, the portion of `req.path` consumed by `layer.regex` is
|
|
348
|
-
* stripped before invoking the middleware, exposing only the remaining
|
|
349
|
-
* suffix to nested routers.
|
|
350
|
-
*
|
|
351
|
-
* Set to `true` for prefix-style registrations (`use`) and
|
|
352
|
-
* `false` for exact-method routes (`all`, `get`, `post`, etc.). Stripping on
|
|
353
|
-
* exact-match routes would break chained middlewares sharing the same path,
|
|
354
|
-
* because each subsequent layer would see a truncated path that no longer
|
|
355
|
-
* matches its own pattern.
|
|
356
|
-
*/
|
|
357
|
-
stripPath: boolean;
|
|
358
|
-
/** The middleware function to invoke when the layer matches. */
|
|
359
|
-
middleware: Middleware;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* The public interface of the object returned by `createRouter()`.
|
|
363
|
-
*
|
|
364
|
-
* All route-registration methods share the same uniform signature: a mandatory
|
|
365
|
-
* `path` argument followed by any number of `MiddlewareArg` values.
|
|
366
|
-
* A `MiddlewareArg` may be a `Middleware` function, a `Router` instance
|
|
367
|
-
* (whose `listener` is used automatically), or an array of either.
|
|
368
|
-
*/
|
|
369
|
-
interface Router {
|
|
370
|
-
/**
|
|
371
|
-
* The path prefix this router was created with, if any.
|
|
372
|
-
*
|
|
373
|
-
* Set by passing a string as the first argument to `createRouter()`:
|
|
374
|
-
* ```ts
|
|
375
|
-
* const v1 = createRouter('/api/v1');
|
|
376
|
-
* console.log(v1.prefix); // '/api/v1'
|
|
377
|
-
* ```
|
|
378
|
-
* When a prefixed router is passed to `parent.use(v1)`, the parent uses
|
|
379
|
-
* this prefix as the mount path automatically.
|
|
380
|
-
*/
|
|
381
|
-
readonly prefix?: string;
|
|
382
|
-
/**
|
|
383
|
-
* Register prefix-style middleware scoped to `path`.
|
|
384
|
-
*
|
|
385
|
-
* The matched path prefix is stripped from `req.path` before the middleware
|
|
386
|
-
* runs, so nested routers only see the remaining suffix.
|
|
387
|
-
* Equivalent to Express's `app.use()`.
|
|
388
|
-
*
|
|
389
|
-
* **No-path shortcut:** when the first argument is a `Router` or `Middleware`
|
|
390
|
-
* (not a string or RegExp), the path defaults to the router's own
|
|
391
|
-
* {@link prefix} (or `'/'` if no prefix was set). This lets you mount a
|
|
392
|
-
* prefixed sub-router without repeating the path:
|
|
393
|
-
*
|
|
394
|
-
* ```ts
|
|
395
|
-
* const v1 = createRouter('/api/v1');
|
|
396
|
-
* v1.get('/users', handler);
|
|
397
|
-
* app.use(v1); // same as app.use('/api/v1', v1)
|
|
398
|
-
* ```
|
|
399
|
-
*/
|
|
400
|
-
use(path: string | RegExp | MiddlewareArg, ...args: MiddlewareArg[]): void;
|
|
401
|
-
/**
|
|
402
|
-
* Register middleware for all HTTP methods without stripping `req.path`.
|
|
403
|
-
* Unlike `use`, the full path remains visible to every chained middleware.
|
|
404
|
-
*/
|
|
405
|
-
all(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
406
|
-
/** Register middleware for `GET` requests. */
|
|
407
|
-
get(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
408
|
-
/** Register middleware for `PUT` requests. */
|
|
409
|
-
put(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
410
|
-
/** Register middleware for `POST` requests. */
|
|
411
|
-
post(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
412
|
-
/** Register middleware for `DELETE` requests. */
|
|
413
|
-
delete(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
414
|
-
/** Register middleware for `PATCH` requests. */
|
|
415
|
-
patch(path: string | RegExp, ...args: MiddlewareArg[]): void;
|
|
416
|
-
/**
|
|
417
|
-
* Register a global error handler for this router.
|
|
418
|
-
*
|
|
419
|
-
* The handler is called whenever:
|
|
420
|
-
* - A middleware throws synchronously.
|
|
421
|
-
* - An `async` middleware returns a rejected `Promise`.
|
|
422
|
-
* - Any middleware calls `next(err)` with a non-null argument.
|
|
423
|
-
*
|
|
424
|
-
* Only one handler is active at a time; subsequent calls replace the
|
|
425
|
-
* previous one. The handler **must** end the response.
|
|
426
|
-
*
|
|
427
|
-
* @example
|
|
428
|
-
* ```ts
|
|
429
|
-
* app.onError((err, _req, res) => {
|
|
430
|
-
* const status = (err as any)?.status ?? 500;
|
|
431
|
-
* res.status(status).json({ error: String(err) });
|
|
432
|
-
* });
|
|
433
|
-
* ```
|
|
434
|
-
*/
|
|
435
|
-
onError(handler: ErrorHandler): void;
|
|
436
|
-
/**
|
|
437
|
-
* Register a custom handler for requests that match no registered route.
|
|
438
|
-
*
|
|
439
|
-
* When no route matches and no `done()` callback was supplied to the
|
|
440
|
-
* listener, this handler is invoked instead of the built-in
|
|
441
|
-
* `Cannot METHOD /path` 404 response.
|
|
442
|
-
*
|
|
443
|
-
* Registering a not-found handler is an explicit, documented alternative to
|
|
444
|
-
* the `app.all('*', handler)` workaround — without the subtle path-stripping
|
|
445
|
-
* interactions that glob `use()` layers introduce.
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```ts
|
|
449
|
-
* app.setNotFound((_req, res) =>
|
|
450
|
-
* res.status(404).json({ error: 'Not Found' }));
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
setNotFound(handler: Middleware): void;
|
|
454
|
-
/**
|
|
455
|
-
* Return a read-only snapshot of all routes registered on this router.
|
|
456
|
-
*
|
|
457
|
-
* Useful for tooling, documentation generation, and debugging. The array
|
|
458
|
-
* is a fresh copy — mutating it does not affect the live route table.
|
|
459
|
-
*
|
|
460
|
-
* @returns An array of {@link RouteInfo} objects, one per registered layer,
|
|
461
|
-
* in registration order.
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
* ```ts
|
|
465
|
-
* app.get('/users', handler);
|
|
466
|
-
* app.post('/users', handler);
|
|
467
|
-
* console.log(app.routes());
|
|
468
|
-
* // [
|
|
469
|
-
* // { method: 'GET', path: '/users', stripPath: false },
|
|
470
|
-
* // { method: 'POST', path: '/users', stripPath: false },
|
|
471
|
-
* // ]
|
|
472
|
-
* ```
|
|
473
|
-
*/
|
|
474
|
-
routes(): RouteInfo[];
|
|
475
|
-
/**
|
|
476
|
-
* Gracefully shut down the HTTP(S) server created by `router.listen()`.
|
|
477
|
-
*
|
|
478
|
-
* Stops accepting new connections and waits for all existing connections to
|
|
479
|
-
* close naturally. After `timeout` milliseconds, any remaining sockets are
|
|
480
|
-
* forcibly destroyed so the process is not kept alive indefinitely.
|
|
481
|
-
*
|
|
482
|
-
* If `router.listen()` was never called on this router (e.g. it is a
|
|
483
|
-
* sub-router mounted inside a parent), `shutdown()` resolves immediately
|
|
484
|
-
* without doing anything.
|
|
485
|
-
*
|
|
486
|
-
* @param timeout - Grace period in milliseconds before sockets are forcibly
|
|
487
|
-
* destroyed. Defaults to `5000`. Pass `0` to skip the forced teardown.
|
|
488
|
-
* @returns A `Promise` that resolves when the server has fully stopped.
|
|
489
|
-
*
|
|
490
|
-
* @example
|
|
491
|
-
* ```ts
|
|
492
|
-
* const app = createRouter();
|
|
493
|
-
* app.listen(3000);
|
|
494
|
-
* process.on('SIGTERM', () => app.shutdown(10_000));
|
|
495
|
-
* ```
|
|
496
|
-
*/
|
|
497
|
-
shutdown(timeout?: number): Promise<void>;
|
|
498
|
-
/**
|
|
499
|
-
* Start listening on the given port and return the underlying server instance.
|
|
500
|
-
*
|
|
501
|
-
* When `opts` contains both `key` and `cert`:
|
|
502
|
-
* - An **HTTPS** server is created (TLS, HTTP/1.1).
|
|
503
|
-
* - Setting `opts.http2 = true` additionally upgrades to **HTTP/2**
|
|
504
|
-
* (`http2.createSecureServer`).
|
|
505
|
-
*
|
|
506
|
-
* When `opts` is absent or contains neither key nor cert, a plain **HTTP**
|
|
507
|
-
* server is used.
|
|
508
|
-
*
|
|
509
|
-
* @returns The underlying server instance. Use it for graceful shutdown,
|
|
510
|
-
* port discovery, or attaching additional event listeners.
|
|
511
|
-
*
|
|
512
|
-
* @example
|
|
513
|
-
* ```ts
|
|
514
|
-
* // Discover the OS-assigned ephemeral port
|
|
515
|
-
* const server = router.listen(0, () => {
|
|
516
|
-
* const { port } = server.address() as AddressInfo;
|
|
517
|
-
* console.log(`Listening on port ${port}`);
|
|
518
|
-
* });
|
|
519
|
-
* ```
|
|
520
|
-
*/
|
|
521
|
-
listen(port: number, opts?: TlsOptions | (() => void), cb?: () => void): http.Server | https.Server | http2.Http2SecureServer;
|
|
522
|
-
/**
|
|
523
|
-
* The underlying `(req, res, next)` function, allowing this router to be
|
|
524
|
-
* mounted as middleware inside another router:
|
|
525
|
-
* `parent.use('/api', child)`.
|
|
526
|
-
*/
|
|
527
|
-
readonly listener: Middleware;
|
|
528
|
-
}
|
|
1
|
+
import type { RouterOptions, Router } from './router-types.js';
|
|
529
2
|
/**
|
|
530
3
|
* Create and return a new `Router` instance.
|
|
531
4
|
*
|
|
@@ -574,7 +47,11 @@ interface Router {
|
|
|
574
47
|
*
|
|
575
48
|
* app.use(v1);
|
|
576
49
|
* app.onError((err, _req, res) => res.status(500).json({ error: String(err) }));
|
|
577
|
-
*
|
|
50
|
+
*
|
|
51
|
+
* // Custom 404: register a catch-all as the LAST layer. Because layers match
|
|
52
|
+
* // in registration order, it only runs when nothing earlier claimed the
|
|
53
|
+
* // request. Use `all('/**', …)` (glob) to match any method and path.
|
|
54
|
+
* app.all('/**', (_req, res) => res.status(404).json({ error: 'Not Found' }));
|
|
578
55
|
*
|
|
579
56
|
* app.listen(3000, () => console.log('Listening'));
|
|
580
57
|
* process.on('SIGTERM', () => app.shutdown(10_000));
|
|
@@ -582,5 +59,5 @@ interface Router {
|
|
|
582
59
|
*/
|
|
583
60
|
declare function createRouter(prefixOrOpts?: string | RouterOptions, opts?: RouterOptions): Router;
|
|
584
61
|
export default createRouter;
|
|
585
|
-
export type { Router, RouterOptions, RouterRequest, RouterResponse, Middleware, MiddlewareArg, NextFunction, ErrorHandler, Layer, RouteInfo, CookieOptions, TlsOptions, StringMap, };
|
|
62
|
+
export type { Router, RouterOptions, RouterRequest, RouterResponse, Middleware, MiddlewareArg, NextFunction, ErrorHandler, ErrorMiddleware, Layer, RouteInfo, RouteBuilder, CookieOptions, TlsOptions, StringMap, } from './router-types.js';
|
|
586
63
|
//# sourceMappingURL=router.d.ts.map
|
package/dist/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAEV,aAAa,EAYb,MAAM,EACP,MAAM,mBAAmB,CAAC;AAoZ3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,iBAAS,YAAY,CACnB,YAAY,CAAC,EAAE,MAAM,GAAG,aAAa,EACrC,IAAI,CAAC,EAAE,aAAa,GACnB,MAAM,CA2WR;AAED,eAAe,YAAY,CAAC;AAC5B,YAAY,EACV,MAAM,EACN,aAAa,EACb,aAAa,EACb,cAAc,EACd,UAAU,EACV,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,KAAK,EACL,SAAS,EACT,YAAY,EACZ,aAAa,EACb,UAAU,EACV,SAAS,GACV,MAAM,mBAAmB,CAAC"}
|