Skip to content

Commit

Permalink
feat!: updated default caching options
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 14, 2022
1 parent eb1c3fa commit 119fa32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type CacheProperties<R = unknown, D = unknown> = {
* If this interceptor should configure the cache from the request cache header When
* used, the ttl property is ignored
*
* @default false
* @default true
*/
interpretHeader: boolean;

Expand Down Expand Up @@ -65,19 +65,19 @@ export type CacheProperties<R = unknown, D = unknown> = {
update: Record<string, CacheUpdater<R, D>>;

/**
* If the request should handle ETag and If-None-Match support. Use a string to force a
* custom value or true to use the response ETag
* If the request should handle `ETag` and `If-None-Match` support. Use a string to
* force a custom value or true to use the response ETag
*
* @default false
* @default true
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
*/
etag: string | boolean;

/**
* Use If-Modified-Since header in this request. Use a date to force a custom value or
* Use `If-Modified-Since` header in this request. Use a date to force a custom value or
* true to use the last cached timestamp. If never cached before, the header is not set.
*
* @default false
* @default false // The opposite of the resulting `etag` option.
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
*/
modifiedSince: Date | boolean;
Expand Down Expand Up @@ -105,7 +105,7 @@ export type CacheProperties<R = unknown, D = unknown> = {
* - `boolean` -> `false` disables and `true` enables with infinite time.
* - `function` -> a predicate that can return `number` or `boolean` as described above.
*
* @default false
* @default true
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#stale-if-error
*/
staleIfError: StaleIfErrorPredicate<R, D>;
Expand Down
25 changes: 19 additions & 6 deletions src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,40 @@ export function setupCache(
throw new Error('Use buildStorage() function');
}

axiosCache.generateKey = options.generateKey || defaultKeyGenerator;
axiosCache.waiting = options.waiting || {};

axiosCache.generateKey = options.generateKey || defaultKeyGenerator;

axiosCache.headerInterpreter = options.headerInterpreter || defaultHeaderInterpreter;

axiosCache.requestInterceptor =
options.requestInterceptor || defaultRequestInterceptor(axiosCache);

axiosCache.responseInterceptor =
options.responseInterceptor || defaultResponseInterceptor(axiosCache);
axiosCache.debug = options.debug;

// CacheRequestConfig values
axiosCache.defaults.cache = {
update: options.update || {},

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,
staleIfError: options.staleIfError ?? false,
update: options.update || {}

etag: options.etag ?? true,

// This option is going to be ignored by servers when ETag is enabled
// Checks strict equality to false to avoid undefined-ish values
modifiedSince: options.modifiedSince ?? options.etag === false,

interpretHeader: options.interpretHeader ?? true,

staleIfError: options.staleIfError ?? true
};

// Apply interceptors
Expand Down

0 comments on commit 119fa32

Please sign in to comment.