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

actions: check result.data is not undefined instead of truthy #11559

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/tasty-rockets-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Let actions return falsy values without an error
bluwy marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/astro/src/actions/runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
28 changes: 28 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/actions/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ export const server = {
return;
}
}),
zero: defineAction({
handler: async () => {
return 0;
}
}),
false: defineAction({
handler: async () => {
return false;
}
})
};
Loading