-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.ts
244 lines (220 loc) · 6.07 KB
/
router.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
import { METHODS } from './constants.ts'
import { THRequest } from './request.ts'
import type { THResponse } from './response.ts'
import type { Handler, Method, Middleware } from './types.ts'
type LowerCaseMethod = Lowercase<Method>
export type RouterMethod<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> = (
path: string | Handler<Req, Res>,
handler?: RouterHandler<Req, Res>,
...handlers: RouterHandler<Req, Res>[]
) => any
type RouterMethodParams<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> = Parameters<RouterMethod<Req, Res>>
type RIM<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
App extends any = any,
> = (...args: RouterMethodParams<Req, Res>) => App
export type MethodHandler<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> = {
path?: string | Handler<Req, Res>
handler?: Handler<Req, Res>
type: 'mw' | 'route'
fullPath?: string
pattern?: URLPattern
}
export type RouterHandler<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> = Handler<Req, Res> | Handler<Req, Res>[]
export type RouterPathOrHandler<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> = string | RouterHandler<Req, Res>
export type UseMethod<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
App extends Router = Router,
> = (
path: RouterPathOrHandler<Req, Res> | App,
handler?: RouterHandler<Req, Res> | App,
...handlers: (RouterHandler<Req, Res> | App)[]
) => any
export type UseMethodParams<
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
App extends Router = Router,
> = Parameters<UseMethod<Req, Res, App>>
const createMiddlewareFromRoute = <
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
>({
path,
handler,
fullPath,
method,
}: MethodHandler<Req, Res> & {
method?: Method
}) => {
return ({
method,
handler: handler || (path as Handler),
path: typeof path === 'string' ? path : '/',
fullPath: typeof path === 'string' ? fullPath : path,
})
}
/**
* Push wares to a middleware array
* @param mw Middleware arrays
*/
export const pushMiddleware =
<Req extends THRequest = THRequest, Res extends THResponse = THResponse>(
mw: Middleware<Req, Res>[],
) =>
({
path,
handler,
method,
handlers,
type,
fullPaths,
pattern,
}: MethodHandler<Req, Res> & {
method?: Method
handlers?: RouterHandler<Req, Res>[]
fullPaths?: string[]
}): void => {
const m = createMiddlewareFromRoute<Req, Res>({
path,
handler,
method,
type,
fullPath: fullPaths?.[0],
pattern,
})
let waresFromHandlers: { handler: Handler<Req, Res> }[] = []
let idx = 1
if (handlers) {
waresFromHandlers = handlers.flat().map((handler) =>
createMiddlewareFromRoute<Req, Res>({
path,
handler,
method,
type,
fullPath: fullPaths == null ? undefined : fullPaths[idx++],
pattern,
})
)
}
for (const mdw of [m, ...waresFromHandlers]) {
mw.push({ ...mdw, type, pattern })
}
}
export class Router<
App extends Router = any,
Req extends THRequest = THRequest,
Res extends THResponse = THResponse,
> {
middleware: Middleware<Req, Res>[] = []
apps: Record<string, App> = {}
mountpath = '/'
parent?: App
acl!: RIM<Req, Res, this>
bind!: RIM<Req, Res, this>
checkout!: RIM<Req, Res, this>
connect!: RIM<Req, Res, this>
copy!: RIM<Req, Res, this>
delete!: RIM<Req, Res, this>
get!: RIM<Req, Res, this>
head!: RIM<Req, Res, this>
link!: RIM<Req, Res, this>
lock!: RIM<Req, Res, this>
merge!: RIM<Req, Res, this>
mkactivity!: RIM<Req, Res, this>
mkcalendar!: RIM<Req, Res, this>
mkcol!: RIM<Req, Res, this>
move!: RIM<Req, Res, this>
notify!: RIM<Req, Res, this>
options!: RIM<Req, Res, this>
patch!: RIM<Req, Res, this>
post!: RIM<Req, Res, this>
pri!: RIM<Req, Res, this>
propfind!: RIM<Req, Res, this>
proppatch!: RIM<Req, Res, this>
purge!: RIM<Req, Res, this>
put!: RIM<Req, Res, this>
rebind!: RIM<Req, Res, this>
report!: RIM<Req, Res, this>
search!: RIM<Req, Res, this>
source!: RIM<Req, Res, this>
subscribe!: RIM<Req, Res, this>
trace!: RIM<Req, Res, this>
unbind!: RIM<Req, Res, this>
unlink!: RIM<Req, Res, this>
unlock!: RIM<Req, Res, this>
unsubscribe!: RIM<Req, Res, this>
'm-search'!: RIM<Req, Res, this>
constructor() {
for (const m of METHODS) {
this[m.toLowerCase() as LowerCaseMethod] = (
...params: RouterMethodParams<Req, Res>
) => this.#add(m as Method, ...params)
}
}
#add(method: Method, ...args: RouterMethodParams<Req, Res>) {
const handlers = args.slice(1).flat() as Handler<Req, Res>[]
pushMiddleware<Req, Res>(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: 'route',
})
return this
}
/**
* Push middleware to the stack
*/
use(...args: UseMethodParams<Req, Res, App>): this {
const base = args[0]
const handlers = args.slice(1).flat()
if (typeof base === 'string') {
pushMiddleware(this.middleware)({
path: base,
handler: handlers[0] as Handler,
handlers: handlers.slice(1) as Handler[],
type: 'mw',
})
} else {
pushMiddleware(this.middleware)({
path: '/',
handler: Array.isArray(base) ? base[0] : (base as Handler),
handlers: Array.isArray(base)
? [...(base.slice(1) as Handler[]), ...(handlers as Handler[])]
: (handlers as Handler[]),
type: 'mw',
})
}
return this
}
/**
* Return the app's absolute pathname
* based on the parent(s) that have
* mounted it.
*
* For example if the application was
* mounted as `"/admin"`, which itself
* was mounted as `"/blog"` then the
* return value would be `"/blog/admin"`.
*/
path(): string {
return this.parent ? this.parent.path() + this.mountpath : ''
}
}