Skip to content

Commit 6b246e5

Browse files
committed
feat: axios-cache-interceptor
Signed-off-by: Adam Setch <[email protected]>
1 parent 7312b0e commit 6b246e5

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
"@types/react-dom": "19.2.2",
9292
"@types/react-router-dom": "5.3.3",
9393
"@types/semver": "7.7.1",
94-
"identity-obj-proxy": "3.0.0",
9594
"axios": "1.12.2",
95+
"axios-cache-interceptor": "1.8.3",
9696
"clsx": "2.1.1",
9797
"concurrently": "9.2.1",
9898
"copy-webpack-plugin": "13.0.1",
@@ -105,6 +105,7 @@
105105
"graphql-tag": "2.12.6",
106106
"html-webpack-plugin": "5.6.4",
107107
"husky": "9.1.7",
108+
"identity-obj-proxy": "3.0.0",
108109
"jest": "30.2.0",
109110
"jest-environment-jsdom": "30.2.0",
110111
"mini-css-extract-plugin": "2.9.4",
@@ -138,4 +139,4 @@
138139
"*": "biome check --fix --no-errors-on-unmatched",
139140
"*.{js,ts,tsx}": "pnpm test --findRelatedTests --passWithNoTests --updateSnapshot"
140141
}
141-
}
142+
}

pnpm-lock.yaml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/utils/api/request.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
import axios, {
1+
import Axios, {
22
type AxiosPromise,
33
type AxiosResponse,
44
type Method,
55
} from 'axios';
6+
import { setupCache } from 'axios-cache-interceptor';
67

78
import type { Link, Token } from '../../types';
89
import { decryptValue } from '../comms';
910
import { rendererLogError } from '../logger';
1011
import { getNextURLFromLinkHeader } from './utils';
1112

13+
const instance = Axios.create();
14+
const axios = setupCache(instance, {
15+
location: 'client',
16+
cachePredicate: {
17+
ignoreUrls: [
18+
'/login/oauth/access_token',
19+
'/notifications',
20+
'/api/v3/notifications',
21+
],
22+
},
23+
});
24+
1225
/**
1326
* Perform an unauthenticated API request
1427
*
@@ -22,7 +35,7 @@ export async function apiRequest(
2235
method: Method,
2336
data = {},
2437
): Promise<AxiosPromise | null> {
25-
const headers = await getHeaders(url);
38+
const headers = await getHeaders();
2639

2740
return axios({ method, url, data, headers });
2841
}
@@ -44,7 +57,7 @@ export async function apiRequestAuth(
4457
data = {},
4558
fetchAllRecords = false,
4659
): AxiosPromise | null {
47-
const headers = await getHeaders(url, token);
60+
const headers = await getHeaders(token);
4861

4962
if (!fetchAllRecords) {
5063
return axios({ method, url, data, headers });
@@ -57,7 +70,12 @@ export async function apiRequestAuth(
5770
let nextUrl: string | null = url;
5871

5972
while (nextUrl) {
60-
response = await axios({ method, url: nextUrl, data, headers });
73+
response = await axios({
74+
method,
75+
url: nextUrl,
76+
data,
77+
headers,
78+
});
6179

6280
// If no data is returned, break the loop
6381
if (!response?.data) {
@@ -80,36 +98,16 @@ export async function apiRequestAuth(
8098
} as AxiosResponse;
8199
}
82100

83-
/**
84-
* Return true if the request should be made with no-cache
85-
*
86-
* @param url
87-
* @returns boolean
88-
*/
89-
function shouldRequestWithNoCache(url: string) {
90-
const parsedUrl = new URL(url);
91-
92-
switch (parsedUrl.pathname) {
93-
case '/api/v3/notifications':
94-
case '/login/oauth/access_token':
95-
case '/notifications':
96-
return true;
97-
default:
98-
return false;
99-
}
100-
}
101-
102101
/**
103102
* Construct headers for API requests
104103
*
105104
* @param username
106105
* @param token
107106
* @returns
108107
*/
109-
async function getHeaders(url: Link, token?: Token) {
108+
async function getHeaders(token?: Token) {
110109
const headers: Record<string, string> = {
111110
Accept: 'application/json',
112-
'Cache-Control': shouldRequestWithNoCache(url) ? 'no-cache' : '',
113111
'Content-Type': 'application/json',
114112
};
115113

0 commit comments

Comments
 (0)