-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.ts
191 lines (171 loc) · 4.88 KB
/
routing.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
// Copyright Zaiste. All rights reserved.
// Licensed under the Apache License, Version 2.0
// import Busboy from 'busboy';
// import {
// isObject,
// parseCookies,
// parseAcceptHeader,
// toBuffer,
// } from './util';
import { Router } from "./router.ts";
import {
Handler,
KeyValue,
Meta,
Middleware,
Params,
Pipeline,
Request,
Route,
RouteOptions,
} from "./types.ts";
export const HTTPMethod = {
GET: "GET",
POST: "POST",
PUT: "PUT",
PATH: "PATCH",
HEAD: "HEAD",
OPTIONS: "OPTIONS",
DELETE: "DELETE",
} as const;
export type HTTPMethod = (typeof HTTPMethod)[keyof typeof HTTPMethod];
function isPipeline(handler: Handler | Pipeline): handler is Pipeline {
return Array.isArray(handler);
}
function makeRoute(
name: HTTPMethod,
path: string,
handler: Handler | Pipeline,
middleware: Middleware[],
meta: Meta,
): Route {
if (isPipeline(handler)) {
const h = handler.pop() as Handler;
return [path, {
[name]: h,
middleware: [...middleware, ...handler as Middleware[]],
meta,
}];
} else {
return [path, { [name]: handler, middleware, meta }];
}
}
export function GET(
path: string,
handler: Handler | Pipeline,
{ middleware = [], meta = {} }: RouteOptions = {},
): Route {
return makeRoute("GET", path, handler, middleware, meta);
}
export function POST(
path: string,
handler: Handler | Pipeline,
{ middleware = [], meta = {} }: RouteOptions = {},
): Route {
return makeRoute("POST", path, handler, middleware, meta);
}
export function PATCH(
path: string,
handler: Handler | Pipeline,
{ middleware = [], meta = {} }: RouteOptions = {},
): Route {
return makeRoute("PATCH", path, handler, middleware, meta);
}
export function PUT(
path: string,
handler: Handler | Pipeline,
{ middleware = [], meta = {} }: RouteOptions = {},
): Route {
return makeRoute("PUT", path, handler, middleware, meta);
}
export function DELETE(
path: string,
handler: Handler | Pipeline,
{ middleware = [], meta = {} }: RouteOptions = {},
): Route {
return makeRoute("DELETE", path, handler, middleware, meta);
}
export const Routing = (router: Router): Middleware => {
return (next: Handler) =>
(request: Request) => {
const method = request.method;
const { pathname, search } = new URL(request.url ?? ""); // TODO Test perf vs RegEx
const query: Record<string, string> = {};
for (const [key, value] of new URLSearchParams(search)) {
query[key] = value;
}
const [handler, dynamicRoutes]: [
Handler | undefined,
(string | KeyValue)[],
] = router.find(method, pathname);
const params = {} as Params;
for (const r of dynamicRoutes as KeyValue[]) {
params[r.name] = r.value;
}
if (handler !== undefined) {
request.params = { ...query, ...params };
handleRequest(request);
request.params = { ...request.params };
return handler(request);
} else {
return next(request);
}
};
};
export function isObject(_: unknown) {
return !!_ && typeof _ === "object";
}
const handleRequest = (request: Request) => {
const { headers = {}, params, body } = request;
// context.headers = headers;
// context.cookies = parseCookies(headers.cookie);
// context.format = format ? format : parseAcceptHeader(headers);
if (body.length > 0) {
const contentType = headers["content-type"]?.split(";")[0];
switch (contentType) {
case "application/x-www-form-urlencoded": {
const decodedBody = new TextDecoder().decode(body);
const query: Record<string, string> = {};
for (const [key, value] of new URLSearchParams(decodedBody)) {
query[key] = value;
}
Object.assign(params, query);
break;
}
case "application/json": {
const decodedBody = new TextDecoder().decode(body);
const result = JSON.parse(decodedBody);
if (isObject(result)) {
Object.assign(params, result);
}
break;
}
// case 'multipart/form-data': {
// context.files = {};
// const busboy = new Busboy({ headers });
// busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
// file.on('data', data => {
// context.files = {
// ...context.files,
// [fieldname]: {
// name: filename,
// length: data.length,
// data,
// encoding,
// mimetype,
// }
// };
// });
// file.on('end', () => {});
// });
// busboy.on('field', (fieldname, val) => {
// context.params = { ...context.params, [fieldname]: val };
// });
// busboy.end(buffer);
// await new Promise(resolve => busboy.on('finish', resolve));
// break;
// }
default:
}
}
};