From 1953dbbd41d2d7803837601a9e192654f02275ef Mon Sep 17 00:00:00 2001 From: Bryan Wood Date: Wed, 31 Jul 2024 00:32:36 +1000 Subject: [PATCH] actions: check result.data is not undefined instead of truthy (#11559) * actions: check result.data is not undefined instead of truthy * add changeset * Update .changeset/tasty-rockets-jog.md --------- Co-authored-by: Bjorn Lu --- .changeset/tasty-rockets-jog.md | 5 ++++ packages/astro/src/actions/runtime/route.ts | 2 +- packages/astro/test/actions.test.js | 28 +++++++++++++++++++ .../fixtures/actions/src/actions/index.ts | 10 +++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .changeset/tasty-rockets-jog.md diff --git a/.changeset/tasty-rockets-jog.md b/.changeset/tasty-rockets-jog.md new file mode 100644 index 000000000000..11286bc27329 --- /dev/null +++ b/.changeset/tasty-rockets-jog.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allows actions to return falsy values without an error diff --git a/packages/astro/src/actions/runtime/route.ts b/packages/astro/src/actions/runtime/route.ts index d5bdf2f897a9..463a4ac6eee0 100644 --- a/packages/astro/src/actions/runtime/route.ts +++ b/packages/astro/src/actions/runtime/route.ts @@ -40,7 +40,7 @@ export const POST: APIRoute = async (context) => { ); } return new Response(JSON.stringify(result.data), { - status: result.data ? 200 : 204, + status: result.data !== undefined ? 200 : 204, headers: { 'Content-Type': 'application/json', }, diff --git a/packages/astro/test/actions.test.js b/packages/astro/test/actions.test.js index 279493a12a58..a4322b186d0c 100644 --- a/packages/astro/test/actions.test.js +++ b/packages/astro/test/actions.test.js @@ -228,5 +228,33 @@ describe('Astro Actions', () => { assert.equal($('[data-url]').text(), '/subscribe'); assert.equal($('[data-channel]').text(), 'bholmesdev'); }); + + it('Returns content when the value is 0', async () => { + const req = new Request('http://example.com/_actions/zero', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': '0', + }, + }); + const res = await app.render(req); + assert.equal(res.status, 200); + const value = await res.json(); + assert.equal(value, 0); + }); + + it('Returns content when the value is false', async () => { + const req = new Request('http://example.com/_actions/false', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': '0', + }, + }); + const res = await app.render(req); + assert.equal(res.status, 200); + const value = await res.json(); + assert.equal(value, false); + }); }); }); diff --git a/packages/astro/test/fixtures/actions/src/actions/index.ts b/packages/astro/test/fixtures/actions/src/actions/index.ts index 62b1f01ba1f6..03de31d0c989 100644 --- a/packages/astro/test/fixtures/actions/src/actions/index.ts +++ b/packages/astro/test/fixtures/actions/src/actions/index.ts @@ -64,4 +64,14 @@ export const server = { return; } }), + zero: defineAction({ + handler: async () => { + return 0; + } + }), + false: defineAction({ + handler: async () => { + return false; + } + }) };