Skip to content

Commit

Permalink
feat: enable cache by creating or applying
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 19, 2021
1 parent 1f88779 commit f4bbd8b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
37 changes: 29 additions & 8 deletions src/axios/cache.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { AxiosInstance } from 'axios';
import Axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { defaultHeaderInterpreter } from '../header';
import { applyRequestInterceptor } from '../interceptors/request';
import { applyResponseInterceptor } from '../interceptors/response';
import { MemoryStorage } from '../storage/memory';
import { defaultKeyGenerator } from '../util/key-generator';
import CacheInstance, { AxiosCacheInstance, CacheProperties } from './types';

export function createCache(
/**
* Apply the caching interceptors for a already created axios instance.
*
* @param axios the already created axios instance
* @param config the config for the caching interceptors
* @returns the same instance but with caching enabled
*/
export function applyCache(
axios: AxiosInstance,
options: Partial<CacheInstance> & Partial<CacheProperties> = {}
config: Partial<CacheInstance> & Partial<CacheProperties> = {}
): AxiosCacheInstance {
const axiosCache = axios as AxiosCacheInstance;

axiosCache.storage = options.storage || new MemoryStorage();
axiosCache.generateKey = options.generateKey || defaultKeyGenerator;
axiosCache.waiting = options.waiting || {};
axiosCache.headerInterpreter = options.headerInterpreter || defaultHeaderInterpreter;
axiosCache.storage = config.storage || new MemoryStorage();
axiosCache.generateKey = config.generateKey || defaultKeyGenerator;
axiosCache.waiting = config.waiting || {};
axiosCache.headerInterpreter = config.headerInterpreter || defaultHeaderInterpreter;

// CacheRequestConfig values
axiosCache.defaults = {
Expand All @@ -26,7 +33,7 @@ export function createCache(
methods: ['get'],
cachePredicate: { statusCheck: [200, 399] },
update: {},
...options
...config
}
};

Expand All @@ -36,3 +43,17 @@ export function createCache(

return axiosCache;
}

/**
* Returns a new axios instance with caching enabled.
*
* @param config the config for the caching interceptors
* @param axiosConfig the config for the created axios instance
* @returns the same instance but with caching enabled
*/
export function createCache(
config: Partial<CacheInstance> & Partial<CacheProperties> = {},
axiosConfig: AxiosRequestConfig = {}
): AxiosCacheInstance {
return applyCache(Axios.create(axiosConfig), config);
}
15 changes: 6 additions & 9 deletions src/axios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { CachePredicate } from '../util/cache-predicate';
import { Deferred } from '../util/deferred';
import { KeyGenerator } from '../util/key-generator';

export type CacheUpdater =
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void)
| 'delete';

export type DefaultCacheRequestConfig = AxiosRequestConfig & {
cache: CacheProperties;
};
Expand Down Expand Up @@ -57,22 +61,15 @@ export type CacheProperties = {
* 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 void, the entry is deleted
* If the function returns nothing, the entry is deleted
*
* This is independent if the request made was cached or not.
*
* The id used is the same as the id on `CacheRequestConfig['id']`, auto-generated or not.
*
* @default {}
*/
update: {
[id: string]:
| 'delete'
| ((
cached: EmptyStorageValue | CachedStorageValue,
newData: any
) => CachedStorageValue | undefined);
};
update: Record<string, CacheUpdater | undefined>;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/header/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
* headers was not enough to determine a valid value. Or a `number` containing
* the number of **milliseconds** to cache the response.
*/
export type HeaderInterpreter = (headers?: Record<string, string>) => false | undefined | number;
export type HeaderInterpreter = (
headers?: Record<Lowercase<string>, string>
) => false | undefined | number;

0 comments on commit f4bbd8b

Please sign in to comment.