-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrt.ts
271 lines (243 loc) · 5.7 KB
/
rt.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
260
261
262
263
264
265
266
267
268
269
270
271
import type { Route } from "@std/http/unstable-route";
import { route } from "@std/http/unstable-route";
export interface RtRoute<TState> extends Omit<Route, "handler" | "pattern"> {
pattern: string | URLPattern;
handler: RequestHandler<TState>;
}
export interface RtContext<TState> {
request: Request;
params: URLPatternResult | undefined;
info: Deno.ServeHandlerInfo | undefined;
next: () => Promise<Response>;
state: TState;
}
export type RequestHandler<TState> = (
context: RtContext<TState>,
) => Response | Promise<Response>;
export type DefaultHandler = () => Response | Promise<Response>;
export type ErrorHandler = (error: Error) => Response | Promise<Response>;
function defaultHandler() {
return new Response("Not found", { status: 404 });
}
function errorHandler(error: Error) {
return new Response(error.message, { status: 500 });
}
/**
* Router is an HTTP router based on the `URLPattern` API.
*/
export class Router<TState = never> {
public constructor(
public routes: RtRoute<TState>[] = [],
public initializeState: () => TState = () => ({} as TState),
public defaultHandler?: DefaultHandler,
public errorHandler?: ErrorHandler,
) {}
/**
* fetch invokes the router for the given request.
*/
public async fetch(
request: Request,
info?: Deno.ServeHandlerInfo,
state: TState = this.initializeState(),
): Promise<Response> {
try {
return await this.execute(
0,
request,
info,
state,
);
} catch (error) {
if (error instanceof Error) {
return await (this.errorHandler ?? errorHandler)(error);
}
throw error;
}
}
/**
* execute executes a route at the given index.
*/
private execute(
i: number,
request: Request,
info: Deno.ServeHandlerInfo | undefined,
state: TState,
): Response | Promise<Response> {
if (i >= this.routes.length) {
return (this.defaultHandler ?? defaultHandler)();
}
const { method, pattern, handler: execute } = this.routes[i];
const next = async () => await this.execute(i + 1, request, info, state);
const handler = route(
[
{
method,
pattern: pattern instanceof URLPattern
? pattern
: new URLPattern({ pathname: pattern }),
handler: (request, params, info) => {
return execute({ request, params, info, next, state });
},
},
],
next,
);
return handler(request, info);
}
/**
* state sets the initial state of the router.
*/
public state(defaultState: () => TState): this {
this.initializeState = defaultState;
return this;
}
/**
* default sets the router's default handler.
*/
public default(handler: DefaultHandler): this {
this.defaultHandler = handler;
return this;
}
/**
* error sets the router's error handler.
*/
public error(handler: ErrorHandler): this {
this.errorHandler = handler;
return this;
}
/**
* use appends a sequence of routers to the router.
*/
public use(data: RtRoute<TState>[] | Router<TState>): this {
if (data instanceof Router) {
this.routes.push(...data.routes);
} else {
this.routes.push(...data);
}
return this;
}
/**
* with appends a route to the router.
*/
public with(route: RtRoute<TState>): this {
this.routes.push(route);
return this;
}
/**
* connect appends a router for the CONNECT method to the router.
*/
public connect(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "CONNECT",
pattern,
handler,
});
}
/**
* delete appends a router for the DELETE method to the router.
*/
public delete(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "DELETE",
pattern,
handler,
});
}
/**
* get appends a router for the GET method to the router.
*/
public get(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "GET",
pattern,
handler,
});
}
/**
* head appends a router for the HEAD method to the router.
*/
public head(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "HEAD",
pattern,
handler,
});
}
/**
* options appends a router for the OPTIONS method to the router.
*/
public options(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "OPTIONS",
pattern,
handler,
});
}
/**
* patch appends a router for the PATCH method to the router.
*/
public patch(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "PATCH",
pattern,
handler,
});
}
/**
* post appends a router for the POST method to the router.
*/
public post(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "POST",
pattern,
handler,
});
}
/**
* put appends a router for the PUT method to the router.
*/
public put(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "PUT",
pattern,
handler,
});
}
/**
* trace appends a router for the TRACE method to the router.
*/
public trace(
pattern: string | URLPattern,
handler: RequestHandler<TState>,
): this {
return this.with({
method: "TRACE",
pattern,
handler,
});
}
}