@koa/router 13.1.1 → 15.0.0
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/LICENSE +1 -1
- package/README.md +1136 -41
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1378 -0
- package/dist/index.mjs +1341 -0
- package/dist/layer.d.mts +761 -0
- package/dist/layer.d.ts +761 -0
- package/dist/layer.js +457 -0
- package/dist/layer.mjs +436 -0
- package/dist/router.d.mts +3 -0
- package/dist/router.d.ts +3 -0
- package/dist/router.js +1371 -0
- package/dist/router.mjs +1340 -0
- package/dist/types.d.mts +3 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/package.json +58 -30
- package/lib/API_tpl.hbs +0 -7
- package/lib/layer.js +0 -240
- package/lib/router.js +0 -824
package/dist/layer.d.mts
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import * as koa from 'koa';
|
|
2
|
+
import { Middleware, DefaultState, DefaultContext, ParameterizedContext } from 'koa';
|
|
3
|
+
import { Key } from 'path-to-regexp';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extended middleware with param metadata
|
|
7
|
+
*/
|
|
8
|
+
interface ParameterMiddleware extends Function {
|
|
9
|
+
param?: string;
|
|
10
|
+
_originalFn?: RouterParameterMiddleware;
|
|
11
|
+
}
|
|
12
|
+
declare class Layer {
|
|
13
|
+
opts: LayerOptions;
|
|
14
|
+
name: string | undefined;
|
|
15
|
+
methods: string[];
|
|
16
|
+
paramNames: Key[];
|
|
17
|
+
stack: (RouterMiddleware | ParameterMiddleware)[];
|
|
18
|
+
path: string | RegExp;
|
|
19
|
+
regexp: RegExp;
|
|
20
|
+
/**
|
|
21
|
+
* Initialize a new routing Layer with given `method`, `path`, and `middleware`.
|
|
22
|
+
*
|
|
23
|
+
* @param path - Path string or regular expression
|
|
24
|
+
* @param methods - Array of HTTP verbs
|
|
25
|
+
* @param middleware - Layer callback/middleware or series of
|
|
26
|
+
* @param opts - Layer options
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
constructor(path: string | RegExp, methods: string[], middleware: RouterMiddleware<any, any> | RouterMiddleware<any, any>[], options?: LayerOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Normalize HTTP methods and add automatic HEAD support for GET
|
|
32
|
+
* @private
|
|
33
|
+
*/
|
|
34
|
+
private _normalizeHttpMethods;
|
|
35
|
+
/**
|
|
36
|
+
* Normalize middleware to array and validate all are functions
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
private _normalizeAndValidateMiddleware;
|
|
40
|
+
/**
|
|
41
|
+
* Configure path matching regexp and parameters
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
44
|
+
private _configurePathMatching;
|
|
45
|
+
/**
|
|
46
|
+
* Configure path-to-regexp for string paths
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private _configurePathToRegexp;
|
|
50
|
+
/**
|
|
51
|
+
* Returns whether request `path` matches route.
|
|
52
|
+
*
|
|
53
|
+
* @param path - Request path
|
|
54
|
+
* @returns Whether path matches
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
match(path: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Returns map of URL parameters for given `path` and `paramNames`.
|
|
60
|
+
*
|
|
61
|
+
* @param _path - Request path (not used, kept for API compatibility)
|
|
62
|
+
* @param captures - Captured values from regexp
|
|
63
|
+
* @param existingParams - Existing params to merge with
|
|
64
|
+
* @returns Parameter map
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
params(_path: string, captures: string[], existingParameters?: Record<string, string>): Record<string, string>;
|
|
68
|
+
/**
|
|
69
|
+
* Returns array of regexp url path captures.
|
|
70
|
+
*
|
|
71
|
+
* @param path - Request path
|
|
72
|
+
* @returns Array of captured values
|
|
73
|
+
* @private
|
|
74
|
+
*/
|
|
75
|
+
captures(path: string): string[];
|
|
76
|
+
/**
|
|
77
|
+
* Generate URL for route using given `params`.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
*
|
|
81
|
+
* ```javascript
|
|
82
|
+
* const route = new Layer('/users/:id', ['GET'], fn);
|
|
83
|
+
*
|
|
84
|
+
* route.url({ id: 123 }); // => "/users/123"
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param args - URL parameters (various formats supported)
|
|
88
|
+
* @returns Generated URL
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
url(...arguments_: any[]): string;
|
|
92
|
+
/**
|
|
93
|
+
* Parse url() arguments into params and options
|
|
94
|
+
* Supports multiple call signatures:
|
|
95
|
+
* - url({ id: 1 })
|
|
96
|
+
* - url(1, 2, 3)
|
|
97
|
+
* - url({ query: {...} })
|
|
98
|
+
* - url({ id: 1 }, { query: {...} })
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
private _parseUrlArguments;
|
|
102
|
+
/**
|
|
103
|
+
* Build parameter replacements for URL generation
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
private _buildParamReplacements;
|
|
107
|
+
/**
|
|
108
|
+
* Add query string to URL
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
111
|
+
private _addQueryString;
|
|
112
|
+
/**
|
|
113
|
+
* Run validations on route named parameters.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
*
|
|
117
|
+
* ```javascript
|
|
118
|
+
* router
|
|
119
|
+
* .param('user', function (id, ctx, next) {
|
|
120
|
+
* ctx.user = users[id];
|
|
121
|
+
* if (!ctx.user) return ctx.status = 404;
|
|
122
|
+
* next();
|
|
123
|
+
* })
|
|
124
|
+
* .get('/users/:user', function (ctx, next) {
|
|
125
|
+
* ctx.body = ctx.user;
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @param paramName - Parameter name
|
|
130
|
+
* @param paramHandler - Middleware function
|
|
131
|
+
* @returns This layer instance
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
param(parameterName: string, parameterHandler: RouterParameterMiddleware): Layer;
|
|
135
|
+
/**
|
|
136
|
+
* Create param middleware with deduplication tracking
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
private _createParamMiddleware;
|
|
140
|
+
/**
|
|
141
|
+
* Insert param middleware at the correct position in the stack
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
private _insertParamMiddleware;
|
|
145
|
+
/**
|
|
146
|
+
* Prefix route path.
|
|
147
|
+
*
|
|
148
|
+
* @param prefixPath - Prefix to prepend
|
|
149
|
+
* @returns This layer instance
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
setPrefix(prefixPath: string): Layer;
|
|
153
|
+
/**
|
|
154
|
+
* Apply prefix to the current path
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
private _applyPrefix;
|
|
158
|
+
/**
|
|
159
|
+
* Reconfigure path matching after prefix is applied
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
private _reconfigurePathMatching;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Middleware with router property
|
|
167
|
+
*/
|
|
168
|
+
interface RouterComposedMiddleware<StateT = koa.DefaultState, ContextT = koa.DefaultContext> extends Middleware<StateT, ContextT & RouterParameterContext<StateT, ContextT>> {
|
|
169
|
+
router?: Router<StateT, ContextT>;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* @module koa-router
|
|
173
|
+
*/
|
|
174
|
+
declare class Router<StateT = koa.DefaultState, ContextT = koa.DefaultContext> {
|
|
175
|
+
opts: RouterOptions;
|
|
176
|
+
methods: string[];
|
|
177
|
+
exclusive: boolean;
|
|
178
|
+
params: Record<string, RouterParameterMiddleware<StateT, ContextT> | RouterParameterMiddleware<StateT, ContextT>[]>;
|
|
179
|
+
stack: Layer[];
|
|
180
|
+
host?: string | string[] | RegExp;
|
|
181
|
+
/**
|
|
182
|
+
* Create a new router.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
*
|
|
186
|
+
* Basic usage:
|
|
187
|
+
*
|
|
188
|
+
* ```javascript
|
|
189
|
+
* const Koa = require('koa');
|
|
190
|
+
* const Router = require('@koa/router');
|
|
191
|
+
*
|
|
192
|
+
* const app = new Koa();
|
|
193
|
+
* const router = new Router();
|
|
194
|
+
*
|
|
195
|
+
* router.get('/', (ctx, next) => {
|
|
196
|
+
* // ctx.router available
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* app
|
|
200
|
+
* .use(router.routes())
|
|
201
|
+
* .use(router.allowedMethods());
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @alias module:koa-router
|
|
205
|
+
* @param opts - Router options
|
|
206
|
+
* @constructor
|
|
207
|
+
*/
|
|
208
|
+
constructor(options?: RouterOptions);
|
|
209
|
+
/**
|
|
210
|
+
* Generate URL from url pattern and given `params`.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
*
|
|
214
|
+
* ```javascript
|
|
215
|
+
* const url = Router.url('/users/:id', {id: 1});
|
|
216
|
+
* // => "/users/1"
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* @param path - URL pattern
|
|
220
|
+
* @param args - URL parameters
|
|
221
|
+
* @returns Generated URL
|
|
222
|
+
*/
|
|
223
|
+
static url(path: string | RegExp, ...arguments_: any[]): string;
|
|
224
|
+
/**
|
|
225
|
+
* Use given middleware.
|
|
226
|
+
*
|
|
227
|
+
* Middleware run in the order they are defined by `.use()`. They are invoked
|
|
228
|
+
* sequentially, requests start at the first middleware and work their way
|
|
229
|
+
* "down" the middleware stack.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
*
|
|
233
|
+
* ```javascript
|
|
234
|
+
* // session middleware will run before authorize
|
|
235
|
+
* router
|
|
236
|
+
* .use(session())
|
|
237
|
+
* .use(authorize());
|
|
238
|
+
*
|
|
239
|
+
* // use middleware only with given path
|
|
240
|
+
* router.use('/users', userAuth());
|
|
241
|
+
*
|
|
242
|
+
* // or with an array of paths
|
|
243
|
+
* router.use(['/users', '/admin'], userAuth());
|
|
244
|
+
*
|
|
245
|
+
* app.use(router.routes());
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @param middleware - Middleware functions
|
|
249
|
+
* @returns This router instance
|
|
250
|
+
*/
|
|
251
|
+
use(...middleware: Array<RouterMiddleware<StateT, ContextT> | RouterComposedMiddleware<StateT, ContextT>>): Router<StateT, ContextT>;
|
|
252
|
+
use(path: string | RegExp | string[], ...middleware: Array<RouterMiddleware<StateT, ContextT> | RouterComposedMiddleware<StateT, ContextT>>): Router<StateT, ContextT>;
|
|
253
|
+
/**
|
|
254
|
+
* Check if first argument is an array of paths
|
|
255
|
+
* @private
|
|
256
|
+
*/
|
|
257
|
+
private _isPathArray;
|
|
258
|
+
/**
|
|
259
|
+
* Check if first argument is an explicit path (string or RegExp)
|
|
260
|
+
* Empty string counts as explicit path to enable param capture
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
263
|
+
private _hasExplicitPath;
|
|
264
|
+
/**
|
|
265
|
+
* Check if middleware contains a nested router
|
|
266
|
+
* @private
|
|
267
|
+
*/
|
|
268
|
+
private _isNestedRouter;
|
|
269
|
+
/**
|
|
270
|
+
* Apply middleware to multiple paths
|
|
271
|
+
* @private
|
|
272
|
+
*/
|
|
273
|
+
private _useWithPathArray;
|
|
274
|
+
/**
|
|
275
|
+
* Mount a nested router
|
|
276
|
+
* @private
|
|
277
|
+
*/
|
|
278
|
+
private _mountNestedRouter;
|
|
279
|
+
/**
|
|
280
|
+
* Clone a router instance
|
|
281
|
+
* @private
|
|
282
|
+
*/
|
|
283
|
+
private _cloneRouter;
|
|
284
|
+
/**
|
|
285
|
+
* Clone a layer instance
|
|
286
|
+
* @private
|
|
287
|
+
*/
|
|
288
|
+
private _cloneLayer;
|
|
289
|
+
/**
|
|
290
|
+
* Apply this router's param middleware to a nested router
|
|
291
|
+
* @private
|
|
292
|
+
*/
|
|
293
|
+
private _applyParamMiddlewareToRouter;
|
|
294
|
+
/**
|
|
295
|
+
* Register regular middleware (not nested router)
|
|
296
|
+
* @private
|
|
297
|
+
*/
|
|
298
|
+
private _registerMiddleware;
|
|
299
|
+
/**
|
|
300
|
+
* Set the path prefix for a Router instance that was already initialized.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
*
|
|
304
|
+
* ```javascript
|
|
305
|
+
* router.prefix('/things/:thing_id')
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @param prefixPath - Prefix string
|
|
309
|
+
* @returns This router instance
|
|
310
|
+
*/
|
|
311
|
+
prefix(prefixPath: string): Router<StateT, ContextT>;
|
|
312
|
+
/**
|
|
313
|
+
* Returns router middleware which dispatches a route matching the request.
|
|
314
|
+
*
|
|
315
|
+
* @returns Router middleware
|
|
316
|
+
*/
|
|
317
|
+
middleware(): RouterComposedMiddleware<StateT, ContextT>;
|
|
318
|
+
/**
|
|
319
|
+
* Get the request path to use for routing
|
|
320
|
+
* @private
|
|
321
|
+
*/
|
|
322
|
+
private _getRequestPath;
|
|
323
|
+
/**
|
|
324
|
+
* Store matched routes on context
|
|
325
|
+
* @private
|
|
326
|
+
*/
|
|
327
|
+
private _storeMatchedRoutes;
|
|
328
|
+
/**
|
|
329
|
+
* Set matched route information on context
|
|
330
|
+
* @private
|
|
331
|
+
*/
|
|
332
|
+
private _setMatchedRouteInfo;
|
|
333
|
+
/**
|
|
334
|
+
* Build middleware chain from matched layers
|
|
335
|
+
* @private
|
|
336
|
+
*/
|
|
337
|
+
private _buildMiddlewareChain;
|
|
338
|
+
routes(): RouterComposedMiddleware<StateT, ContextT>;
|
|
339
|
+
/**
|
|
340
|
+
* Returns separate middleware for responding to `OPTIONS` requests with
|
|
341
|
+
* an `Allow` header containing the allowed methods, as well as responding
|
|
342
|
+
* with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
*
|
|
346
|
+
* ```javascript
|
|
347
|
+
* const Koa = require('koa');
|
|
348
|
+
* const Router = require('@koa/router');
|
|
349
|
+
*
|
|
350
|
+
* const app = new Koa();
|
|
351
|
+
* const router = new Router();
|
|
352
|
+
*
|
|
353
|
+
* app.use(router.routes());
|
|
354
|
+
* app.use(router.allowedMethods());
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* **Example with [Boom](https://github.com/hapijs/boom)**
|
|
358
|
+
*
|
|
359
|
+
* ```javascript
|
|
360
|
+
* const Koa = require('koa');
|
|
361
|
+
* const Router = require('@koa/router');
|
|
362
|
+
* const Boom = require('boom');
|
|
363
|
+
*
|
|
364
|
+
* const app = new Koa();
|
|
365
|
+
* const router = new Router();
|
|
366
|
+
*
|
|
367
|
+
* app.use(router.routes());
|
|
368
|
+
* app.use(router.allowedMethods({
|
|
369
|
+
* throw: true,
|
|
370
|
+
* notImplemented: () => new Boom.notImplemented(),
|
|
371
|
+
* methodNotAllowed: () => new Boom.methodNotAllowed()
|
|
372
|
+
* }));
|
|
373
|
+
* ```
|
|
374
|
+
*
|
|
375
|
+
* @param options - Options object
|
|
376
|
+
* @returns Middleware function
|
|
377
|
+
*/
|
|
378
|
+
allowedMethods(options?: AllowedMethodsOptions): RouterMiddleware<StateT, ContextT>;
|
|
379
|
+
/**
|
|
380
|
+
* Check if we should process allowed methods
|
|
381
|
+
* @private
|
|
382
|
+
*/
|
|
383
|
+
private _shouldProcessAllowedMethods;
|
|
384
|
+
/**
|
|
385
|
+
* Collect all allowed methods from matched routes
|
|
386
|
+
* @private
|
|
387
|
+
*/
|
|
388
|
+
private _collectAllowedMethods;
|
|
389
|
+
/**
|
|
390
|
+
* Handle 501 Not Implemented response
|
|
391
|
+
* @private
|
|
392
|
+
*/
|
|
393
|
+
private _handleNotImplemented;
|
|
394
|
+
/**
|
|
395
|
+
* Handle OPTIONS request
|
|
396
|
+
* @private
|
|
397
|
+
*/
|
|
398
|
+
private _handleOptionsRequest;
|
|
399
|
+
/**
|
|
400
|
+
* Handle 405 Method Not Allowed response
|
|
401
|
+
* @private
|
|
402
|
+
*/
|
|
403
|
+
private _handleMethodNotAllowed;
|
|
404
|
+
/**
|
|
405
|
+
* Register route with all methods.
|
|
406
|
+
*
|
|
407
|
+
* @param args - Route arguments (name, path, middleware)
|
|
408
|
+
* @returns This router instance
|
|
409
|
+
*/
|
|
410
|
+
all<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
411
|
+
all<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
412
|
+
/**
|
|
413
|
+
* Redirect `source` to `destination` URL with optional 30x status `code`.
|
|
414
|
+
*
|
|
415
|
+
* Both `source` and `destination` can be route names.
|
|
416
|
+
*
|
|
417
|
+
* ```javascript
|
|
418
|
+
* router.redirect('/login', 'sign-in');
|
|
419
|
+
* ```
|
|
420
|
+
*
|
|
421
|
+
* This is equivalent to:
|
|
422
|
+
*
|
|
423
|
+
* ```javascript
|
|
424
|
+
* router.all('/login', ctx => {
|
|
425
|
+
* ctx.redirect('/sign-in');
|
|
426
|
+
* ctx.status = 301;
|
|
427
|
+
* });
|
|
428
|
+
* ```
|
|
429
|
+
*
|
|
430
|
+
* @param source - URL or route name
|
|
431
|
+
* @param destination - URL or route name
|
|
432
|
+
* @param code - HTTP status code (default: 301)
|
|
433
|
+
* @returns This router instance
|
|
434
|
+
*/
|
|
435
|
+
redirect(source: string | symbol, destination: string | symbol, code?: number): Router<StateT, ContextT>;
|
|
436
|
+
/**
|
|
437
|
+
* Create and register a route.
|
|
438
|
+
*
|
|
439
|
+
* @param path - Path string
|
|
440
|
+
* @param methods - Array of HTTP verbs
|
|
441
|
+
* @param middleware - Middleware functions
|
|
442
|
+
* @param additionalOptions - Additional options
|
|
443
|
+
* @returns Created layer
|
|
444
|
+
* @private
|
|
445
|
+
*/
|
|
446
|
+
register(path: string | RegExp | string[], methods: string[], middleware: RouterMiddleware<StateT, ContextT> | RouterMiddleware<StateT, ContextT>[], additionalOptions?: LayerOptions): Layer | Router<StateT, ContextT>;
|
|
447
|
+
/**
|
|
448
|
+
* Register multiple paths with the same configuration
|
|
449
|
+
* @private
|
|
450
|
+
*/
|
|
451
|
+
private _registerMultiplePaths;
|
|
452
|
+
/**
|
|
453
|
+
* Create a route layer with given configuration
|
|
454
|
+
* @private
|
|
455
|
+
*/
|
|
456
|
+
private _createRouteLayer;
|
|
457
|
+
/**
|
|
458
|
+
* Lookup route with given `name`.
|
|
459
|
+
*
|
|
460
|
+
* @param name - Route name
|
|
461
|
+
* @returns Matched layer or false
|
|
462
|
+
*/
|
|
463
|
+
route(name: string): Layer | false;
|
|
464
|
+
/**
|
|
465
|
+
* Generate URL for route. Takes a route name and map of named `params`.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
*
|
|
469
|
+
* ```javascript
|
|
470
|
+
* router.get('user', '/users/:id', (ctx, next) => {
|
|
471
|
+
* // ...
|
|
472
|
+
* });
|
|
473
|
+
*
|
|
474
|
+
* router.url('user', 3);
|
|
475
|
+
* // => "/users/3"
|
|
476
|
+
*
|
|
477
|
+
* router.url('user', { id: 3 });
|
|
478
|
+
* // => "/users/3"
|
|
479
|
+
*
|
|
480
|
+
* router.use((ctx, next) => {
|
|
481
|
+
* // redirect to named route
|
|
482
|
+
* ctx.redirect(ctx.router.url('sign-in'));
|
|
483
|
+
* })
|
|
484
|
+
*
|
|
485
|
+
* router.url('user', { id: 3 }, { query: { limit: 1 } });
|
|
486
|
+
* // => "/users/3?limit=1"
|
|
487
|
+
*
|
|
488
|
+
* router.url('user', { id: 3 }, { query: "limit=1" });
|
|
489
|
+
* // => "/users/3?limit=1"
|
|
490
|
+
* ```
|
|
491
|
+
*
|
|
492
|
+
* @param name - Route name
|
|
493
|
+
* @param args - URL parameters
|
|
494
|
+
* @returns Generated URL or Error
|
|
495
|
+
*/
|
|
496
|
+
url(name: string, ...arguments_: any[]): string | Error;
|
|
497
|
+
/**
|
|
498
|
+
* Match given `path` and return corresponding routes.
|
|
499
|
+
*
|
|
500
|
+
* @param path - Request path
|
|
501
|
+
* @param method - HTTP method
|
|
502
|
+
* @returns Match result with matched layers
|
|
503
|
+
* @private
|
|
504
|
+
*/
|
|
505
|
+
match(path: string, method: string): MatchResult;
|
|
506
|
+
/**
|
|
507
|
+
* Match given `input` to allowed host
|
|
508
|
+
* @param input - Host to check
|
|
509
|
+
* @returns Whether host matches
|
|
510
|
+
*/
|
|
511
|
+
matchHost(input?: string): boolean;
|
|
512
|
+
/**
|
|
513
|
+
* Run middleware for named route parameters. Useful for auto-loading or
|
|
514
|
+
* validation.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
*
|
|
518
|
+
* ```javascript
|
|
519
|
+
* router
|
|
520
|
+
* .param('user', (id, ctx, next) => {
|
|
521
|
+
* ctx.user = users[id];
|
|
522
|
+
* if (!ctx.user) return ctx.status = 404;
|
|
523
|
+
* return next();
|
|
524
|
+
* })
|
|
525
|
+
* .get('/users/:user', ctx => {
|
|
526
|
+
* ctx.body = ctx.user;
|
|
527
|
+
* })
|
|
528
|
+
* .get('/users/:user/friends', ctx => {
|
|
529
|
+
* return ctx.user.getFriends().then(function(friends) {
|
|
530
|
+
* ctx.body = friends;
|
|
531
|
+
* });
|
|
532
|
+
* })
|
|
533
|
+
* // /users/3 => {"id": 3, "name": "Alex"}
|
|
534
|
+
* // /users/3/friends => [{"id": 4, "name": "TJ"}]
|
|
535
|
+
* ```
|
|
536
|
+
*
|
|
537
|
+
* @param param - Parameter name
|
|
538
|
+
* @param middleware - Parameter middleware
|
|
539
|
+
* @returns This router instance
|
|
540
|
+
*/
|
|
541
|
+
param(parameter: string, middleware: RouterParameterMiddleware<StateT, ContextT>): Router<StateT, ContextT>;
|
|
542
|
+
/**
|
|
543
|
+
* Helper method for registering HTTP verb routes
|
|
544
|
+
* @internal - Used by dynamically added HTTP methods
|
|
545
|
+
*/
|
|
546
|
+
_registerMethod(method: string, ...arguments_: any[]): Router<StateT, ContextT>;
|
|
547
|
+
/**
|
|
548
|
+
* HTTP GET method
|
|
549
|
+
*/
|
|
550
|
+
get<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
551
|
+
get<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
552
|
+
/**
|
|
553
|
+
* HTTP POST method
|
|
554
|
+
*/
|
|
555
|
+
post<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
556
|
+
post<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
557
|
+
/**
|
|
558
|
+
* HTTP PUT method
|
|
559
|
+
*/
|
|
560
|
+
put<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
561
|
+
put<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
562
|
+
/**
|
|
563
|
+
* HTTP PATCH method
|
|
564
|
+
*/
|
|
565
|
+
patch<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
566
|
+
patch<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
567
|
+
/**
|
|
568
|
+
* HTTP DELETE method
|
|
569
|
+
*/
|
|
570
|
+
delete<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
571
|
+
delete<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
572
|
+
/**
|
|
573
|
+
* HTTP DELETE method alias (del)
|
|
574
|
+
*/
|
|
575
|
+
del<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
576
|
+
del<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
577
|
+
/**
|
|
578
|
+
* HTTP HEAD method
|
|
579
|
+
*/
|
|
580
|
+
head<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
581
|
+
head<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
582
|
+
/**
|
|
583
|
+
* HTTP OPTIONS method
|
|
584
|
+
*/
|
|
585
|
+
options<T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
586
|
+
options<T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Type definitions for @koa/router
|
|
591
|
+
*/
|
|
592
|
+
|
|
593
|
+
interface RouterOptions {
|
|
594
|
+
/**
|
|
595
|
+
* Only run last matched route's controller when there are multiple matches
|
|
596
|
+
*/
|
|
597
|
+
exclusive?: boolean;
|
|
598
|
+
/**
|
|
599
|
+
* Prefix for all routes
|
|
600
|
+
*/
|
|
601
|
+
prefix?: string;
|
|
602
|
+
/**
|
|
603
|
+
* Host for router match (string, array of strings, or RegExp)
|
|
604
|
+
* - string: exact match
|
|
605
|
+
* - string[]: matches if input equals any string in the array
|
|
606
|
+
* - RegExp: pattern match
|
|
607
|
+
*/
|
|
608
|
+
host?: string | string[] | RegExp;
|
|
609
|
+
/**
|
|
610
|
+
* HTTP methods this router should respond to
|
|
611
|
+
*/
|
|
612
|
+
methods?: string[];
|
|
613
|
+
/**
|
|
614
|
+
* Path to use for routing (internal)
|
|
615
|
+
*/
|
|
616
|
+
routerPath?: string;
|
|
617
|
+
/**
|
|
618
|
+
* Whether to use case-sensitive routing
|
|
619
|
+
*/
|
|
620
|
+
sensitive?: boolean;
|
|
621
|
+
/**
|
|
622
|
+
* Whether trailing slashes are significant
|
|
623
|
+
*/
|
|
624
|
+
strict?: boolean;
|
|
625
|
+
}
|
|
626
|
+
interface LayerOptions {
|
|
627
|
+
/**
|
|
628
|
+
* Route name for URL generation
|
|
629
|
+
*/
|
|
630
|
+
name?: string | null;
|
|
631
|
+
/**
|
|
632
|
+
* Case sensitive routing
|
|
633
|
+
*/
|
|
634
|
+
sensitive?: boolean;
|
|
635
|
+
/**
|
|
636
|
+
* Require trailing slash
|
|
637
|
+
*/
|
|
638
|
+
strict?: boolean;
|
|
639
|
+
/**
|
|
640
|
+
* Whether trailing slashes matter (path-to-regexp v8)
|
|
641
|
+
*/
|
|
642
|
+
trailing?: boolean;
|
|
643
|
+
/**
|
|
644
|
+
* Route path ends at this path
|
|
645
|
+
*/
|
|
646
|
+
end?: boolean;
|
|
647
|
+
/**
|
|
648
|
+
* Prefix for the route
|
|
649
|
+
*/
|
|
650
|
+
prefix?: string;
|
|
651
|
+
/**
|
|
652
|
+
* Ignore captures in route matching
|
|
653
|
+
*/
|
|
654
|
+
ignoreCaptures?: boolean;
|
|
655
|
+
/**
|
|
656
|
+
* Treat path as a regular expression
|
|
657
|
+
*/
|
|
658
|
+
pathAsRegExp?: boolean;
|
|
659
|
+
}
|
|
660
|
+
interface UrlOptions {
|
|
661
|
+
/**
|
|
662
|
+
* Query string parameters
|
|
663
|
+
*/
|
|
664
|
+
query?: Record<string, any> | string;
|
|
665
|
+
[key: string]: any;
|
|
666
|
+
}
|
|
667
|
+
interface RouterParameterContext<StateT = DefaultState, ContextT = DefaultContext> {
|
|
668
|
+
/**
|
|
669
|
+
* URL parameters
|
|
670
|
+
*/
|
|
671
|
+
params: Record<string, string>;
|
|
672
|
+
/**
|
|
673
|
+
* Router instance
|
|
674
|
+
*/
|
|
675
|
+
router: Router<StateT, ContextT>;
|
|
676
|
+
/**
|
|
677
|
+
* Matched route path (internal)
|
|
678
|
+
*/
|
|
679
|
+
_matchedRoute?: string | RegExp;
|
|
680
|
+
/**
|
|
681
|
+
* Matched route name (internal)
|
|
682
|
+
*/
|
|
683
|
+
_matchedRouteName?: string;
|
|
684
|
+
}
|
|
685
|
+
interface RouterParameterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> {
|
|
686
|
+
(parameterValue: string, context: RouterContext<StateT, ContextT, BodyT>, next: () => Promise<any>): any;
|
|
687
|
+
}
|
|
688
|
+
interface MatchResult {
|
|
689
|
+
/**
|
|
690
|
+
* Layers that matched the path
|
|
691
|
+
*/
|
|
692
|
+
path: Layer[];
|
|
693
|
+
/**
|
|
694
|
+
* Layers that matched both path and HTTP method
|
|
695
|
+
*/
|
|
696
|
+
pathAndMethod: Layer[];
|
|
697
|
+
/**
|
|
698
|
+
* Whether a route (not just middleware) was matched
|
|
699
|
+
*/
|
|
700
|
+
route: boolean;
|
|
701
|
+
}
|
|
702
|
+
interface AllowedMethodsOptions {
|
|
703
|
+
/**
|
|
704
|
+
* Throw error instead of setting status and header
|
|
705
|
+
*/
|
|
706
|
+
throw?: boolean;
|
|
707
|
+
/**
|
|
708
|
+
* Throw the returned value in place of the default NotImplemented error
|
|
709
|
+
*/
|
|
710
|
+
notImplemented?: () => Error;
|
|
711
|
+
/**
|
|
712
|
+
* Throw the returned value in place of the default MethodNotAllowed error
|
|
713
|
+
*/
|
|
714
|
+
methodNotAllowed?: () => Error;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Extended Koa context with router-specific properties
|
|
718
|
+
* Matches the structure from @types/koa-router
|
|
719
|
+
*/
|
|
720
|
+
type RouterContext<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = ParameterizedContext<StateT, ContextT & RouterParameterContext<StateT, ContextT>, BodyT> & {
|
|
721
|
+
/**
|
|
722
|
+
* Request with params (params added dynamically)
|
|
723
|
+
*/
|
|
724
|
+
request: {
|
|
725
|
+
params?: Record<string, string>;
|
|
726
|
+
};
|
|
727
|
+
/**
|
|
728
|
+
* Path of matched route
|
|
729
|
+
*/
|
|
730
|
+
routerPath?: string;
|
|
731
|
+
/**
|
|
732
|
+
* Name of matched route
|
|
733
|
+
*/
|
|
734
|
+
routerName?: string;
|
|
735
|
+
/**
|
|
736
|
+
* Array of matched layers
|
|
737
|
+
*/
|
|
738
|
+
matched?: Layer[];
|
|
739
|
+
/**
|
|
740
|
+
* Captured values from path
|
|
741
|
+
*/
|
|
742
|
+
captures?: string[];
|
|
743
|
+
/**
|
|
744
|
+
* New router path (for nested routers)
|
|
745
|
+
*/
|
|
746
|
+
newRouterPath?: string;
|
|
747
|
+
/**
|
|
748
|
+
* Track param middleware execution (internal)
|
|
749
|
+
*/
|
|
750
|
+
_matchedParams?: WeakMap<Function, boolean>;
|
|
751
|
+
};
|
|
752
|
+
/**
|
|
753
|
+
* Router middleware function type
|
|
754
|
+
*/
|
|
755
|
+
type RouterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = Middleware<StateT, ContextT & RouterParameterContext<StateT, ContextT>, BodyT>;
|
|
756
|
+
/**
|
|
757
|
+
* HTTP method names in lowercase
|
|
758
|
+
*/
|
|
759
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'del' | 'head' | 'options' | 'connect' | 'trace' | string;
|
|
760
|
+
|
|
761
|
+
export { type AllowedMethodsOptions as A, type HttpMethod as H, type LayerOptions as L, type MatchResult as M, Router as R, type UrlOptions as U, type RouterOptions as a, type RouterParameterContext as b, type RouterParameterMiddleware as c, type RouterContext as d, Layer as default, type RouterMiddleware as e };
|