mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Actions: fix 500 on empty object for Vercel serverless (#11634)
* fix: only set content-type when body is defined * feat(test): content-type is omitted * chore: changeset
This commit is contained in:
parent
d532df1222
commit
2716f52aae
5 changed files with 33 additions and 6 deletions
5
.changeset/tiny-poems-battle.md
Normal file
5
.changeset/tiny-poems-battle.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes internal server error when calling an Astro Action without arguments on Vercel.
|
BIN
packages/astro/astro-4.13.1.tgz
Normal file
BIN
packages/astro/astro-4.13.1.tgz
Normal file
Binary file not shown.
|
@ -12,7 +12,7 @@ export const POST: APIRoute = async (context) => {
|
|||
const contentType = request.headers.get('Content-Type');
|
||||
const contentLength = request.headers.get('Content-Length');
|
||||
let args: unknown;
|
||||
if (contentLength === '0') {
|
||||
if (!contentType || contentLength === '0') {
|
||||
args = undefined;
|
||||
} else if (contentType && hasContentType(contentType, formContentTypes)) {
|
||||
args = await request.clone().formData();
|
||||
|
|
|
@ -65,15 +65,18 @@ async function handleAction(param, path, context) {
|
|||
let body = param;
|
||||
if (!(body instanceof FormData)) {
|
||||
try {
|
||||
body = param ? JSON.stringify(param) : undefined;
|
||||
body = JSON.stringify(param);
|
||||
} catch (e) {
|
||||
throw new ActionError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: `Failed to serialize request body to JSON. Full error: ${e.message}`,
|
||||
});
|
||||
}
|
||||
headers.set('Content-Type', 'application/json');
|
||||
headers.set('Content-Length', body?.length.toString() ?? '0');
|
||||
if (body) {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
} else {
|
||||
headers.set('Content-Length', '0');
|
||||
}
|
||||
}
|
||||
const rawResult = await fetch(`/_actions/${path}`, {
|
||||
method: 'POST',
|
||||
|
|
|
@ -234,11 +234,10 @@ describe('Astro Actions', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('Sets status to 204 when no content', async () => {
|
||||
it('Sets status to 204 when content-length is 0', async () => {
|
||||
const req = new Request('http://example.com/_actions/fireAndForget', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': '0',
|
||||
},
|
||||
});
|
||||
|
@ -246,6 +245,26 @@ describe('Astro Actions', () => {
|
|||
assert.equal(res.status, 204);
|
||||
});
|
||||
|
||||
it('Sets status to 204 when content-type is omitted', async () => {
|
||||
const req = new Request('http://example.com/_actions/fireAndForget', {
|
||||
method: 'POST',
|
||||
});
|
||||
const res = await app.render(req);
|
||||
assert.equal(res.status, 204);
|
||||
});
|
||||
|
||||
it('Sets status to 415 when content-type is unexpected', async () => {
|
||||
const req = new Request('http://example.com/_actions/fireAndForget', {
|
||||
method: 'POST',
|
||||
body: 'hey',
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
},
|
||||
});
|
||||
const res = await app.render(req);
|
||||
assert.equal(res.status, 415);
|
||||
});
|
||||
|
||||
it('Is callable from the server with rewrite', async () => {
|
||||
const req = new Request('http://example.com/rewrite');
|
||||
const res = await app.render(req);
|
||||
|
|
Loading…
Reference in a new issue