Skip to content

Commit

Permalink
r - committer resten av refaktor
Browse files Browse the repository at this point in the history
Det gikk ikke så bra å dele opp de to
  • Loading branch information
toresbe committed Jun 19, 2024
1 parent 6e206f7 commit fe5e1f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/lib/api/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Axios, {AxiosError, AxiosRequestConfig, AxiosResponse, isCancel} from "axios";
import {isLocalhost, isMockAlt} from "../utils";
import {UnauthorizedMelding} from "../../generated/model";
import {logError, logInfo, logWarning} from "../log/loggerUtils";
import {baseURL, linkPagePath} from "../config";
import {buildGotoSearchParameter} from "./auth/buildGotoSearchParameter";
import {isLoginError} from "./error/isLoginError";
import {getGotoParameter} from "./auth/getGotoParameter";

const AXIOS_INSTANCE = Axios.create({
baseURL,
Expand All @@ -25,6 +25,8 @@ export type DigisosAxiosConfig = {
digisosIgnoreErrors?: boolean;
};

const neverResolves = <T>() => new Promise<T>(() => {});

/**
* Digisos Axios client
*
Expand Down Expand Up @@ -52,29 +54,30 @@ export const axiosInstance = <T>(
.catch(async (e) => {
if (!(e instanceof AxiosError)) await logWarning(`non-axioserror error ${e} in axiosinstance`);

if (isCancel(e) || options?.digisosIgnoreErrors) return new Promise<T>(() => {});
if (isCancel(e) || options?.digisosIgnoreErrors) return neverResolves();

const {response} = e;

if (!e.response) {
if (!response) {
await logWarning(`Nettverksfeil i axiosInstance: ${config.method} ${config.url} ${e}`);
console.warn(e);
throw e;
}

const {status, data} = e.response;

if (status === 401) {
const {loginUrl} = data as UnauthorizedMelding;
const loginPage = new URL(loginUrl);
const redirectUrl = `${origin}${linkPagePath}?${buildGotoSearchParameter(window.location)}`;
loginPage.searchParams.set("redirect", redirectUrl);
window.location.assign(loginPage);
return new Promise<T>(() => {});
if (isLoginError(response)) {
const loginUrl = new URL(response.data.loginUrl);
const redirect = `${origin}${linkPagePath}?goto=${getGotoParameter(window.location)}`;
loginUrl.searchParams.set("redirect", redirect);
window.location.assign(loginUrl);
return neverResolves();
}

const {status, data} = response;

// 403 burde gi feilmelding, men visse HTTP-kall som burde returnere 404 gir 403
if ([403, 404, 410].includes(status)) {
window.location.href = `/sosialhjelp/soknad/informasjon?reason=axios${status}`;
return new Promise<T>(() => {});
return neverResolves();
}

// Conflict -- try again
Expand Down
7 changes: 7 additions & 0 deletions src/lib/api/error/isLoginError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {AxiosResponse} from "axios";
import {UnauthorizedMelding} from "../../../generated/model";

export const isLoginError = (response: AxiosResponse): response is AxiosResponse<UnauthorizedMelding> => {
if (!response.data?.loginUrl) throw new Error("Missing loginUrl in 401 response");
return response.status === 401;
};

0 comments on commit fe5e1f8

Please sign in to comment.