Skip to content

Commit

Permalink
Upgrade axios (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersrognstad authored Jan 2, 2024
1 parent c17cd90 commit 0f6b86a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 54 deletions.
55 changes: 23 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@types/react-dom": "18.0.5",
"@types/redis": "2.8.32",
"@types/styled-components": "5.1.25",
"axios": "0.26.1",
"axios": "1.6.3",
"classnames": "2.3.1",
"connect-redis": "6.1.3",
"core-js": "3.22.6",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@typescript-eslint/eslint-plugin": "5.26.0",
"@typescript-eslint/parser": "5.26.0",
"autoprefixer": "10.4.7",
"axios-mock-adapter": "1.20.0",
"axios-mock-adapter": "1.22.0",
"babel-loader": "8.2.5",
"babel-plugin-module-resolver": "5.0.0",
"babel-plugin-styled-components": "2.0.7",
Expand Down
9 changes: 4 additions & 5 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError, AxiosRequestHeaders } from "axios";
import axios, { AxiosError } from "axios";
import {
accessDeniedError,
ApiErrorException,
Expand All @@ -15,8 +15,8 @@ export const NAV_PERSONIDENT_HEADER = "nav-personident";

export const defaultRequestHeaders = (
personIdent?: string
): AxiosRequestHeaders => {
const headers: AxiosRequestHeaders = {
): Record<string, string> => {
const headers: Record<string, string> = {
"Content-Type": "application/json",
[NAV_CONSUMER_ID_HEADER]: NAV_CONSUMER_ID,
[NAV_CALL_ID_HEADER]: `${NAV_CONSUMER_ID}-${generateUUID()}`,
Expand All @@ -40,9 +40,8 @@ function handleAxiosError(error: AxiosError) {
);
}
case 403: {
const message = error.response.data.message || error.message;
throw new ApiErrorException(
accessDeniedError(message),
accessDeniedError(error.message),
error.response.status
);
}
Expand Down
16 changes: 2 additions & 14 deletions test/api/apiTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "chai";
import { get } from "@/api/axios";
import MockAdapter from "axios-mock-adapter";
import { Tilgang } from "@/data/tilgang/tilgangTypes";
import { ApiErrorException, ErrorType } from "@/api/errors";
import { ApiErrorException, defaultErrorTexts, ErrorType } from "@/api/errors";

let stub: MockAdapter;

Expand Down Expand Up @@ -35,18 +35,6 @@ describe("Axios API tests", () => {
});

describe("Access denied tests", () => {
it("Throws access denied for http 403, and handles Tilgang-object", async function () {
try {
await get(pathAccessDenied);
} catch (e) {
expect(e instanceof ApiErrorException).to.equal(true);

const { error, code } = e as ApiErrorException;
expect(code).to.equal(403);
expect(error.type).to.equal(ErrorType.ACCESS_DENIED);
}
});

it("Throws access denied for http 403, and handles message", async function () {
try {
await get(pathAccessDeniedMessage);
Expand All @@ -56,7 +44,7 @@ describe("Axios API tests", () => {
const { error, code } = e as ApiErrorException;
expect(code).to.equal(403);
expect(error.type).to.equal(ErrorType.ACCESS_DENIED);
expect(error.message).to.equal(tilgangDeniedMessage.message);
expect(error.defaultErrorMsg).to.equal(defaultErrorTexts.accessDenied);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/stubs/stubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import axios from "axios";
import nock from "nock";

export const apiMock = () => {
axios.defaults.adapter = require("axios/lib/adapters/http");
axios.defaults.adapter = "http";
return nock("http://localhost").persist();
};

0 comments on commit 0f6b86a

Please sign in to comment.