mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: dependencies, lint and null assertions
This commit is contained in:
parent
156198e19f
commit
67819a3d78
@ -51,9 +51,9 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@arthurfiorette/prettier-config": "*",
|
||||
"@types/jest": "^28.1.0",
|
||||
"@types/jest": "^28.1.8",
|
||||
"@types/webpack": "^5.28.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.14.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.34.0",
|
||||
"@typescript-eslint/parser": "^5.14.0",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"axios": "^0.27.2",
|
||||
@ -64,7 +64,7 @@
|
||||
"jest": "^28.1.2",
|
||||
"jest-environment-jsdom": "^28.1.2",
|
||||
"prettier": "^2.5.1",
|
||||
"prettier-plugin-jsdoc": "^0.3.30",
|
||||
"prettier-plugin-jsdoc": "^0.4.2",
|
||||
"prettier-plugin-organize-imports": "^3.0.0",
|
||||
"ts-jest": "^28.0.1",
|
||||
"ts-loader": "^9.2.6",
|
||||
|
||||
12
src/cache/cache.ts
vendored
12
src/cache/cache.ts
vendored
@ -19,7 +19,8 @@ export type CacheProperties<R = unknown, D = unknown> = {
|
||||
/**
|
||||
* The time until the cached value is expired in milliseconds.
|
||||
*
|
||||
* If a function is used, it will receive the complete response and waits to return a TTL value
|
||||
* If a function is used, it will receive the complete response and waits to return a
|
||||
* TTL value
|
||||
*
|
||||
* When using `interpretHeader: true`, this value will only be used if the interpreter
|
||||
* can't determine their TTL value to override this
|
||||
@ -58,7 +59,8 @@ export type CacheProperties<R = unknown, D = unknown> = {
|
||||
*
|
||||
* If an provided id represents and loading cache, he will be ignored.
|
||||
*
|
||||
* The id used is the same as the id on `CacheRequestConfig['id']`, auto-generated or not.
|
||||
* The id used is the same as the id on `CacheRequestConfig['id']`, auto-generated or
|
||||
* not.
|
||||
*
|
||||
* **Using a function instead of an object is supported but not recommended, as it's
|
||||
* better to just consume the response normally and write your own code after it. But
|
||||
@ -79,7 +81,8 @@ export type CacheProperties<R = unknown, D = unknown> = {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* true to use the last cached timestamp. If never cached before, the header is not
|
||||
* set.
|
||||
*
|
||||
* @default false // The opposite of the resulting `etag` option.
|
||||
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
|
||||
@ -116,7 +119,8 @@ export type CacheProperties<R = unknown, D = unknown> = {
|
||||
|
||||
/**
|
||||
* This option bypasses the current cache and always make a new http request. This will
|
||||
* not delete the current cache, it will just replace the cache when the response arrives.
|
||||
* not delete the current cache, it will just replace the cache when the response
|
||||
* arrives.
|
||||
*
|
||||
* Unlike as `cache: false`, this will not disable the cache, it will just ignore the
|
||||
* pre-request cache checks before making the request. This way, all post-request
|
||||
|
||||
@ -84,10 +84,11 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
axios.waiting[key] = deferred();
|
||||
|
||||
/**
|
||||
* Add a default reject handler to catch when the request is aborted without others
|
||||
* waiting for it.
|
||||
* Adds a default reject handler to catch when the request gets aborted without
|
||||
* others waiting for it.
|
||||
*/
|
||||
axios.waiting[key]?.catch(() => undefined);
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
axios.waiting[key]!.catch(() => undefined);
|
||||
|
||||
await axios.storage.set(
|
||||
key,
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import type { CacheProperties } from '..';
|
||||
import type {
|
||||
AxiosCacheInstance,
|
||||
CacheAxiosResponse,
|
||||
CacheRequestConfig
|
||||
} from '../cache/axios';
|
||||
import type { CacheProperties } from '../cache/cache';
|
||||
import { Header } from '../header/headers';
|
||||
import type { CachedStorageValue } from '../storage/types';
|
||||
import { testCachePredicate } from '../util/cache-predicate';
|
||||
@ -20,10 +20,10 @@ export function defaultResponseInterceptor(
|
||||
* Also update the waiting list for this key by rejecting it.
|
||||
*/
|
||||
const rejectResponse = async (responseId: string, config: CacheRequestConfig) => {
|
||||
// Update the cache to empty to prevent infinite loading state
|
||||
// Updates the cache to empty to prevent infinite loading state
|
||||
await axios.storage.remove(responseId, config);
|
||||
|
||||
// Reject the deferred if present
|
||||
// Rejects the deferred, if present
|
||||
axios.waiting[responseId]?.reject();
|
||||
|
||||
delete axios.waiting[responseId];
|
||||
|
||||
@ -25,7 +25,10 @@ export function canStale(value: CachedStorageValue): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/** Checks if the provided cache is expired. You should also check if the cache {@link canStale} */
|
||||
/**
|
||||
* Checks if the provided cache is expired. You should also check if the cache
|
||||
* {@link canStale}
|
||||
*/
|
||||
export function isExpired(value: CachedStorageValue): boolean {
|
||||
return value.createdAt + value.ttl <= Date.now();
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ import { buildStorage } from './build';
|
||||
import type { AxiosStorage, StorageValue } from './types';
|
||||
|
||||
/**
|
||||
* Modern function to natively deep clone
|
||||
* Modern function to natively deep clone.
|
||||
*
|
||||
* @link https://caniuse.com/mdn-api_structuredclone (07/03/2022 -> 59.4%)
|
||||
*/
|
||||
|
||||
@ -27,7 +27,10 @@ export type StaleStorageValue = {
|
||||
|
||||
export type CachedStorageValue = {
|
||||
data: CachedResponse;
|
||||
/** The number in milliseconds to wait after createdAt before the value is considered stale. */
|
||||
/**
|
||||
* The number in milliseconds to wait after createdAt before the value is considered
|
||||
* stale.
|
||||
*/
|
||||
ttl: number;
|
||||
createdAt: number;
|
||||
state: 'cached';
|
||||
@ -100,9 +103,11 @@ export type AxiosStorage = {
|
||||
remove: (key: string, currentRequest?: CacheRequestConfig) => MaybePromise<void>;
|
||||
|
||||
/**
|
||||
* Returns the value for the given key. This method make checks for cache invalidation or etc.
|
||||
* Returns the value for the given key. This method make checks for cache invalidation
|
||||
* or etc.
|
||||
*
|
||||
* If the provided `find()` method returned null, this will map it to a `'empty'` storage value.
|
||||
* If the provided `find()` method returned null, this will map it to a `'empty'`
|
||||
* storage value.
|
||||
*
|
||||
* @param key The key to look for
|
||||
* @param currentRequest The current {@link CacheRequestConfig}, if any
|
||||
|
||||
@ -28,7 +28,10 @@ export type CachePredicateObject<R = unknown, D = unknown> = {
|
||||
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>;
|
||||
};
|
||||
|
||||
/** A simple function that receives a cache request config and should return a string id for it. */
|
||||
/**
|
||||
* A simple function that receives a cache request config and should return a string id
|
||||
* for it.
|
||||
*/
|
||||
export type KeyGenerator<R = unknown, D = unknown> = (
|
||||
options: CacheRequestConfig<R, D>
|
||||
) => string;
|
||||
|
||||
88
yarn.lock
88
yarn.lock
@ -846,10 +846,10 @@
|
||||
dependencies:
|
||||
"@types/istanbul-lib-report" "*"
|
||||
|
||||
"@types/jest@^28.1.0":
|
||||
version "28.1.7"
|
||||
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-28.1.7.tgz#a680c5d05b69634c2d54a63cb106d7fb1adaba16"
|
||||
integrity sha512-acDN4VHD40V24tgu0iC44jchXavRNVFXQ/E6Z5XNsswgoSO/4NgsXoEYmPUGookKldlZQyIpmrEXsHI9cA3ZTA==
|
||||
"@types/jest@^28.1.8":
|
||||
version "28.1.8"
|
||||
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-28.1.8.tgz#6936409f3c9724ea431efd412ea0238a0f03b09b"
|
||||
integrity sha512-8TJkV++s7B6XqnDrzR1m/TT0A0h948Pnl/097veySPN67VRAgQ4gZ7n2KfJo2rVq6njQjdxU3GCCyDvAeuHoiw==
|
||||
dependencies:
|
||||
expect "^28.0.0"
|
||||
pretty-format "^28.0.0"
|
||||
@ -931,14 +931,14 @@
|
||||
dependencies:
|
||||
"@types/yargs-parser" "*"
|
||||
|
||||
"@typescript-eslint/eslint-plugin@^5.14.0":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.33.1.tgz#c0a480d05211660221eda963cc844732fe9b1714"
|
||||
integrity sha512-S1iZIxrTvKkU3+m63YUOxYPKaP+yWDQrdhxTglVDVEVBf+aCSw85+BmJnyUaQQsk5TXFG/LpBu9fa+LrAQ91fQ==
|
||||
"@typescript-eslint/eslint-plugin@^5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.34.0.tgz#d690f60e335596f38b01792e8f4b361d9bd0cb35"
|
||||
integrity sha512-eRfPPcasO39iwjlUAMtjeueRGuIrW3TQ9WseIDl7i5UWuFbf83yYaU7YPs4j8+4CxUMIsj1k+4kV+E+G+6ypDQ==
|
||||
dependencies:
|
||||
"@typescript-eslint/scope-manager" "5.33.1"
|
||||
"@typescript-eslint/type-utils" "5.33.1"
|
||||
"@typescript-eslint/utils" "5.33.1"
|
||||
"@typescript-eslint/scope-manager" "5.34.0"
|
||||
"@typescript-eslint/type-utils" "5.34.0"
|
||||
"@typescript-eslint/utils" "5.34.0"
|
||||
debug "^4.3.4"
|
||||
functional-red-black-tree "^1.0.1"
|
||||
ignore "^5.2.0"
|
||||
@ -956,14 +956,6 @@
|
||||
"@typescript-eslint/typescript-estree" "5.34.0"
|
||||
debug "^4.3.4"
|
||||
|
||||
"@typescript-eslint/scope-manager@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.33.1.tgz#8d31553e1b874210018ca069b3d192c6d23bc493"
|
||||
integrity sha512-8ibcZSqy4c5m69QpzJn8XQq9NnqAToC8OdH/W6IXPXv83vRyEDPYLdjAlUx8h/rbusq6MkW4YdQzURGOqsn3CA==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "5.33.1"
|
||||
"@typescript-eslint/visitor-keys" "5.33.1"
|
||||
|
||||
"@typescript-eslint/scope-manager@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.34.0.tgz#14efd13dc57602937e25f188fd911f118781e527"
|
||||
@ -972,38 +964,20 @@
|
||||
"@typescript-eslint/types" "5.34.0"
|
||||
"@typescript-eslint/visitor-keys" "5.34.0"
|
||||
|
||||
"@typescript-eslint/type-utils@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.33.1.tgz#1a14e94650a0ae39f6e3b77478baff002cec4367"
|
||||
integrity sha512-X3pGsJsD8OiqhNa5fim41YtlnyiWMF/eKsEZGsHID2HcDqeSC5yr/uLOeph8rNF2/utwuI0IQoAK3fpoxcLl2g==
|
||||
"@typescript-eslint/type-utils@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.34.0.tgz#7a324ab9ddd102cd5e1beefc94eea6f3eb32d32d"
|
||||
integrity sha512-Pxlno9bjsQ7hs1pdWRUv9aJijGYPYsHpwMeCQ/Inavhym3/XaKt1ZKAA8FIw4odTBfowBdZJDMxf2aavyMDkLg==
|
||||
dependencies:
|
||||
"@typescript-eslint/utils" "5.33.1"
|
||||
"@typescript-eslint/utils" "5.34.0"
|
||||
debug "^4.3.4"
|
||||
tsutils "^3.21.0"
|
||||
|
||||
"@typescript-eslint/types@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.33.1.tgz#3faef41793d527a519e19ab2747c12d6f3741ff7"
|
||||
integrity sha512-7K6MoQPQh6WVEkMrMW5QOA5FO+BOwzHSNd0j3+BlBwd6vtzfZceJ8xJ7Um2XDi/O3umS8/qDX6jdy2i7CijkwQ==
|
||||
|
||||
"@typescript-eslint/types@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.34.0.tgz#217bf08049e9e7b86694d982e88a2c1566330c78"
|
||||
integrity sha512-49fm3xbbUPuzBIOcy2CDpYWqy/X7VBkxVN+DC21e0zIm3+61Z0NZi6J9mqPmSW1BDVk9FIOvuCFyUPjXz93sjA==
|
||||
|
||||
"@typescript-eslint/typescript-estree@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.33.1.tgz#a573bd360790afdcba80844e962d8b2031984f34"
|
||||
integrity sha512-JOAzJ4pJ+tHzA2pgsWQi4804XisPHOtbvwUyqsuuq8+y5B5GMZs7lI1xDWs6V2d7gE/Ez5bTGojSK12+IIPtXA==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "5.33.1"
|
||||
"@typescript-eslint/visitor-keys" "5.33.1"
|
||||
debug "^4.3.4"
|
||||
globby "^11.1.0"
|
||||
is-glob "^4.0.3"
|
||||
semver "^7.3.7"
|
||||
tsutils "^3.21.0"
|
||||
|
||||
"@typescript-eslint/typescript-estree@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.34.0.tgz#ba7b83f4bf8ccbabf074bbf1baca7a58de3ccb9a"
|
||||
@ -1017,26 +991,18 @@
|
||||
semver "^7.3.7"
|
||||
tsutils "^3.21.0"
|
||||
|
||||
"@typescript-eslint/utils@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.33.1.tgz#171725f924fe1fe82bb776522bb85bc034e88575"
|
||||
integrity sha512-uphZjkMaZ4fE8CR4dU7BquOV6u0doeQAr8n6cQenl/poMaIyJtBu8eys5uk6u5HiDH01Mj5lzbJ5SfeDz7oqMQ==
|
||||
"@typescript-eslint/utils@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.34.0.tgz#0cae98f48d8f9e292e5caa9343611b6faf49e743"
|
||||
integrity sha512-kWRYybU4Rn++7lm9yu8pbuydRyQsHRoBDIo11k7eqBWTldN4xUdVUMCsHBiE7aoEkFzrUEaZy3iH477vr4xHAQ==
|
||||
dependencies:
|
||||
"@types/json-schema" "^7.0.9"
|
||||
"@typescript-eslint/scope-manager" "5.33.1"
|
||||
"@typescript-eslint/types" "5.33.1"
|
||||
"@typescript-eslint/typescript-estree" "5.33.1"
|
||||
"@typescript-eslint/scope-manager" "5.34.0"
|
||||
"@typescript-eslint/types" "5.34.0"
|
||||
"@typescript-eslint/typescript-estree" "5.34.0"
|
||||
eslint-scope "^5.1.1"
|
||||
eslint-utils "^3.0.0"
|
||||
|
||||
"@typescript-eslint/visitor-keys@5.33.1":
|
||||
version "5.33.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.33.1.tgz#0155c7571c8cd08956580b880aea327d5c34a18b"
|
||||
integrity sha512-nwIxOK8Z2MPWltLKMLOEZwmfBZReqUdbEoHQXeCpa+sRVARe5twpJGHCB4dk9903Yaf0nMAlGbQfaAH92F60eg==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "5.33.1"
|
||||
eslint-visitor-keys "^3.3.0"
|
||||
|
||||
"@typescript-eslint/visitor-keys@5.34.0":
|
||||
version "5.34.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.34.0.tgz#d0fb3e31033e82ddd5de048371ad39eb342b2d40"
|
||||
@ -3561,10 +3527,10 @@ prettier-linter-helpers@^1.0.0:
|
||||
dependencies:
|
||||
fast-diff "^1.1.2"
|
||||
|
||||
prettier-plugin-jsdoc@^0.3.30:
|
||||
version "0.3.38"
|
||||
resolved "https://registry.yarnpkg.com/prettier-plugin-jsdoc/-/prettier-plugin-jsdoc-0.3.38.tgz#b8adbe9efc1dc11f3cc5ff0b07e0233a0fdf533d"
|
||||
integrity sha512-h81ZV/nFk5gr3fzWMWzWoz/M/8FneAZxscT7DVSy+5jMIuWYnBFZfSswVKYZyTaZ5r6+6k4hpFTDWhRp85C1tg==
|
||||
prettier-plugin-jsdoc@^0.4.2:
|
||||
version "0.4.2"
|
||||
resolved "https://registry.yarnpkg.com/prettier-plugin-jsdoc/-/prettier-plugin-jsdoc-0.4.2.tgz#c5668fc622ed10b87d988279476f96af96b058b7"
|
||||
integrity sha512-w2jnAQm3z0GAG0bhzVJeehzDtrhGMSxJjit5ApCc2oxWfc7+jmLAkbtdOXaSpfwZz3IWkk+PiQPeRrLNpbM+Mw==
|
||||
dependencies:
|
||||
binary-searching "^2.0.5"
|
||||
comment-parser "^1.3.1"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user