mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: more type and naming refactors
This commit is contained in:
parent
891f1d05e3
commit
b42d3f13d7
@ -151,13 +151,13 @@ To you use this cache interceptor, you can apply to an existing instance or crea
|
||||
one.
|
||||
|
||||
```js
|
||||
import { applyCache } from 'axios-cache-interceptor';
|
||||
import { useCache } from 'axios-cache-interceptor';
|
||||
|
||||
// Your axios instance
|
||||
let axios;
|
||||
|
||||
// Return the same axios instance, but with a modified Typescript type.
|
||||
axios = applyCache(axios, {
|
||||
axios = useCache(axios, {
|
||||
/* options here */
|
||||
});
|
||||
```
|
||||
|
||||
161
src/cache/axios.ts
vendored
Normal file
161
src/cache/axios.ts
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
import type {
|
||||
AxiosDefaults,
|
||||
AxiosInterceptorManager,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse
|
||||
} from 'axios';
|
||||
import type { CacheInstance, CacheProperties } from './cache';
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body was
|
||||
*/
|
||||
export type CacheAxiosResponse<R, D> = AxiosResponse<R, D> & {
|
||||
config: CacheRequestConfig<D>;
|
||||
|
||||
/**
|
||||
* The id used for this request. if config specified an id, the id
|
||||
* will be returned
|
||||
*/
|
||||
id: string;
|
||||
|
||||
/**
|
||||
* A simple boolean to check whether this request was cached or not
|
||||
*/
|
||||
cached: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Options that can be overridden per request
|
||||
*
|
||||
* @template D The type for the request body
|
||||
*/
|
||||
export type CacheRequestConfig<D> = AxiosRequestConfig<D> & {
|
||||
/**
|
||||
* An id for this request, if this request is used in cache, only
|
||||
* the last request made with this id will be returned.
|
||||
*
|
||||
* @default undefined
|
||||
*/
|
||||
id?: string;
|
||||
|
||||
/**
|
||||
* All cache options for the request.
|
||||
*
|
||||
* False means ignore everything about cache, for this request.
|
||||
*/
|
||||
cache?: false | Partial<CacheProperties>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as the AxiosInstance but with CacheRequestConfig as a config
|
||||
* type and CacheAxiosResponse as response type.
|
||||
*
|
||||
* @see Axios
|
||||
* @see CacheRequestConfig
|
||||
* @see CacheInstance
|
||||
*/
|
||||
export interface AxiosCacheInstance extends CacheInstance {
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
<R = unknown, D = any>(config: CacheRequestConfig<D>): Promise<
|
||||
CacheAxiosResponse<R, D>
|
||||
>;
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
<R = unknown, D = any>(url: string, config?: CacheRequestConfig<D>): Promise<
|
||||
CacheAxiosResponse<R, D>
|
||||
>;
|
||||
|
||||
defaults: AxiosDefaults<any> & {
|
||||
cache: CacheProperties;
|
||||
};
|
||||
|
||||
interceptors: {
|
||||
request: AxiosInterceptorManager<CacheRequestConfig<any>>;
|
||||
response: AxiosInterceptorManager<CacheAxiosResponse<never, any>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
getUri<D>(config?: CacheRequestConfig<D>): string;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
request<R = unknown, D = any>(
|
||||
config: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
get<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
delete<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
head<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
options<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
post<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
put<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body use
|
||||
*/
|
||||
patch<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
}
|
||||
117
src/axios/types.ts → src/cache/cache.ts
vendored
117
src/axios/types.ts → src/cache/cache.ts
vendored
@ -1,17 +1,11 @@
|
||||
import type {
|
||||
AxiosDefaults,
|
||||
AxiosInterceptorManager,
|
||||
AxiosPromise,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
Method
|
||||
} from 'axios';
|
||||
import type { Method } from 'axios';
|
||||
import type { Deferred } from 'typed-core/dist/promises/deferred';
|
||||
import type { HeaderInterpreter } from '../header/types';
|
||||
import type { AxiosInterceptor } from '../interceptors/types';
|
||||
import type { CachedResponse, CacheStorage } from '../storage/types';
|
||||
import type { CachePredicate, KeyGenerator } from '../util/types';
|
||||
import type { CacheUpdater } from '../util/update-cache';
|
||||
import type { CacheAxiosResponse, CacheRequestConfig } from './axios';
|
||||
|
||||
export type CacheProperties = {
|
||||
/**
|
||||
@ -65,47 +59,6 @@ export type CacheProperties = {
|
||||
update: Record<string, CacheUpdater>;
|
||||
};
|
||||
|
||||
/**
|
||||
* @template R The type returned by this response
|
||||
* @template D The type that the request body was
|
||||
*/
|
||||
export type CacheAxiosResponse<R, D> = AxiosResponse<R, D> & {
|
||||
config: CacheRequestConfig<D>;
|
||||
|
||||
/**
|
||||
* The id used for this request. if config specified an id, the id
|
||||
* will be returned
|
||||
*/
|
||||
id: string;
|
||||
|
||||
/**
|
||||
* A simple boolean to check whether this request was cached or not
|
||||
*/
|
||||
cached: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Options that can be overridden per request
|
||||
*
|
||||
* @template D The type for the request body
|
||||
*/
|
||||
export type CacheRequestConfig<D> = AxiosRequestConfig<D> & {
|
||||
/**
|
||||
* An id for this request, if this request is used in cache, only
|
||||
* the last request made with this id will be returned.
|
||||
*
|
||||
* @default undefined
|
||||
*/
|
||||
id?: string;
|
||||
|
||||
/**
|
||||
* All cache options for the request.
|
||||
*
|
||||
* False means ignore everything about cache, for this request.
|
||||
*/
|
||||
cache?: false | Partial<CacheProperties>;
|
||||
};
|
||||
|
||||
export interface CacheInstance {
|
||||
/**
|
||||
* The storage to save the cache data.
|
||||
@ -146,69 +99,3 @@ export interface CacheInstance {
|
||||
*/
|
||||
responseInterceptor: AxiosInterceptor<CacheAxiosResponse<unknown, any>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as the AxiosInstance but with CacheRequestConfig as a config
|
||||
* type and CacheAxiosResponse as response type.
|
||||
*
|
||||
* @see Axios
|
||||
* @see CacheRequestConfig
|
||||
* @see CacheInstance
|
||||
*/
|
||||
export interface AxiosCacheInstance extends CacheInstance {
|
||||
<T>(config: CacheRequestConfig<T>): AxiosPromise;
|
||||
<T>(url: string, config?: CacheRequestConfig<T>): AxiosPromise;
|
||||
|
||||
defaults: AxiosDefaults<any> & {
|
||||
cache: CacheProperties;
|
||||
};
|
||||
|
||||
interceptors: {
|
||||
request: AxiosInterceptorManager<CacheRequestConfig<any>>;
|
||||
response: AxiosInterceptorManager<CacheAxiosResponse<never, any>>;
|
||||
};
|
||||
|
||||
getUri<T>(config?: CacheRequestConfig<T>): string;
|
||||
|
||||
request<R = unknown, D = any>(
|
||||
config: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
get<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
delete<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
head<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
options<R = unknown, D = any>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
post<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
put<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
|
||||
patch<R = unknown, D = any>(
|
||||
url: string,
|
||||
data?: D,
|
||||
config?: CacheRequestConfig<D>
|
||||
): Promise<CacheAxiosResponse<R, D>>;
|
||||
}
|
||||
13
src/axios/cache.ts → src/cache/create.ts
vendored
13
src/axios/cache.ts → src/cache/create.ts
vendored
@ -4,7 +4,8 @@ import { CacheRequestInterceptor } from '../interceptors/request';
|
||||
import { CacheResponseInterceptor } from '../interceptors/response';
|
||||
import { MemoryStorage } from '../storage/memory';
|
||||
import { defaultKeyGenerator } from '../util/key-generator';
|
||||
import type { AxiosCacheInstance, CacheInstance, CacheProperties } from './types';
|
||||
import type { AxiosCacheInstance } from './axios';
|
||||
import type { CacheInstance, CacheProperties } from './cache';
|
||||
|
||||
/**
|
||||
* Apply the caching interceptors for a already created axios instance.
|
||||
@ -13,7 +14,7 @@ import type { AxiosCacheInstance, CacheInstance, CacheProperties } from './types
|
||||
* @param config The config for the caching interceptors
|
||||
* @returns The same instance but with caching enabled
|
||||
*/
|
||||
export function applyCache(
|
||||
export function useCache(
|
||||
axios: AxiosInstance,
|
||||
{
|
||||
storage,
|
||||
@ -23,7 +24,7 @@ export function applyCache(
|
||||
requestInterceptor,
|
||||
responseInterceptor,
|
||||
...cacheOptions
|
||||
}: CreateCacheOptions['cache'] = {}
|
||||
}: CacheOptions = {}
|
||||
): AxiosCacheInstance {
|
||||
const axiosCache = axios as AxiosCacheInstance;
|
||||
|
||||
@ -68,10 +69,12 @@ export function createCache({
|
||||
axios,
|
||||
cache
|
||||
}: CreateCacheOptions = {}): AxiosCacheInstance {
|
||||
return applyCache(Axios.create(axios), cache);
|
||||
return useCache(Axios.create(axios), cache);
|
||||
}
|
||||
|
||||
export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;
|
||||
|
||||
export type CreateCacheOptions = {
|
||||
axios?: Partial<AxiosRequestConfig>;
|
||||
cache?: Partial<CacheInstance> & Partial<CacheProperties>;
|
||||
cache?: CacheOptions;
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
export * from './axios/cache';
|
||||
export * from './axios/types';
|
||||
export * from './cache/axios';
|
||||
export * from './cache/cache';
|
||||
export * from './cache/create';
|
||||
export * from './header/types';
|
||||
export * from './interceptors/types';
|
||||
export * from './storage/types';
|
||||
|
||||
@ -3,7 +3,7 @@ import type {
|
||||
AxiosCacheInstance,
|
||||
CacheAxiosResponse,
|
||||
CacheRequestConfig
|
||||
} from '../axios/types';
|
||||
} from '../cache/axios';
|
||||
import type {
|
||||
CachedResponse,
|
||||
CachedStorageValue,
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import { extract } from 'typed-core/dist/core/object';
|
||||
import type {
|
||||
AxiosCacheInstance,
|
||||
CacheAxiosResponse,
|
||||
CacheProperties
|
||||
} from '../axios/types';
|
||||
import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios';
|
||||
import type { CacheProperties } from '../cache/cache';
|
||||
import type { CachedStorageValue } from '../storage/types';
|
||||
import { checkPredicateObject } from '../util/cache-predicate';
|
||||
import { updateCache } from '../util/update-cache';
|
||||
|
||||
@ -3,7 +3,7 @@ export interface AxiosInterceptor<T> {
|
||||
onRejected?(error: any): any;
|
||||
|
||||
/**
|
||||
* Should apply this interceptor to an already provided axios instance
|
||||
* Should apply this interceptor to an already provided axios instance. Does not call this method explicitly.
|
||||
*/
|
||||
use(): void;
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import type { CacheRequestConfig } from '../axios/types';
|
||||
import type { CacheRequestConfig } from '../cache/axios';
|
||||
|
||||
export type CachePredicate =
|
||||
| CachePredicateObject
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { AxiosCacheInstance, CacheProperties, createCache } from '../../src';
|
||||
import type { CacheInstance } from '../../src/axios/types';
|
||||
import type { CacheInstance } from '../../src/cache/cache';
|
||||
|
||||
export function mockAxios(
|
||||
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user