From 78f8feae21483fd0b5b054bb9ca37f01325f4303 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 21 Nov 2022 23:54:39 +0000 Subject: [PATCH] case 4311 --- server/AyaNova/resource/rpt/ay-report.js | 34 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/server/AyaNova/resource/rpt/ay-report.js b/server/AyaNova/resource/rpt/ay-report.js index 6ce564c9..58f31e13 100644 --- a/server/AyaNova/resource/rpt/ay-report.js +++ b/server/AyaNova/resource/rpt/ay-report.js @@ -305,7 +305,7 @@ async function ayGetFromAPI(route, token) { Authorization: token } }); - return await r.json(); + return await extractBodyEx(r); } catch (error) { //fundamental error, can't proceed with this call // handleError("GET", error, route); @@ -338,7 +338,7 @@ async function ayPostToAPI(route, data, token) { body: JSON.stringify(data) }; let r = await fetch(route, fetchOptions); - return await r.json(); + return await extractBodyEx(r); } catch (error) { throw error; } @@ -369,7 +369,7 @@ async function ayPutToAPI(route, data, token) { body: JSON.stringify(data) }; let r = await fetch(route, fetchOptions); - return await r.json(); + return await extractBodyEx(r); } catch (error) { throw error; } @@ -398,6 +398,34 @@ function attachmentDownloadUrl(fileId, ctype) { //##################################### CODE UTILITIES ################################################### +async function extractBodyEx(response) { + if (response.status == 204) { + //no content, nothing to process + return response; + } + if (response.status == 202) { + //Accepted, nothing to process + return response; + } + + const contentType = response.headers.get("content-type"); + + if (!contentType) { + return response; + } + if (contentType.includes("json")) { + return await response.json(); + } + if (contentType.includes("text/plain")) { + return await response.text(); + } + + if (contentType.includes("application/pdf")) { + return await response.blob(); + } + return response; +} + ///////////////////////////////////////////////////////// // Group by function // reshapes input array into a new array grouped with