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

chore(core): try to parse a text/plain content-type response as json #3430

Merged
Merged
Show file tree
Hide file tree
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
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