diff --git a/.vscode/launch.json b/.vscode/launch.json index a732363..3ab5d02 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,14 +6,7 @@ "type": "node", "request": "launch", "runtimeExecutable": "pnpm", - "runtimeArgs": [ - "--inspect-brk", - "jest", - "--", - "--runInBand", - "--watch", - "--coverage" - ], + "runtimeArgs": ["--inspect-brk", "jest", "--", "--runInBand", "--watch", "--coverage"], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 3938299..d76141c 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -10,9 +10,7 @@ const VERSION = isVersion > -1 ? process.argv[isVersion + 1].slice(1, -1) : 'Lat const BASE_URL = isVersion > -1 ? process.argv[isVersion + 1] : '/'; console.log( - isVersion > -1 - ? `Building docs for version ${VERSION}` - : 'Building docs for latest version' + isVersion > -1 ? `Building docs for version ${VERSION}` : 'Building docs for latest version' ); const description = @@ -44,22 +42,10 @@ export default defineConfig({ head: [ // Attach a custom favicon ['link', { rel: 'icon', href: `${BASE_URL}favicon.ico', type: 'image/x-icon` }], - [ - 'link', - { rel: 'apple-touch-icon', sizes: '57x57', href: `${BASE_URL}apple-icon-57x57.png` } - ], - [ - 'link', - { rel: 'apple-touch-icon', sizes: '60x60', href: `${BASE_URL}apple-icon-60x60.png` } - ], - [ - 'link', - { rel: 'apple-touch-icon', sizes: '72x72', href: `${BASE_URL}apple-icon-72x72.png` } - ], - [ - 'link', - { rel: 'apple-touch-icon', sizes: '76x76', href: `${BASE_URL}apple-icon-76x76.png` } - ], + ['link', { rel: 'apple-touch-icon', sizes: '57x57', href: `${BASE_URL}apple-icon-57x57.png` }], + ['link', { rel: 'apple-touch-icon', sizes: '60x60', href: `${BASE_URL}apple-icon-60x60.png` }], + ['link', { rel: 'apple-touch-icon', sizes: '72x72', href: `${BASE_URL}apple-icon-72x72.png` }], + ['link', { rel: 'apple-touch-icon', sizes: '76x76', href: `${BASE_URL}apple-icon-76x76.png` }], [ 'link', { @@ -139,10 +125,7 @@ export default defineConfig({ ['link', { rel: 'manifest', href: `${BASE_URL}manifest.json` }], ['meta', { name: 'msapplication-TileColor', content: '#e5972a' }], - [ - 'meta', - { name: 'msapplication-TileImage', content: `${BASE_URL}ms-icon-144x144.png` } - ], + ['meta', { name: 'msapplication-TileImage', content: `${BASE_URL}ms-icon-144x144.png` }], ['meta', { name: 'theme-color', content: '#e5972a' }], ['meta', { name: 'description', content: description }], @@ -171,10 +154,7 @@ export default defineConfig({ content: 'u9Nw6WpRrWDhdPTAv-LGIE9aJ0C15t7zkjuaUizDJnA' } ], - [ - 'script', - { async: 'true', src: 'https://www.googletagmanager.com/gtag/js?id=G-K548ZF395X' } - ], + ['script', { async: 'true', src: 'https://www.googletagmanager.com/gtag/js?id=G-K548ZF395X' }], [ 'script', {}, @@ -228,24 +208,21 @@ export default defineConfig({ ], nav: [ - { text: 'Guide', link: `/guide` }, - { text: 'Config', link: `/config` }, - { text: 'Others', link: `/others/license` }, + { text: 'Guide', link: '/guide' }, + { text: 'Config', link: '/config' }, + { text: 'Others', link: '/others/license' }, { text: VERSION, items: [ { text: 'Latest', link: url }, { text: 'v0.x', link: `${url}/v0/` } - ].filter((i) => - BASE_URL === '/' ? i.text !== 'Latest' : !i.link.includes(BASE_URL) - ) + ].filter((i) => (BASE_URL === '/' ? i.text !== 'Latest' : !i.link.includes(BASE_URL))) } ], //! Temp link for testing, will be changed to the real one before merged to production editLink: { - pattern: - 'https://github.com/arthurfiorette/axios-cache-interceptor/edit/main/docs/src/:path' + pattern: 'https://github.com/arthurfiorette/axios-cache-interceptor/edit/main/docs/src/:path' }, footer: { diff --git a/examples/runkit.js b/examples/runkit.js index 26bbd1f..95ea103 100644 --- a/examples/runkit.js +++ b/examples/runkit.js @@ -36,7 +36,7 @@ const { log } = console; // log(`Fetched response Cache-Control: ${fetchedResponse.headers['cache-control']}`); - log(`Fetched response Age: ${fetchedResponse.headers['age']}`); + log(`Fetched response Age: ${fetchedResponse.headers.age}`); const cacheInformation = await axios.storage.get(fetchedResponse.id); diff --git a/src/cache/axios.ts b/src/cache/axios.ts index 700acd6..8c34a42 100644 --- a/src/cache/axios.ts +++ b/src/cache/axios.ts @@ -5,7 +5,6 @@ import type { AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders, - InternalAxiosRequestConfig } from 'axios'; import type { CacheInstance, CacheProperties } from './cache'; @@ -84,8 +83,7 @@ export interface CacheRequestConfig extends AxiosRequestConfig } /** Cached version of type {@link InternalAxiosRequestConfig} */ -export interface InternalCacheRequestConfig - extends CacheRequestConfig { +export interface InternalCacheRequestConfig extends CacheRequestConfig { headers: AxiosResponseHeaders; } @@ -96,7 +94,6 @@ export interface InternalCacheRequestConfig * @see https://axios-cache-interceptor.js.org/guide/getting-started */ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { - new (config?: CacheRequestConfig): AxiosCacheInstance; /** diff --git a/src/cache/create.ts b/src/cache/create.ts index b681ad7..4fe4c86 100644 --- a/src/cache/create.ts +++ b/src/cache/create.ts @@ -26,10 +26,7 @@ export interface CacheOptions extends Partial, Partial - [200, 203, 300, 301, 302, 404, 405, 410, 414, 501].includes(status) + statusCheck: (status) => [200, 203, 300, 301, 302, 404, 405, 410, 414, 501].includes(status) }, etag: options.etag ?? true, diff --git a/src/header/types.ts b/src/header/types.ts index 5e58f78..976ad26 100644 --- a/src/header/types.ts +++ b/src/header/types.ts @@ -34,6 +34,4 @@ export type InterpreterResult = * **milliseconds** to cache the response. * @see https://axios-cache-interceptor.js.org/config#headerinterpreter */ -export type HeaderInterpreter = ( - headers?: CacheAxiosResponse['headers'] -) => InterpreterResult; +export type HeaderInterpreter = (headers?: CacheAxiosResponse['headers']) => InterpreterResult; diff --git a/src/interceptors/build.ts b/src/interceptors/build.ts index 655bb0c..2db17ed 100644 --- a/src/interceptors/build.ts +++ b/src/interceptors/build.ts @@ -10,7 +10,5 @@ export interface AxiosInterceptor { apply: () => void; } -export type RequestInterceptor = AxiosInterceptor< - InternalCacheRequestConfig ->; +export type RequestInterceptor = AxiosInterceptor>; export type ResponseInterceptor = AxiosInterceptor>; diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 01940dc..f707f95 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -1,18 +1,9 @@ import { deferred } from 'fast-defer'; import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios'; import { Header } from '../header/headers'; -import type { - CachedResponse, - CachedStorageValue, - LoadingStorageValue -} from '../storage/types'; +import type { CachedResponse, CachedStorageValue, LoadingStorageValue } from '../storage/types'; import type { RequestInterceptor } from './build'; -import { - ConfigWithCache, - createValidateStatus, - isMethodIn, - updateStaleRequest -} from './util'; +import { ConfigWithCache, createValidateStatus, isMethodIn, updateStaleRequest } from './util'; export function defaultRequestInterceptor(axios: AxiosCacheInstance) { const onFulfilled: RequestInterceptor['onFulfilled'] = async (config) => { @@ -58,11 +49,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { // Not cached, continue the request, and mark it as fetching // biome-ignore lint/suspicious/noConfusingLabels: required to break condition in simultaneous accesses - ignoreAndRequest: if ( - cache.state === 'empty' || - cache.state === 'stale' || - overrideCache - ) { + ignoreAndRequest: if (cache.state === 'empty' || cache.state === 'stale' || overrideCache) { /** * This checks for simultaneous access to a new key. The js event loop jumps on the * first await statement, so the second (asynchronous call) request may have already @@ -121,8 +108,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { // If the cache is empty and asked to override it, use the current timestamp - createdAt: - overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any) + createdAt: overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any) }, config ); diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index d8d19c8..bc9c9d1 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -1,10 +1,6 @@ import type { AxiosResponseHeaders } from 'axios'; import { parse } from 'cache-parser'; -import type { - AxiosCacheInstance, - CacheAxiosResponse, - CacheRequestConfig -} from '../cache/axios'; +import type { AxiosCacheInstance, CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; import type { CacheProperties } from '../cache/cache'; import { Header } from '../header/headers'; import type { CachedStorageValue } from '../storage/types'; @@ -13,9 +9,7 @@ import { updateCache } from '../util/update-cache'; import type { ResponseInterceptor } from './build'; import { createCacheResponse, isMethodIn } from './util'; -export function defaultResponseInterceptor( - axios: AxiosCacheInstance -): ResponseInterceptor { +export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseInterceptor { /** * Rejects cache for an response response. * @@ -45,7 +39,6 @@ export function defaultResponseInterceptor( throw response; } - response.id = response.config.id!; response.cached ??= false; diff --git a/src/interceptors/util.ts b/src/interceptors/util.ts index c4a565f..3f9318d 100644 --- a/src/interceptors/util.ts +++ b/src/interceptors/util.ts @@ -33,17 +33,13 @@ export interface ConfigWithCache extends CacheRequestConfig { * This function updates the cache when the request is stale. So, the next request to the * server will be made with proper header / settings. */ -export function updateStaleRequest( - cache: StaleStorageValue, - config: ConfigWithCache -): void { +export function updateStaleRequest(cache: StaleStorageValue, config: ConfigWithCache): void { config.headers ||= {}; const { etag, modifiedSince } = config.cache; if (etag) { - const etagValue = - etag === true ? (cache.data?.headers[Header.ETag] as unknown) : etag; + const etagValue = etag === true ? (cache.data?.headers[Header.ETag] as unknown) : etag; if (etagValue) { config.headers[Header.IfNoneMatch] = etagValue; diff --git a/src/storage/build.ts b/src/storage/build.ts index 341fb53..d0ebac1 100644 --- a/src/storage/build.ts +++ b/src/storage/build.ts @@ -1,20 +1,13 @@ import type { CacheRequestConfig } from '../cache/axios'; import { Header } from '../header/headers'; import type { MaybePromise } from '../util/types'; -import type { - AxiosStorage, - CachedStorageValue, - StaleStorageValue, - StorageValue -} from './types'; +import type { AxiosStorage, CachedStorageValue, StaleStorageValue, StorageValue } from './types'; /** Returns true if the provided object was created from {@link buildStorage} function. */ export const isStorage = (obj: unknown): obj is AxiosStorage => !!obj && !!(obj as Record)['is-storage']; -function hasUniqueIdentifierHeader( - value: CachedStorageValue | StaleStorageValue -): boolean { +function hasUniqueIdentifierHeader(value: CachedStorageValue | StaleStorageValue): boolean { const headers = value.data.headers; return ( @@ -102,7 +95,7 @@ export interface BuildStorage extends Omit { export function buildStorage({ set, find, remove }: BuildStorage): AxiosStorage { return { //@ts-expect-error - we don't want to expose this - ['is-storage']: 1, + 'is-storage': 1, set, remove, get: async (key, config) => { diff --git a/src/storage/memory.ts b/src/storage/memory.ts index 6cc9201..fdbce27 100644 --- a/src/storage/memory.ts +++ b/src/storage/memory.ts @@ -63,7 +63,7 @@ export function buildMemoryStorage( while (keys.length >= maxEntries) { // There's always at least one key here, otherwise it would not be // in the loop. - + delete storage.data[keys.shift()!]; } } @@ -108,9 +108,9 @@ export function buildMemoryStorage( storage.cleanup = () => { const keys = Object.keys(storage.data); - let i = -1, - value: StorageValue, - key: string; + let i = -1; + let value: StorageValue; + let key: string; // Looping forward, as older entries are more likely to be expired // than newer ones. @@ -120,7 +120,7 @@ export function buildMemoryStorage( if (value.state === 'empty') { // this storage returns void. - + storage.remove(key); continue; } @@ -128,7 +128,7 @@ export function buildMemoryStorage( // If the value is expired and can't be stale, remove it if (value.state === 'cached' && isExpired(value) && !canStale(value)) { // this storage returns void. - + storage.remove(key); } } diff --git a/src/util/key-generator.ts b/src/util/key-generator.ts index 375e84f..c02bdf9 100644 --- a/src/util/key-generator.ts +++ b/src/util/key-generator.ts @@ -45,35 +45,33 @@ export function buildKeyGenerator( }; } -export const defaultKeyGenerator = buildKeyGenerator( - ({ baseURL, url, method, params, data }) => { - // Remove trailing slashes to avoid generating different keys for the "same" final url. - if (baseURL !== undefined) { - baseURL = baseURL.replace(SLASHES_REGEX, ''); - } else { - // just to have a consistent hash - baseURL = ''; - } - - if (url !== undefined) { - url = url.replace(SLASHES_REGEX, ''); - } else { - // just to have a consistent hash - url = ''; - } - - if (method !== undefined) { - method = method.toLowerCase() as Method; - } else { - // just to have a consistent hash - method = 'get'; - } - - return { - url: baseURL + (baseURL && url ? '/' : '') + url, - params: params, - method: method, - data: data - }; +export const defaultKeyGenerator = buildKeyGenerator(({ baseURL, url, method, params, data }) => { + // Remove trailing slashes to avoid generating different keys for the "same" final url. + if (baseURL !== undefined) { + baseURL = baseURL.replace(SLASHES_REGEX, ''); + } else { + // just to have a consistent hash + baseURL = ''; } -); \ No newline at end of file + + if (url !== undefined) { + url = url.replace(SLASHES_REGEX, ''); + } else { + // just to have a consistent hash + url = ''; + } + + if (method !== undefined) { + method = method.toLowerCase() as Method; + } else { + // just to have a consistent hash + method = 'get'; + } + + return { + url: baseURL + (baseURL && url ? '/' : '') + url, + params: params, + method: method, + data: data + }; +}); diff --git a/src/util/types.ts b/src/util/types.ts index faacc5e..902d7dd 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -1,9 +1,5 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; -import type { - CachedStorageValue, - LoadingStorageValue, - StorageValue -} from '../storage/types'; +import type { CachedStorageValue, LoadingStorageValue, StorageValue } from '../storage/types'; export type CachePredicate = Exclude< CachePredicateObject | CachePredicateObject['responseMatch'], @@ -35,9 +31,7 @@ export interface CachePredicateObject { * A simple function that receives a cache request config and should return a string id * for it. */ -export type KeyGenerator = ( - options: CacheRequestConfig -) => string; +export type KeyGenerator = (options: CacheRequestConfig) => string; export type MaybePromise = T | Promise | PromiseLike; @@ -58,9 +52,7 @@ export type StaleIfErrorPredicate = error: Record ) => MaybePromise); -export type CacheUpdaterFn = ( - response: CacheAxiosResponse -) => MaybePromise; +export type CacheUpdaterFn = (response: CacheAxiosResponse) => MaybePromise; /** * A record for a custom cache updater for each specified request id. diff --git a/src/util/update-cache.ts b/src/util/update-cache.ts index ac62239..91193a4 100644 --- a/src/util/update-cache.ts +++ b/src/util/update-cache.ts @@ -9,7 +9,7 @@ export async function updateCache( cacheUpdater: CacheUpdater ): Promise { // Global cache update function. - if (typeof cacheUpdater === `function`) { + if (typeof cacheUpdater === 'function') { return cacheUpdater(data); } diff --git a/test/cache/create.test.ts b/test/cache/create.test.ts index 8d3507a..f294059 100644 --- a/test/cache/create.test.ts +++ b/test/cache/create.test.ts @@ -1,6 +1,6 @@ -import Axios from 'axios'; import assert from 'node:assert'; import { describe, it, mock } from 'node:test'; +import Axios from 'axios'; import { setupCache } from '../../src/cache/create'; describe('Axios Cache Interceptor instances', () => { diff --git a/test/header/interpreter.test.ts b/test/header/interpreter.test.ts index 68cb17c..7dbe0f9 100644 --- a/test/header/interpreter.test.ts +++ b/test/header/interpreter.test.ts @@ -10,15 +10,9 @@ describe('Header Interpreter', () => { assert.equal(defaultHeaderInterpreter({}), 'not enough headers'); - assert.equal( - defaultHeaderInterpreter({ [Header.CacheControl]: '' }), - 'not enough headers' - ); + assert.equal(defaultHeaderInterpreter({ [Header.CacheControl]: '' }), 'not enough headers'); - assert.equal( - defaultHeaderInterpreter({ ['x-random-header']: '' }), - 'not enough headers' - ); + assert.equal(defaultHeaderInterpreter({ 'x-random-header': '' }), 'not enough headers'); }); it('MaxAge=10 and Age=3', () => { diff --git a/test/interceptors/hydrate.test.ts b/test/interceptors/hydrate.test.ts index f40dc96..051d99f 100644 --- a/test/interceptors/hydrate.test.ts +++ b/test/interceptors/hydrate.test.ts @@ -73,10 +73,7 @@ describe('Hydrate handling', () => { }); it('Only hydrates when stale while revalidate is set', async () => { - const axios = mockAxios( - {}, - { [Header.CacheControl]: 'max-age=0, stale-while-revalidate=0' } - ); + const axios = mockAxios({}, { [Header.CacheControl]: 'max-age=0, stale-while-revalidate=0' }); const id = 'some-unique-id'; const m = mock.fn(); @@ -98,10 +95,7 @@ describe('Hydrate handling', () => { }); it('Only hydrates when stale while revalidate is not expired', async () => { - const axios = mockAxios( - {}, - { [Header.CacheControl]: 'max-age=0, stale-while-revalidate=1' } - ); + const axios = mockAxios({}, { [Header.CacheControl]: 'max-age=0, stale-while-revalidate=1' }); const id = 'some-unique-id'; const m = mock.fn(); @@ -126,7 +120,7 @@ describe('Hydrate handling', () => { }); it('Hydrates when force stale', async () => { - const axios = mockAxios({}, { [Header.CacheControl]: `max-age=100` }); + const axios = mockAxios({}, { [Header.CacheControl]: 'max-age=100' }); const id = 'some-unique-id'; const m = mock.fn(); diff --git a/test/interceptors/request.test.ts b/test/interceptors/request.test.ts index 9476a2a..fa1252b 100644 --- a/test/interceptors/request.test.ts +++ b/test/interceptors/request.test.ts @@ -1,11 +1,8 @@ -import type { AxiosAdapter, AxiosResponse } from 'axios'; import assert from 'node:assert'; import { describe, it, mock } from 'node:test'; +import type { AxiosAdapter, AxiosResponse } from 'axios'; import { setTimeout } from 'timers/promises'; -import type { - CacheRequestConfig, - InternalCacheRequestConfig -} from '../../src/cache/axios'; +import type { CacheRequestConfig, InternalCacheRequestConfig } from '../../src/cache/axios'; import { Header } from '../../src/header/headers'; import type { LoadingStorageValue } from '../../src/storage/types'; import { mockAxios } from '../mocks/axios'; @@ -100,10 +97,7 @@ describe('Request Interceptor', () => { }); it('Cache expiration', async () => { - const axios = mockAxios( - {}, - { [Header.CacheControl]: 'max-age=1,stale-while-revalidate=10' } - ); + const axios = mockAxios({}, { [Header.CacheControl]: 'max-age=1,stale-while-revalidate=10' }); await axios.get('http://test.com', { cache: { interpretHeader: true } }); @@ -240,9 +234,7 @@ describe('Request Interceptor', () => { adapter: async (config: InternalCacheRequestConfig) => { await setTimeout(150); - const response = (await (axios.defaults.adapter as AxiosAdapter)( - config - )) as AxiosResponse; + const response = (await (axios.defaults.adapter as AxiosAdapter)(config)) as AxiosResponse; // Changes the response to be different from `true` (default) response.data = 'overridden response'; @@ -347,7 +339,7 @@ describe('Request Interceptor', () => { assert.deepEqual(Object.assign({}, req1.request.config.headers), { [Header.CacheControl]: 'no-cache', - ['Accept']: 'application/json, text/plain, */*', + Accept: 'application/json, text/plain, */*', 'Content-Type': undefined, [Header.Pragma]: 'no-cache', [Header.Expires]: '0' diff --git a/test/interceptors/response.test.ts b/test/interceptors/response.test.ts index e05fca7..87a502f 100644 --- a/test/interceptors/response.test.ts +++ b/test/interceptors/response.test.ts @@ -1,7 +1,7 @@ -import Axios from 'axios'; import assert from 'node:assert'; import { describe, it, mock } from 'node:test'; import { setImmediate } from 'node:timers/promises'; +import Axios from 'axios'; import { setupCache } from '../../src/cache/create'; import { Header } from '../../src/header/headers'; import { XMockRandom, mockAxios } from '../mocks/axios'; @@ -92,10 +92,7 @@ describe('Response Interceptor', () => { assert.equal(resultNoCache.cached, false); - const axiosCache = mockAxios( - {}, - { [Header.CacheControl]: `max-age=${60 * 60 * 24 * 365}` } - ); + const axiosCache = mockAxios({}, { [Header.CacheControl]: `max-age=${60 * 60 * 24 * 365}` }); // Make first request to cache it await axiosCache.get('http://test.com', { cache: { interpretHeader: true } }); diff --git a/test/interceptors/stale-if-error.test.ts b/test/interceptors/stale-if-error.test.ts index 9edf61f..611151b 100644 --- a/test/interceptors/stale-if-error.test.ts +++ b/test/interceptors/stale-if-error.test.ts @@ -1,6 +1,6 @@ -import Axios, { AxiosError } from 'axios'; import assert from 'node:assert'; import { describe, it } from 'node:test'; +import Axios, { AxiosError } from 'axios'; import { setupCache } from '../../src/cache/create'; import { Header } from '../../src/header/headers'; import { mockAxios } from '../mocks/axios'; @@ -406,7 +406,7 @@ describe('StaleIfError handling', () => { // https://github.com/arthurfiorette/axios-cache-interceptor/issues/685 it('ensure failed responses always cleans up waiting promise', async () => { - const axios = mockAxios({ staleIfError: false, ttl: -1 }); + const axios = mockAxios({ staleIfError: false, ttl: -1 }); axios.defaults.adapter = async (config) => { if (config.params?.fail) { @@ -431,7 +431,7 @@ describe('StaleIfError handling', () => { const id = 'arthurfiorette/axios-cache-interceptor#685'; - let data = await axios.get('url', { id }); + const data = await axios.get('url', { id }); assert.equal(data.cached, false); diff --git a/test/interceptors/util.test.ts b/test/interceptors/util.test.ts index 3f7d5be..8da9122 100644 --- a/test/interceptors/util.test.ts +++ b/test/interceptors/util.test.ts @@ -1,6 +1,6 @@ -import Axios from 'axios'; import assert from 'node:assert'; import { describe, it } from 'node:test'; +import Axios from 'axios'; import { createValidateStatus, isMethodIn } from '../../src/interceptors/util'; import { mockAxios } from '../mocks/axios'; diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index f30a86d..9abf8b3 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -1,5 +1,5 @@ -import Axios, { AxiosError } from 'axios'; import { setTimeout } from 'node:timers/promises'; +import Axios, { AxiosError } from 'axios'; import type { AxiosCacheInstance } from '../../src/cache/axios'; import { CacheOptions, setupCache } from '../../src/cache/create'; import { Header } from '../../src/header/headers'; diff --git a/test/storage/memory.test.ts b/test/storage/memory.test.ts index b60f757..1c3aba3 100644 --- a/test/storage/memory.test.ts +++ b/test/storage/memory.test.ts @@ -30,7 +30,7 @@ describe('MemoryStorage', () => { assert.equal(result.data.data, 'data'); // Deletes the value - delete result.data.data; + result.data.data = undefined; // Check if the value has been modified const result2 = await storage.get('key'); @@ -58,10 +58,10 @@ describe('MemoryStorage', () => { data.data = 'another data'; - assert.notEqual(storage.data['key'], null); - assert.equal(storage.data['key']!.state, 'cached'); - assert.notEqual(storage.data['key']!.data, null); - assert.equal(storage.data['key']!.data!.data, 'data'); + assert.notEqual(storage.data.key, null); + assert.equal(storage.data.key!.state, 'cached'); + assert.notEqual(storage.data.key!.data, null); + assert.equal(storage.data.key!.data!.data, 'data'); const result = (await storage.get('key')) as CachedStorageValue; @@ -118,12 +118,12 @@ describe('MemoryStorage', () => { }); // Ensure that the values are still there - assert.equal(storage.data['empty']?.state, 'empty'); - assert.equal(storage.data['stale']?.state, 'stale'); - assert.equal(storage.data['expiredStale']?.state, 'stale'); - assert.equal(storage.data['loading']?.state, 'loading'); - assert.equal(storage.data['cached']?.state, 'cached'); - assert.equal(storage.data['expiredCache']?.state, 'cached'); + assert.equal(storage.data.empty?.state, 'empty'); + assert.equal(storage.data.stale?.state, 'stale'); + assert.equal(storage.data.expiredStale?.state, 'stale'); + assert.equal(storage.data.loading?.state, 'loading'); + assert.equal(storage.data.cached?.state, 'cached'); + assert.equal(storage.data.expiredCache?.state, 'cached'); // Waits for the cleanup function to run await mockDateNow(600); @@ -157,9 +157,9 @@ describe('MemoryStorage', () => { }); assert.equal(Object.keys(storage.data).length, 2); - assert.ok(storage.data['key']); - assert.ok(storage.data['key2']); - assert.equal(storage.data['key3'], undefined); + assert.ok(storage.data.key); + assert.ok(storage.data.key2); + assert.equal(storage.data.key3, undefined); await storage.set('key3', { state: 'cached', @@ -170,9 +170,9 @@ describe('MemoryStorage', () => { assert.equal(Object.keys(storage.data).length, 2); - assert.equal(storage.data['key'], undefined); - assert.ok(storage.data['key2']); - assert.ok(storage.data['key3']); + assert.equal(storage.data.key, undefined); + assert.ok(storage.data.key2); + assert.ok(storage.data.key3); }); it('tests maxEntries with cleanup', async () => { @@ -200,10 +200,10 @@ describe('MemoryStorage', () => { }); assert.equal(Object.keys(storage.data).length, 3); - assert.ok(storage.data['exp']); + assert.ok(storage.data.exp); assert.ok(storage.data['not exp']); - assert.ok(storage.data['exp2']); - assert.equal(storage.data['key'], undefined); + assert.ok(storage.data.exp2); + assert.equal(storage.data.key, undefined); await storage.set('key', { state: 'cached', @@ -214,9 +214,9 @@ describe('MemoryStorage', () => { assert.equal(Object.keys(storage.data).length, 2); - assert.equal(storage.data['exp'], undefined); - assert.equal(storage.data['exp2'], undefined); + assert.equal(storage.data.exp, undefined); + assert.equal(storage.data.exp2, undefined); assert.ok(storage.data['not exp']); - assert.ok(storage.data['key']); + assert.ok(storage.data.key); }); }); diff --git a/test/storage/storages.test.ts b/test/storage/storages.test.ts index e9bc2d3..537c4a0 100644 --- a/test/storage/storages.test.ts +++ b/test/storage/storages.test.ts @@ -1,6 +1,6 @@ -import { Axios } from 'axios'; import assert from 'node:assert'; import { describe, it } from 'node:test'; +import { Axios } from 'axios'; import { buildStorage, canStale, isStorage } from '../../src/storage/build'; import { buildMemoryStorage } from '../../src/storage/memory'; import type { AxiosStorage, StorageValue } from '../../src/storage/types'; diff --git a/test/util/cache-predicate.test.ts b/test/util/cache-predicate.test.ts index 1ce6e89..64b894c 100644 --- a/test/util/cache-predicate.test.ts +++ b/test/util/cache-predicate.test.ts @@ -127,10 +127,7 @@ describe('CachePredicate', () => { ); assert.ok( - await testCachePredicate( - response, - ({ data }) => data && data.a === true && data.b === 1 - ) + await testCachePredicate(response, ({ data }) => data && data.a === true && data.b === 1) ); assert.equal( @@ -224,10 +221,7 @@ describe('CachePredicate', () => { } }, update: { - id: ( - _, - { data: { a, b }, headers, status, statusText } - ): CachedStorageValue => { + id: (_, { data: { a, b }, headers, status, statusText }): CachedStorageValue => { return { state: 'cached', ttl: Number.MAX_SAFE_INTEGER, diff --git a/test/util/key-generator.test.ts b/test/util/key-generator.test.ts index 9766a6a..c0dff39 100644 --- a/test/util/key-generator.test.ts +++ b/test/util/key-generator.test.ts @@ -101,10 +101,7 @@ describe('KeyGeneration', () => { ]; for (const [first, second] of groups) { - assert.equal( - defaultKeyGenerator({ url: first }), - defaultKeyGenerator({ url: second }) - ); + assert.equal(defaultKeyGenerator({ url: first }), defaultKeyGenerator({ url: second })); assert.equal( defaultKeyGenerator({ baseURL: first }), diff --git a/test/utils.ts b/test/utils.ts index 2fbaf00..1f5552d 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,5 +1,5 @@ -import { AxiosHeaders } from 'axios'; import { mock } from 'node:test'; +import { AxiosHeaders } from 'axios'; import type { CacheAxiosResponse } from '../src/cache/axios'; export const EMPTY_RESPONSE = Object.freeze({ @@ -9,9 +9,7 @@ export const EMPTY_RESPONSE = Object.freeze({ data: true }); -export function createResponse( - config: Partial> -): CacheAxiosResponse { +export function createResponse(config: Partial>): CacheAxiosResponse { return { ...EMPTY_RESPONSE, config: { headers: new AxiosHeaders() },