diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index 2468126..d215e3d 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -60,14 +60,26 @@ async function _handleRequest( ): Promise { 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(