0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00

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 <bjornlu.dev@gmail.com>
This commit is contained in:
Bryan Wood 2024-07-31 00:32:36 +10:00 committed by GitHub
parent e3f29d416a
commit 1953dbbd41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allows actions to return falsy values without an error

View file

@ -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',
},

View file

@ -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);
});
});
});

View file

@ -64,4 +64,14 @@ export const server = {
return;
}
}),
zero: defineAction({
handler: async () => {
return 0;
}
}),
false: defineAction({
handler: async () => {
return false;
}
})
};