diff --git a/packages/server/src/adapter/sveltekit/handler.ts b/packages/server/src/adapter/sveltekit/handler.ts index 355f7ee5..84eb14bb 100644 --- a/packages/server/src/adapter/sveltekit/handler.ts +++ b/packages/server/src/adapter/sveltekit/handler.ts @@ -1,4 +1,4 @@ -import type { Handle, RequestEvent } from '@sveltejs/kit'; +import { type Handle, type RequestEvent } from '@sveltejs/kit'; import type { ClientContract } from '@zenstackhq/orm'; import type { SchemaDef } from '@zenstackhq/orm/schema'; import { logInternalError, type CommonAdapterOptions } from '../common'; @@ -37,9 +37,18 @@ export default function createHandler(options: SvelteK const query = Object.fromEntries(event.url.searchParams); let requestBody: unknown; if (event.request.body) { - const text = await event.request.text(); - if (text) { - requestBody = JSON.parse(text); + try { + const text = await event.request.text(); + if (text) { + requestBody = JSON.parse(text); + } + } catch { + return new Response(JSON.stringify({ message: 'invalid JSON payload' }), { + status: 400, + headers: { + 'content-type': 'application/json', + }, + }); } } diff --git a/packages/server/test/adapter/fastify.test.ts b/packages/server/test/adapter/fastify.test.ts index 85c0a463..d5d2be6b 100644 --- a/packages/server/test/adapter/fastify.test.ts +++ b/packages/server/test/adapter/fastify.test.ts @@ -146,7 +146,7 @@ describe('Fastify adapter tests - rest handler', () => { app.register(ZenStackFastifyPlugin, { prefix: '/api', getClient: () => client, - apiHandler: new RestApiHandler({ schema: client.schema, endpoint: 'http://localhost/api' }), + apiHandler: new RestApiHandler({ schema: client.$schema, endpoint: 'http://localhost/api' }), }); let r = await app.inject({ diff --git a/packages/server/test/adapter/tanstack-start.test.ts b/packages/server/test/adapter/tanstack-start.test.ts index 1fb83a25..4d4031ef 100644 --- a/packages/server/test/adapter/tanstack-start.test.ts +++ b/packages/server/test/adapter/tanstack-start.test.ts @@ -89,7 +89,7 @@ model M { const db = await createTestClient(model); const options: TanStackStartOptions = { getClient: () => db, - apiHandler: new RPCApiHandler({ schema: db.schema }), + apiHandler: new RPCApiHandler({ schema: db.$schema }), }; const client = await makeTestClient('/m/create', options) @@ -179,7 +179,7 @@ model M { const db = await createPolicyTestClient(model); const options: TanStackStartOptions = { getClient: () => db, - apiHandler: new RPCApiHandler({ schema: db.schema }), + apiHandler: new RPCApiHandler({ schema: db.$schema }), }; const createForbidden = await makeTestClient('/m/create', options) @@ -233,7 +233,7 @@ model M { const options: TanStackStartOptions = { getClient: () => db, - apiHandler: new RestApiHandler({ endpoint: 'http://localhost/api', schema: db.schema }), + apiHandler: new RestApiHandler({ endpoint: 'http://localhost/api', schema: db.$schema }), }; const create = await makeTestClient('/m', options)