Skip to content

Commit

Permalink
Always return either json or text
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Jun 12, 2024
1 parent 19a44d2 commit 45562d1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/msw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,19 @@ export default class MswConfig {

// Return the correct type of response based on the `accept` header
const accept = request.headers?.get('accept')?.toLowerCase() || '';
if (accept.includes('json')) {
return HttpResponse.json(JSON.parse(responseBody || '{}'), init);
if (!responseBody) {
return new HttpResponse(null, init);
} else if (accept.includes('json')) {
return HttpResponse.json(JSON.parse(responseBody), init);
} else if (accept.includes('text')) {
return HttpResponse.text(responseBody, init);
} else {
throw new Error(
`Mirage-msw: Only json and text responses are supported at this time. Please open an issue requesting support for ${accept}.`
);
try {
const json = JSON.parse(responseBody);
return HttpResponse.json(json, init);
} catch (e) {
return HttpResponse.text(responseBody, init);
}
}
});
if (this.msw) {
Expand Down

0 comments on commit 45562d1

Please sign in to comment.