Skip to content

Commit

Permalink
chore: upgrade axios (#1100)
Browse files Browse the repository at this point in the history
* chore: upgrade packages

* fix: types for axios

* chore: fix tests
  • Loading branch information
alexanderleegs authored Jan 10, 2024
1 parent 541bdf3 commit daec033
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
18 changes: 12 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/middleware/__tests__/errorHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosError } from "axios"
import { AxiosError, AxiosRequestHeaders } from "axios"
import _ from "lodash"
import { serializeError } from "serialize-error"

Expand All @@ -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(),
Expand Down
14 changes: 9 additions & 5 deletions src/services/api/AxiosInstance.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/services/db/__tests__/TokenService.spec.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit daec033

Please sign in to comment.