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
78import type { Link , Token } from '../../types' ;
89import { decryptValue } from '../comms' ;
910import { rendererLogError } from '../logger' ;
1011import { 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