also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,40 @@ | ||
| 1 | import type { HandlerConfig, HandlerContext } from './types' | |
| 2 | ||
| 3 | export function defineHandler<T>(config: HandlerConfig<T>): HandlerConfig<T> { | |
| 4 | return config | |
| 5 | } | |
| 6 | ||
| 7 | export function createHandlerContext(request: Request, params: Record<string, string> = {}): HandlerContext { | |
| 8 | const url = new URL(request.url) | |
| 9 | ||
| 10 | return { | |
| 11 | params, | |
| 12 | request, | |
| 13 | headers: request.headers, | |
| 14 | url, | |
| 15 | body: null, | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | export async function createHandlerContextWithBody(request: Request, params: Record<string, string> = {}): Promise<HandlerContext> { | |
| 20 | const url = new URL(request.url) | |
| 21 | let body: unknown = null | |
| 22 | ||
| 23 | const contentType = request.headers.get('content-type') | |
| 24 | if (contentType?.includes('application/json')) { | |
| 25 | try { | |
| 26 | body = await request.json() | |
| 27 | } | |
| 28 | catch { | |
| 29 | body = null | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | return { | |
| 34 | params, | |
| 35 | request, | |
| 36 | headers: request.headers, | |
| 37 | url, | |
| 38 | body, | |
| 39 | } | |
| 40 | } | |