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 3 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.
22 changes: 20 additions & 2 deletions packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,28 @@ async function* fetchOperation(
results = parseMultipartMixed(contentType, response);
} else if (/text\/event-stream/i.test(contentType)) {
results = parseEventStream(response);
} else if (!/text\//i.test(contentType)) {
} else if (!/text\//i.test(contentType) || !contentType) {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
results = parseJSON(response);
} else {
throw new Error(await response.text());
if (contentType === 'text/plain') {
const text = await response.text();
if (text.startsWith('{')) {
try {
results = JSON.parse(text);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My generator foo is lacking here, does this count, because yield JSON.parse() seems invalid

if (process.env.NODE_ENV !== 'production') {
console.warn(
`Found response with content-type "text/plain" but it had a valid "application/json" response.`
);
}
} catch (e) {
throw new Error(text);
}
} else {
throw new Error(text);
}
} else {
throw new Error(await response.text());
}
}

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