mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
fix: typing issues with axios >= 11.7.8 (#953)
This commit is contained in:
parent
3031fd1134
commit
ff5589f9fb
@ -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` |
|
||||
|
||||
6
src/cache/axios.ts
vendored
6
src/cache/axios.ts
vendored
@ -123,8 +123,10 @@ export interface AxiosCacheInstance extends CacheInstance, AxiosInstance {
|
||||
};
|
||||
|
||||
interceptors: {
|
||||
request: AxiosInterceptorManager<InternalCacheRequestConfig>;
|
||||
response: AxiosInterceptorManager<CacheAxiosResponse>;
|
||||
request: AxiosInterceptorManager<InternalCacheRequestConfig<unknown, unknown>>;
|
||||
response: AxiosInterceptorManager<
|
||||
Partial<CacheAxiosResponse<unknown, unknown>> & AxiosResponse<unknown, unknown>
|
||||
>;
|
||||
};
|
||||
|
||||
/** @template D The type that the request body use */
|
||||
|
||||
10
src/cache/cache.ts
vendored
10
src/cache/cache.ts
vendored
@ -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<CacheRequestConfig<unknown, unknown>>;
|
||||
requestInterceptor: AxiosInterceptor<InternalCacheRequestConfig<unknown, unknown>>;
|
||||
|
||||
/**
|
||||
* 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<CacheAxiosResponse<unknown, unknown>>;
|
||||
responseInterceptor: AxiosInterceptor<
|
||||
Partial<CacheAxiosResponse<unknown, unknown>> & AxiosResponse<unknown, unknown>
|
||||
>;
|
||||
|
||||
/**
|
||||
* The debug option will print debug information in the console. It is good if you need
|
||||
|
||||
10
src/cache/create.ts
vendored
10
src/cache/create.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import type { CacheAxiosResponse, InternalCacheRequestConfig } from '../cache/axios.js';
|
||||
import type { AxiosInterceptorManager } from 'axios';
|
||||
|
||||
/** See {@link AxiosInterceptorManager} */
|
||||
export interface AxiosInterceptor<T> {
|
||||
@ -6,8 +7,6 @@ export interface AxiosInterceptor<T> {
|
||||
|
||||
/** Returns a successful response or re-throws the error */
|
||||
onRejected?(error: Record<string, unknown>): T | Promise<T>;
|
||||
|
||||
apply: () => void;
|
||||
}
|
||||
|
||||
export type RequestInterceptor = AxiosInterceptor<InternalCacheRequestConfig<unknown, unknown>>;
|
||||
|
||||
@ -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
|
||||
};
|
||||
}
|
||||
|
||||
@ -399,7 +399,6 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI
|
||||
|
||||
return {
|
||||
onFulfilled,
|
||||
onRejected,
|
||||
apply: () => axios.interceptors.response.use(onFulfilled, onRejected)
|
||||
onRejected
|
||||
};
|
||||
}
|
||||
|
||||
@ -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<R = unknown, D = unknown> = NonNullable<
|
||||
CachePredicateObject<R, D> | CachePredicateObject<R, D>['responseMatch']
|
||||
@ -52,9 +48,7 @@ export interface CachePredicateObject<R = unknown, D = unknown> {
|
||||
* A simple function that receives a cache request config and should return a string id
|
||||
* for it.
|
||||
*/
|
||||
export type KeyGenerator<R = unknown, D = unknown> = (
|
||||
options: CacheRequestConfig<R, D>
|
||||
) => string;
|
||||
export type KeyGenerator<R = unknown, D = unknown> = (options: CacheRequestConfig<R, D>) => string;
|
||||
|
||||
export type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
|
||||
|
||||
@ -75,9 +69,7 @@ export type StaleIfErrorPredicate<R, D> =
|
||||
error: Record<string, unknown>
|
||||
) => MaybePromise<number | boolean>);
|
||||
|
||||
export type CacheUpdaterFn<R, D> = (
|
||||
response: CacheAxiosResponse<R, D>
|
||||
) => MaybePromise<void>;
|
||||
export type CacheUpdaterFn<R, D> = (response: CacheAxiosResponse<R, D>) => MaybePromise<void>;
|
||||
|
||||
/**
|
||||
* A record for a custom cache updater for each specified request id.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user