diff --git a/README.md b/README.md index 24d633f..f6b9099 100644 --- a/README.md +++ b/README.md @@ -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 */ }); ``` diff --git a/src/cache/axios.ts b/src/cache/axios.ts new file mode 100644 index 0000000..d3916ae --- /dev/null +++ b/src/cache/axios.ts @@ -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 = AxiosResponse & { + config: CacheRequestConfig; + + /** + * 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 = AxiosRequestConfig & { + /** + * 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; +}; + +/** + * 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 + */ + (config: CacheRequestConfig): Promise< + CacheAxiosResponse + >; + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + (url: string, config?: CacheRequestConfig): Promise< + CacheAxiosResponse + >; + + defaults: AxiosDefaults & { + cache: CacheProperties; + }; + + interceptors: { + request: AxiosInterceptorManager>; + response: AxiosInterceptorManager>; + }; + + /** + * @template D The type that the request body use + */ + getUri(config?: CacheRequestConfig): string; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + request( + config: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + get( + url: string, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + delete( + url: string, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + head( + url: string, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + options( + url: string, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + post( + url: string, + data?: D, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + put( + url: string, + data?: D, + config?: CacheRequestConfig + ): Promise>; + + /** + * @template R The type returned by this response + * @template D The type that the request body use + */ + patch( + url: string, + data?: D, + config?: CacheRequestConfig + ): Promise>; +} diff --git a/src/axios/types.ts b/src/cache/cache.ts similarity index 50% rename from src/axios/types.ts rename to src/cache/cache.ts index 5e10904..6f3b973 100644 --- a/src/axios/types.ts +++ b/src/cache/cache.ts @@ -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; }; -/** - * @template R The type returned by this response - * @template D The type that the request body was - */ -export type CacheAxiosResponse = AxiosResponse & { - config: CacheRequestConfig; - - /** - * 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 = AxiosRequestConfig & { - /** - * 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; -}; - export interface CacheInstance { /** * The storage to save the cache data. @@ -146,69 +99,3 @@ export interface CacheInstance { */ responseInterceptor: AxiosInterceptor>; } - -/** - * 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 { - (config: CacheRequestConfig): AxiosPromise; - (url: string, config?: CacheRequestConfig): AxiosPromise; - - defaults: AxiosDefaults & { - cache: CacheProperties; - }; - - interceptors: { - request: AxiosInterceptorManager>; - response: AxiosInterceptorManager>; - }; - - getUri(config?: CacheRequestConfig): string; - - request( - config: CacheRequestConfig - ): Promise>; - - get( - url: string, - config?: CacheRequestConfig - ): Promise>; - - delete( - url: string, - config?: CacheRequestConfig - ): Promise>; - - head( - url: string, - config?: CacheRequestConfig - ): Promise>; - - options( - url: string, - config?: CacheRequestConfig - ): Promise>; - - post( - url: string, - data?: D, - config?: CacheRequestConfig - ): Promise>; - - put( - url: string, - data?: D, - config?: CacheRequestConfig - ): Promise>; - - patch( - url: string, - data?: D, - config?: CacheRequestConfig - ): Promise>; -} diff --git a/src/axios/cache.ts b/src/cache/create.ts similarity index 87% rename from src/axios/cache.ts rename to src/cache/create.ts index cdb1f74..5d048d3 100644 --- a/src/axios/cache.ts +++ b/src/cache/create.ts @@ -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 & Partial; + export type CreateCacheOptions = { axios?: Partial; - cache?: Partial & Partial; + cache?: CacheOptions; }; diff --git a/src/index.ts b/src/index.ts index fcd7934..edc5dea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index f7a1605..7749c67 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -3,7 +3,7 @@ import type { AxiosCacheInstance, CacheAxiosResponse, CacheRequestConfig -} from '../axios/types'; +} from '../cache/axios'; import type { CachedResponse, CachedStorageValue, diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index fcc701a..3009454 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -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'; diff --git a/src/interceptors/types.ts b/src/interceptors/types.ts index cf07f86..87724ad 100644 --- a/src/interceptors/types.ts +++ b/src/interceptors/types.ts @@ -3,7 +3,7 @@ export interface AxiosInterceptor { 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; } diff --git a/src/util/types.ts b/src/util/types.ts index 51f6878..81e5a0c 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -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 diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index 0a3b264..ad30e65 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -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 & Partial = {},