@eggjs/router 4.0.0-beta.35 → 4.0.1-beta.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/dist/Router.d.ts CHANGED
@@ -1,494 +1,495 @@
1
- /**
2
- * RESTful resource routing middleware for eggjs.
3
- */
4
- import methods from 'methods';
5
- import { Layer, type LayerURLOptions } from './Layer.ts';
6
- import { type MiddlewareFunc, type MiddlewareFuncWithRouter, type ParamMiddlewareFunc, type ResourcesController } from './types.ts';
7
- export type RouterMethod = (typeof methods)[0];
8
- export interface RouterOptions {
9
- methods?: string[];
10
- prefix?: string;
11
- sensitive?: boolean;
12
- strict?: boolean;
13
- routerPath?: string;
1
+ import { MiddlewareFunc, MiddlewareFuncWithRouter, ParamMiddlewareFunc, ResourcesController } from "./types.js";
2
+ import { Layer, LayerURLOptions } from "./Layer.js";
3
+ import methods from "methods";
4
+
5
+ //#region src/Router.d.ts
6
+ type RouterMethod = (typeof methods)[0];
7
+ interface RouterOptions {
8
+ methods?: string[];
9
+ prefix?: string;
10
+ sensitive?: boolean;
11
+ strict?: boolean;
12
+ routerPath?: string;
14
13
  }
15
- export interface RegisterOptions {
16
- name?: string;
17
- prefix?: string;
18
- sensitive?: boolean;
19
- strict?: boolean;
20
- ignoreCaptures?: boolean;
21
- end?: boolean;
14
+ interface RegisterOptions {
15
+ name?: string;
16
+ prefix?: string;
17
+ sensitive?: boolean;
18
+ strict?: boolean;
19
+ ignoreCaptures?: boolean;
20
+ end?: boolean;
22
21
  }
23
- export interface AllowedMethodsOptions {
24
- throw?: boolean;
25
- notImplemented?: () => Error;
26
- methodNotAllowed?: () => Error;
22
+ interface AllowedMethodsOptions {
23
+ throw?: boolean;
24
+ notImplemented?: () => Error;
25
+ methodNotAllowed?: () => Error;
27
26
  }
28
- export interface MatchedResult {
29
- path: Layer[];
30
- pathAndMethod: Layer[];
31
- route: boolean;
27
+ interface MatchedResult {
28
+ path: Layer[];
29
+ pathAndMethod: Layer[];
30
+ route: boolean;
32
31
  }
33
- export declare class Router {
34
- #private;
35
- readonly opts: RouterOptions;
36
- readonly methods: string[];
37
- /** Layer stack */
38
- readonly stack: Layer[];
39
- readonly params: Record<string, ParamMiddlewareFunc>;
40
- /**
41
- * Create a new router.
42
- *
43
- * @example
44
- *
45
- * Basic usage:
46
- *
47
- * ```javascript
48
- * var Koa = require('koa');
49
- * var Router = require('koa-router');
50
- *
51
- * var app = new Koa();
52
- * var router = new Router();
53
- *
54
- * router.get('/', (ctx, next) => {
55
- * // ctx.router available
56
- * });
57
- *
58
- * app
59
- * .use(router.routes())
60
- * .use(router.allowedMethods());
61
- * ```
62
- *
63
- * @alias module:koa-router
64
- * @param {Object=} opts optional
65
- * @param {String=} opts.prefix prefix router paths
66
- * @class
67
- */
68
- constructor(opts?: RouterOptions);
69
- /**
70
- * Use given middleware.
71
- *
72
- * Middleware run in the order they are defined by `.use()`. They are invoked
73
- * sequentially, requests start at the first middleware and work their way
74
- * "down" the middleware stack.
75
- *
76
- * @example
77
- *
78
- * ```javascript
79
- * // session middleware will run before authorize
80
- * router
81
- * .use(session())
82
- * .use(authorize());
83
- *
84
- * // use middleware only with given path
85
- * router.use('/users', userAuth());
86
- *
87
- * // or with an array of paths
88
- * router.use(['/users', '/admin'], userAuth());
89
- *
90
- * app.use(router.routes());
91
- * ```
92
- *
93
- * @param {String=} path path string
94
- * @param {Function} middleware middleware function
95
- * @return {Router} router instance
96
- */
97
- use(...middlewares: MiddlewareFunc[]): Router;
98
- use(path: string | string[], ...middlewares: MiddlewareFunc[]): Router;
99
- /**
100
- * Set the path prefix for a Router instance that was already initialized.
101
- *
102
- * @example
103
- *
104
- * ```javascript
105
- * router.prefix('/things/:thing_id')
106
- * ```
107
- *
108
- * @param {String} prefix prefix string
109
- * @return {Router} router instance
110
- */
111
- prefix(prefix: string): Router;
112
- /**
113
- * Returns router middleware which dispatches a route matching the request.
114
- *
115
- * @return {Function} middleware function
116
- */
117
- routes(): MiddlewareFuncWithRouter<Router>;
118
- /**
119
- * @alias to routes()
120
- */
121
- middleware(): MiddlewareFuncWithRouter<Router>;
122
- /**
123
- * Returns separate middleware for responding to `OPTIONS` requests with
124
- * an `Allow` header containing the allowed methods, as well as responding
125
- * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
126
- *
127
- * @example
128
- *
129
- * ```javascript
130
- * var Koa = require('koa');
131
- * var Router = require('koa-router');
132
- *
133
- * var app = new Koa();
134
- * var router = new Router();
135
- *
136
- * app.use(router.routes());
137
- * app.use(router.allowedMethods());
138
- * ```
139
- *
140
- * **Example with [Boom](https://github.com/hapijs/boom)**
141
- *
142
- * ```javascript
143
- * var Koa = require('koa');
144
- * var Router = require('koa-router');
145
- * var Boom = require('boom');
146
- *
147
- * var app = new Koa();
148
- * var router = new Router();
149
- *
150
- * app.use(router.routes());
151
- * app.use(router.allowedMethods({
152
- * throw: true,
153
- * notImplemented: () => new Boom.notImplemented(),
154
- * methodNotAllowed: () => new Boom.methodNotAllowed()
155
- * }));
156
- * ```
157
- *
158
- * @param {Object=} options optional params
159
- * @param {Boolean=} options.throw throw error instead of setting status and header
160
- * @param {Function=} options.notImplemented throw the returned value in place of the default NotImplemented error
161
- * @param {Function=} options.methodNotAllowed throw the returned value in place of the default MethodNotAllowed error
162
- * @return {Function} middleware function
163
- */
164
- allowedMethods(options?: AllowedMethodsOptions): MiddlewareFunc;
165
- /**
166
- * Redirect `source` to `destination` URL with optional 30x status `code`.
167
- *
168
- * Both `source` and `destination` can be route names.
169
- *
170
- * ```javascript
171
- * router.redirect('/login', 'sign-in');
172
- * ```
173
- *
174
- * This is equivalent to:
175
- *
176
- * ```javascript
177
- * router.all('/login', ctx => {
178
- * ctx.redirect('/sign-in');
179
- * ctx.status = 301;
180
- * });
181
- * ```
182
- *
183
- * @param {String} source URL or route name.
184
- * @param {String} destination URL or route name.
185
- * @param {Number=} status HTTP status code (default: 301).
186
- * @return {Router} router instance
187
- */
188
- redirect(source: string, destination: string, status?: number): Router;
189
- /**
190
- * Create and register a route.
191
- *
192
- * @param {String|RegExp|(String|RegExp)[]} path Path string.
193
- * @param {String[]} methods Array of HTTP verbs.
194
- * @param {Function|Function[]} middleware Multiple middleware also accepted.
195
- * @param {Object} [opts] optional params
196
- * @private
197
- */
198
- register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | MiddlewareFunc[], opts?: RegisterOptions): Layer | Layer[];
199
- /**
200
- * Lookup route with given `name`.
201
- *
202
- * @param {String} name route name
203
- * @return {Layer|false} layer instance of false
204
- */
205
- route(name: string): Layer | false;
206
- /**
207
- * Generate URL for route. Takes a route name and map of named `params`.
208
- *
209
- * @example
210
- *
211
- * ```javascript
212
- * router.get('user', '/users/:id', (ctx, next) => {
213
- * // ...
214
- * });
215
- *
216
- * router.url('user', 3);
217
- * // => "/users/3"
218
- *
219
- * router.url('user', { id: 3 });
220
- * // => "/users/3"
221
- *
222
- * router.use((ctx, next) => {
223
- * // redirect to named route
224
- * ctx.redirect(ctx.router.url('sign-in'));
225
- * })
226
- *
227
- * router.url('user', { id: 3 }, { query: { limit: 1 } });
228
- * // => "/users/3?limit=1"
229
- *
230
- * router.url('user', { id: 3 }, { query: "limit=1" });
231
- * // => "/users/3?limit=1"
232
- * ```
233
- */
234
- url(name: string, params?: string | number | object, ...paramsOrOptions: (string | number | object | LayerURLOptions)[]): string | Error;
235
- /**
236
- * Generate URL from url pattern and given `params`.
237
- *
238
- * @example
239
- *
240
- * ```javascript
241
- * var url = Router.url('/users/:id', { id: 1 });
242
- * // => "/users/1"
243
- * ```
244
- *
245
- * @param {String} path url pattern
246
- * @param {Object} params url parameters
247
- * @return {String} url string
248
- */
249
- static url(path: string, params?: string | number | object, ...paramsOrOptions: (string | number | object | LayerURLOptions)[]): string;
250
- /**
251
- * Match given `path` and return corresponding routes.
252
- *
253
- * @param {String} path path string
254
- * @param {String} method method name
255
- * @return {Object.<path, pathAndMethod>} returns layers that matched path and
256
- * path and method.
257
- * @private
258
- */
259
- match(path: string, method: string): MatchedResult;
260
- /**
261
- * Run middleware for named route parameters. Useful for auto-loading or
262
- * validation.
263
- *
264
- * @example
265
- *
266
- * ```javascript
267
- * router
268
- * .param('user', (id, ctx, next) => {
269
- * ctx.user = users[id];
270
- * if (!ctx.user) return ctx.status = 404;
271
- * return next();
272
- * })
273
- * .get('/users/:user', ctx => {
274
- * ctx.body = ctx.user;
275
- * })
276
- * .get('/users/:user/friends', ctx => {
277
- * return ctx.user.getFriends().then(function(friends) {
278
- * ctx.body = friends;
279
- * });
280
- * })
281
- * // /users/3 => {"id": 3, "name": "Alex"}
282
- * // /users/3/friends => [{"id": 4, "name": "TJ"}]
283
- * ```
284
- *
285
- * @param {String} param param
286
- * @param {Function} middleware route middleware
287
- * @return {Router} instance
288
- */
289
- param(param: string, middleware: ParamMiddlewareFunc): Router;
290
- protected _formatRouteParams(nameOrPath: string | RegExp | (string | RegExp)[], pathOrMiddleware: string | RegExp | (string | RegExp)[] | MiddlewareFunc | ResourcesController, middlewares: (MiddlewareFunc | string | ResourcesController)[]): {
291
- path: string | RegExp | (string | RegExp)[];
292
- middlewares: (MiddlewareFunc | string | ResourcesController)[];
293
- options: RegisterOptions;
294
- };
295
- /**
296
- * Create `router.verb()` methods, where *verb* is one of the HTTP verbs such
297
- * as `router.get()` or `router.post()`.
298
- *
299
- * Match URL patterns to callback functions or controller actions using `router.verb()`,
300
- * where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()`.
301
- *
302
- * Additionally, `router.all()` can be used to match against all methods.
303
- *
304
- * ```javascript
305
- * router
306
- * .get('/', (ctx, next) => {
307
- * ctx.body = 'Hello World!';
308
- * })
309
- * .post('/users', (ctx, next) => {
310
- * // ...
311
- * })
312
- * .put('/users/:id', (ctx, next) => {
313
- * // ...
314
- * })
315
- * .del('/users/:id', (ctx, next) => {
316
- * // ...
317
- * })
318
- * .all('/users/:id', (ctx, next) => {
319
- * // ...
320
- * });
321
- * ```
322
- *
323
- * When a route is matched, its path is available at `ctx._matchedRoute` and if named,
324
- * the name is available at `ctx._matchedRouteName`
325
- *
326
- * Route paths will be translated to regular expressions using
327
- * [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
328
- *
329
- * Query strings will not be considered when matching requests.
330
- *
331
- * #### Named routes
332
- *
333
- * Routes can optionally have names. This allows generation of URLs and easy
334
- * renaming of URLs during development.
335
- *
336
- * ```javascript
337
- * router.get('user', '/users/:id', (ctx, next) => {
338
- * // ...
339
- * });
340
- *
341
- * router.url('user', 3);
342
- * // => "/users/3"
343
- * ```
344
- *
345
- * #### Multiple middleware
346
- *
347
- * Multiple middleware may be given:
348
- *
349
- * ```javascript
350
- * router.get(
351
- * '/users/:id',
352
- * (ctx, next) => {
353
- * return User.findOne(ctx.params.id).then(function(user) {
354
- * ctx.user = user;
355
- * next();
356
- * });
357
- * },
358
- * ctx => {
359
- * console.log(ctx.user);
360
- * // => { id: 17, name: "Alex" }
361
- * }
362
- * );
363
- * ```
364
- *
365
- * ### Nested routers
366
- *
367
- * Nesting routers is supported:
368
- *
369
- * ```javascript
370
- * var forums = new Router();
371
- * var posts = new Router();
372
- *
373
- * posts.get('/', (ctx, next) => {...});
374
- * posts.get('/:pid', (ctx, next) => {...});
375
- * forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
376
- *
377
- * // responds to "/forums/123/posts" and "/forums/123/posts/123"
378
- * app.use(forums.routes());
379
- * ```
380
- *
381
- * #### Router prefixes
382
- *
383
- * Route paths can be prefixed at the router level:
384
- *
385
- * ```javascript
386
- * var router = new Router({
387
- * prefix: '/users'
388
- * });
389
- *
390
- * router.get('/', ...); // responds to "/users"
391
- * router.get('/:id', ...); // responds to "/users/:id"
392
- * ```
393
- *
394
- * #### URL parameters
395
- *
396
- * Named route parameters are captured and added to `ctx.params`.
397
- *
398
- * ```javascript
399
- * router.get('/:category/:title', (ctx, next) => {
400
- * console.log(ctx.params);
401
- * // => { category: 'programming', title: 'how-to-node' }
402
- * });
403
- * ```
404
- *
405
- * The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
406
- * used to convert paths to regular expressions.
407
- *
408
- */
409
- verb(method: string | string[], nameOrPath: string | RegExp | (string | RegExp)[], pathOrMiddleware: string | RegExp | (string | RegExp)[] | MiddlewareFunc, ...middleware: MiddlewareFunc[]): Router;
410
- /**
411
- * Register route with all methods.
412
- *
413
- * @param {String} name Optional.
414
- * @param {String} path path string
415
- * @param {Function=} middleware You may also pass multiple middleware.
416
- * @return {Router} router instance
417
- * @private
418
- */
419
- all(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
420
- all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
421
- acl(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
422
- acl(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
423
- bind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
424
- bind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
425
- checkout(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
426
- checkout(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
427
- connect(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
428
- connect(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
429
- copy(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
430
- copy(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
431
- delete(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
432
- delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
433
- /** Alias for `router.delete()` because delete is a reserved word */
434
- del(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
435
- del(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
436
- get(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
437
- get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
438
- query(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
439
- query(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
440
- head(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
441
- head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
442
- link(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
443
- link(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
444
- lock(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
445
- lock(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
446
- ['m-search'](path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
447
- ['m-search'](name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
448
- merge(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
449
- merge(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
450
- mkactivity(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
451
- mkactivity(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
452
- mkcalendar(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
453
- mkcalendar(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
454
- mkcol(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
455
- mkcol(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
456
- move(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
457
- move(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
458
- notify(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
459
- notify(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
460
- options(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
461
- options(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
462
- patch(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
463
- patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
464
- post(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
465
- post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
466
- propfind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
467
- propfind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
468
- proppatch(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
469
- proppatch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
470
- purge(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
471
- purge(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
472
- put(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
473
- put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
474
- rebind(path: string | RegExp, ...middlewares: MiddlewareFunc[]): Router;
475
- rebind(name: string, path: string | RegExp, ...middlewares: MiddlewareFunc[]): Router;
476
- report(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
477
- report(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
478
- search(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
479
- search(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
480
- source(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
481
- source(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
482
- subscribe(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
483
- subscribe(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
484
- trace(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
485
- trace(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
486
- unbind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
487
- unbind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
488
- unlink(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
489
- unlink(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
490
- unlock(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
491
- unlock(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
492
- unsubscribe(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
493
- unsubscribe(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
32
+ declare class Router {
33
+ #private;
34
+ readonly opts: RouterOptions;
35
+ readonly methods: string[];
36
+ /** Layer stack */
37
+ readonly stack: Layer[];
38
+ readonly params: Record<string, ParamMiddlewareFunc>;
39
+ /**
40
+ * Create a new router.
41
+ *
42
+ * @example
43
+ *
44
+ * Basic usage:
45
+ *
46
+ * ```javascript
47
+ * var Koa = require('koa');
48
+ * var Router = require('koa-router');
49
+ *
50
+ * var app = new Koa();
51
+ * var router = new Router();
52
+ *
53
+ * router.get('/', (ctx, next) => {
54
+ * // ctx.router available
55
+ * });
56
+ *
57
+ * app
58
+ * .use(router.routes())
59
+ * .use(router.allowedMethods());
60
+ * ```
61
+ *
62
+ * @alias module:koa-router
63
+ * @param {Object=} opts optional
64
+ * @param {String=} opts.prefix prefix router paths
65
+ * @class
66
+ */
67
+ constructor(opts?: RouterOptions);
68
+ /**
69
+ * Use given middleware.
70
+ *
71
+ * Middleware run in the order they are defined by `.use()`. They are invoked
72
+ * sequentially, requests start at the first middleware and work their way
73
+ * "down" the middleware stack.
74
+ *
75
+ * @example
76
+ *
77
+ * ```javascript
78
+ * // session middleware will run before authorize
79
+ * router
80
+ * .use(session())
81
+ * .use(authorize());
82
+ *
83
+ * // use middleware only with given path
84
+ * router.use('/users', userAuth());
85
+ *
86
+ * // or with an array of paths
87
+ * router.use(['/users', '/admin'], userAuth());
88
+ *
89
+ * app.use(router.routes());
90
+ * ```
91
+ *
92
+ * @param {String=} path path string
93
+ * @param {Function} middleware middleware function
94
+ * @return {Router} router instance
95
+ */
96
+ use(...middlewares: MiddlewareFunc[]): Router;
97
+ use(path: string | string[], ...middlewares: MiddlewareFunc[]): Router;
98
+ /**
99
+ * Set the path prefix for a Router instance that was already initialized.
100
+ *
101
+ * @example
102
+ *
103
+ * ```javascript
104
+ * router.prefix('/things/:thing_id')
105
+ * ```
106
+ *
107
+ * @param {String} prefix prefix string
108
+ * @return {Router} router instance
109
+ */
110
+ prefix(prefix: string): Router;
111
+ /**
112
+ * Returns router middleware which dispatches a route matching the request.
113
+ *
114
+ * @return {Function} middleware function
115
+ */
116
+ routes(): MiddlewareFuncWithRouter<Router>;
117
+ /**
118
+ * @alias to routes()
119
+ */
120
+ middleware(): MiddlewareFuncWithRouter<Router>;
121
+ /**
122
+ * Returns separate middleware for responding to `OPTIONS` requests with
123
+ * an `Allow` header containing the allowed methods, as well as responding
124
+ * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
125
+ *
126
+ * @example
127
+ *
128
+ * ```javascript
129
+ * var Koa = require('koa');
130
+ * var Router = require('koa-router');
131
+ *
132
+ * var app = new Koa();
133
+ * var router = new Router();
134
+ *
135
+ * app.use(router.routes());
136
+ * app.use(router.allowedMethods());
137
+ * ```
138
+ *
139
+ * **Example with [Boom](https://github.com/hapijs/boom)**
140
+ *
141
+ * ```javascript
142
+ * var Koa = require('koa');
143
+ * var Router = require('koa-router');
144
+ * var Boom = require('boom');
145
+ *
146
+ * var app = new Koa();
147
+ * var router = new Router();
148
+ *
149
+ * app.use(router.routes());
150
+ * app.use(router.allowedMethods({
151
+ * throw: true,
152
+ * notImplemented: () => new Boom.notImplemented(),
153
+ * methodNotAllowed: () => new Boom.methodNotAllowed()
154
+ * }));
155
+ * ```
156
+ *
157
+ * @param {Object=} options optional params
158
+ * @param {Boolean=} options.throw throw error instead of setting status and header
159
+ * @param {Function=} options.notImplemented throw the returned value in place of the default NotImplemented error
160
+ * @param {Function=} options.methodNotAllowed throw the returned value in place of the default MethodNotAllowed error
161
+ * @return {Function} middleware function
162
+ */
163
+ allowedMethods(options?: AllowedMethodsOptions): MiddlewareFunc;
164
+ /**
165
+ * Redirect `source` to `destination` URL with optional 30x status `code`.
166
+ *
167
+ * Both `source` and `destination` can be route names.
168
+ *
169
+ * ```javascript
170
+ * router.redirect('/login', 'sign-in');
171
+ * ```
172
+ *
173
+ * This is equivalent to:
174
+ *
175
+ * ```javascript
176
+ * router.all('/login', ctx => {
177
+ * ctx.redirect('/sign-in');
178
+ * ctx.status = 301;
179
+ * });
180
+ * ```
181
+ *
182
+ * @param {String} source URL or route name.
183
+ * @param {String} destination URL or route name.
184
+ * @param {Number=} status HTTP status code (default: 301).
185
+ * @return {Router} router instance
186
+ */
187
+ redirect(source: string, destination: string, status?: number): Router;
188
+ /**
189
+ * Create and register a route.
190
+ *
191
+ * @param {String|RegExp|(String|RegExp)[]} path Path string.
192
+ * @param {String[]} methods Array of HTTP verbs.
193
+ * @param {Function|Function[]} middleware Multiple middleware also accepted.
194
+ * @param {Object} [opts] optional params
195
+ * @private
196
+ */
197
+ register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | MiddlewareFunc[], opts?: RegisterOptions): Layer | Layer[];
198
+ /**
199
+ * Lookup route with given `name`.
200
+ *
201
+ * @param {String} name route name
202
+ * @return {Layer|false} layer instance of false
203
+ */
204
+ route(name: string): Layer | false;
205
+ /**
206
+ * Generate URL for route. Takes a route name and map of named `params`.
207
+ *
208
+ * @example
209
+ *
210
+ * ```javascript
211
+ * router.get('user', '/users/:id', (ctx, next) => {
212
+ * // ...
213
+ * });
214
+ *
215
+ * router.url('user', 3);
216
+ * // => "/users/3"
217
+ *
218
+ * router.url('user', { id: 3 });
219
+ * // => "/users/3"
220
+ *
221
+ * router.use((ctx, next) => {
222
+ * // redirect to named route
223
+ * ctx.redirect(ctx.router.url('sign-in'));
224
+ * })
225
+ *
226
+ * router.url('user', { id: 3 }, { query: { limit: 1 } });
227
+ * // => "/users/3?limit=1"
228
+ *
229
+ * router.url('user', { id: 3 }, { query: "limit=1" });
230
+ * // => "/users/3?limit=1"
231
+ * ```
232
+ */
233
+ url(name: string, params?: string | number | object, ...paramsOrOptions: (string | number | object | LayerURLOptions)[]): string | Error;
234
+ /**
235
+ * Generate URL from url pattern and given `params`.
236
+ *
237
+ * @example
238
+ *
239
+ * ```javascript
240
+ * var url = Router.url('/users/:id', { id: 1 });
241
+ * // => "/users/1"
242
+ * ```
243
+ *
244
+ * @param {String} path url pattern
245
+ * @param {Object} params url parameters
246
+ * @return {String} url string
247
+ */
248
+ static url(path: string, params?: string | number | object, ...paramsOrOptions: (string | number | object | LayerURLOptions)[]): string;
249
+ /**
250
+ * Match given `path` and return corresponding routes.
251
+ *
252
+ * @param {String} path path string
253
+ * @param {String} method method name
254
+ * @return {Object.<path, pathAndMethod>} returns layers that matched path and
255
+ * path and method.
256
+ * @private
257
+ */
258
+ match(path: string, method: string): MatchedResult;
259
+ /**
260
+ * Run middleware for named route parameters. Useful for auto-loading or
261
+ * validation.
262
+ *
263
+ * @example
264
+ *
265
+ * ```javascript
266
+ * router
267
+ * .param('user', (id, ctx, next) => {
268
+ * ctx.user = users[id];
269
+ * if (!ctx.user) return ctx.status = 404;
270
+ * return next();
271
+ * })
272
+ * .get('/users/:user', ctx => {
273
+ * ctx.body = ctx.user;
274
+ * })
275
+ * .get('/users/:user/friends', ctx => {
276
+ * return ctx.user.getFriends().then(function(friends) {
277
+ * ctx.body = friends;
278
+ * });
279
+ * })
280
+ * // /users/3 => {"id": 3, "name": "Alex"}
281
+ * // /users/3/friends => [{"id": 4, "name": "TJ"}]
282
+ * ```
283
+ *
284
+ * @param {String} param param
285
+ * @param {Function} middleware route middleware
286
+ * @return {Router} instance
287
+ */
288
+ param(param: string, middleware: ParamMiddlewareFunc): Router;
289
+ protected _formatRouteParams(nameOrPath: string | RegExp | (string | RegExp)[], pathOrMiddleware: string | RegExp | (string | RegExp)[] | MiddlewareFunc | ResourcesController, middlewares: (MiddlewareFunc | string | ResourcesController)[]): {
290
+ path: string | RegExp | (string | RegExp)[];
291
+ middlewares: (MiddlewareFunc | string | ResourcesController)[];
292
+ options: RegisterOptions;
293
+ };
294
+ /**
295
+ * Create `router.verb()` methods, where *verb* is one of the HTTP verbs such
296
+ * as `router.get()` or `router.post()`.
297
+ *
298
+ * Match URL patterns to callback functions or controller actions using `router.verb()`,
299
+ * where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()`.
300
+ *
301
+ * Additionally, `router.all()` can be used to match against all methods.
302
+ *
303
+ * ```javascript
304
+ * router
305
+ * .get('/', (ctx, next) => {
306
+ * ctx.body = 'Hello World!';
307
+ * })
308
+ * .post('/users', (ctx, next) => {
309
+ * // ...
310
+ * })
311
+ * .put('/users/:id', (ctx, next) => {
312
+ * // ...
313
+ * })
314
+ * .del('/users/:id', (ctx, next) => {
315
+ * // ...
316
+ * })
317
+ * .all('/users/:id', (ctx, next) => {
318
+ * // ...
319
+ * });
320
+ * ```
321
+ *
322
+ * When a route is matched, its path is available at `ctx._matchedRoute` and if named,
323
+ * the name is available at `ctx._matchedRouteName`
324
+ *
325
+ * Route paths will be translated to regular expressions using
326
+ * [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
327
+ *
328
+ * Query strings will not be considered when matching requests.
329
+ *
330
+ * #### Named routes
331
+ *
332
+ * Routes can optionally have names. This allows generation of URLs and easy
333
+ * renaming of URLs during development.
334
+ *
335
+ * ```javascript
336
+ * router.get('user', '/users/:id', (ctx, next) => {
337
+ * // ...
338
+ * });
339
+ *
340
+ * router.url('user', 3);
341
+ * // => "/users/3"
342
+ * ```
343
+ *
344
+ * #### Multiple middleware
345
+ *
346
+ * Multiple middleware may be given:
347
+ *
348
+ * ```javascript
349
+ * router.get(
350
+ * '/users/:id',
351
+ * (ctx, next) => {
352
+ * return User.findOne(ctx.params.id).then(function(user) {
353
+ * ctx.user = user;
354
+ * next();
355
+ * });
356
+ * },
357
+ * ctx => {
358
+ * console.log(ctx.user);
359
+ * // => { id: 17, name: "Alex" }
360
+ * }
361
+ * );
362
+ * ```
363
+ *
364
+ * ### Nested routers
365
+ *
366
+ * Nesting routers is supported:
367
+ *
368
+ * ```javascript
369
+ * var forums = new Router();
370
+ * var posts = new Router();
371
+ *
372
+ * posts.get('/', (ctx, next) => {...});
373
+ * posts.get('/:pid', (ctx, next) => {...});
374
+ * forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
375
+ *
376
+ * // responds to "/forums/123/posts" and "/forums/123/posts/123"
377
+ * app.use(forums.routes());
378
+ * ```
379
+ *
380
+ * #### Router prefixes
381
+ *
382
+ * Route paths can be prefixed at the router level:
383
+ *
384
+ * ```javascript
385
+ * var router = new Router({
386
+ * prefix: '/users'
387
+ * });
388
+ *
389
+ * router.get('/', ...); // responds to "/users"
390
+ * router.get('/:id', ...); // responds to "/users/:id"
391
+ * ```
392
+ *
393
+ * #### URL parameters
394
+ *
395
+ * Named route parameters are captured and added to `ctx.params`.
396
+ *
397
+ * ```javascript
398
+ * router.get('/:category/:title', (ctx, next) => {
399
+ * console.log(ctx.params);
400
+ * // => { category: 'programming', title: 'how-to-node' }
401
+ * });
402
+ * ```
403
+ *
404
+ * The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
405
+ * used to convert paths to regular expressions.
406
+ *
407
+ */
408
+ verb(method: string | string[], nameOrPath: string | RegExp | (string | RegExp)[], pathOrMiddleware: string | RegExp | (string | RegExp)[] | MiddlewareFunc, ...middleware: MiddlewareFunc[]): Router;
409
+ /**
410
+ * Register route with all methods.
411
+ *
412
+ * @param {String} name Optional.
413
+ * @param {String} path path string
414
+ * @param {Function=} middleware You may also pass multiple middleware.
415
+ * @return {Router} router instance
416
+ * @private
417
+ */
418
+ all(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
419
+ all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
420
+ acl(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
421
+ acl(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
422
+ bind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
423
+ bind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
424
+ checkout(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
425
+ checkout(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
426
+ connect(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
427
+ connect(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
428
+ copy(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
429
+ copy(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
430
+ delete(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
431
+ delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
432
+ /** Alias for `router.delete()` because delete is a reserved word */
433
+ del(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
434
+ del(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
435
+ get(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
436
+ get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
437
+ query(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
438
+ query(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
439
+ head(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
440
+ head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
441
+ link(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
442
+ link(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
443
+ lock(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
444
+ lock(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
445
+ ["m-search"](path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
446
+ ["m-search"](name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
447
+ merge(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
448
+ merge(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
449
+ mkactivity(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
450
+ mkactivity(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
451
+ mkcalendar(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
452
+ mkcalendar(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
453
+ mkcol(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
454
+ mkcol(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
455
+ move(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
456
+ move(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
457
+ notify(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
458
+ notify(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
459
+ options(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
460
+ options(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
461
+ patch(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
462
+ patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
463
+ post(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
464
+ post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
465
+ propfind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
466
+ propfind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
467
+ proppatch(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
468
+ proppatch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
469
+ purge(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
470
+ purge(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
471
+ put(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
472
+ put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
473
+ rebind(path: string | RegExp, ...middlewares: MiddlewareFunc[]): Router;
474
+ rebind(name: string, path: string | RegExp, ...middlewares: MiddlewareFunc[]): Router;
475
+ report(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
476
+ report(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
477
+ search(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
478
+ search(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
479
+ source(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
480
+ source(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
481
+ subscribe(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
482
+ subscribe(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
483
+ trace(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
484
+ trace(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
485
+ unbind(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
486
+ unbind(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
487
+ unlink(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
488
+ unlink(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
489
+ unlock(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
490
+ unlock(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
491
+ unsubscribe(path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
492
+ unsubscribe(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: MiddlewareFunc[]): Router;
494
493
  }
494
+ //#endregion
495
+ export { AllowedMethodsOptions, MatchedResult, RegisterOptions, Router, RouterMethod, RouterOptions };