diff --git a/src/index.ts b/src/index.ts index 94282d3..3776135 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,15 +44,24 @@ export default class PineFetch< body: normalizedBody, }); + let responseBody; + try { + // Fetch the response as text + responseBody = await response.text(); + // And then try to parse it as JSON as it is likely to be JSON + responseBody = JSON.parse(responseBody); + // TODO: base this upon the returned content-type header + } catch { + // empty + } + if (response.status >= 400) { - let responseBody = 'Unknown error'; - try { - responseBody = await response.text(); - } catch { - // empty - } - throw new RequestError(responseBody, response.status, response.headers); + throw new RequestError( + responseBody ?? 'Unknown error', + response.status, + response.headers, + ); } - return await response.json(); + return responseBody; } }