Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Håndtering av 204 respons #152

Merged
merged 3 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 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,27 @@ 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}`);

// TODO: Midlertidig, til innsending returnerer json på alle OK-responser
if (!isJson) {
return (await response.text()) as T;
if (response.status === 204) {
return {} as T;
}

const headers = response.headers.get('content-type');
const isJson = headers?.includes('application/json');

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
Loading