-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.ts
More file actions
205 lines (193 loc) · 5.91 KB
/
router.ts
File metadata and controls
205 lines (193 loc) · 5.91 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Type-safe HTTP Router module.
*
* Provides a mechanism to define HTTP routes with built-in support for:
* - Input validation (via `input/output` schemas).
* - Request Authorization (via `authorize` hooks).
* - Automatic Response formatting.
* - Export types for typed API client
*
* @module
*/
import type { Awaitable } from '@01edu/types'
import type { RequestContext } from '@01edu/types/context'
import type { Def } from '@01edu/types/validator'
import type {
GenericRoutes,
Handler,
HttpMethod,
Route,
SimpleHandler,
} from '@01edu/types/router'
import type { Log } from './log.ts'
import { respond, ResponseError } from './response.ts'
import type { Sql } from '@01edu/types/db'
import { createSqlDevRoute } from './dev.ts'
import { createDocRoute } from './doc.ts'
import { createHealthRoute } from './health.ts'
/**
* Options for configuring the router.
*/
export type RouterOptions = {
log: Log
sql?: Sql
sensitiveKeys?: string[]
}
/**
* A declaration function for creating a route handler.
* This is primarily used for type inference and doesn't have any runtime logic.
*
* @param h - The route handler definition.
* @returns The same handler definition.
*
* @example
* ```ts
* import { route } from '@01edu/router';
* import { STR } from '@01edu/validator';
*
* const helloRoute = route({
* input: { name: STR() },
* output: { message: STR() },
* fn: (_, { name }) => ({ message: `Hello, ${name}!` }),
* });
* ```
*/
export const route = <
Session,
In extends Def | undefined,
Out extends Def | undefined,
>(h: Handler<Session, In, Out>) => h
const getPayloadParams = (ctx: RequestContext) =>
Object.fromEntries(ctx.url.searchParams)
const getPayloadBody = async (ctx: RequestContext) => {
try {
return await ctx.req.json()
} catch {
return {}
}
}
const sensitiveData = (
logPayload: unknown,
sensitiveKeys: string[],
): Record<string, unknown> | undefined => {
if (typeof logPayload !== 'object' || !logPayload) return
let redactedPayload: Record<string, unknown> | undefined
for (const key of sensitiveKeys) {
if (key in logPayload) {
redactedPayload || (redactedPayload = { ...logPayload })
redactedPayload[key] = undefined
}
}
return redactedPayload || (logPayload as Record<string, unknown>)
}
/**
* Creates a router function from a set of route definitions.
*
* @param log - A logger instance.
* @param defs - An object where keys are route patterns and values are route handlers.
* @param sensitiveKeys - A list of keys to redact from logs.
* @returns A router function that takes a `RequestContext` and returns a `Response`.
*
* @example
* ```ts
* import { makeRouter, route } from '@01edu/router';
* import { logger } from '@01edu/log';
* import { STR } from '@01edu/validator';
*
* const log = await logger({});
* const routes = {
* 'GET/hello': route({
* input: { name: STR() },
* output: { message: STR() },
* fn: (_, { name }) => ({ message: `Hello, ${name}!` }),
* }),
* };
*
* const router = makeRouter(routes, { log });
* ```
*/
export const makeRouter = <T extends GenericRoutes>(
defs: T,
{
log,
sql,
sensitiveKeys = [
'password',
'confPassword',
'currentPassword',
'newPassword',
],
}: RouterOptions,
): (ctx: RequestContext) => Awaitable<Response> => {
const routeMaps: Record<string, Route> = Object.create(null)
if (!defs['POST/api/execute-sql']) {
defs['POST/api/execute-sql'] = createSqlDevRoute(sql)
}
if (!defs['GET/api/doc']) {
defs['GET/api/doc'] = createDocRoute(defs)
}
if (!defs['GET/api/health']) {
defs['GET/api/health'] = createHealthRoute()
}
for (const key in defs) {
const slashIndex = key.indexOf('/')
const method = key.slice(0, slashIndex) as HttpMethod
const url = key.slice(slashIndex)
if (!routeMaps[url]) {
routeMaps[url] = Object.create(null) as Route
routeMaps[`${url}/`] = routeMaps[url]
}
const { fn, input, authorize } = defs[key] as Handler<unknown, Def, Def>
const handler = async (
ctx: RequestContext & { session: unknown },
payload?: unknown,
) => {
try {
ctx.session = await authorize?.(ctx, payload)
} catch (err) {
if (err instanceof ResponseError) throw err
const message = err instanceof Error ? err.message : 'Unauthorized'
return respond.Unauthorized({ message })
}
const result = await (fn as SimpleHandler)(ctx, payload)
if (result == null) return respond.NoContent()
return result instanceof Response ? result : respond.OK(result)
}
if (input) {
const getPayload = method === 'GET' ? getPayloadParams : getPayloadBody
const assert = input.assert
const report = input.report || (() => [`Expect a ${input?.type}`])
routeMaps[url][method] = async (
ctx: RequestContext & { session: unknown },
) => {
const payload = await getPayload(ctx)
let asserted
try {
asserted = assert(payload)
} catch {
const message = 'Input validation failed'
const failures = report(payload)
return respond.BadRequest({ message, failures })
}
try {
ctx.session = await authorize?.(ctx, payload)
} catch (err) {
if (err instanceof ResponseError) throw err
const message = err instanceof Error ? err.message : 'Unauthorized'
return respond.Unauthorized({ message })
}
log.info('in-params', sensitiveData(asserted, sensitiveKeys))
return handler(ctx, asserted)
}
} else {
routeMaps[url][method] = handler
}
}
return (ctx: RequestContext) => {
const route = routeMaps[ctx.url.pathname]
if (!route) return respond.NotFound()
const handler = route[ctx.req.method as HttpMethod]
if (!handler) return respond.MethodNotAllowed()
return handler(ctx as RequestContext & { session: unknown })
}
}