mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
docs: added usage and examples page
This commit is contained in:
parent
c5de32672c
commit
06d9979cd7
@ -2,6 +2,7 @@
|
||||
|
||||
- [Introduction](pages/introduction.md 'Introduction')
|
||||
- [Installing](pages/installing.md 'Installing')
|
||||
- [Usage & Examples](pages/usage-examples.md 'Interceptor')
|
||||
- [Storages](pages/storages.md 'Custom storages')
|
||||
|
||||
- Cache Interceptor API
|
||||
|
||||
@ -39,9 +39,9 @@ determine their TTL value to override this
|
||||
|
||||
If activated, when the response is received, the `ttl` property will be inferred from the
|
||||
requests headers. See the actual implementation of the
|
||||
[`interpretHeader`](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/header/interpreter.ts) method for more information. You can
|
||||
override the default behavior by setting the `headerInterpreter` when creating the cached
|
||||
axios client.
|
||||
[`interpretHeader`](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/header/interpreter.ts)
|
||||
method for more information. You can override the default behavior by setting the
|
||||
`headerInterpreter` when creating the cached axios client.
|
||||
|
||||
## `cache.methods`
|
||||
|
||||
@ -52,7 +52,9 @@ Defaults to only `GET` methods.
|
||||
## `cache.cachePredicate`
|
||||
|
||||
An object or function that will be tested against the response to test if it can be
|
||||
cached. See the [inline documentation](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/util/cache-predicate.ts) for more.
|
||||
cached. See the
|
||||
[inline documentation](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/util/cache-predicate.ts)
|
||||
for more.
|
||||
|
||||
An simple example with all values:
|
||||
|
||||
|
||||
@ -19,4 +19,6 @@ axios.get('...', {
|
||||
});
|
||||
```
|
||||
|
||||
The [default](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/util/key-generator.ts) id generation can clarify this idea.
|
||||
The
|
||||
[default](https://github.com/arthurfiorette/axios-cache-interceptor/blob/main/src/util/key-generator.ts)
|
||||
id generation can clarify this idea.
|
||||
|
||||
@ -3,8 +3,10 @@
|
||||
A storage is the main object responsible for saving, retrieving and serializing (if
|
||||
needed) cache data. There are two simple ones that comes by default:
|
||||
|
||||
- [In Memory](https://github.com/arthurfiorette/axios-cache-interceptor/tree/main/src/storage/memory.ts) with `buildMemoryStorage` (Node and Web)
|
||||
- [Web Storage API](https://github.com/arthurfiorette/axios-cache-interceptor/tree/main/src/storage/web-api.ts) with `buildWebStorage` (Web only)
|
||||
- [In Memory](https://github.com/arthurfiorette/axios-cache-interceptor/tree/main/src/storage/memory.ts)
|
||||
with `buildMemoryStorage` (Node and Web)
|
||||
- [Web Storage API](https://github.com/arthurfiorette/axios-cache-interceptor/tree/main/src/storage/web-api.ts)
|
||||
with `buildWebStorage` (Web only)
|
||||
|
||||
Both of them are included in all bundles.
|
||||
|
||||
|
||||
58
docs/pages/usage-examples.md
Normal file
58
docs/pages/usage-examples.md
Normal file
@ -0,0 +1,58 @@
|
||||
# Interceptor
|
||||
|
||||
## Applying
|
||||
|
||||
This library is based on axios interceptors, so, under the hood, it uses
|
||||
`axios.interceptors.use()` to apply the interceptors. But you don't. All you have to do is
|
||||
call `setupCache` and you are ready to go!
|
||||
|
||||
```js
|
||||
import { setupCache } from 'axios-cache-interceptor';
|
||||
|
||||
setupCache(axios);
|
||||
```
|
||||
|
||||
### How to get the axios instance
|
||||
|
||||
There are two types of axios instances, the `AxiosStatic` and the `AxiosInstance`. The
|
||||
`AxiosStatic` is the default instance of axios. The `AxiosInstance` is the instance you
|
||||
get when you call `axios.create()`.
|
||||
|
||||
Both of them work seamlessly, but when messing with the axios static, your hole code,
|
||||
_including those libraries you don't know that their exists_, are also affected. **You
|
||||
should be careful when using it.**
|
||||
|
||||
```js
|
||||
// AxiosStatic
|
||||
import axios from 'axios';
|
||||
|
||||
// AxiosInstance
|
||||
const instance = axios.create();
|
||||
```
|
||||
|
||||
## Customizing behaviors
|
||||
|
||||
You can customize the behaviors of this library in two ways, in a
|
||||
[per request](pages/per-request-configuration.md) or in a
|
||||
[global](pages/global-configuration.md) way.
|
||||
|
||||
```js
|
||||
import Axios from 'axios';
|
||||
|
||||
const instance = Axios.create({
|
||||
/** Here you can pass the axios options * */
|
||||
});
|
||||
|
||||
// Global
|
||||
setupCache(instance, {
|
||||
/** Here you can pass the interceptor options * */
|
||||
});
|
||||
|
||||
// Per request
|
||||
await instance.get('url', {
|
||||
/** Override axios options * */
|
||||
cache: {
|
||||
/** Override cache options * */
|
||||
}
|
||||
});
|
||||
```
|
||||
Loading…
x
Reference in New Issue
Block a user