forked from utily/cloudly-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.ts
More file actions
47 lines (46 loc) · 1.57 KB
/
Router.ts
File metadata and controls
47 lines (46 loc) · 1.57 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
import * as http from "cloudly-http"
import { Handler } from "./Handler"
import { Route } from "./Route"
export class Router<T> {
private readonly alternatePrefix: string[]
private readonly routes: Route<T>[] = []
origin: string[] = ["*"]
constructor(...alternatePrefix: string[]) {
this.alternatePrefix = alternatePrefix
}
add(method: http.Method | http.Method[], pattern: string, handler: Handler<T>) {
this.routes.push(Route.create(method, pattern, handler))
}
async handle(request: http.Request.Like | http.Request, context: T): Promise<http.Response> {
let result: http.Response
if (http.Request.is(request)) {
let response: http.Response.Like | undefined
let allowedMethods: http.Method[] = []
for (const route of this.routes) {
const r = route.match(request, ...this.alternatePrefix)
if (r)
if (route.methods.some(m => m == request.method)) {
response = await route.handler(r, context)
break
} else
allowedMethods = allowedMethods.concat(...route.methods)
}
result = http.Response.create(
response ||
(allowedMethods.length == 0
? { status: 404 }
: request.method == "OPTIONS"
? {
status: 204,
header: {
accessControlAllowMethods: allowedMethods,
accessControlAllowHeaders: ["Content-Type", "Authorization"],
},
}
: { status: 405, header: { allow: allowedMethods } })
)
} else
result = await this.handle(http.Request.create(request), context)
return { ...result, header: { ...result.header, accessControlAllowOrigin: this.origin[0] } }
}
}