From 86f44c8babf452ead21578daf100992b3fa33d94 Mon Sep 17 00:00:00 2001 From: James Collins Date: Wed, 1 Mar 2023 20:13:53 +1000 Subject: [PATCH] dont attempt to decode body on DELETE method --- resources/js/helpers/api.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/resources/js/helpers/api.ts b/resources/js/helpers/api.ts index 7ae1a7c..c3a4c25 100644 --- a/resources/js/helpers/api.ts +++ b/resources/js/helpers/api.ts @@ -100,14 +100,26 @@ export const api = { fetch(url, fetchOptions) .then(async (response) => { let data: string | object = ""; - if (response.headers.get("content-type") == null) { - try { - data = response.json ? await response.json() : {}; - } catch (error) { - data = response.text ? await response.text() : ""; + if (fetchOptions.method.toLowerCase() != "delete") { + if ( + response && + response.headers.get("content-type") == null + ) { + try { + data = response.json + ? await response.json() + : {}; + } catch (error) { + data = response.text + ? await response.text() + : ""; + } + } else { + data = + response && response.json + ? await response.json() + : {}; } - } else { - data = response.json ? await response.json() : {}; } const result = { @@ -125,6 +137,7 @@ export const api = { resolve(result); }) .catch((error) => { + console.error("ie", error); // Handle any errors thrown during the fetch process const { response, ...rest } = error; reject({