Repository navigation
Expand file tree
/
Copy pathindex.d.ts
More file actions
188 lines (168 loc) · 6.98 KB
/
Copy pathindex.d.ts
File metadata and controls
188 lines (168 loc) · 6.98 KB
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
import type { IncomingMessage, ServerResponse } from 'node:http'
import type { MatchFunction } from 'path-to-regexp'
import RouteModule = require('./lib/route')
declare const Router: Router.RouterConstructor
// eslint-disable-next-line @typescript-eslint/no-redeclare -- Merge types with the callable and constructable CommonJS export.
declare namespace Router {
/**
* Continues dispatch, forwards an error, or skips the current route or router.
* Use `next('route')` to skip a route and `next('router')` to exit the router.
*/
type NextFunction = RouteModule.NextFunction
type Params = Record<string, string | string[] | undefined>
/** Can be augmented to describe properties added by middleware. */
interface Request<P = Params> extends IncomingMessage {
params: P
baseUrl: string
originalUrl: string
route?: Route
next?: NextFunction
}
/** Can be augmented to describe properties and methods added by middleware. */
interface Response extends ServerResponse {}
type RequestHandler<P = Params> = RouteModule.RequestHandler<P>
type ErrorRequestHandler<P = Params> = RouteModule.ErrorRequestHandler<P>
/**
* Processes a matched path parameter before the layer's handlers run.
* Call `next()` to continue, or respond to the request.
* Rejected promises are forwarded as errors; resolved promises do not call `next()`.
*
* @param value The matched parameter value.
* @param name The parameter name supplied to `router.param()`.
*/
type RequestParamHandler = (req: Request, res: Response, next: NextFunction, value: any, name: string) => void
type Handler<P = Params> = RouteModule.Handler<P>
type HandlerArg<F extends Handler<any> = Handler> = RouteModule.HandlerArg<F>
type Method = RouteModule.Method
type Path = RouteModule.Path
type Route<R extends Path = Path> = RouteModule.Route<R>
type RouteParameters<R extends Path> = RouteModule.RouteParameters<R>
/** A mounted router may come from a version without route introspection. */
interface MountedRouter {
(req: IncomingMessage, res: ServerResponse, next: NextFunction): unknown
stack: unknown[]
listRoutes?(): RouteInfo[]
}
/** One registered path, with either route methods or a mounted router. */
type RouteInfo = {
path: string | RegExp
} & ({
/** Uppercase methods and `'*'` for `.all()`, or an empty array for a route without handlers. */
methods: string[]
router: undefined
} | {
methods: undefined
router: MountedRouter
})
/** The result of one internal layer match, separate from other requests. */
interface LayerMatch {
params: Params
path: string
keys: string[]
pattern: string | RegExp
}
/** Internal layer metadata. Its shape may change between releases. */
interface Layer {
handle: Handler<any>
name: string
/** The registered path, with path arrays flattened and copied at registration. */
rawPath: string | RegExp | Array<string | RegExp>
slash: boolean
route?: Route | undefined
method?: Method | undefined
matchers: Array<MatchFunction<Params>>
match(path: string | null | undefined): LayerMatch | false
handleRequest(req: Request<any>, res: Response, next: NextFunction): void
handleError(error: any, req: Request<any>, res: Response, next: NextFunction): void
}
type RouterMethod<T> = {
<R extends string | RegExp, P = RouteParameters<R>>(path: R, handler: HandlerArg<RequestHandler<P>>, ...handlers: Array<HandlerArg<RequestHandler<P>>>): T
<R extends string | RegExp, P = RouteParameters<R>>(path: R, handler: HandlerArg<Handler<P>>, ...handlers: Array<HandlerArg<Handler<P>>>): T
<F extends Handler<any> = RequestHandler>(path: Path, handler: HandlerArg<F>, ...handlers: Array<HandlerArg<F>>): T
<P = Params>(path: Path, handler: HandlerArg<RequestHandler<P>>, ...handlers: Array<HandlerArg<RequestHandler<P>>>): T
<P = Params>(path: Path, handler: HandlerArg<Handler<P>>, ...handlers: Array<HandlerArg<Handler<P>>>): T
}
interface RouterOptions {
caseSensitive?: boolean
/**
* Preserve parent `req.params` when matching this router's layers.
* This router's named parameters take precedence over parent parameters.
* Parent parameter names are not inferred from the child route's path;
* include them in an explicit parameter type when needed.
* @default false
*/
mergeParams?: boolean
strict?: boolean
}
type MethodHandlers<T> = { [M in Method | 'all']: RouterMethod<T> }
interface Router {
(req: IncomingMessage, res: ServerResponse, callback: NextFunction): void
params: Record<string, any>
stack: Layer[]
caseSensitive: boolean | undefined
/** Whether to preserve parent `req.params`. Disabled when false or undefined. */
mergeParams: boolean | undefined
strict: boolean | undefined
handle(req: IncomingMessage, res: ServerResponse, callback: NextFunction): void
/**
* Register a callback for a named path parameter on this router.
* Callbacks run in registration order before the matching layer's handlers.
* They are local to this router and are not inherited by mounted routers.
*
* @param name The parameter name, without the `:` or `*` prefix.
* @param fn Receives `(req, res, next, value, name)`.
* @returns This router for chaining.
*/
param(name: string, fn: RequestParamHandler): this
route<R extends Path>(path: R): Route<R>
/**
* List routes and mounted routers in registration order, one entry per path.
* Methods include `'*'` for `.all()` and implicit `HEAD` for `GET` routes.
* Plain middleware is omitted; mounted routers are not traversed recursively.
*/
listRoutes(): RouteInfo[]
use: RouterMethod<this> & RouteModule.RouteMethod<this>
all: RouterMethod<this>
acl: RouterMethod<this>
bind: RouterMethod<this>
checkout: RouterMethod<this>
connect: RouterMethod<this>
copy: RouterMethod<this>
delete: RouterMethod<this>
get: RouterMethod<this>
head: RouterMethod<this>
link: RouterMethod<this>
lock: RouterMethod<this>
'm-search': RouterMethod<this>
merge: RouterMethod<this>
mkactivity: RouterMethod<this>
mkcalendar: RouterMethod<this>
mkcol: RouterMethod<this>
move: RouterMethod<this>
notify: RouterMethod<this>
options: RouterMethod<this>
patch: RouterMethod<this>
post: RouterMethod<this>
propfind: RouterMethod<this>
proppatch: RouterMethod<this>
purge: RouterMethod<this>
put: RouterMethod<this>
query: RouterMethod<this>
rebind: RouterMethod<this>
report: RouterMethod<this>
search: RouterMethod<this>
source: RouterMethod<this>
subscribe: RouterMethod<this>
trace: RouterMethod<this>
unbind: RouterMethod<this>
unlink: RouterMethod<this>
unlock: RouterMethod<this>
unsubscribe: RouterMethod<this>
}
interface RouterConstructor {
(options?: RouterOptions): Router
new (options?: RouterOptions): Router
Route: typeof RouteModule
}
}
export = Router