@eggjs/router 3.0.6 → 4.0.0-beta.17

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.
@@ -0,0 +1,496 @@
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
+
7
+ type RouterMethod = (typeof methods)[0];
8
+ interface RouterOptions {
9
+ methods?: string[];
10
+ prefix?: string;
11
+ sensitive?: boolean;
12
+ strict?: boolean;
13
+ routerPath?: string;
14
+ }
15
+ interface RegisterOptions {
16
+ name?: string;
17
+ prefix?: string;
18
+ sensitive?: boolean;
19
+ strict?: boolean;
20
+ ignoreCaptures?: boolean;
21
+ end?: boolean;
22
+ }
23
+ interface AllowedMethodsOptions {
24
+ throw?: boolean;
25
+ notImplemented?: () => Error;
26
+ methodNotAllowed?: () => Error;
27
+ }
28
+ interface MatchedResult {
29
+ path: Layer[];
30
+ pathAndMethod: Layer[];
31
+ route: boolean;
32
+ }
33
+ 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: (string | MiddlewareFunc | 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;
494
+ }
495
+ //#endregion
496
+ export { AllowedMethodsOptions, MatchedResult, RegisterOptions, Router, RouterMethod, RouterOptions };