diff --git a/package-lock.json b/package-lock.json index 3f8d57f6a..e2bbde7ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "auto-bind": "^4.0.0", "aws-lambda": "^1.0.7", "aws-sdk": "^2.1428.0", - "axios": "~0.27.2", + "axios": "^1.6.5", "axios-cache-interceptor": "^0.10.7", "base-64": "^0.1.0", "bcryptjs": "^2.4.3", @@ -5518,12 +5518,13 @@ } }, "node_modules/axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", + "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", "dependencies": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" } }, "node_modules/axios-cache-interceptor": { @@ -14026,6 +14027,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", diff --git a/package.json b/package.json index be403fa13..bc2a49e26 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "auto-bind": "^4.0.0", "aws-lambda": "^1.0.7", "aws-sdk": "^2.1428.0", - "axios": "~0.27.2", + "axios": "^1.6.5", "axios-cache-interceptor": "^0.10.7", "base-64": "^0.1.0", "bcryptjs": "^2.4.3", diff --git a/src/middleware/__tests__/errorHandler.spec.ts b/src/middleware/__tests__/errorHandler.spec.ts index 5a4ba75b8..5f3d82b02 100644 --- a/src/middleware/__tests__/errorHandler.spec.ts +++ b/src/middleware/__tests__/errorHandler.spec.ts @@ -1,4 +1,4 @@ -import { AxiosError } from "axios" +import { AxiosError, AxiosRequestHeaders } from "axios" import _ from "lodash" import { serializeError } from "serialize-error" @@ -21,7 +21,9 @@ const mockRes = { } const mockNext = jest.fn() const mockAxiosError: AxiosError = { - config: {}, + config: { + headers: ({} as unknown) as AxiosRequestHeaders, + }, request: {}, isAxiosError: true, toJSON: jest.fn().mockReturnThis(), diff --git a/src/services/api/AxiosInstance.ts b/src/services/api/AxiosInstance.ts index 767f5db66..1a78320d2 100644 --- a/src/services/api/AxiosInstance.ts +++ b/src/services/api/AxiosInstance.ts @@ -1,5 +1,5 @@ -import axios, { AxiosRequestConfig, AxiosResponse } from "axios" -import { setupCache } from "axios-cache-interceptor" +import axios, { AxiosRequestConfig } from "axios" +import { CacheAxiosResponse, setupCache } from "axios-cache-interceptor" import _ from "lodash" import { config } from "@config/config" @@ -91,13 +91,13 @@ const requestFormatter = async (axiosConfig: AxiosRequestConfig) => { } } -const respHandler = (response: AxiosResponse) => { +const respHandler = (response: CacheAxiosResponse) => { // Any status code that lie within the range of 2xx will cause this function to trigger tokenServiceInstance.onResponse(response) return response } -const githubApiInterceptor = (resp: AxiosResponse) => { +const githubApiInterceptor = (resp: CacheAxiosResponse) => { const fullUrl = `${resp.config.baseURL || ""}${resp.config.url || ""}` if ( resp.status !== 304 && @@ -126,7 +126,11 @@ isomerRepoAxiosInstance.interceptors.request.use(requestFormatter) isomerRepoAxiosInstance.interceptors.response.use(respHandler) isomerRepoAxiosInstance.interceptors.response.use(githubApiInterceptor) -const genericGitHubAxiosInstance = axios.create() +const genericGitHubAxiosInstance = setupCache(axios.create(), { + interpretHeader: true, + etag: true, + headerInterpreter: customHeaderInterpreter, +}) genericGitHubAxiosInstance.interceptors.request.use(requestFormatter) genericGitHubAxiosInstance.interceptors.response.use(respHandler) genericGitHubAxiosInstance.interceptors.response.use(githubApiInterceptor) diff --git a/src/services/db/__tests__/TokenService.spec.ts b/src/services/db/__tests__/TokenService.spec.ts index 64dd0779c..a097f37f8 100644 --- a/src/services/db/__tests__/TokenService.spec.ts +++ b/src/services/db/__tests__/TokenService.spec.ts @@ -1,5 +1,5 @@ import { expect, jest } from "@jest/globals" -import { AxiosResponse } from "axios" +import { AxiosRequestHeaders, AxiosResponse } from "axios" import { ok, err } from "neverthrow" import { ModelStatic } from "sequelize" @@ -594,9 +594,9 @@ describe("Token Service", () => { (resetTime: number) => ({ config: { - headers: { + headers: ({ Authorization: `token ${sampleGithubToken}`, - }, + } as unknown) as AxiosRequestHeaders, }, headers: { [GITHUB_TOKEN_REMAINING_HEADER]: "1234",