Skip to content

Commit

Permalink
supports setting json opt per request
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierbower committed Sep 28, 2023
1 parent 989acfa commit 9dd0ce0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/transport/node-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class FetchTransport extends Transport {
return opts;
}

parseAsJson(ctx, contentType) {
if (ctx.opts?.json !== undefined) return ctx.opts.json;

return this.defaults?.json || contentType?.includes('json');
}

async toResponse(ctx, from) {
const to = ctx.res;
REQUIRED_PROPERTIES.forEach((property) => {
Expand All @@ -67,9 +73,8 @@ class FetchTransport extends Transport {

// currently supports json and text formats only
to.body = await from.text();

const contentType = to.headers['content-type'];
if (this.defaults?.json || contentType?.includes('json')) {
if (this.parseAsJson(ctx, contentType)) {
try {
to.body = JSON.parse(to.body);
} catch {} // If body is not parseable, leave as text
Expand Down

0 comments on commit 9dd0ce0

Please sign in to comment.