Skip to content

Commit

Permalink
✨ Prøver å parse til json dersom backend sier det er json
Browse files Browse the repository at this point in the history
Co-authored-by: Tor Idland <[email protected]>
Co-authored-by: Halvor Grizzly Bjørn <[email protected]>
Co-authored-by: Vetle Hollund <[email protected]>
  • Loading branch information
4 people committed May 21, 2024
1 parent 1d22856 commit 4143aee
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/utils/api/simpleTokenXProxy.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -64,21 +64,22 @@ export const simpleTokenXProxy = async <T>({
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}`,
);
Expand Down

0 comments on commit 4143aee

Please sign in to comment.