Skip to content

Commit

Permalink
fix: handle all types of BodyInit correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 29, 2024
1 parent 7c74168 commit 0242ea2
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Route {
constructResponseBody(
responseInput: RouteResponseConfig,
responseOptions: ResponseInitUsingHeaders,
): string | null {
): BodyInit | null {
// start to construct the body
let body = responseInput.body;
// convert to json if we need to
Expand All @@ -217,8 +217,25 @@ class Route {
}
return body;
}
// @ts-expect-error TODO need to implement handling of non-string bodies properlyy
return body || null;

if (
body instanceof Blob ||
body instanceof ArrayBuffer ||
// checks for TypedArray
ArrayBuffer.isView(body) ||
body instanceof DataView ||
body instanceof FormData ||
body instanceof ReadableStream ||
body instanceof URLSearchParams ||
body instanceof String ||
typeof body === 'string'
) {
return body as BodyInit;
}
if (!body) {
return null;
}
throw new TypeError('Invalid body provided to construct response');
}

static defineMatcher(matcher: MatcherDefinition) {
Expand Down

0 comments on commit 0242ea2

Please sign in to comment.