docs: updated docs

This commit is contained in:
Hazork 2021-09-21 12:39:26 -03:00
parent bf4f299870
commit 07731e4b35

View File

@ -2,7 +2,7 @@
<div align="center"> <div align="center">
<pre> <pre>
<br /> <br />
<h1>🗄️📦💿 <h1>🗄️🔥💨
Axios Cache Interceptor</h1> Axios Cache Interceptor</h1>
<br /> <br />
</pre> </pre>
@ -62,7 +62,7 @@ Axios Cache Interceptor</h1>
<br /> <br />
<br /> <br />
#### `axios-cache-interceptor` is a axios wrapper for caching and preventing unneeded requests ### `axios-cache-interceptor` is a axios wrapper for caching and preventing unneeded requests
<br /> <br />
@ -89,7 +89,7 @@ const { data } = await cachedApi.get('https://api.example.com/');
<br /> <br />
<br /> <br />
### Installing ## Installing
> Axios is a peer dependency and must be installed separately. > Axios is a peer dependency and must be installed separately.
@ -103,7 +103,7 @@ yarn add axios axios-cache-interceptor
<br /> <br />
### 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.
@ -133,15 +133,25 @@ After that, you can made your own requests normally.
<br /> <br />
### 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 ## Basic Knowledge
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 the same request. The ### 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 example of this is a request with `{ baseUrl: 'https://a.com/', url: '/b' }` results to the same id
with `{ url: 'https://a.com/b/' }`. with `{ url: 'https://a.com/b/' }`.
@ -156,7 +166,7 @@ const id = result.id; // <-- The id to find the cache and more;
Also, a custom id can be used to treat two requests as the same. Also, a custom id can be used to treat two requests as the same.
```js ```js
axios.get('', { axios.get('...', {
id: 'my-custom-id', id: 'my-custom-id',
cache: { cache: {
// other properties... // other properties...
@ -168,7 +178,7 @@ The [default](src/util/key-generator.ts) id generation can clarify this idea.
<br /> <br />
### Global configuration ## Global configuration
When applying the interceptor, you can customize some properties: When applying the interceptor, you can customize some properties:
@ -178,7 +188,7 @@ const axios = createCache({
}); });
``` ```
#### storage ### storage
The storage used to save the cache. Here will probably be the most changed property. Defaults to The storage used to save the cache. Here will probably be the most changed property. Defaults to
[MemoryStorage](src/storage/memory.ts). [MemoryStorage](src/storage/memory.ts).
@ -188,33 +198,34 @@ You can create your own implementation by implementing [CacheStorage](src/storag
There are few built in storage implementations, you can use them by importing: There are few built in storage implementations, you can use them by importing:
```js ```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';` - [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';` - [Session and Local Storage](src/storage/web.ts)
`import 'axios-cache-interceptor/dist/storage/web';`
- _Maybe your own?_ (PR's are welcome) - _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 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 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 A simple object that will hold a promise for each pending request. Used to handle concurrent
requests. requests.
Can also be used as type of _listener_ to know when a request is finished. 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`) The function used to interpret all headers from a request and determine a time to live (`ttl`)
number. number.
Check out the [inline documentation](src/header/types.ts) to know how to modify your own. 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 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 used [request](src/interceptors/request.ts) and [response](src/interceptors/response.ts) to see
@ -222,34 +233,34 @@ the default used.
<br /> <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 By using this axios client and using an ide with intellisense, you'll see a custom property called
`cache`. `cache`.
The inline documentation is self explanatory, but here are some examples and information: 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 The time that the request will remain in cache. Some custom storage implementations may not respect
100% the time. 100% the time.
When using `interpretHeader`, this value is ignored. 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 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 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 for more information. You can override the default behavior by setting the `headerInterpreter` when
creating the cached axios client. creating the cached axios client.
#### methods ### methods
Specify what request methods should be cached. Specify what request methods should be cached.
Defaults to only `GET` methods. Defaults to only `GET` methods.
#### cachePredicate ### cachePredicate
An object or function that will be tested against the response to test if it can be cached. See the 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. [inline documentation](src/util/cache-predicate.ts) for more.
@ -280,7 +291,7 @@ axios.get('url', {
}); });
``` ```
#### update ### update
Once the request is resolved, this specifies what other responses should change their cache. Can be 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. used to update the request or delete other caches. It is a simple `Record` with the request id.
@ -310,7 +321,7 @@ axios.get('url', {
<br /> <br />
### Inspiration ## Inspiration
This project is highly inspired by several projects, written entirely in typescript, supporting This project is highly inspired by several projects, written entirely in typescript, supporting
https headers and much more. https headers and much more.
@ -323,13 +334,13 @@ Take a look at some similar projects:
<br /> <br />
### License ## License
Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations. Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations.
<br /> <br />
### Contact ## Contact
See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new
issue. issue.