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

feat: always consume JSON to prevent resource leaks #174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,26 @@ async function _handleRequest(
): Promise<any> {
return new Promise((resolve, reject) => {
fetcher(url, _getRequestParams(method, options, parameters, body))
.then((result) => {
if (!result.ok) throw result
if (options?.noResolveJson) return result
return result.json()
.then(async (result) => {
if (!result.ok) throw result;
// Check content type of response
const contentType = result.headers.get('content-type');
// If the content type is JSON, parse the response as text and then parse that as JSON
if (contentType?.includes('application/json')) {
const resultText = await result.text();
resolve(options?.noResolveJson ? result : JSON.parse(resultText));
}
// If not JSON, check for options.noResolveJson. It either returns the blob result if set true or the parsed text
else if (options?.noResolveJson) {
resolve(result);
}
else {
const resultText = await result.text();
resolve(resultText);
}
})
.then((data) => resolve(data))
.catch((error) => handleError(error, reject))
})
.catch((error) => handleError(error, reject));
});
}

export async function get(
Expand Down