diff --git a/src/cache/axios.ts b/src/cache/axios.ts index 889adf5..d7953c1 100644 --- a/src/cache/axios.ts +++ b/src/cache/axios.ts @@ -12,7 +12,7 @@ import type { CacheInstance, CacheProperties } from './cache'; * @template D The type that the request body was */ export type CacheAxiosResponse = AxiosResponse & { - config: CacheRequestConfig; + config: CacheRequestConfig; /** The id used for this request. if config specified an id, the id will be returned */ id: string; @@ -24,9 +24,10 @@ export type CacheAxiosResponse = AxiosResponse & { /** * Options that can be overridden per request * + * @template R The type returned by this response * @template D The type for the request body */ -export type CacheRequestConfig = AxiosRequestConfig & { +export type CacheRequestConfig = AxiosRequestConfig & { /** * An id for this request, if this request is used in cache, only the last request made * with this id will be returned. @@ -40,7 +41,7 @@ export type CacheRequestConfig = AxiosRequestConfig & { * * False means ignore everything about cache, for this request. */ - cache?: false | Partial; + cache?: false | Partial>; }; /** @@ -58,7 +59,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { * @template D The type that the request body use */ >( - config: CacheRequestConfig + config: CacheRequestConfig ): Promise; /** * @template T The type returned by this response @@ -67,7 +68,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { */ >( url: string, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; defaults: AxiosDefaults & { @@ -75,12 +76,12 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { }; interceptors: { - request: AxiosInterceptorManager>; - response: AxiosInterceptorManager>; + request: AxiosInterceptorManager>; + response: AxiosInterceptorManager>; }; /** @template D The type that the request body use */ - getUri(config?: CacheRequestConfig): string; + getUri(config?: CacheRequestConfig): string; /** * @template T The type returned by this response @@ -88,7 +89,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { * @template D The type that the request body use */ request>( - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -98,7 +99,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { */ get>( url: string, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -108,7 +109,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { */ delete>( url: string, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -118,7 +119,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { */ head>( url: string, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -128,7 +129,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { */ options>( url: string, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -139,7 +140,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { post>( url: string, data?: D, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -150,7 +151,7 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { put>( url: string, data?: D, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; /** @@ -161,6 +162,6 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { patch>( url: string, data?: D, - config?: CacheRequestConfig + config?: CacheRequestConfig ): Promise; } diff --git a/src/cache/cache.ts b/src/cache/cache.ts index 1c5d6d1..1b050f2 100644 --- a/src/cache/cache.ts +++ b/src/cache/cache.ts @@ -3,11 +3,14 @@ import type { Deferred } from 'fast-defer'; import type { HeadersInterpreter } from '../header/types'; import type { AxiosInterceptor } from '../interceptors/types'; import type { AxiosStorage, CachedResponse } from '../storage/types'; -import type { CachePredicate, KeyGenerator } from '../util/types'; -import type { CacheUpdater } from '../util/update-cache'; +import type { CachePredicate, CacheUpdater, KeyGenerator } from '../util/types'; import type { CacheAxiosResponse, CacheRequestConfig } from './axios'; -export type CacheProperties = { +/** + * @template R The type returned by this response + * @template D The type for the request body + */ +export type CacheProperties = { /** * The time until the cached value is expired in milliseconds. * @@ -18,7 +21,7 @@ export type CacheProperties = { * * @default 1000 * 60 * 5 // 5 Minutes */ - ttl: number | ((response: CacheAxiosResponse) => number | Promise); + ttl: number | ((response: CacheAxiosResponse) => number | Promise); /** * If this interceptor should configure the cache from the request cache header When @@ -40,21 +43,21 @@ export type CacheProperties = { * * @default {statusCheck: [200, 399]} */ - cachePredicate: CachePredicate; + cachePredicate: CachePredicate; /** * Once the request is resolved, this specifies what requests should we change the * cache. Can be used to update the request or delete other caches. * - * If the function returns nothing, the entry is deleted - * * This is independent if the request made was cached or not. * + * If an provided id represents and loading cache, he will be ignored. + * * The id used is the same as the id on `CacheRequestConfig['id']`, auto-generated or not. * * @default {{}} */ - update: Record; + update: Record>; /** * If the request should handle ETag and If-None-Match support. Use a string to force a @@ -104,8 +107,8 @@ export interface CacheInstance { headerInterpreter: HeadersInterpreter; /** The request interceptor that will be used to handle the cache. */ - requestInterceptor: AxiosInterceptor>; + requestInterceptor: AxiosInterceptor>; /** The response interceptor that will be used to handle the cache. */ - responseInterceptor: AxiosInterceptor>; + responseInterceptor: AxiosInterceptor>; } diff --git a/src/cache/create.ts b/src/cache/create.ts index f4b4375..7d909e8 100644 --- a/src/cache/create.ts +++ b/src/cache/create.ts @@ -65,7 +65,7 @@ export function setupCache( axiosCache.storage = storage || buildMemoryStorage(); if (!isStorage(axiosCache.storage)) { - throw new Error('create an storage with buildStorage()'); + throw new Error('Use buildStorage()'); } axiosCache.generateKey = generateKey || defaultKeyGenerator; diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index b6ec7b2..0c3a132 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -17,8 +17,8 @@ import { setRevalidationHeaders } from './util'; -export class CacheRequestInterceptor - implements AxiosInterceptor> +export class CacheRequestInterceptor + implements AxiosInterceptor> { constructor(readonly axios: AxiosCacheInstance) {} @@ -27,8 +27,8 @@ export class CacheRequestInterceptor }; readonly onFulfilled = async ( - config: CacheRequestConfig - ): Promise> => { + config: CacheRequestConfig + ): Promise> => { if (config.cache === false) { return config; } @@ -77,7 +77,7 @@ export class CacheRequestInterceptor }); if (cache.state === 'stale') { - setRevalidationHeaders(cache, config as ConfigWithCache); + setRevalidationHeaders(cache, config as ConfigWithCache); } config.validateStatus = createValidateStatus(config.validateStatus); @@ -112,8 +112,8 @@ export class CacheRequestInterceptor * Even though the response interceptor receives this one from here, it has been * configured to ignore cached responses: true */ - Promise.resolve>({ - config: config, + Promise.resolve>({ + config, data: cachedResponse.data, headers: cachedResponse.headers, status: cachedResponse.status, diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 5ae91ee..c5250db 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -8,8 +8,8 @@ import { updateCache } from '../util/update-cache'; import type { AxiosInterceptor } from './types'; import { setupCacheData } from './util'; -export class CacheResponseInterceptor - implements AxiosInterceptor> +export class CacheResponseInterceptor + implements AxiosInterceptor> { constructor(readonly axios: AxiosCacheInstance) {} @@ -18,8 +18,8 @@ export class CacheResponseInterceptor }; readonly onFulfilled = async ( - axiosResponse: AxiosResponse - ): Promise> => { + axiosResponse: AxiosResponse + ): Promise> => { const response = this.cachedResponse(axiosResponse); // Response is already cached @@ -101,7 +101,7 @@ export class CacheResponseInterceptor // Update other entries before updating himself if (cacheConfig?.update) { - updateCache(this.axios.storage, response.data, cacheConfig.update); + updateCache(this.axios.storage, response, cacheConfig.update); } const deferred = this.axios.waiting[response.id]; @@ -126,12 +126,14 @@ export class CacheResponseInterceptor delete this.axios.waiting[key]; }; - readonly cachedResponse = (response: AxiosResponse): CacheAxiosResponse => { + readonly cachedResponse = ( + response: AxiosResponse + ): CacheAxiosResponse => { return { id: this.axios.generateKey(response.config), // The request interceptor response.cache will return true or undefined. And true only when the response was cached. - cached: (response as CacheAxiosResponse).cached || false, + cached: (response as CacheAxiosResponse).cached || false, ...response }; }; diff --git a/src/interceptors/util.ts b/src/interceptors/util.ts index 636a84e..e7d8478 100644 --- a/src/interceptors/util.ts +++ b/src/interceptors/util.ts @@ -1,6 +1,7 @@ import type { Method } from 'axios'; -import type { CachedResponse, CacheProperties, StaleStorageValue } from '..'; import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; +import type { CacheProperties } from '../cache/cache'; +import type { CachedResponse, StaleStorageValue } from '../storage/types'; import { Header } from '../util/headers'; /** @@ -28,7 +29,7 @@ export function isMethodIn(requestMethod: Method, methodList: Method[] = []): bo return false; } -export type ConfigWithCache = CacheRequestConfig & { +export type ConfigWithCache = CacheRequestConfig & { cache: Partial; };