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: Fetch handler should account for empty body #8876

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Changes from all 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
13 changes: 10 additions & 3 deletions packages/request/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function cloneResponse(response: Response, overrides: Partial<Response>) {
const props = cloneResponseProperties(response);
return new Response(response.body, Object.assign(props, overrides));
}

const MUTATION_OPS = new Set(['updateRecord', 'createRecord', 'deleteRecord']);

/**
* A basic handler which converts a request into a
* `fetch` call presuming the response to be `json`.
Expand All @@ -46,7 +49,11 @@ const Fetch = {
async request(context: Context) {
let response = await _fetch(context.request.url!, context.request);

if (!response.headers.has('date')) {
const isError = !response.ok || response.status >= 400;
const op = context.request.op;
const isMutationOp = Boolean(op && MUTATION_OPS.has(op));

if (!isError && !isMutationOp && response.status !== 204 && !response.headers.has('date')) {
const headers = new Headers(response.headers);
headers.set('date', new Date().toUTCString());
response = cloneResponse(response, { headers });
Expand All @@ -55,7 +62,7 @@ const Fetch = {
context.setResponse(response);

// if we are an error, we will want to throw
if (!response.ok || response.status >= 400) {
if (isError) {
const text = await response.text();
let errorPayload: object | undefined;
try {
Expand All @@ -69,7 +76,7 @@ const Fetch = {
error.content = errorPayload;
throw error;
} else {
return response.json();
return response.status === 204 ? null : response.json();
}
},
};
Expand Down
Loading