also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,92 @@ | ||
| 1 | import type { ApiRoute, ApiRouterConfig, HandlerContext, HttpMethod } from './types' | |
| 2 | import { createHandlerContextWithBody } from './handler' | |
| 3 | import { scanApiRoutes } from './scanner' | |
| 4 | ||
| 5 | interface MatchResult { | |
| 6 | route: ApiRoute | |
| 7 | params: Record<string, string> | |
| 8 | } | |
| 9 | ||
| 10 | function matchRoute(pathname: string, method: HttpMethod, routes: ApiRoute[]): MatchResult | null { | |
| 11 | for (const route of routes) { | |
| 12 | if (route.method !== method) | |
| 13 | continue | |
| 14 | ||
| 15 | const routeParts = route.path.split('/').filter(Boolean) | |
| 16 | const pathParts = pathname.split('/').filter(Boolean) | |
| 17 | ||
| 18 | if (routeParts.length !== pathParts.length) | |
| 19 | continue | |
| 20 | ||
| 21 | const params: Record<string, string> = {} | |
| 22 | let matched = true | |
| 23 | ||
| 24 | for (let i = 0; i < routeParts.length; i++) { | |
| 25 | const routePart = routeParts[i] | |
| 26 | const pathPart = pathParts[i] | |
| 27 | ||
| 28 | if (routePart.startsWith(':')) { | |
| 29 | params[routePart.slice(1)] = pathPart | |
| 30 | } | |
| 31 | else if (routePart !== pathPart) { | |
| 32 | matched = false | |
| 33 | break | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | if (matched) | |
| 38 | return { route, params } | |
| 39 | } | |
| 40 | ||
| 41 | return null | |
| 42 | } | |
| 43 | ||
| 44 | export async function handleApiRequest(request: Request, routes: ApiRoute[]): Promise<Response> { | |
| 45 | const url = new URL(request.url) | |
| 46 | const method = request.method as HttpMethod | |
| 47 | const match = matchRoute(url.pathname, method, routes) | |
| 48 | ||
| 49 | if (!match) { | |
| 50 | return new Response(JSON.stringify({ error: 'Not Found' }), { | |
| 51 | status: 404, | |
| 52 | headers: { 'Content-Type': 'application/json' }, | |
| 53 | }) | |
| 54 | } | |
| 55 | ||
| 56 | const { route, params } = match | |
| 57 | const handler = route.handler | |
| 58 | ||
| 59 | try { | |
| 60 | const ctx: HandlerContext = await createHandlerContextWithBody(request, params) | |
| 61 | ||
| 62 | if (handler.input) { | |
| 63 | ctx.body = handler.input.parse(ctx.body) | |
| 64 | } | |
| 65 | ||
| 66 | const result = await handler.handler(ctx) | |
| 67 | ||
| 68 | return new Response(JSON.stringify(result), { | |
| 69 | status: 200, | |
| 70 | headers: { 'Content-Type': 'application/json' }, | |
| 71 | }) | |
| 72 | } | |
| 73 | catch (error) { | |
| 74 | const message = error instanceof Error ? error.message : 'Internal Server Error' | |
| 75 | return new Response(JSON.stringify({ error: message }), { | |
| 76 | status: 500, | |
| 77 | headers: { 'Content-Type': 'application/json' }, | |
| 78 | }) | |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | export async function createApiRouter(config: ApiRouterConfig): Promise<{ | |
| 83 | routes: ApiRoute[] | |
| 84 | handle: (request: Request) => Promise<Response> | |
| 85 | }> { | |
| 86 | const routes = await scanApiRoutes(config.apiDir, config.prefix) | |
| 87 | ||
| 88 | return { | |
| 89 | routes, | |
| 90 | handle: (request: Request) => handleApiRequest(request, routes), | |
| 91 | } | |
| 92 | } | |