diff --git a/docs/src/guide/getting-started.md b/docs/src/guide/getting-started.md index 237d961..334c3a0 100644 --- a/docs/src/guide/getting-started.md +++ b/docs/src/guide/getting-started.md @@ -39,7 +39,7 @@ import Axios from 'axios'; import { setupCache } from 'axios-cache-interceptor'; const instance = Axios.create(); // [!code focus] -const axios = setupCache(instance);// [!code focus] +const axios = setupCache(instance); // [!code focus] const req1 = axios.get('https://api.example.com/'); // [!code focus] const req2 = axios.get('https://api.example.com/'); // [!code focus] @@ -55,7 +55,7 @@ const Axios = require('axios'); const { setupCache } = require('axios-cache-interceptor'); const instance = Axios.create(); // [!code focus] -const axios = setupCache(instance);// [!code focus] +const axios = setupCache(instance); // [!code focus] const req1 = axios.get('https://api.example.com/'); // [!code focus] const req2 = axios.get('https://api.example.com/'); // [!code focus] @@ -71,7 +71,7 @@ const Axios = window.axios; const { setupCache } = window.AxiosCacheInterceptor; const instance = Axios.create(); // [!code focus] -const axios = setupCache(instance);// [!code focus] +const axios = setupCache(instance); // [!code focus] const req1 = axios.get('https://api.example.com/'); // [!code focus] const req2 = axios.get('https://api.example.com/'); // [!code focus] @@ -86,9 +86,8 @@ res2.cached; // true // [!code focus] import Axios from 'https://cdn.skypack.dev/axios'; import { setupCache } from 'https://cdn.skypack.dev/axios-cache-interceptor'; - const instance = Axios.create(); // [!code focus] -const axios = setupCache(instance);// [!code focus] +const axios = setupCache(instance); // [!code focus] const req1 = axios.get('https://api.example.com/'); // [!code focus] const req2 = axios.get('https://api.example.com/'); // [!code focus] @@ -118,7 +117,8 @@ Axios and Axios Cache Interceptor v1** | [Axios](https://github.com/axios/axios/releases) | [Axios Cache Interceptor](https://github.com/arthurfiorette/axios-cache-interceptor/releases) | | ------------------------------------------------ | --------------------------------------------------------------------------------------------- | -| `>= v1.6` | `>= v1.3.0` | +| `>= v1.7.8` | `>= v1.7.0` | +| `>= v1.6` | `>= v1.3.0 && <= 1.6.2` | | `>= v1.4` | `>= v1.2.0` | | `>= v1.3.1` | `>= v1` | | `>= v0.27` | `>= v0.10.3` | diff --git a/src/cache/axios.ts b/src/cache/axios.ts index 84a889c..52fcae1 100644 --- a/src/cache/axios.ts +++ b/src/cache/axios.ts @@ -123,8 +123,10 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance { }; interceptors: { - request: AxiosInterceptorManager; - response: AxiosInterceptorManager; + request: AxiosInterceptorManager>; + response: AxiosInterceptorManager< + Partial> & AxiosResponse + >; }; /** @template D The type that the request body use */ diff --git a/src/cache/cache.ts b/src/cache/cache.ts index 7e81a08..aef4cf6 100644 --- a/src/cache/cache.ts +++ b/src/cache/cache.ts @@ -1,4 +1,4 @@ -import type { Method } from 'axios'; +import type { AxiosResponse, Method } from 'axios'; import type { Deferred } from 'fast-defer'; import type { HeaderInterpreter } from '../header/types.js'; import type { AxiosInterceptor } from '../interceptors/build.js'; @@ -15,7 +15,7 @@ import type { KeyGenerator, StaleIfErrorPredicate } from '../util/types.js'; -import type { CacheAxiosResponse, CacheRequestConfig } from './axios.js'; +import type { CacheAxiosResponse, InternalCacheRequestConfig } from './axios.js'; /** * @template R The type returned by this response @@ -314,7 +314,7 @@ export interface CacheInstance { * @default defaultRequestInterceptor * @see https://axios-cache-interceptor.js.org/config#requestinterceptor */ - requestInterceptor: AxiosInterceptor>; + requestInterceptor: AxiosInterceptor>; /** * The function that will be used to intercept the request after it is returned by the @@ -332,7 +332,9 @@ export interface CacheInstance { * @default defaultResponseInterceptor * @see https://axios-cache-interceptor.js.org/config#responseinterceptor */ - responseInterceptor: AxiosInterceptor>; + responseInterceptor: AxiosInterceptor< + Partial> & AxiosResponse + >; /** * The debug option will print debug information in the console. It is good if you need diff --git a/src/cache/create.ts b/src/cache/create.ts index 0c9a241..6719c9e 100644 --- a/src/cache/create.ts +++ b/src/cache/create.ts @@ -88,8 +88,14 @@ export function setupCache(axios: AxiosInstance, options: CacheOptions = {}): Ax }; // Apply interceptors - axiosCache.requestInterceptor.apply(); - axiosCache.responseInterceptor.apply(); + axiosCache.interceptors.request.use( + axiosCache.requestInterceptor.onFulfilled, + axiosCache.requestInterceptor.onRejected + ); + axiosCache.interceptors.response.use( + axiosCache.responseInterceptor.onFulfilled, + axiosCache.responseInterceptor.onRejected + ); return axiosCache; } diff --git a/src/interceptors/build.ts b/src/interceptors/build.ts index f813e46..eb5f18b 100644 --- a/src/interceptors/build.ts +++ b/src/interceptors/build.ts @@ -1,4 +1,5 @@ import type { CacheAxiosResponse, InternalCacheRequestConfig } from '../cache/axios.js'; +import type { AxiosInterceptorManager } from 'axios'; /** See {@link AxiosInterceptorManager} */ export interface AxiosInterceptor { @@ -6,8 +7,6 @@ export interface AxiosInterceptor { /** Returns a successful response or re-throws the error */ onRejected?(error: Record): T | Promise; - - apply: () => void; } export type RequestInterceptor = AxiosInterceptor>; diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 388e995..78e5096 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -10,7 +10,7 @@ import { updateStaleRequest } from './util.js'; -export function defaultRequestInterceptor(axios: AxiosCacheInstance) { +export function defaultRequestInterceptor(axios: AxiosCacheInstance): RequestInterceptor { const onFulfilled: RequestInterceptor['onFulfilled'] = async (config) => { config.id = axios.generateKey(config); @@ -218,7 +218,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { }); } - return onFulfilled(config); + return onFulfilled!(config); } /* c8 ignore end */ @@ -241,7 +241,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { // The deferred is rejected when the request that we are waiting rejects its cache. // In this case, we need to redo the request all over again. - return onFulfilled(config); + return onFulfilled!(config); } } else { cachedResponse = cache.data; @@ -279,7 +279,6 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { }; return { - onFulfilled, - apply: () => axios.interceptors.request.use(onFulfilled) + onFulfilled }; } diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 6f8c9ce..1d716a1 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -399,7 +399,6 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI return { onFulfilled, - onRejected, - apply: () => axios.interceptors.response.use(onFulfilled, onRejected) + onRejected }; } diff --git a/src/util/types.ts b/src/util/types.ts index 108b53b..3eb9852 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -1,9 +1,5 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios.js'; -import type { - CachedStorageValue, - LoadingStorageValue, - StorageValue -} from '../storage/types.js'; +import type { CachedStorageValue, LoadingStorageValue, StorageValue } from '../storage/types.js'; export type CachePredicate = NonNullable< CachePredicateObject | CachePredicateObject['responseMatch'] @@ -52,9 +48,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; @@ -75,9 +69,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.