diff --git a/.changeset/odd-trees-visit.md b/.changeset/odd-trees-visit.md new file mode 100644 index 000000000000..ad3c47459fba --- /dev/null +++ b/.changeset/odd-trees-visit.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Allow actions to return undefined diff --git a/packages/kit/src/index/private.js b/packages/kit/src/index/private.js index e2f45c5f165d..912572472dff 100644 --- a/packages/kit/src/index/private.js +++ b/packages/kit/src/index/private.js @@ -3,6 +3,8 @@ export class HttpError { // include a stack for these sorts of errors, but we also don't want red // squigglies everywhere, so this feels like a not-terribile compromise name = 'HttpError'; + + /** @type {void} */ stack = undefined; /** diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index fc84e687d079..e1eaa4547e4d 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -364,21 +364,16 @@ export async function handle_json_request(event, options, mod) { return json(result); } - if (method === 'POST') { + if (result?.errors) { // @ts-ignore - if (result.errors) { - // @ts-ignore - return json({ errors: result.errors }, { status: result.status || 400 }); - } - - return new Response(undefined, { - status: 201, - // @ts-ignore - headers: result.location ? { location: result.location } : undefined - }); + return json({ errors: result.errors }, { status: result.status || 400 }); } - return new Response(undefined, { status: 204 }); + return new Response(undefined, { + status: 204, + // @ts-ignore + headers: result?.location ? { location: result.location } : undefined + }); } catch (e) { const error = normalize_error(e); diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/simple/post/+page.server.js b/packages/kit/test/apps/basics/src/routes/shadowed/simple/post/+page.server.js new file mode 100644 index 000000000000..fee7fff4e694 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/shadowed/simple/post/+page.server.js @@ -0,0 +1,2 @@ +/** @type {import('./$types').Action} */ +export function POST() {} diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/simple/post/+page.svelte b/packages/kit/test/apps/basics/src/routes/shadowed/simple/post/+page.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/test/apps/basics/test/server.test.js b/packages/kit/test/apps/basics/test/server.test.js index e54dfb7eb681..45615bcb5630 100644 --- a/packages/kit/test/apps/basics/test/server.test.js +++ b/packages/kit/test/apps/basics/test/server.test.js @@ -257,6 +257,17 @@ test.describe('Shadowed pages', () => { expect(await response.json()).toEqual({ answer: 42 }); }); + + test('Action can return undefined', async ({ request }) => { + const response = await request.post('/shadowed/simple/post', { + headers: { + accept: 'application/json' + } + }); + + expect(response.status()).toBe(204); + expect(await response.text()).toEqual(''); + }); }); test.describe('Static files', () => { diff --git a/packages/migrate/migrations/routes/migrate_page_js/index.spec.js b/packages/migrate/migrations/routes/migrate_page_js/index.spec.js index 0ac56c8d2fa4..48b543509920 100644 --- a/packages/migrate/migrations/routes/migrate_page_js/index.spec.js +++ b/packages/migrate/migrations/routes/migrate_page_js/index.spec.js @@ -5,7 +5,7 @@ import { migrate_page } from './index.js'; for (const sample of read_samples(import.meta.url)) { test(sample.description, () => { - const actual = migrate_page(sample.before); + const actual = migrate_page(sample.before, sample.filename ?? '+page.js'); assert.equal(actual, sample.after); }); } diff --git a/packages/migrate/migrations/routes/utils.js b/packages/migrate/migrations/routes/utils.js index 23dd1183d503..8152f9d9114b 100644 --- a/packages/migrate/migrations/routes/utils.js +++ b/packages/migrate/migrations/routes/utils.js @@ -472,7 +472,9 @@ export function read_samples(test_file) { * @param {string} new_type */ export function rewrite_type(node, code, old_type, new_type) { + // @ts-ignore if (node.jsDoc) { + // @ts-ignore for (const comment of node.jsDoc) { const str = comment.getText(); const index = str.indexOf(old_type);