mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
style: updated prettier config
This commit is contained in:
parent
35ca874c65
commit
d96a9dc5f0
78
README.md
78
README.md
@ -100,7 +100,8 @@ yarn add axios axios-cache-interceptor
|
||||
|
||||
## Getting Started
|
||||
|
||||
To you use this cache interceptor, you can apply to an existing instance or create a new one.
|
||||
To you use this cache interceptor, you can apply to an existing instance or create a new
|
||||
one.
|
||||
|
||||
```js
|
||||
import { applyCache } from 'axios-cache-interceptor';
|
||||
@ -142,13 +143,13 @@ After that, you can made your own requests normally.
|
||||
|
||||
### Request id
|
||||
|
||||
A good thing to know is that every request passed through this interceptor, has an id. **This does
|
||||
not mean that is a unique id**. The id is used in a number of ways, but the most important is to
|
||||
bind a request to its cache.
|
||||
A good thing to know is that every request passed through this interceptor, has an id.
|
||||
**This does not mean that is a unique id**. The id is used in a number of ways, but the
|
||||
most important is to bind a request to its cache.
|
||||
|
||||
The id generation is good enough to generate the same id for theoretically sames requests. The
|
||||
example of this is a request with `{ baseUrl: 'https://a.com/', url: '/b' }` results to the same id
|
||||
with `{ url: 'https://a.com/b/' }`.
|
||||
The id generation is good enough to generate the same id for theoretically sames requests.
|
||||
The example of this is a request with `{ baseUrl: 'https://a.com/', url: '/b' }` results
|
||||
to the same id with `{ url: 'https://a.com/b/' }`.
|
||||
|
||||
The id is retrieved with the response object.
|
||||
|
||||
@ -185,10 +186,11 @@ const axios = createCache({
|
||||
|
||||
### storage
|
||||
|
||||
The storage used to save the cache. Here will probably be the most changed property. Defaults to
|
||||
[MemoryStorage](src/storage/memory.ts).
|
||||
The storage used to save the cache. Here will probably be the most changed property.
|
||||
Defaults to [MemoryStorage](src/storage/memory.ts).
|
||||
|
||||
You can create your own implementation by implementing [CacheStorage](src/storage/types.ts).
|
||||
You can create your own implementation by implementing
|
||||
[CacheStorage](src/storage/types.ts).
|
||||
|
||||
There are few built in storage implementations, you can use them by importing:
|
||||
|
||||
@ -196,58 +198,61 @@ There are few built in storage implementations, you can use them by importing:
|
||||
import /* ... */ 'axios-cache-interceptor/dist/storage/{name}';
|
||||
```
|
||||
|
||||
- [MemoryStorage](src/storage/memory.ts) `import 'axios-cache-interceptor/dist/storage/memory';`
|
||||
- [MemoryStorage](src/storage/memory.ts)
|
||||
`import 'axios-cache-interceptor/dist/storage/memory';`
|
||||
- [Session and Local Storage](src/storage/web.ts)
|
||||
`import 'axios-cache-interceptor/dist/storage/web';`
|
||||
- _Maybe your own?_ (PR's are welcome)
|
||||
|
||||
### generateKey
|
||||
|
||||
The function used to create different keys for each request. Defaults to a function that priorizes
|
||||
the id, and if not specified, a string is generated using the method, baseUrl, params, and url.
|
||||
The function used to create different keys for each request. Defaults to a function that
|
||||
priorizes the id, and if not specified, a string is generated using the method, baseUrl,
|
||||
params, and url.
|
||||
|
||||
### waiting
|
||||
|
||||
A simple object that will hold a promise for each pending request. Used to handle concurrent
|
||||
requests.
|
||||
A simple object that will hold a promise for each pending request. Used to handle
|
||||
concurrent requests.
|
||||
|
||||
Can also be used as type of _listener_ to know when a request is finished.
|
||||
|
||||
### headerInterpreter
|
||||
|
||||
The function used to interpret all headers from a request and determine a time to live (`ttl`)
|
||||
number.
|
||||
The function used to interpret all headers from a request and determine a time to live
|
||||
(`ttl`) number.
|
||||
|
||||
Check out the [inline documentation](src/header/types.ts) to know how to modify your own.
|
||||
|
||||
### requestInterceptor and responseInterceptor
|
||||
|
||||
The used request and response interceptor. Basically the core function of this library. Check out
|
||||
the used [request](src/interceptors/request.ts) and [response](src/interceptors/response.ts) to see
|
||||
the default used.
|
||||
The used request and response interceptor. Basically the core function of this library.
|
||||
Check out the used [request](src/interceptors/request.ts) and
|
||||
[response](src/interceptors/response.ts) to see the default used.
|
||||
|
||||
<br />
|
||||
|
||||
## Per-request configuration
|
||||
|
||||
By using this axios client and using an ide with intellisense, you'll see a custom property called
|
||||
`cache`.
|
||||
By using this axios client and using an ide with intellisense, you'll see a custom
|
||||
property called `cache`.
|
||||
|
||||
The inline documentation is self explanatory, but here are some examples and information:
|
||||
|
||||
### ttl
|
||||
|
||||
The time that the request will remain in cache. Some custom storage implementations may not respect
|
||||
100% the time.
|
||||
The time that the request will remain in cache. Some custom storage implementations may
|
||||
not respect 100% the time.
|
||||
|
||||
When using `interpretHeader`, this value is ignored.
|
||||
|
||||
### interpretHeader
|
||||
|
||||
If activated, when the response is received, the `ttl` property will be inferred from the requests
|
||||
headers. See the actual implementation of the [`interpretHeader`](src/header/interpreter.ts) method
|
||||
for more information. You can override the default behavior by setting the `headerInterpreter` when
|
||||
creating the cached axios client.
|
||||
If activated, when the response is received, the `ttl` property will be inferred from the
|
||||
requests headers. See the actual implementation of the
|
||||
[`interpretHeader`](src/header/interpreter.ts) method for more information. You can
|
||||
override the default behavior by setting the `headerInterpreter` when creating the cached
|
||||
axios client.
|
||||
|
||||
### methods
|
||||
|
||||
@ -257,8 +262,8 @@ Defaults to only `GET` methods.
|
||||
|
||||
### cachePredicate
|
||||
|
||||
An object or function that will be tested against the response to test if it can be cached. See the
|
||||
[inline documentation](src/util/cache-predicate.ts) for more.
|
||||
An object or function that will be tested against the response to test if it can be
|
||||
cached. See the [inline documentation](src/util/cache-predicate.ts) for more.
|
||||
|
||||
An simple example with all values:
|
||||
|
||||
@ -288,8 +293,9 @@ axios.get('url', {
|
||||
|
||||
### update
|
||||
|
||||
Once the request is resolved, this specifies what other responses should change their cache. Can be
|
||||
used to update the request or delete other caches. It is a simple `Record` with the request id.
|
||||
Once the request is resolved, this specifies what other responses should change their
|
||||
cache. Can be used to update the request or delete other caches. It is a simple `Record`
|
||||
with the request id.
|
||||
|
||||
Example:
|
||||
|
||||
@ -318,8 +324,8 @@ axios.get('url', {
|
||||
|
||||
## Inspiration
|
||||
|
||||
This project is highly inspired by several projects, written entirely in typescript, supporting
|
||||
https headers and much more.
|
||||
This project is highly inspired by several projects, written entirely in typescript,
|
||||
supporting https headers and much more.
|
||||
|
||||
Take a look at some similar projects:
|
||||
|
||||
@ -337,7 +343,7 @@ Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations.
|
||||
|
||||
## Contact
|
||||
|
||||
See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new
|
||||
issue.
|
||||
See my contact information on my [github profile](https://github.com/ArthurFiorette) or
|
||||
open a new issue.
|
||||
|
||||
<br />
|
||||
|
||||
@ -40,7 +40,7 @@
|
||||
"@tusbar/cache-control": "^0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@arthurfiorette/prettier-config": "^1.0.5",
|
||||
"@arthurfiorette/prettier-config": "^1.0.6",
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/node": "^16.7.10",
|
||||
"@typescript-eslint/eslint-plugin": "^4.30.0",
|
||||
|
||||
@ -31,8 +31,10 @@ export function applyCache(
|
||||
axiosCache.generateKey = generateKey || defaultKeyGenerator;
|
||||
axiosCache.waiting = waiting || {};
|
||||
axiosCache.headerInterpreter = headerInterpreter || defaultHeaderInterpreter;
|
||||
axiosCache.requestInterceptor = requestInterceptor || new CacheRequestInterceptor(axiosCache);
|
||||
axiosCache.responseInterceptor = responseInterceptor || new CacheResponseInterceptor(axiosCache);
|
||||
axiosCache.requestInterceptor =
|
||||
requestInterceptor || new CacheRequestInterceptor(axiosCache);
|
||||
axiosCache.responseInterceptor =
|
||||
responseInterceptor || new CacheResponseInterceptor(axiosCache);
|
||||
|
||||
// CacheRequestConfig values
|
||||
axiosCache.defaults = {
|
||||
|
||||
@ -19,7 +19,10 @@ import { CachePredicate, KeyGenerator } from '../util/types';
|
||||
|
||||
export type CacheUpdater =
|
||||
| 'delete'
|
||||
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void);
|
||||
| ((
|
||||
cached: EmptyStorageValue | CachedStorageValue,
|
||||
newData: any
|
||||
) => CachedStorageValue | void);
|
||||
|
||||
export type DefaultCacheRequestConfig = AxiosRequestConfig & {
|
||||
cache: CacheProperties;
|
||||
@ -168,10 +171,22 @@ export interface AxiosCacheInstance extends AxiosInstance, CacheInstance {
|
||||
|
||||
request<T = any, R = CacheAxiosResponse<T>>(config: CacheRequestConfig): Promise<R>;
|
||||
|
||||
get<T = any, R = CacheAxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
delete<T = any, R = CacheAxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
head<T = any, R = CacheAxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
options<T = any, R = CacheAxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
get<T = any, R = CacheAxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
delete<T = any, R = CacheAxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
head<T = any, R = CacheAxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
options<T = any, R = CacheAxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
post<T = any, R = CacheAxiosResponse<T>>(
|
||||
url: string,
|
||||
data?: any,
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
import { AxiosCacheInstance, CacheRequestConfig } from '../axios/types';
|
||||
import { CachedResponse, CachedStorageValue, LoadingStorageValue } from '../storage/types';
|
||||
import {
|
||||
CachedResponse,
|
||||
CachedStorageValue,
|
||||
LoadingStorageValue
|
||||
} from '../storage/types';
|
||||
import { deferred } from '../util/deferred';
|
||||
import { CACHED_STATUS_CODE, CACHED_STATUS_TEXT } from '../util/status-codes';
|
||||
import { AxiosInterceptor } from './types';
|
||||
@ -20,7 +24,9 @@ export class CacheRequestInterceptor implements AxiosInterceptor<CacheRequestCon
|
||||
// Only cache specified methods
|
||||
const allowedMethods = config.cache?.methods || this.axios.defaults.cache.methods;
|
||||
|
||||
if (!allowedMethods.some((method) => (config.method || 'get').toLowerCase() == method)) {
|
||||
if (
|
||||
!allowedMethods.some((method) => (config.method || 'get').toLowerCase() == method)
|
||||
) {
|
||||
return config;
|
||||
}
|
||||
|
||||
@ -37,7 +43,9 @@ export class CacheRequestInterceptor implements AxiosInterceptor<CacheRequestCon
|
||||
* (asynchronous call) request may have already started executing.
|
||||
*/
|
||||
if (this.axios.waiting[key]) {
|
||||
cache = (await this.axios.storage.get(key)) as CachedStorageValue | LoadingStorageValue;
|
||||
cache = (await this.axios.storage.get(key)) as
|
||||
| CachedStorageValue
|
||||
| LoadingStorageValue;
|
||||
break emptyState;
|
||||
}
|
||||
|
||||
|
||||
@ -19,12 +19,17 @@ export class CacheResponseInterceptor implements AxiosInterceptor<CacheAxiosResp
|
||||
this.axios.interceptors.response.use(this.onFulfilled);
|
||||
};
|
||||
|
||||
private testCachePredicate = (response: AxiosResponse, { cache }: CacheConfig): boolean => {
|
||||
const cachePredicate = cache?.cachePredicate || this.axios.defaults.cache.cachePredicate;
|
||||
private testCachePredicate = (
|
||||
response: AxiosResponse,
|
||||
{ cache }: CacheConfig
|
||||
): boolean => {
|
||||
const cachePredicate =
|
||||
cache?.cachePredicate || this.axios.defaults.cache.cachePredicate;
|
||||
|
||||
return (
|
||||
(typeof cachePredicate === 'function' && cachePredicate(response)) ||
|
||||
(typeof cachePredicate === 'object' && checkPredicateObject(response, cachePredicate))
|
||||
(typeof cachePredicate === 'object' &&
|
||||
checkPredicateObject(response, cachePredicate))
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { CacheRequestConfig } from '../axios/types';
|
||||
|
||||
export type CachePredicate = CachePredicateObject | ((response: AxiosResponse) => boolean);
|
||||
export type CachePredicate =
|
||||
| CachePredicateObject
|
||||
| ((response: AxiosResponse) => boolean);
|
||||
|
||||
export type CachePredicateObject = {
|
||||
/**
|
||||
@ -24,6 +26,7 @@ export type CachePredicateObject = {
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 = (options: CacheRequestConfig) => string;
|
||||
|
||||
42
yarn.lock
42
yarn.lock
@ -2,10 +2,10 @@
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@arthurfiorette/prettier-config@^1.0.5":
|
||||
version "1.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@arthurfiorette/prettier-config/-/prettier-config-1.0.5.tgz#65447a3830768907f7a3806cbc1eca7c445f98d0"
|
||||
integrity sha512-ZVGhdM+LVt3T0IfceBlVECb6UsgFJstNBb3BtjXUv8kcVHGmxhj/PGzgBTMeS7cHQJaYQAtoB1qiKsgSc8wv1w==
|
||||
"@arthurfiorette/prettier-config@^1.0.6":
|
||||
version "1.0.6"
|
||||
resolved "https://registry.yarnpkg.com/@arthurfiorette/prettier-config/-/prettier-config-1.0.6.tgz#dca729366cca252d94d4557621ae6d755981da8c"
|
||||
integrity sha512-Le91x+bTfQogkMSLitxGr3xDjIaQYbsGmhljLihkA1jEuqrXTdg6CDrYSgbXHJblNXaigswZ66rPyP/poBRbOw==
|
||||
|
||||
"@babel/code-frame@7.12.11":
|
||||
version "7.12.11"
|
||||
@ -719,14 +719,6 @@
|
||||
"@typescript-eslint/typescript-estree" "4.31.2"
|
||||
debug "^4.3.1"
|
||||
|
||||
"@typescript-eslint/scope-manager@4.31.1":
|
||||
version "4.31.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.31.1.tgz#0c21e8501f608d6a25c842fcf59541ef4f1ab561"
|
||||
integrity sha512-N1Uhn6SqNtU2XpFSkD4oA+F0PfKdWHyr4bTX0xTj8NRx1314gBDRL1LUuZd5+L3oP+wo6hCbZpaa1in6SwMcVQ==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "4.31.1"
|
||||
"@typescript-eslint/visitor-keys" "4.31.1"
|
||||
|
||||
"@typescript-eslint/scope-manager@4.31.2":
|
||||
version "4.31.2"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.31.2.tgz#1d528cb3ed3bcd88019c20a57c18b897b073923a"
|
||||
@ -735,29 +727,11 @@
|
||||
"@typescript-eslint/types" "4.31.2"
|
||||
"@typescript-eslint/visitor-keys" "4.31.2"
|
||||
|
||||
"@typescript-eslint/types@4.31.1":
|
||||
version "4.31.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.31.1.tgz#5f255b695627a13401d2fdba5f7138bc79450d66"
|
||||
integrity sha512-kixltt51ZJGKENNW88IY5MYqTBA8FR0Md8QdGbJD2pKZ+D5IvxjTYDNtJPDxFBiXmka2aJsITdB1BtO1fsgmsQ==
|
||||
|
||||
"@typescript-eslint/types@4.31.2":
|
||||
version "4.31.2"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.31.2.tgz#2aea7177d6d744521a168ed4668eddbd912dfadf"
|
||||
integrity sha512-kWiTTBCTKEdBGrZKwFvOlGNcAsKGJSBc8xLvSjSppFO88AqGxGNYtF36EuEYG6XZ9vT0xX8RNiHbQUKglbSi1w==
|
||||
|
||||
"@typescript-eslint/typescript-estree@4.31.1":
|
||||
version "4.31.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.31.1.tgz#4a04d5232cf1031232b7124a9c0310b577a62d17"
|
||||
integrity sha512-EGHkbsUvjFrvRnusk6yFGqrqMBTue5E5ROnS5puj3laGQPasVUgwhrxfcgkdHNFECHAewpvELE1Gjv0XO3mdWg==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "4.31.1"
|
||||
"@typescript-eslint/visitor-keys" "4.31.1"
|
||||
debug "^4.3.1"
|
||||
globby "^11.0.3"
|
||||
is-glob "^4.0.1"
|
||||
semver "^7.3.5"
|
||||
tsutils "^3.21.0"
|
||||
|
||||
"@typescript-eslint/typescript-estree@4.31.2":
|
||||
version "4.31.2"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.31.2.tgz#abfd50594d8056b37e7428df3b2d185ef2d0060c"
|
||||
@ -771,14 +745,6 @@
|
||||
semver "^7.3.5"
|
||||
tsutils "^3.21.0"
|
||||
|
||||
"@typescript-eslint/visitor-keys@4.31.1":
|
||||
version "4.31.1"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.31.1.tgz#f2e7a14c7f20c4ae07d7fc3c5878c4441a1da9cc"
|
||||
integrity sha512-PCncP8hEqKw6SOJY+3St4LVtoZpPPn+Zlpm7KW5xnviMhdqcsBty4Lsg4J/VECpJjw1CkROaZhH4B8M1OfnXTQ==
|
||||
dependencies:
|
||||
"@typescript-eslint/types" "4.31.1"
|
||||
eslint-visitor-keys "^2.0.0"
|
||||
|
||||
"@typescript-eslint/visitor-keys@4.31.2":
|
||||
version "4.31.2"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.31.2.tgz#7d5b4a4705db7fe59ecffb273c1d082760f635cc"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user