Skip to content

Commit dce6fda

Browse files
committed
chore: several server adapter improvements
1 parent f9cbedc commit dce6fda

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/server/src/adapter/sveltekit/handler.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Handle, RequestEvent } from '@sveltejs/kit';
1+
import { type Handle, type RequestEvent } from '@sveltejs/kit';
22
import type { ClientContract } from '@zenstackhq/orm';
33
import type { SchemaDef } from '@zenstackhq/orm/schema';
44
import { logInternalError, type CommonAdapterOptions } from '../common';
@@ -37,9 +37,18 @@ export default function createHandler<Schema extends SchemaDef>(options: SvelteK
3737
const query = Object.fromEntries(event.url.searchParams);
3838
let requestBody: unknown;
3939
if (event.request.body) {
40-
const text = await event.request.text();
41-
if (text) {
42-
requestBody = JSON.parse(text);
40+
try {
41+
const text = await event.request.text();
42+
if (text) {
43+
requestBody = JSON.parse(text);
44+
}
45+
} catch {
46+
return new Response(JSON.stringify({ message: 'invalid JSON payload' }), {
47+
status: 400,
48+
headers: {
49+
'content-type': 'application/json',
50+
},
51+
});
4352
}
4453
}
4554

packages/server/test/adapter/fastify.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('Fastify adapter tests - rest handler', () => {
146146
app.register(ZenStackFastifyPlugin, {
147147
prefix: '/api',
148148
getClient: () => client,
149-
apiHandler: new RestApiHandler({ schema: client.schema, endpoint: 'http://localhost/api' }),
149+
apiHandler: new RestApiHandler({ schema: client.$schema, endpoint: 'http://localhost/api' }),
150150
});
151151

152152
let r = await app.inject({

0 commit comments

Comments
 (0)