forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koa-router.d.ts
259 lines (208 loc) · 8.02 KB
/
koa-router.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Type definitions for koa-router v7.x
// Project: https://github.com/alexmingoia/koa-router/
// Definitions by: Jerry Chin <https://github.com/hellopao/>
// Definitions: https://github.com/hellopao/DefinitelyTyped
/* =================== USAGE ===================
import * as Router from "koa-router";
var router = new Router();
=============================================== */
/// <reference path="../koa/koa.d.ts" />
declare module "koa-router" {
import * as Koa from "koa";
module Layer {
export interface ILayerOptions {
name: string;
sensitive?: boolean;
strict?: boolean;
}
}
module Router {
export interface IRouterOptions {
/**
* Router prefixes
*/
prefix?: string;
/**
* HTTP verbs
*/
methods?: string[];
routerPath?: string;
sensitive?: boolean;
}
export interface IRouterContext extends Koa.Context {
/**
* url params
*/
params: any;
}
export interface IMiddleware {
(ctx: Router.IRouterContext, next: () => Promise<any>): any;
}
export interface IParamMiddleware {
(param: string, ctx: Router.IRouterContext, next: () => Promise<any>): any;
}
export interface IRouterAllowedMethodsOptions {
/**
* throw error instead of setting status and header
*/
throw?: boolean;
/**
* throw the returned value in place of the default NotImplemented error
*/
notImplemented?: () => any;
/**
* throw the returned value in place of the default MethodNotAllowed error
*/
methodNotAllowed?: () => any;
}
}
class Layer {
opts: Layer.ILayerOptions;
name: string;
methods: string[];
paramNames: string[];
stack: Router.IMiddleware[];
regexp: RegExp;
path: string;
constructor(path: string | RegExp, methods: string[], middleware: Router.IMiddleware, opts?: Layer.ILayerOptions);
constructor(path: string | RegExp, methods: string[], middleware: Array<Router.IMiddleware>, opts?: Layer.ILayerOptions);
/**
* Returns whether request `path` matches route.
*/
match(path: string): boolean;
/**
* Returns map of URL parameters for given `path` and `paramNames`.
*/
params(path: string | RegExp, captures: string[], existingParams?: Object): Object;
/**
* Returns array of regexp url path captures.
*/
captures(path: string): string[];
/**
* Generate URL for route using given `params`.
*/
url(params: Object): string;
/**
* Run validations on route named parameters.
*/
param(param: string, fn: Router.IMiddleware): Layer;
/**
* Prefix route path.
*/
setPrefix(prefix: string): Layer;
}
class Router {
params: Object;
stack: Array<Layer>;
/**
* Create a new router.
*/
constructor(opt?: Router.IRouterOptions);
/**
* Use given middleware.
*
* Middleware run in the order they are defined by `.use()`. They are invoked
* sequentially, requests start at the first middleware and work their way
* "down" the middleware stack.
*/
use(...middleware: Array<Router.IMiddleware>): Router;
use(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP get method
*/
get(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
get(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP post method
*/
post(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
post(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP put method
*/
put(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
put(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP delete method
*/
delete(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
delete(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* Alias for `router.delete()` because delete is a reserved word
*/
del(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
del(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP head method
*/
head(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
head(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP options method
*/
options(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
options(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* HTTP path method
*/
patch(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
patch(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* Register route with all methods.
*/
all(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
all(path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
/**
* Set the path prefix for a Router instance that was already initialized.
*/
prefix(prefix: string): Router;
/**
* Returns router middleware which dispatches a route matching the request.
*/
routes(): Router.IMiddleware;
/**
* Returns router middleware which dispatches a route matching the request.
*/
middlewares(): Router.IMiddleware;
/**
* Returns separate middleware for responding to `OPTIONS` requests with
* an `Allow` header containing the allowed methods, as well as responding
* with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
*/
allowedMethods(options?: Router.IRouterAllowedMethodsOptions): Router.IMiddleware;
/**
* Redirect `source` to `destination` URL with optional 30x status `code`.
*
* Both `source` and `destination` can be route names.
*/
redirect(source: string, destination: string, code?: number): Router;
/**
* Create and register a route.
*/
register(path: string | RegExp, methods: string[], middleware: Router.IMiddleware, opts?: Object): Layer;
/**
* Lookup route with given `name`.
*/
route(name: string): Layer;
route(name: string): boolean;
/**
* Generate URL for route. Takes either map of named `params` or series of
* arguments (for regular expression routes)
*/
url(name: string, params: Object): string;
url(name: string, params: Object): Error;
/**
* Match given `path` and return corresponding routes.
*/
match(name: string, method: string): Object;
/**
* Run middleware for named route parameters. Useful for auto-loading or validation.
*/
param(param: string, middleware: Router.IParamMiddleware): Router;
/**
* Generate URL from url pattern and given `params`.
*/
static url(path: string | RegExp, params: Object): string;
}
export = Router;
}