(
options: TanStackStartOptions<Schema>,
)
| 9 | * @returns A TanStack Start server route handler |
| 10 | */ |
| 11 | export default function factory<Schema extends SchemaDef>( |
| 12 | options: TanStackStartOptions<Schema>, |
| 13 | ): ({ request, params }: { request: Request; params: Record<string, string> }) => Promise<Response> { |
| 14 | return async ({ request, params }: { request: Request; params: Record<string, string> }) => { |
| 15 | const client = await options.getClient(request, params); |
| 16 | if (!client) { |
| 17 | return new Response(JSON.stringify({ message: 'unable to get ZenStackClient from request context' }), { |
| 18 | status: 500, |
| 19 | headers: { |
| 20 | 'Content-Type': 'application/json', |
| 21 | }, |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | const url = new URL(request.url); |
| 26 | const query = Object.fromEntries(url.searchParams); |
| 27 | |
| 28 | // Extract path from params._splat for catch-all routes |
| 29 | const path = params['_splat']; |
| 30 | |
| 31 | if (!path) { |
| 32 | return new Response(JSON.stringify({ message: 'missing path parameter' }), { |
| 33 | status: 400, |
| 34 | headers: { |
| 35 | 'Content-Type': 'application/json', |
| 36 | }, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | let requestBody: unknown; |
| 41 | if (request.body) { |
| 42 | try { |
| 43 | requestBody = await request.json(); |
| 44 | } catch { |
| 45 | // noop |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | try { |
| 50 | const r = await options.apiHandler.handleRequest({ |
| 51 | method: request.method!, |
| 52 | path, |
| 53 | query, |
| 54 | requestBody, |
| 55 | client, |
| 56 | }); |
| 57 | return new Response(JSON.stringify(r.body), { |
| 58 | status: r.status, |
| 59 | headers: { |
| 60 | 'Content-Type': 'application/json', |
| 61 | }, |
| 62 | }); |
| 63 | } catch (err) { |
| 64 | logInternalError(options.apiHandler.log, err); |
| 65 | return new Response(JSON.stringify({ message: 'An internal server error occurred' }), { |
| 66 | status: 500, |
| 67 | headers: { |
| 68 | 'Content-Type': 'application/json', |
nothing calls this directly
no test coverage detected