88 selfHostedCodecovURLStorageKey ,
99 selfHostedGitHubURLStorageKey ,
1010 providers ,
11+ cacheTtlMs ,
1112} from "src/constants" ;
1213
1314export class Codecov {
@@ -137,10 +138,65 @@ export class Codecov {
137138 } ;
138139 }
139140
141+ async getCached (
142+ type : "flags" | "components" ,
143+ owner : string ,
144+ repo : string
145+ ) : Promise < any > {
146+ const cacheKey = `${ owner } /${ repo } /${ type } ` ;
147+ const cacheExpiryKey = `${ owner } /${ repo } /${ type } /expiry` ;
148+
149+ const storage = await browser . storage . local . get ( [ cacheKey , cacheExpiryKey ] ) ;
150+
151+ if ( ! storage [ cacheKey ] || ! storage [ cacheExpiryKey ] ) {
152+ // Cache is not set
153+ return null ;
154+ }
155+
156+ const value = JSON . parse ( storage [ cacheKey ] ) ;
157+ const expiry = storage [ cacheExpiryKey ] ;
158+
159+ if ( Date . now ( ) <= expiry ) {
160+ // Cache is valid, return cached value
161+ return value ;
162+ }
163+
164+ // Cache is expired, clear cache
165+ await browser . storage . local . remove ( [ cacheKey , cacheExpiryKey ] ) ;
166+
167+ return null ;
168+ }
169+
170+ async setCached (
171+ type : "flags" | "components" ,
172+ owner : string ,
173+ repo : string ,
174+ data : any
175+ ) : Promise < void > {
176+ const cacheKey = `${ owner } /${ repo } /${ type } ` ;
177+ const cacheExpiryKey = `${ owner } /${ repo } /${ type } /expiry` ;
178+
179+ await browser . storage . local . set ( {
180+ [ cacheKey ] : JSON . stringify ( data ) ,
181+ [ cacheExpiryKey ] : Date . now ( ) + cacheTtlMs ,
182+ } ) ;
183+
184+ return ;
185+ }
186+
140187 async listFlags ( payload : any , referrer : string ) : Promise < any > {
141188 await this . init ( ) ;
142189 const { owner, repo } = payload ;
143190
191+ const cachedFlags = await this . getCached ( "flags" , owner , repo ) ;
192+
193+ if ( cachedFlags != null ) {
194+ return {
195+ ok : true ,
196+ data : cachedFlags ,
197+ } ;
198+ }
199+
144200 const url = new URL (
145201 `/api/v2/${ this . provider } /${ owner } /repos/${ repo } /flags` ,
146202 this . apiUrl
@@ -153,6 +209,8 @@ export class Codecov {
153209 } ) ;
154210 const data = await response . json ( ) ;
155211
212+ await this . setCached ( "flags" , owner , repo , data ) ;
213+
156214 return {
157215 ok : response . ok ,
158216 data,
@@ -163,6 +221,15 @@ export class Codecov {
163221 await this . init ( ) ;
164222 const { owner, repo } = payload ;
165223
224+ const cachedComponents = await this . getCached ( "components" , owner , repo ) ;
225+
226+ if ( cachedComponents != null ) {
227+ return {
228+ ok : true ,
229+ data : cachedComponents ,
230+ } ;
231+ }
232+
166233 const url = new URL (
167234 `/api/v2/${ this . provider } /${ owner } /repos/${ repo } /components` ,
168235 this . apiUrl
@@ -175,6 +242,8 @@ export class Codecov {
175242 } ) ;
176243 const data = await response . json ( ) ;
177244
245+ await this . setCached ( "components" , owner , repo , data ) ;
246+
178247 return {
179248 ok : response . ok ,
180249 data,
0 commit comments