ReviewOS

also looking at this

stacks/stx

chore: wip stx improvements

#1649
Merged fingersandmind wants to merge feat/stx-improvements into main
241 files +19022 -1323
packages/api/src/handler.tsadded+40-0
Changes to packages/api/src/handler.ts
@@ -0,0 +1,40 @@
1import type { HandlerConfig, HandlerContext } from './types'
2
3export function defineHandler<T>(config: HandlerConfig<T>): HandlerConfig<T> {
4 return config
5}
6
7export 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
19export 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}