@defillama/sdk 5.0.204 → 5.0.207

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,17 +27,18 @@ export declare function getTempLocalCache({ file, defaultData, clearAfter, retur
27
27
  export declare function deleteCache(file: string): Promise<void>;
28
28
  export declare function readExpiringJsonCache(file: string): Promise<any>;
29
29
  export declare function writeExpiringJsonCache(file: string, data: any, { expireAfter, // cache for 1 day by default
30
- expiryTimestamp, }: {
30
+ expiryTimestamp, }?: {
31
31
  expireAfter?: number;
32
32
  expiryTimestamp?: number;
33
33
  }): Promise<void>;
34
- export declare function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache, writeCacheOptions, readCacheOptions, }: {
34
+ export declare function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache, cacheMaxAge, writeCacheOptions, readCacheOptions, }: {
35
35
  key: string;
36
36
  endpoint?: string;
37
37
  postData?: any;
38
38
  fetcher?: () => Promise<any>;
39
39
  fetchOptions?: any;
40
40
  errorOnNoCache?: boolean;
41
+ cacheMaxAge?: number;
41
42
  writeCacheOptions?: WriteCacheOptions;
42
43
  readCacheOptions?: ReadCacheOptions;
43
44
  }): Promise<any>;
@@ -222,14 +222,14 @@ async function readExpiringJsonCache(file) {
222
222
  return data.data;
223
223
  }
224
224
  async function writeExpiringJsonCache(file, data, { expireAfter = 60 * 60 * 24, // cache for 1 day by default
225
- expiryTimestamp, }) {
225
+ expiryTimestamp, } = {}) {
226
226
  file = 'expiring/' + file;
227
227
  if (!expiryTimestamp)
228
228
  expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
229
229
  const options = { skipR2CacheWrite: true, skipCompression: true };
230
230
  await writeCache(file, { data, expiryTimestamp }, options);
231
231
  }
232
- async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache = true, writeCacheOptions = {
232
+ async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache = true, cacheMaxAge = 0, writeCacheOptions = {
233
233
  skipCompression: true,
234
234
  skipR2CacheWrite: true,
235
235
  }, readCacheOptions = {
@@ -242,6 +242,21 @@ async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, err
242
242
  key = (0, common_1.getHash)(endpoint);
243
243
  }
244
244
  const fileKey = 'cached-fetch/' + key;
245
+ // check local cache first
246
+ if (cacheMaxAge > 0) {
247
+ try {
248
+ const filePath = getFilePath(readCacheOptions.skipCompression ? `${fileKey}-uncompressed` : fileKey);
249
+ const stats = await fs.stat(filePath);
250
+ if (Date.now() - stats.mtimeMs < cacheMaxAge) {
251
+ const cachedData = await readCache(fileKey, readCacheOptions);
252
+ if (cachedData && Object.keys(cachedData).length > 0)
253
+ return cachedData;
254
+ }
255
+ }
256
+ catch (error) {
257
+ // cache miss or stat error, proceed to fetch
258
+ }
259
+ }
245
260
  try {
246
261
  let data;
247
262
  if (fetcher)
@@ -260,7 +275,7 @@ async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, err
260
275
  catch (error) {
261
276
  const errorMessage = (0, common_1.formError)(error).message;
262
277
  (0, debugLog_1.debugLog)('Error in cachedFetch:', errorMessage);
263
- // if fetch fails, read from cache
278
+ // if fetch fails, read from cache (regardless of age)
264
279
  const cachedData = await readCache(fileKey, readCacheOptions);
265
280
  if (!cachedData && errorOnNoCache)
266
281
  throw new Error('Fetch failed and no cache available');