From b809509b4fa9990648e7bda447ad435a54f78f26 Mon Sep 17 00:00:00 2001 From: Anders Rognstad Date: Tue, 2 Jan 2024 12:12:12 +0100 Subject: [PATCH] Upgrade axios --- package-lock.json | 55 ++++++++++++++++++------------------------- package.json | 4 ++-- src/api/axios.ts | 9 ++++--- test/api/apiTest.tsx | 16 ++----------- test/stubs/stubApi.ts | 2 +- 5 files changed, 32 insertions(+), 54 deletions(-) diff --git a/package-lock.json b/package-lock.json index f9d2a3f..98390c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,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", @@ -66,7 +66,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", @@ -4095,8 +4095,7 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "node_modules/autoprefixer": { "version": "10.4.7", @@ -4132,25 +4131,26 @@ } }, "node_modules/axios": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", - "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.3.tgz", + "integrity": "sha512-fWyNdeawGam70jXSVlKl+SUNVcL6j6W79CuSIPfi6HnDUmSCH6gyUys/HrqHeA/wU0Az41rRgean494d0Jb+ww==", "dependencies": { - "follow-redirects": "^1.14.8" + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" } }, "node_modules/axios-mock-adapter": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.20.0.tgz", - "integrity": "sha512-shZRhTjLP0WWdcvHKf3rH3iW9deb3UdKbdnKUoHmmsnBhVXN3sjPJM6ZvQ2r/ywgvBVQrMnjrSyQab60G1sr2w==", + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz", + "integrity": "sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==", "dev": true, "dependencies": { "fast-deep-equal": "^3.1.3", - "is-blob": "^2.1.0", "is-buffer": "^2.0.5" }, "peerDependencies": { - "axios": ">= 0.9.0" + "axios": ">= 0.17.0" } }, "node_modules/axios-mock-adapter/node_modules/is-buffer": { @@ -4925,7 +4925,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -5419,7 +5418,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -6797,9 +6795,9 @@ "dev": true }, "node_modules/follow-redirects": { - "version": "1.14.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", - "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", "funding": [ { "type": "individual", @@ -6819,7 +6817,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -7647,18 +7644,6 @@ "node": ">=8" } }, - "node_modules/is-blob": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-blob/-/is-blob-2.1.0.tgz", - "integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==", - "dev": true, - "engines": { - "node": ">=6" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-boolean-object": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/is-boolean-object/-/is-boolean-object-1.1.2.tgz", @@ -9742,6 +9727,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", @@ -9895,7 +9885,8 @@ "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", - "integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==" + "integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==", + "dev": true }, "node_modules/react-lifecycles-compat": { "version": "3.0.4", diff --git a/package.json b/package.json index 6c4ad7c..6938697 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/api/axios.ts b/src/api/axios.ts index 134712f..0a4a57d 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,4 +1,4 @@ -import axios, { AxiosError, AxiosRequestHeaders } from "axios"; +import axios, { AxiosError } from "axios"; import { accessDeniedError, ApiErrorException, @@ -15,8 +15,8 @@ export const NAV_PERSONIDENT_HEADER = "nav-personident"; export const defaultRequestHeaders = ( personIdent?: string -): AxiosRequestHeaders => { - const headers: AxiosRequestHeaders = { +): Record => { + const headers: Record = { "Content-Type": "application/json", [NAV_CONSUMER_ID_HEADER]: NAV_CONSUMER_ID, [NAV_CALL_ID_HEADER]: `${NAV_CONSUMER_ID}-${generateUUID()}`, @@ -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 ); } diff --git a/test/api/apiTest.tsx b/test/api/apiTest.tsx index 64ae12b..3b3f321 100644 --- a/test/api/apiTest.tsx +++ b/test/api/apiTest.tsx @@ -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; @@ -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); @@ -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); } }); }); diff --git a/test/stubs/stubApi.ts b/test/stubs/stubApi.ts index d2b3b5e..ac8fcb4 100644 --- a/test/stubs/stubApi.ts +++ b/test/stubs/stubApi.ts @@ -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(); };