Skip to content

Commit

Permalink
refactor: Header as const and renaming...
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 15, 2022
1 parent d51c622 commit e391f56
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 39 deletions.
37 changes: 14 additions & 23 deletions src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,35 @@ export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;
*/
export function setupCache(
axios: AxiosInstance,
{
storage,
generateKey,
waiting,
headerInterpreter,
requestInterceptor,
responseInterceptor,
...cacheOptions
}: CacheOptions = {}
options: CacheOptions = {}
): AxiosCacheInstance {
const axiosCache = axios as AxiosCacheInstance;

axiosCache.storage = storage || buildMemoryStorage();
axiosCache.storage = options.storage || buildMemoryStorage();

if (!isStorage(axiosCache.storage)) {
throw new Error('Use buildStorage() function');
}

axiosCache.generateKey = generateKey || defaultKeyGenerator;
axiosCache.waiting = waiting || {};
axiosCache.headerInterpreter = headerInterpreter || defaultHeaderInterpreter;
axiosCache.generateKey = options.generateKey || defaultKeyGenerator;
axiosCache.waiting = options.waiting || {};
axiosCache.headerInterpreter = options.headerInterpreter || defaultHeaderInterpreter;
axiosCache.requestInterceptor =
requestInterceptor || defaultRequestInterceptor(axiosCache);
options.requestInterceptor || defaultRequestInterceptor(axiosCache);
axiosCache.responseInterceptor =
responseInterceptor || defaultResponseInterceptor(axiosCache);
options.responseInterceptor || defaultResponseInterceptor(axiosCache);

// CacheRequestConfig values
axiosCache.defaults = {
...axios.defaults,
cache: {
ttl: 1000 * 60 * 5,
interpretHeader: false,
methods: ['get'],
cachePredicate: { statusCheck: (status) => status >= 200 && status < 400 },
etag: false,
modifiedSince: false,
update: {},
...cacheOptions
ttl: options.ttl ?? 1000 * 60 * 5,
interpretHeader: options.interpretHeader ?? false,
methods: options.methods || ['get'],
cachePredicate: options.cachePredicate || { statusCheck: (status) => status >= 200 && status < 400 },
etag: options.etag ?? false,
modifiedSince: options.modifiedSince ?? false,
update: options.update || {},
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from './header/types';
export * from './interceptors/build';
export * from './interceptors/request';
export * from './interceptors/response';
export * as InterceptorUtil from './interceptors/util';
export * from './interceptors/util';
export * from './storage/build';
export * from './storage/memory';
export * from './storage/types';
Expand Down
1 change: 0 additions & 1 deletion src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {

//Even though the response interceptor receives this one from here,
// it has been configured to ignore cached responses = true
// eslint-disable-next-line @typescript-eslint/require-await
config.adapter = (): Promise<CacheAxiosResponse<unknown, unknown>> =>
Promise.resolve({
config,
Expand Down
4 changes: 2 additions & 2 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { testCachePredicate } from '../util/cache-predicate';
import { Header } from '../util/headers';
import { updateCache } from '../util/update-cache';
import type { ResponseInterceptor } from './build';
import { setupCacheData } from './util';
import { createCacheResponse } from './util';

export function defaultResponseInterceptor(
axios: AxiosCacheInstance
Expand Down Expand Up @@ -94,7 +94,7 @@ export function defaultResponseInterceptor(
ttl = expirationTime === 'not enough headers' ? ttl : expirationTime;
}

const data = setupCacheData(response, cache.data);
const data = createCacheResponse(response, cache.data);

if (typeof ttl === 'function') {
ttl = await ttl(response);
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function setRevalidationHeaders<D>(
* Creates the new date to the cache by the provided response. Also handles possible 304
* Not Modified by updating response properties.
*/
export function setupCacheData<R, D>(
export function createCacheResponse<R, D>(
response: CacheAxiosResponse<R, D>,
cache?: CachedResponse
): CachedResponse {
Expand Down
22 changes: 11 additions & 11 deletions src/util/headers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export enum Header {
export const Header = {
/**
* ```txt
* If-Modified-Since: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
* ```
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
*/
IfModifiedSince = 'if-modified-since',
IfModifiedSince : 'if-modified-since',

/**
* ```txt
Expand All @@ -15,7 +15,7 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified
*/
LastModified = 'last-modified',
LastModified : 'last-modified',

/**
* ```txt
Expand All @@ -26,10 +26,10 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match
*/
IfNoneMatch = 'if-none-match',
IfNoneMatch : 'if-none-match',

/** @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control */
CacheControl = 'cache-control',
CacheControl : 'cache-control',

/**
* ```txt
Expand All @@ -39,7 +39,7 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
*/
ETag = 'etag',
ETag : 'etag',

/**
* ```txt
Expand All @@ -48,7 +48,7 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires
*/
Expires = 'expires',
Expires : 'expires',

/**
* ```txt
Expand All @@ -57,7 +57,7 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age
*/
Age = 'age',
Age : 'age',

/**
* ```txt
Expand All @@ -67,7 +67,7 @@ export enum Header {
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
*/
ContentType = 'content-type',
ContentType : 'content-type',

/**
* Used internally to mark the cache item as being revalidatable and enabling stale
Expand All @@ -78,7 +78,7 @@ export enum Header {
* X-Axios-Cache-Etag: "<etag_value>"
* ```
*/
XAxiosCacheEtag = 'x-axios-cache-etag',
XAxiosCacheEtag : 'x-axios-cache-etag',

/**
* Used internally to mark the cache item as being revalidatable and enabling stale
Expand All @@ -92,5 +92,5 @@ export enum Header {
* X-Axios-Cache-Last-Modified: use-cache-timestamp
* ```
*/
XAxiosCacheLastModified = 'x-axios-cache-last-modified'
XAxiosCacheLastModified : 'x-axios-cache-last-modified'
}

0 comments on commit e391f56

Please sign in to comment.