Skip to content

Commit 7badde8

Browse files
committed
feat: axios-cache-interceptor
Signed-off-by: Adam Setch <[email protected]>
1 parent b5f4e70 commit 7badde8

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/renderer/utils/api/request.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getNextURLFromLinkHeader } from './utils';
1313

1414
type AxiosRequestConfigWithAccount = AxiosRequestConfig & { account: Account };
1515

16-
// const MUTATION_HTTP_METHODS: Set<Method> = new Set(['PATCH', 'PUT', 'DELETE']);
16+
const MUTATION_HTTP_METHODS: Set<Method> = new Set(['PATCH', 'PUT', 'DELETE']);
1717

1818
const instance = Axios.create();
1919
const axios = setupCache(instance, {
@@ -43,21 +43,21 @@ const axios = setupCache(instance, {
4343

4444
// Invalidate cache on mutating requests (PATCH, PUT, DELETE)
4545
// Only clears cache entries for the same account that made the mutation
46-
// axios.interceptors.response.use(
47-
// async (response) => {
48-
// const method = response.config.method?.toUpperCase() as Method;
49-
// const config = response.config as AxiosRequestConfigWithAccount;
50-
51-
// if (MUTATION_HTTP_METHODS.has(method) && config.account) {
52-
// await clearFullApiCache();
53-
// }
54-
// return response;
55-
// },
56-
// (error: Error) => {
57-
// // Pass through errors without clearing cache
58-
// return Promise.reject(error);
59-
// },
60-
// );
46+
axios.interceptors.response.use(
47+
async (response) => {
48+
const method = response.config.method?.toUpperCase() as Method;
49+
const config = response.config as AxiosRequestConfigWithAccount;
50+
51+
if (MUTATION_HTTP_METHODS.has(method) && config.account) {
52+
await clearFullApiCache();
53+
}
54+
return response;
55+
},
56+
(error: Error) => {
57+
// Pass through errors without clearing cache
58+
return Promise.reject(error);
59+
},
60+
);
6161

6262
/**
6363
* Perform an unauthenticated API request
@@ -165,9 +165,9 @@ async function getHeaders(token?: Token) {
165165
}
166166

167167
export async function clearFullApiCache(): Promise<void> {
168-
// try {
169-
// axios.storage.clear();
170-
// } catch (err) {
171-
// rendererLogError('clearFullApiCache', 'Failed to clear API cache', err);
172-
// }
168+
try {
169+
axios.storage.clear();
170+
} catch (err) {
171+
rendererLogError('clearFullApiCache', 'Failed to clear API cache', err);
172+
}
173173
}

0 commit comments

Comments
 (0)