From 4143aeebc016f45f179b36d81a4aa5b3dc43297c Mon Sep 17 00:00:00 2001 From: Thomas Rognes <33520517+thomasrognes@users.noreply.github.com> Date: Tue, 21 May 2024 13:23:16 +0200 Subject: [PATCH 1/3] =?UTF-8?q?:sparkles:=20Pr=C3=B8ver=20=C3=A5=20parse?= =?UTF-8?q?=20til=20json=20dersom=20backend=20sier=20det=20er=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tor Idland Co-authored-by: Halvor Grizzly Bjørn Co-authored-by: Vetle Hollund --- lib/utils/api/simpleTokenXProxy.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/utils/api/simpleTokenXProxy.ts b/lib/utils/api/simpleTokenXProxy.ts index 6ac0f965..78059d5c 100644 --- a/lib/utils/api/simpleTokenXProxy.ts +++ b/lib/utils/api/simpleTokenXProxy.ts @@ -1,5 +1,5 @@ -import { validateToken, requestOboToken, getToken } from '@navikt/oasis'; -import { logError, logInfo } from '@navikt/aap-felles-utils'; +import { getToken, requestOboToken, validateToken } from '@navikt/oasis'; +import { logError, logInfo, logWarning } from '@navikt/aap-felles-utils'; import { randomUUID } from 'crypto'; import { IncomingMessage } from 'http'; import { ErrorMedStatus } from 'lib/utils/api/ErrorMedStatus'; @@ -64,21 +64,22 @@ export const simpleTokenXProxy = async ({ body: method === 'POST' ? JSON.stringify(body) : undefined, }); - try { - if (response.ok) { - logInfo(`OK ${url}, status ${response.status}, callId ${navCallId}`); - const headers = response.headers.get('content-type'); - const isJson = headers?.includes('application/json'); + if (response.ok) { + logInfo(`OK ${url}, status ${response.status}, callId ${navCallId}`); + const headers = response.headers.get('content-type'); + const isJson = headers?.includes('application/json'); - // TODO: Midlertidig, til innsending returnerer json på alle OK-responser - if (!isJson) { - return (await response.text()) as T; + if (isJson) { + try { + return await response.json(); + } catch (e) { + logWarning(`Kunne ikke parse json i simpleTokenXProxy for ${url}`); } - return await response.json(); } - } catch (error) { - logError(`Unable to parse response for ${url}`, error); + + return (await response.text()) as T; } + logError( `Error fetching simpleTokenXProxy. Fikk responskode ${response.status} fra ${url} med navCallId: ${navCallId}`, ); From b54cc703cf2b5ed139e505f3a24afb74c4b628af Mon Sep 17 00:00:00 2001 From: Thomas Rognes <33520517+thomasrognes@users.noreply.github.com> Date: Tue, 21 May 2024 15:20:38 +0200 Subject: [PATCH 2/3] =?UTF-8?q?:sparkles:=20H=C3=A5ndterer=20204=20respons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/api/simpleTokenXProxy.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/utils/api/simpleTokenXProxy.ts b/lib/utils/api/simpleTokenXProxy.ts index 78059d5c..5bd9a298 100644 --- a/lib/utils/api/simpleTokenXProxy.ts +++ b/lib/utils/api/simpleTokenXProxy.ts @@ -66,6 +66,11 @@ export const simpleTokenXProxy = async ({ if (response.ok) { logInfo(`OK ${url}, status ${response.status}, callId ${navCallId}`); + + if (response.status === 204) { + return 'request gikk fint' as T; + } + const headers = response.headers.get('content-type'); const isJson = headers?.includes('application/json'); From 7cfe1279b649c67fe4403490cfcbb46fbbb5c539 Mon Sep 17 00:00:00 2001 From: Thomas Rognes <33520517+thomasrognes@users.noreply.github.com> Date: Wed, 22 May 2024 09:41:16 +0200 Subject: [PATCH 3/3] =?UTF-8?q?:sparkles:=20H=C3=A5ndterer=20204=20respons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/api/simpleTokenXProxy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/api/simpleTokenXProxy.ts b/lib/utils/api/simpleTokenXProxy.ts index 5bd9a298..54ef4315 100644 --- a/lib/utils/api/simpleTokenXProxy.ts +++ b/lib/utils/api/simpleTokenXProxy.ts @@ -68,7 +68,7 @@ export const simpleTokenXProxy = async ({ logInfo(`OK ${url}, status ${response.status}, callId ${navCallId}`); if (response.status === 204) { - return 'request gikk fint' as T; + return {} as T; } const headers = response.headers.get('content-type');