axios-cache-interceptor
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -26,4 +26,6 @@ "use strict"; | ||
axiosCache.headerInterpreter = headerInterpreter || interpreter_1.defaultHeaderInterpreter; | ||
axiosCache.requestInterceptor = requestInterceptor || new request_1.CacheRequestInterceptor(axiosCache); | ||
axiosCache.responseInterceptor = responseInterceptor || new response_1.CacheResponseInterceptor(axiosCache); | ||
axiosCache.requestInterceptor = | ||
requestInterceptor || new request_1.CacheRequestInterceptor(axiosCache); | ||
axiosCache.responseInterceptor = | ||
responseInterceptor || new response_1.CacheResponseInterceptor(axiosCache); | ||
// CacheRequestConfig values | ||
@@ -30,0 +32,0 @@ axiosCache.defaults = { |
@@ -17,3 +17,4 @@ "use strict"; | ||
return ((typeof cachePredicate === 'function' && cachePredicate(response)) || | ||
(typeof cachePredicate === 'object' && (0, cache_predicate_1.checkPredicateObject)(response, cachePredicate))); | ||
(typeof cachePredicate === 'object' && | ||
(0, cache_predicate_1.checkPredicateObject)(response, cachePredicate))); | ||
}; | ||
@@ -20,0 +21,0 @@ /** |
@@ -22,5 +22,6 @@ import { AxiosResponse } from 'axios'; | ||
/** | ||
* 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 declare type KeyGenerator = (options: CacheRequestConfig) => string; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "axios-cache-interceptor", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Cache interceptor for axios", | ||
@@ -43,7 +43,7 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@arthurfiorette/prettier-config": "^1.0.5", | ||
"@types/jest": "^27.0.1", | ||
"@arthurfiorette/prettier-config": "^1.0.6", | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^16.7.10", | ||
"@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"@typescript-eslint/parser": "^4.30.0", | ||
"@typescript-eslint/eslint-plugin": "^4.31.2", | ||
"@typescript-eslint/parser": "^4.31.2", | ||
"auto-changelog": "^2.3.0", | ||
@@ -54,3 +54,3 @@ "axios": "^0.21.1", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.2.0", | ||
"jest": "^27.2.1", | ||
"prettier": "^2.3.2", | ||
@@ -57,0 +57,0 @@ "prettier-plugin-jsdoc": "^0.3.23", |
142
README.md
@@ -5,3 +5,3 @@ <br /> | ||
<br /> | ||
<h1>🗄️📦💿 | ||
<h1>🗄️🔥💨 | ||
Axios Cache Interceptor</h1> | ||
@@ -61,11 +61,6 @@ <br /> | ||
<div align="center"><b><pre>This library is in beta and can have breaking changes until v1.<br />Not ready for production usage!</pre></b></div> | ||
### `axios-cache-interceptor` is a axios wrapper for caching and preventing unneeded requests | ||
<br /> | ||
<br /> | ||
#### `axios-cache-interceptor` is a axios wrapper for caching and preventing unneeded requests | ||
<br /> | ||
```ts | ||
@@ -94,3 +89,3 @@ import axios from 'axios'; | ||
### Installing | ||
## Installing | ||
@@ -109,5 +104,6 @@ > Axios is a peer dependency and must be installed separately. | ||
### Getting Started | ||
## 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. | ||
@@ -140,14 +136,24 @@ ```js | ||
### Basic Knowledge | ||
## What we support | ||
#### Request id | ||
- [x] Cache concurrent requests | ||
- [x] Typescript support | ||
- [x] Unit tests | ||
- [x] Header interpretation | ||
- [x] Infinity storage options | ||
- [x] Cache revalidation from responses | ||
- [ ] External storages, like redis | ||
A good thing to know is that every request passed through this interceptor. **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. | ||
## Basic Knowledge | ||
The id generation is good enough to generate the same id for theoretically the same request. 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/' }`. | ||
### 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. | ||
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. | ||
@@ -164,3 +170,3 @@ | ||
```js | ||
axios.get('', { | ||
axios.get('...', { | ||
id: 'my-custom-id', | ||
@@ -177,3 +183,3 @@ cache: { | ||
### Global configuration | ||
## Global configuration | ||
@@ -188,8 +194,9 @@ When applying the interceptor, you can customize some properties: | ||
#### storage | ||
### 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). | ||
@@ -199,58 +206,62 @@ There are few built in storage implementations, you can use them by importing: | ||
```js | ||
import { /* ... */ } from 'axios-cache-interceptor/dist/storage/{name}'; | ||
import /* ... */ 'axios-cache-interceptor/dist/storage/{name}'; | ||
``` | ||
- [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';` | ||
- [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 | ||
### 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 | ||
### 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 | ||
### 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 | ||
### 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 | ||
## 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 | ||
### 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 | ||
### 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 | ||
### methods | ||
@@ -261,6 +272,6 @@ Specify what request methods should be cached. | ||
#### cachePredicate | ||
### 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. | ||
@@ -293,6 +304,7 @@ An simple example with all values: | ||
#### update | ||
### 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. | ||
@@ -324,6 +336,6 @@ Example: | ||
### Inspiration | ||
## 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. | ||
@@ -338,3 +350,3 @@ Take a look at some similar projects: | ||
### License | ||
## License | ||
@@ -345,7 +357,7 @@ Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations. | ||
### Contact | ||
## 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 /> |
@@ -34,4 +34,6 @@ import Axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
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); | ||
@@ -38,0 +40,0 @@ // CacheRequestConfig values |
@@ -22,3 +22,6 @@ import type { | ||
| 'delete' | ||
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void); | ||
| (( | ||
cached: EmptyStorageValue | CachedStorageValue, | ||
newData: any | ||
) => CachedStorageValue | void); | ||
@@ -172,6 +175,18 @@ export type DefaultCacheRequestConfig = AxiosRequestConfig & { | ||
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>>( | ||
@@ -178,0 +193,0 @@ url: string, |
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'; | ||
@@ -23,3 +27,5 @@ import { CACHED_STATUS_CODE, CACHED_STATUS_TEXT } from '../util/status-codes'; | ||
if (!allowedMethods.some((method) => (config.method || 'get').toLowerCase() == method)) { | ||
if ( | ||
!allowedMethods.some((method) => (config.method || 'get').toLowerCase() == method) | ||
) { | ||
return config; | ||
@@ -41,3 +47,5 @@ } | ||
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; | ||
@@ -44,0 +52,0 @@ } |
@@ -22,8 +22,13 @@ import { AxiosResponse } from 'axios'; | ||
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)) | ||
); | ||
@@ -30,0 +35,0 @@ }; |
import { AxiosResponse } from 'axios'; | ||
import { CacheRequestConfig } from '../axios/types'; | ||
export type CachePredicate = CachePredicateObject | ((response: AxiosResponse) => boolean); | ||
export type CachePredicate = | ||
| CachePredicateObject | ||
| ((response: AxiosResponse) => boolean); | ||
@@ -27,4 +29,5 @@ 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; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119277
1586
350