Skip to content

Commit

Permalink
fix actions (#5892)
Browse files Browse the repository at this point in the history
* fix actions

* changeset

* what the hell why did it suddenly start complaining about this of all things

* what fun
  • Loading branch information
Rich-Harris authored Aug 15, 2022
1 parent c141dce commit 9adc18c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-trees-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow actions to return undefined
2 changes: 2 additions & 0 deletions packages/kit/src/index/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
19 changes: 7 additions & 12 deletions packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {import('./$types').Action} */
export function POST() {}
Empty file.
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/migrate/migrations/routes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9adc18c

Please sign in to comment.