1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import * as Koa from "koa";
|
9 |
|
10 | declare namespace Router {
|
11 | interface RouterOptions {
|
12 | |
13 |
|
14 |
|
15 | prefix?: string | undefined;
|
16 | |
17 |
|
18 |
|
19 | methods?: string[] | undefined;
|
20 | routerPath?: string | undefined;
|
21 | |
22 |
|
23 |
|
24 | sensitive?: boolean | undefined;
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | strict?: boolean | undefined;
|
32 | |
33 |
|
34 |
|
35 | exclusive?: boolean | undefined;
|
36 | |
37 |
|
38 |
|
39 | host?: string | RegExp | undefined;
|
40 | }
|
41 |
|
42 | interface RouterParamContext<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
43 | |
44 |
|
45 |
|
46 | params: Record<string, string>;
|
47 | |
48 |
|
49 |
|
50 | router: Router<StateT, ContextT>;
|
51 | |
52 |
|
53 |
|
54 | _matchedRoute: string | RegExp | undefined;
|
55 | _matchedRouteName: string | undefined;
|
56 | }
|
57 |
|
58 | type RouterContext<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext, BodyT = unknown> =
|
59 | Koa.ParameterizedContext<StateT, ContextT & RouterParamContext<StateT, ContextT>, BodyT>;
|
60 |
|
61 | type Middleware<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext, BodyT = unknown> = Koa.Middleware<
|
62 | StateT,
|
63 | ContextT & RouterParamContext<StateT, ContextT>,
|
64 | BodyT
|
65 | >;
|
66 |
|
67 | interface ParamMiddleware<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext, BodyT = unknown> {
|
68 | (param: string, ctx: RouterContext<StateT, ContextT, BodyT>, next: Koa.Next): any;
|
69 | }
|
70 |
|
71 | interface RouterAllowedMethodsOptions {
|
72 | |
73 |
|
74 |
|
75 | throw?: boolean | undefined;
|
76 | |
77 |
|
78 |
|
79 | notImplemented?: (() => any) | undefined;
|
80 | /**
|
81 | * throw the returned value in place of the default MethodNotAllowed error
|
82 | */
|
83 | methodNotAllowed?: (() => any) | undefined;
|
84 | }
|
85 |
|
86 | interface LayerOptions {
|
87 | /**
|
88 | * Route name
|
89 | */
|
90 | name: string | null;
|
91 | /**
|
92 | * Case sensitive (default: false)
|
93 | */
|
94 | sensitive?: boolean | undefined;
|
95 | /**
|
96 | * Require the trailing slash (default: false)
|
97 | */
|
98 | strict?: boolean | undefined;
|
99 | /**
|
100 | * (default: false)
|
101 | */
|
102 | end?: boolean | undefined;
|
103 | /**
|
104 | * (default: '')
|
105 | */
|
106 | prefix?: string | undefined;
|
107 | ignoreCaptures?: boolean | undefined;
|
108 | }
|
109 |
|
110 | interface UrlOptionsQuery {
|
111 | query: object | string;
|
112 | }
|
113 |
|
114 | interface RoutesMatch {
|
115 | path: Layer[];
|
116 | pathAndMethod: Layer[];
|
117 | route: boolean;
|
118 | }
|
119 |
|
120 | class ParamName {
|
121 | asterisk: boolean;
|
122 | delimiter: string;
|
123 | name: string;
|
124 | optional: boolean;
|
125 | partial: boolean;
|
126 | pattern: string;
|
127 | prefix: string;
|
128 | repeat: string;
|
129 | }
|
130 |
|
131 | class Layer {
|
132 | opts: LayerOptions;
|
133 | name: string | null;
|
134 | methods: string[];
|
135 | paramNames: ParamName[];
|
136 | stack: Middleware[];
|
137 | regexp: RegExp;
|
138 | path: string | RegExp;
|
139 |
|
140 | constructor(
|
141 | path: string | RegExp,
|
142 | methods: string[],
|
143 | middleware: Middleware | Middleware[],
|
144 | opts?: LayerOptions,
|
145 | );
|
146 |
|
147 | /**
|
148 | * Returns whether request `path` matches route.
|
149 | */
|
150 | match(path: string): boolean;
|
151 |
|
152 | /**
|
153 | * Returns map of URL parameters for given `path` and `paramNames`.
|
154 | */
|
155 | params<ParamT extends string = string>(
|
156 | path: string | RegExp,
|
157 | captures: ParamT[],
|
158 | params?: Record<string, any>,
|
159 | ): { [key in ParamT]?: string };
|
160 |
|
161 | /**
|
162 | * Returns array of regexp url path captures.
|
163 | */
|
164 | captures(path: string): string[];
|
165 |
|
166 | /**
|
167 | * Generate URL for route using given `params`.
|
168 | *
|
169 | * @example
|
170 | *
|
171 | * ```javascript
|
172 | * const route = new Layer('/users/:id', ['GET'], fn);
|
173 | *
|
174 | * route.url({ id: 123 }); // => "/users/123"
|
175 | * ```
|
176 | */
|
177 | url(params: object): string;
|
178 |
|
179 | /**
|
180 | * Run validations on route named parameters.
|
181 | *
|
182 | * @example
|
183 | *
|
184 | * ```javascript
|
185 | * router
|
186 | * .param('user', function (id, ctx, next) {
|
187 | * ctx.user = users[id];
|
188 | * if (!ctx.user) return ctx.status = 404;
|
189 | * next();
|
190 | * })
|
191 | * .get('/users/:user', function (ctx, next) {
|
192 | * ctx.body = ctx.user;
|
193 | * });
|
194 | * ```
|
195 | */
|
196 | param(param: string, middleware: ParamMiddleware): Layer;
|
197 |
|
198 | /**
|
199 | * Prefix route path.
|
200 | */
|
201 | setPrefix(prefix: string): Layer;
|
202 | }
|
203 | }
|
204 |
|
205 | declare class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
206 | opts: Router.RouterOptions;
|
207 | methods: string[];
|
208 | params: object;
|
209 | stack: Router.Layer[];
|
210 |
|
211 | /**
|
212 | * Create a new router.
|
213 | */
|
214 | constructor(opt?: Router.RouterOptions);
|
215 |
|
216 | /**
|
217 | * Use given middleware.
|
218 | *
|
219 | * Middleware run in the order they are defined by `.use()`. They are invoked
|
220 | * sequentially, requests start at the first middleware and work their way
|
221 | * "down" the middleware stack.
|
222 | */
|
223 | use(...middleware: Array<Router.Middleware<StateT, ContextT>>): Router<StateT, ContextT>;
|
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 | use(
|
232 | path: string | string[] | RegExp,
|
233 | ...middleware: Array<Router.Middleware<StateT, ContextT>>
|
234 | ): Router<StateT, ContextT>;
|
235 |
|
236 | /**
|
237 | * HTTP get method
|
238 | */
|
239 | get<T = {}, U = {}, B = unknown>(
|
240 | name: string,
|
241 | path: string | RegExp,
|
242 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
243 | ): Router<StateT, ContextT>;
|
244 | /**
|
245 | * HTTP get method
|
246 | */
|
247 | get<T = {}, U = {}, B = unknown>(
|
248 | path: string | RegExp | Array<string | RegExp>,
|
249 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
250 | ): Router<StateT, ContextT>;
|
251 |
|
252 | /**
|
253 | * HTTP post method
|
254 | */
|
255 | post<T = {}, U = {}, B = unknown>(
|
256 | name: string,
|
257 | path: string | RegExp,
|
258 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
259 | ): Router<StateT, ContextT>;
|
260 | /**
|
261 | * HTTP post method
|
262 | */
|
263 | post<T = {}, U = {}, B = unknown>(
|
264 | path: string | RegExp | Array<string | RegExp>,
|
265 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
266 | ): Router<StateT, ContextT>;
|
267 |
|
268 | /**
|
269 | * HTTP put method
|
270 | */
|
271 | put<T = {}, U = {}, B = unknown>(
|
272 | name: string,
|
273 | path: string | RegExp,
|
274 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
275 | ): Router<StateT, ContextT>;
|
276 | /**
|
277 | * HTTP put method
|
278 | */
|
279 | put<T = {}, U = {}, B = unknown>(
|
280 | path: string | RegExp | Array<string | RegExp>,
|
281 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
282 | ): Router<StateT, ContextT>;
|
283 |
|
284 | /**
|
285 | * HTTP link method
|
286 | */
|
287 | link<T = {}, U = {}, B = unknown>(
|
288 | name: string,
|
289 | path: string | RegExp,
|
290 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
291 | ): Router<StateT, ContextT>;
|
292 | /**
|
293 | * HTTP link method
|
294 | */
|
295 | link<T = {}, U = {}, B = unknown>(
|
296 | path: string | RegExp | Array<string | RegExp>,
|
297 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
298 | ): Router<StateT, ContextT>;
|
299 |
|
300 | /**
|
301 | * HTTP unlink method
|
302 | */
|
303 | unlink<T = {}, U = {}, B = unknown>(
|
304 | name: string,
|
305 | path: string | RegExp,
|
306 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
307 | ): Router<StateT, ContextT>;
|
308 | /**
|
309 | * HTTP unlink method
|
310 | */
|
311 | unlink<T = {}, U = {}, B = unknown>(
|
312 | path: string | RegExp | Array<string | RegExp>,
|
313 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
314 | ): Router<StateT, ContextT>;
|
315 |
|
316 | /**
|
317 | * HTTP delete method
|
318 | */
|
319 | delete<T = {}, U = {}, B = unknown>(
|
320 | name: string,
|
321 | path: string | RegExp,
|
322 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
323 | ): Router<StateT, ContextT>;
|
324 | /**
|
325 | * HTTP delete method
|
326 | */
|
327 | delete<T = {}, U = {}, B = unknown>(
|
328 | path: string | RegExp | Array<string | RegExp>,
|
329 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
330 | ): Router<StateT, ContextT>;
|
331 |
|
332 | /**
|
333 | * Alias for `router.delete()` because delete is a reserved word
|
334 | */
|
335 | del<T = {}, U = {}, B = unknown>(
|
336 | name: string,
|
337 | path: string | RegExp,
|
338 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
339 | ): Router<StateT, ContextT>;
|
340 | /**
|
341 | * Alias for `router.delete()` because delete is a reserved word
|
342 | */
|
343 | del<T = {}, U = {}, B = unknown>(
|
344 | path: string | RegExp | Array<string | RegExp>,
|
345 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
346 | ): Router<StateT, ContextT>;
|
347 |
|
348 | /**
|
349 | * HTTP head method
|
350 | */
|
351 | head<T = {}, U = {}, B = unknown>(
|
352 | name: string,
|
353 | path: string | RegExp,
|
354 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
355 | ): Router<StateT, ContextT>;
|
356 | /**
|
357 | * HTTP head method
|
358 | */
|
359 | head<T = {}, U = {}, B = unknown>(
|
360 | path: string | RegExp | Array<string | RegExp>,
|
361 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
362 | ): Router<StateT, ContextT>;
|
363 |
|
364 | /**
|
365 | * HTTP options method
|
366 | */
|
367 | options<T = {}, U = {}, B = unknown>(
|
368 | name: string,
|
369 | path: string | RegExp,
|
370 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
371 | ): Router<StateT, ContextT>;
|
372 | /**
|
373 | * HTTP options method
|
374 | */
|
375 | options<T = {}, U = {}, B = unknown>(
|
376 | path: string | RegExp | Array<string | RegExp>,
|
377 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
378 | ): Router<StateT, ContextT>;
|
379 |
|
380 | /**
|
381 | * HTTP patch method
|
382 | */
|
383 | patch<T = {}, U = {}, B = unknown>(
|
384 | name: string,
|
385 | path: string | RegExp,
|
386 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
387 | ): Router<StateT, ContextT>;
|
388 | /**
|
389 | * HTTP patch method
|
390 | */
|
391 | patch<T = {}, U = {}, B = unknown>(
|
392 | path: string | RegExp | Array<string | RegExp>,
|
393 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
394 | ): Router<StateT, ContextT>;
|
395 |
|
396 | /**
|
397 | * Register route with all methods.
|
398 | */
|
399 | all<T = {}, U = {}, B = unknown>(
|
400 | name: string,
|
401 | path: string | RegExp,
|
402 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
403 | ): Router<StateT, ContextT>;
|
404 | /**
|
405 | * Register route with all methods.
|
406 | */
|
407 | all<T = {}, U = {}, B = unknown>(
|
408 | path: string | RegExp | Array<string | RegExp>,
|
409 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
|
410 | ): Router<StateT, ContextT>;
|
411 |
|
412 | /**
|
413 | * Set the path prefix for a Router instance that was already initialized.
|
414 | *
|
415 | * @example
|
416 | *
|
417 | * ```javascript
|
418 | * router.prefix('/things/:thing_id')
|
419 | * ```
|
420 | */
|
421 | prefix(prefix: string): Router<StateT, ContextT>;
|
422 |
|
423 | /**
|
424 | * Returns router middleware which dispatches a route matching the request.
|
425 | */
|
426 | routes(): Router.Middleware<StateT, ContextT>;
|
427 |
|
428 | /**
|
429 | * Returns router middleware which dispatches a route matching the request.
|
430 | */
|
431 | middleware(): Router.Middleware<StateT, ContextT>;
|
432 |
|
433 | /**
|
434 | * Returns separate middleware for responding to `OPTIONS` requests with
|
435 | * an `Allow` header containing the allowed methods, as well as responding
|
436 | * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
437 | *
|
438 | * @example
|
439 | *
|
440 | * ```javascript
|
441 | * var Koa = require('koa');
|
442 | * var Router = require('koa-router');
|
443 | *
|
444 | * var app = new Koa();
|
445 | * var router = new Router();
|
446 | *
|
447 | * app.use(router.routes());
|
448 | * app.use(router.allowedMethods());
|
449 | * ```
|
450 | *
|
451 | * **Example with [Boom](https://github.com/hapijs/boom)**
|
452 | *
|
453 | * ```javascript
|
454 | * var Koa = require('koa');
|
455 | * var Router = require('koa-router');
|
456 | * var Boom = require('boom');
|
457 | *
|
458 | * var app = new Koa();
|
459 | * var router = new Router();
|
460 | *
|
461 | * app.use(router.routes());
|
462 | * app.use(router.allowedMethods({
|
463 | * throw: true,
|
464 | * notImplemented: () => new Boom.notImplemented(),
|
465 | * methodNotAllowed: () => new Boom.methodNotAllowed()
|
466 | * }));
|
467 | * ```
|
468 | */
|
469 | allowedMethods(
|
470 | options?: Router.RouterAllowedMethodsOptions,
|
471 | ): Router.Middleware<StateT, ContextT>;
|
472 |
|
473 | /**
|
474 | * Redirect `source` to `destination` URL with optional 30x status `code`.
|
475 | *
|
476 | * Both `source` and `destination` can be route names.
|
477 | *
|
478 | * ```javascript
|
479 | * router.redirect('/login', 'sign-in');
|
480 | * ```
|
481 | *
|
482 | * This is equivalent to:
|
483 | *
|
484 | * ```javascript
|
485 | * router.all('/login', ctx => {
|
486 | * ctx.redirect('/sign-in');
|
487 | * ctx.status = 301;
|
488 | * });
|
489 | * ```
|
490 | */
|
491 | redirect(source: string, destination: string, code?: number): Router<StateT, ContextT>;
|
492 |
|
493 | /**
|
494 | * Create and register a route.
|
495 | */
|
496 | register(
|
497 | path: string | RegExp,
|
498 | methods: string[],
|
499 | middleware: Router.Middleware<StateT, ContextT> | Array<Router.Middleware<StateT, ContextT>>,
|
500 | opts?: Router.LayerOptions,
|
501 | ): Router.Layer;
|
502 |
|
503 | /**
|
504 | * Lookup route with given `name`.
|
505 | */
|
506 | route(name: string): Router.Layer | boolean;
|
507 |
|
508 | /**
|
509 | * Generate URL for route. Takes either map of named `params` or series of
|
510 | * arguments (for regular expression routes)
|
511 | *
|
512 | * router = new Router();
|
513 | * router.get('user', "/users/:id", ...
|
514 | *
|
515 | * router.url('user', { id: 3 });
|
516 | * // => "/users/3"
|
517 | *
|
518 | * Query can be generated from third argument:
|
519 | *
|
520 | * router.url('user', { id: 3 }, { query: { limit: 1 } });
|
521 | * // => "/users/3?limit=1"
|
522 | *
|
523 | * router.url('user', { id: 3 }, { query: "limit=1" });
|
524 | * // => "/users/3?limit=1"
|
525 | */
|
526 | url(name: string, params?: any, options?: Router.UrlOptionsQuery): Error | string;
|
527 |
|
528 | /**
|
529 | * Match given `path` and return corresponding routes.
|
530 | */
|
531 | match(path: string, method: string): Router.RoutesMatch;
|
532 |
|
533 | /**
|
534 | * Run middleware for named route parameters. Useful for auto-loading or
|
535 | * validation.
|
536 | *
|
537 | * @example
|
538 | *
|
539 | * ```javascript
|
540 | * router
|
541 | * .param('user', (id, ctx, next) => {
|
542 | * ctx.user = users[id];
|
543 | * if (!ctx.user) return ctx.status = 404;
|
544 | * return next();
|
545 | * })
|
546 | * .get('/users/:user', ctx => {
|
547 | * ctx.body = ctx.user;
|
548 | * })
|
549 | * .get('/users/:user/friends', ctx => {
|
550 | * return ctx.user.getFriends().then(function(friends) {
|
551 | * ctx.body = friends;
|
552 | * });
|
553 | * })
|
554 | *
|
555 | *
|
556 | * ```
|
557 | */
|
558 |
|
559 | param<BodyT = unknown>(
|
560 | param: string,
|
561 | middleware: Router.ParamMiddleware<StateT, ContextT, BodyT>,
|
562 | ): Router<StateT, ContextT>;
|
563 |
|
564 | /**
|
565 | * Generate URL for route. Takes a route name and map of named `params`.
|
566 | *
|
567 | * @example
|
568 | *
|
569 | * ```javascript
|
570 | * router.get('user', '/users/:id', (ctx, next) => {
|
571 | *
|
572 | * });
|
573 | *
|
574 | * router.url('user', 3);
|
575 | *
|
576 | *
|
577 | * router.url('user', { id: 3 });
|
578 | *
|
579 | *
|
580 | * router.use((ctx, next) => {
|
581 | *
|
582 | * ctx.redirect(ctx.router.url('sign-in'));
|
583 | * })
|
584 | *
|
585 | * router.url('user', { id: 3 }, { query: { limit: 1 } });
|
586 | *
|
587 | *
|
588 | * router.url('user', { id: 3 }, { query: "limit=1" });
|
589 | *
|
590 | * ```
|
591 | */
|
592 | static url(path: string | RegExp, params: object): string;
|
593 | }
|
594 |
|
595 | export = Router;
|
596 |
|
\ | No newline at end of file |