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 actions #5892

Merged
merged 4 commits into from Aug 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-trees-visit.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow actions to return undefined
2 changes: 2 additions & 0 deletions packages/kit/src/index/private.js
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
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
@@ -0,0 +1,2 @@
/** @type {import('./$types').Action} */
export function POST() {}
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
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
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
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