Skip to content

Commit

Permalink
docs: added usage and examples page
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 6, 2022
1 parent c5de326 commit 06d9979
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/config/sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions docs/pages/per-request-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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:

Expand Down
4 changes: 3 additions & 1 deletion docs/pages/request-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 4 additions & 2 deletions docs/pages/storages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
58 changes: 58 additions & 0 deletions docs/pages/usage-examples.md
Original file line number Diff line number Diff line change
@@ -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 * */
}
});
```

0 comments on commit 06d9979

Please sign in to comment.