Skip to content

Commit

Permalink
chore(core): try to parse a text/plain content-type response as json (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Nov 14, 2023
1 parent f2c59c5 commit 94e0c31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silver-planets-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': minor
---

Try to parse `text/plain` content-type as JSON before bailing out with an error.
19 changes: 18 additions & 1 deletion packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ async function* parseMultipartMixed(
}
}

async function* parseMaybeJSON(
response: Response
): AsyncIterableIterator<ExecutionResult> {
const text = await response.text();
try {
const result = JSON.parse(text);
if (process.env.NODE_ENV !== 'production') {
console.warn(
`Found response with content-type "text/plain" but it had a valid "application/json" response.`
);
}
yield result;
} catch (e) {
throw new Error(text);
}
}

async function* fetchOperation(
operation: Operation,
url: string,
Expand All @@ -172,7 +189,7 @@ async function* fetchOperation(
} else if (!/text\//i.test(contentType)) {
results = parseJSON(response);
} else {
throw new Error(await response.text());
results = parseMaybeJSON(response);
}

let pending: ExecutionResult['pending'];
Expand Down

0 comments on commit 94e0c31

Please sign in to comment.