* SvelteKit server hooks handler for handling CRUD requests. This handler is to be used in `hooks.server.ts`. * @deprecated use `SvelteKitRouteHandler` instead.
(options: SvelteKitHandlerOptions<Schema>)
| 23 | * @deprecated use `SvelteKitRouteHandler` instead. |
| 24 | */ |
| 25 | function createHandler<Schema extends SchemaDef>(options: SvelteKitHandlerOptions<Schema>): Handle { |
| 26 | return async ({ event, resolve }) => { |
| 27 | if (event.url.pathname.startsWith(options.prefix)) { |
| 28 | const client = await options.getClient(event); |
| 29 | if (!client) { |
| 30 | return new Response(JSON.stringify({ message: 'unable to get ZenStackClient from request context' }), { |
| 31 | status: 400, |
| 32 | headers: { |
| 33 | 'content-type': 'application/json', |
| 34 | }, |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | const query = Object.fromEntries(event.url.searchParams); |
| 39 | let requestBody: unknown; |
| 40 | if (event.request.body) { |
| 41 | try { |
| 42 | const text = await event.request.text(); |
| 43 | if (text) { |
| 44 | requestBody = JSON.parse(text); |
| 45 | } |
| 46 | } catch { |
| 47 | return new Response(JSON.stringify({ message: 'invalid JSON payload' }), { |
| 48 | status: 400, |
| 49 | headers: { |
| 50 | 'content-type': 'application/json', |
| 51 | }, |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const path = event.url.pathname.substring(options.prefix.length); |
| 57 | |
| 58 | try { |
| 59 | const r = await options.apiHandler.handleRequest({ |
| 60 | method: event.request.method, |
| 61 | path, |
| 62 | query, |
| 63 | requestBody, |
| 64 | client, |
| 65 | }); |
| 66 | |
| 67 | return new Response(JSON.stringify(r.body), { |
| 68 | status: r.status, |
| 69 | headers: { |
| 70 | 'content-type': 'application/json', |
| 71 | }, |
| 72 | }); |
| 73 | } catch (err) { |
| 74 | logInternalError(options.apiHandler.log, err); |
| 75 | return new Response(JSON.stringify({ message: 'An internal server error occurred' }), { |
| 76 | status: 500, |
| 77 | headers: { |
| 78 | 'content-type': 'application/json', |
| 79 | }, |
| 80 | }); |
| 81 | } |
| 82 | } |
nothing calls this directly
no test coverage detected