1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | import * as Koa from "koa";
|
21 |
|
22 | declare namespace Router {
|
23 | interface RouterOptions {
|
24 | |
25 |
|
26 |
|
27 | prefix?: string | undefined;
|
28 | |
29 |
|
30 |
|
31 | methods?: string[] | undefined;
|
32 | routerPath?: string | undefined;
|
33 | |
34 |
|
35 |
|
36 | sensitive?: boolean | undefined;
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | strict?: boolean | undefined;
|
44 | }
|
45 |
|
46 | interface RouterParamContext<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
47 | |
48 |
|
49 |
|
50 | params: Record<string, string>;
|
51 | |
52 |
|
53 |
|
54 | router: Router<StateT, ContextT>;
|
55 | |
56 |
|
57 |
|
58 | _matchedRoute: string | RegExp | undefined;
|
59 | _matchedRouteName: string | undefined;
|
60 | }
|
61 |
|
62 | type RouterContext<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> = Koa.ParameterizedContext<StateT, ContextT & RouterParamContext<StateT, ContextT>>;
|
63 |
|
64 | type Middleware<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> = Koa.Middleware<StateT, ContextT & RouterParamContext<StateT, ContextT>>;
|
65 |
|
66 | interface ParamMiddleware {
|
67 | (param: string, ctx: RouterContext, next: Koa.Next): any;
|
68 | }
|
69 |
|
70 | interface RouterAllowedMethodsOptions {
|
71 | |
72 |
|
73 |
|
74 | throw?: boolean | undefined;
|
75 | |
76 |
|
77 |
|
78 | notImplemented?: (() => any) | undefined;
|
79 | /**
|
80 | * throw the returned value in place of the default MethodNotAllowed error
|
81 | */
|
82 | methodNotAllowed?: (() => any) | undefined;
|
83 | }
|
84 |
|
85 | interface LayerOptions {
|
86 | name: string | null;
|
87 | sensitive?: boolean | undefined;
|
88 | strict?: boolean | undefined;
|
89 | end?: boolean | undefined;
|
90 | prefix?: string | undefined;
|
91 | ignoreCaptures?: boolean | undefined;
|
92 | }
|
93 |
|
94 | interface UrlOptionsQuery {
|
95 | query: object | string;
|
96 | }
|
97 |
|
98 | interface RoutesMatch {
|
99 | path: Layer[];
|
100 | pathAndMethod: Layer[];
|
101 | route: boolean;
|
102 | }
|
103 |
|
104 | class ParamName {
|
105 | asterisk: boolean;
|
106 | delimiter: string;
|
107 | name: string;
|
108 | optional: boolean;
|
109 | partial: boolean;
|
110 | pattern: string;
|
111 | prefix: string;
|
112 | repeat: string;
|
113 | }
|
114 |
|
115 | class Layer {
|
116 | opts: LayerOptions;
|
117 | name: string | null;
|
118 | methods: string[];
|
119 | paramNames: ParamName[];
|
120 | stack: Middleware[];
|
121 | regexp: RegExp;
|
122 | path: string;
|
123 |
|
124 | constructor(path: string | RegExp, methods: string[], middleware: Middleware | Middleware[], opts?: LayerOptions);
|
125 |
|
126 | /**
|
127 | * Returns whether request `path` matches route.
|
128 | */
|
129 | match(path: string): boolean;
|
130 |
|
131 | /**
|
132 | * Returns map of URL parameters for given `path` and `paramNames`.
|
133 | */
|
134 | params(path: string | RegExp, captures: string[], existingParams?: object): object;
|
135 |
|
136 | /**
|
137 | * Returns array of regexp url path captures.
|
138 | */
|
139 | captures(path: string): string[];
|
140 |
|
141 | /**
|
142 | * Generate URL for route using given `params`.
|
143 | */
|
144 | url(params: object): string;
|
145 |
|
146 | /**
|
147 | * Run validations on route named parameters.
|
148 | */
|
149 | param(param: string, fn: Middleware): Layer;
|
150 |
|
151 | /**
|
152 | * Prefix route path.
|
153 | */
|
154 | setPrefix(prefix: string): Layer;
|
155 | }
|
156 | }
|
157 |
|
158 | declare class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
159 | opts: Router.RouterOptions;
|
160 | methods: string[];
|
161 | params: object;
|
162 | stack: Router.Layer[];
|
163 |
|
164 | /**
|
165 | * Create a new router.
|
166 | */
|
167 | constructor(opt?: Router.RouterOptions);
|
168 |
|
169 | /**
|
170 | * Use given middleware.
|
171 | *
|
172 | * Middleware run in the order they are defined by `.use()`. They are invoked
|
173 | * sequentially, requests start at the first middleware and work their way
|
174 | * "down" the middleware stack.
|
175 | */
|
176 | use(...middleware: Array<Router.Middleware<StateT, ContextT>>): Router<StateT, ContextT>;
|
177 | use(
|
178 | path: string | string[] | RegExp,
|
179 | ...middleware: Array<Router.Middleware<StateT, ContextT>>
|
180 | ): Router<StateT, ContextT>;
|
181 |
|
182 | /**
|
183 | * HTTP get method
|
184 | */
|
185 | get<T = {}, U = {}>(
|
186 | name: string,
|
187 | path: string | RegExp,
|
188 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
189 | ): Router<StateT, ContextT>;
|
190 | get<T = {}, U = {}>(
|
191 | path: string | RegExp | Array<string | RegExp>,
|
192 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
193 | ): Router<StateT, ContextT>;
|
194 |
|
195 | /**
|
196 | * HTTP post method
|
197 | */
|
198 | post<T = {}, U = {}>(
|
199 | name: string,
|
200 | path: string | RegExp,
|
201 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
202 | ): Router<StateT, ContextT>;
|
203 | post<T = {}, U = {}>(
|
204 | path: string | RegExp | Array<string | RegExp>,
|
205 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
206 | ): Router<StateT, ContextT>;
|
207 |
|
208 | /**
|
209 | * HTTP put method
|
210 | */
|
211 | put<T = {}, U = {}>(
|
212 | name: string,
|
213 | path: string | RegExp,
|
214 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
215 | ): Router<StateT, ContextT>;
|
216 | put<T = {}, U = {}>(
|
217 | path: string | RegExp | Array<string | RegExp>,
|
218 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
219 | ): Router<StateT, ContextT>;
|
220 |
|
221 | /**
|
222 | * HTTP link method
|
223 | */
|
224 | link<T = {}, U = {}>(
|
225 | name: string,
|
226 | path: string | RegExp,
|
227 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
228 | ): Router<StateT, ContextT>;
|
229 | link<T = {}, U = {}>(
|
230 | path: string | RegExp | Array<string | RegExp>,
|
231 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
232 | ): Router<StateT, ContextT>;
|
233 |
|
234 | /**
|
235 | * HTTP unlink method
|
236 | */
|
237 | unlink<T = {}, U = {}>(
|
238 | name: string,
|
239 | path: string | RegExp,
|
240 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
241 | ): Router<StateT, ContextT>;
|
242 | unlink<T = {}, U = {}>(
|
243 | path: string | RegExp | Array<string | RegExp>,
|
244 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
245 | ): Router<StateT, ContextT>;
|
246 |
|
247 | /**
|
248 | * HTTP delete method
|
249 | */
|
250 | delete<T = {}, U = {}>(
|
251 | name: string,
|
252 | path: string | RegExp,
|
253 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
254 | ): Router<StateT, ContextT>;
|
255 | delete<T = {}, U = {}>(
|
256 | path: string | RegExp | Array<string | RegExp>,
|
257 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
258 | ): Router<StateT, ContextT>;
|
259 |
|
260 | /**
|
261 | * Alias for `router.delete()` because delete is a reserved word
|
262 | */
|
263 | del<T = {}, U = {}>(
|
264 | name: string,
|
265 | path: string | RegExp,
|
266 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
267 | ): Router<StateT, ContextT>;
|
268 | del<T = {}, U = {}>(
|
269 | path: string | RegExp | Array<string | RegExp>,
|
270 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
271 | ): Router<StateT, ContextT>;
|
272 |
|
273 | /**
|
274 | * HTTP head method
|
275 | */
|
276 | head<T = {}, U = {}>(
|
277 | name: string,
|
278 | path: string | RegExp,
|
279 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
280 | ): Router<StateT, ContextT>;
|
281 | head<T = {}, U = {}>(
|
282 | path: string | RegExp | Array<string | RegExp>,
|
283 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
284 | ): Router<StateT, ContextT>;
|
285 |
|
286 | /**
|
287 | * HTTP options method
|
288 | */
|
289 | options<T = {}, U = {}>(
|
290 | name: string,
|
291 | path: string | RegExp,
|
292 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
293 | ): Router<StateT, ContextT>;
|
294 | options<T = {}, U = {}>(
|
295 | path: string | RegExp | Array<string | RegExp>,
|
296 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
297 | ): Router<StateT, ContextT>;
|
298 |
|
299 | /**
|
300 | * HTTP patch method
|
301 | */
|
302 | patch<T = {}, U = {}>(
|
303 | name: string,
|
304 | path: string | RegExp,
|
305 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
306 | ): Router<StateT, ContextT>;
|
307 | patch<T = {}, U = {}>(
|
308 | path: string | RegExp | Array<string | RegExp>,
|
309 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
310 | ): Router<StateT, ContextT>;
|
311 |
|
312 | /**
|
313 | * Register route with all methods.
|
314 | */
|
315 | all<T = {}, U = {}>(
|
316 | name: string,
|
317 | path: string | RegExp,
|
318 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
319 | ): Router<StateT, ContextT>;
|
320 | all<T = {}, U = {}>(
|
321 | path: string | RegExp | Array<string | RegExp>,
|
322 | ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
|
323 | ): Router<StateT, ContextT>;
|
324 |
|
325 | /**
|
326 | * Set the path prefix for a Router instance that was already initialized.
|
327 | */
|
328 | prefix(prefix: string): Router<StateT, ContextT>;
|
329 |
|
330 | /**
|
331 | * Returns router middleware which dispatches a route matching the request.
|
332 | */
|
333 | routes(): Router.Middleware<StateT, ContextT>;
|
334 |
|
335 | /**
|
336 | * Returns router middleware which dispatches a route matching the request.
|
337 | */
|
338 | middleware(): Router.Middleware<StateT, ContextT>;
|
339 |
|
340 | /**
|
341 | * Returns separate middleware for responding to `OPTIONS` requests with
|
342 | * an `Allow` header containing the allowed methods, as well as responding
|
343 | * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
344 | */
|
345 | allowedMethods(
|
346 | options?: Router.RouterAllowedMethodsOptions
|
347 | ): Router.Middleware<StateT, ContextT>;
|
348 |
|
349 | /**
|
350 | * Redirect `source` to `destination` URL with optional 30x status `code`.
|
351 | *
|
352 | * Both `source` and `destination` can be route names.
|
353 | */
|
354 | redirect(source: string, destination: string, code?: number): Router<StateT, ContextT>;
|
355 |
|
356 | /**
|
357 | * Create and register a route.
|
358 | */
|
359 | register(
|
360 | path: string | RegExp,
|
361 | methods: string[],
|
362 | middleware: Router.Middleware<StateT, ContextT> | Array<Router.Middleware<StateT, ContextT>>,
|
363 | opts?: Router.LayerOptions,
|
364 | ): Router.Layer;
|
365 |
|
366 | /**
|
367 | * Lookup route with given `name`.
|
368 | */
|
369 | route(name: string): Router.Layer | boolean;
|
370 |
|
371 | /**
|
372 | * Generate URL for route. Takes either map of named `params` or series of
|
373 | * arguments (for regular expression routes)
|
374 | *
|
375 | * router = new Router();
|
376 | * router.get('user', "/users/:id", ...
|
377 | *
|
378 | * router.url('user', { id: 3 });
|
379 | *
|
380 | *
|
381 | * Query can be generated from third argument:
|
382 | *
|
383 | * router.url('user', { id: 3 }, { query: { limit: 1 } });
|
384 | *
|
385 | *
|
386 | * router.url('user', { id: 3 }, { query: "limit=1" });
|
387 | *
|
388 | *
|
389 | */
|
390 | url(name: string, params?: any, options?: Router.UrlOptionsQuery): Error | string;
|
391 |
|
392 | |
393 |
|
394 |
|
395 | match(path: string, method: string): Router.RoutesMatch;
|
396 |
|
397 | |
398 |
|
399 |
|
400 | param(param: string, middleware: Router.ParamMiddleware): Router<StateT, ContextT>;
|
401 |
|
402 | |
403 |
|
404 |
|
405 | static url(path: string | RegExp, params: object): string;
|
406 | }
|
407 |
|
408 | export = Router;
|
409 |
|
\ | No newline at end of file |