From d4f5895dbfe8dac92d81a436840dce081df3f312 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sun, 9 Jun 2024 14:29:28 -1000 Subject: [PATCH] fix(client): :bug: Don't try to decode non-JSON response as JSON --- client/lysand/base.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/lysand/base.ts b/client/lysand/base.ts index bd5bde8..45b57be 100644 --- a/client/lysand/base.ts +++ b/client/lysand/base.ts @@ -80,9 +80,12 @@ export class BaseClient { request: Request, ): Promise> { const result = await fetch(request); + const isJson = result.headers + .get("Content-Type") + ?.includes("application/json"); if (!result.ok) { - const error = await result.json(); + const error = isJson ? await result.json() : await result.text(); throw new ResponseError( `Request failed (${result.status}): ${ error.error || error.message || result.statusText @@ -91,7 +94,7 @@ export class BaseClient { } return { - data: await result.json(), + data: isJson ? await result.json() : (await result.text()) || null, headers: result.headers, }; }