Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix returning null from endpoints #1886

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-turkeys-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix returning null from endpoints
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async function render_route(request, route) {
(!type || type.startsWith('application/json'))
) {
headers = { ...headers, 'content-type': 'application/json; charset=utf-8' };
normalized_body = JSON.stringify(body || {});
normalized_body = JSON.stringify(typeof body === 'undefined' ? {} : body);
} else {
normalized_body = /** @type {import('types/hooks').StrictBody} */ (body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ export default function (test) {
const res = await fetch('/endpoint-output/empty');
assert.type(await res.buffer(), 'object');
});

test('null body returns null json value', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/null');
assert.equal(res.status, 200);
assert.equal(await res.json(), null);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return { body: null };
}