@@ -5,6 +5,7 @@ import type { RecursiveRecord } from '~/utils/typescript.utils';
5
5
import { CancellableFetch , type CancellablePromise } from '~/utils/fetch.utils' ;
6
6
import { HttpMethod , type HttpMethods } from '~/utils/http.utils' ;
7
7
import { Observable , ObservableState , type Observer , type Updater } from '~/utils/observable.utils' ;
8
+ import { ExactMatchRegex } from '~/utils/regex.utils' ;
8
9
9
10
export const BaseApiHeaders = {
10
11
/** The authorization token bearer */
@@ -192,15 +193,17 @@ export const getCachedFunction = <
192
193
clientFn : ClientEndpointCall < Parameter , ResponseBody > ,
193
194
{
194
195
key,
196
+ evictionKey,
195
197
cache,
196
198
retention,
197
199
} : {
198
200
key : string | ( ( param ?: Parameter , init ?: BaseInit ) => string ) ;
201
+ evictionKey ?: string | ( ( param ?: Parameter , init ?: BaseInit ) => string ) ;
199
202
cache : CacheStore < ResponseType > ;
200
203
retention ?: BaseTemplateOptions [ 'cache' ] ;
201
204
} ,
202
205
) : ClientEndpointCache < Parameter , ResponseBody > => {
203
- return async ( param , init , cacheOptions ) => {
206
+ const cachedFn : ClientEndpointCache < Parameter , ResponseBody > = async ( param , init , cacheOptions ) => {
204
207
const _key = typeof key === 'function' ? key ( param , init ) : key ;
205
208
const cached = await cache . get ( _key ) ;
206
209
if ( cached && ! cacheOptions ?. force ) {
@@ -228,6 +231,18 @@ export const getCachedFunction = <
228
231
throw error ;
229
232
}
230
233
} ;
234
+
235
+ const evictFn = async ( param ?: Parameter , init ?: BaseInit ) => {
236
+ const _key = evictionKey ?? key ;
237
+ if ( ! _key ) return ;
238
+ const _resolvedKey = typeof _key === 'function' ? _key ( param , init ) : _key ;
239
+ if ( ! _resolvedKey . trim ( ) ) return ;
240
+ await cache . clear ( _resolvedKey ) ;
241
+ return _resolvedKey ;
242
+ } ;
243
+
244
+ Object . defineProperty ( cachedFn , 'evict' , { value : evictFn } ) ;
245
+ return cachedFn ;
231
246
} ;
232
247
233
248
/**
@@ -336,6 +351,7 @@ export abstract class BaseClient<
336
351
key : ( param : unknown , init : unknown ) => JSON . stringify ( { template : template . config , param, init } ) ,
337
352
cache : this . _cache ,
338
353
retention : template . opts ?. cache ,
354
+ evictionKey : `{"template":${ JSON . stringify ( template . config ) . replace ( ExactMatchRegex , '\\$&' ) } ` ,
339
355
} ) ;
340
356
341
357
const parseUrl = ( param : Record < string , unknown > = { } ) => {
0 commit comments