Skip to content

Commit

Permalink
feat(199): add ability to disable cache
Browse files Browse the repository at this point in the history
This feature introduces the ability to disable caching
entirely when using `configure` or `makeUseAxios`,
by setting the `cache` option to false:

```
// with configure
configure({ cache: false })

// with makeUseAxios
const useAxios = makeUseAxios({ cache: false })
```

fixes #199
  • Loading branch information
simoneb committed Apr 11, 2020
1 parent 32e4303 commit b8f504a
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 529 deletions.
4 changes: 3 additions & 1 deletion .prettierrc
@@ -1,4 +1,6 @@
{
"semi": false,
"singleQuote": true
"singleQuote": true,
"trailingComma": "none",
"arrowParens": "avoid"
}
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -93,7 +93,7 @@ Returns:

Allows to provide custom instances of cache and axios.

- `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache)
- `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache), or `false` to disable the cache
- `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)

### serializeCache()
Expand All @@ -114,7 +114,7 @@ Populates the cache with serialized data generated by `serializeCache`.

Creates an instance of the `useAxios` hook configured with the supplied cache and axios instance.

- `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache)
- `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache), or `false` to disable the cache
- `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)

Returns:
Expand All @@ -137,10 +137,11 @@ Unless provided via the `configure` function, `axios-hooks` uses as defaults:
- `axios` - the default `axios` package export
- `cache` - a new instance of the default `lru-cache` package export, with no arguments

These defaults may not suit your needs:
These defaults may not suit your needs, for example:

- you may want a common base url for axios requests
- the default (Infinite) cache size may not be a sensible default
- you want to disable caching altogether

In such cases you can use the `configure` function to provide your custom implementation of both.

Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -88,8 +88,7 @@
},
"lint-staged": {
"src/**/*.{js,md}": [
"prettier --write",
"git add"
"eslint --fix"
]
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Expand Up @@ -26,7 +26,7 @@ export interface RefetchOptions {

export interface ConfigureOptions {
axios?: AxiosInstance | AxiosStatic | any
cache?: LRUCache<any, any>
cache?: LRUCache<any, any> | false
}

export interface UseAxios {
Expand Down
9 changes: 4 additions & 5 deletions src/index.js
Expand Up @@ -32,18 +32,17 @@ export function makeUseAxios(configurationOptions) {
axiosInstance = StaticAxios
}

resetConfigure()

function configure(options = {}) {
if (options.axios) {
if (options.axios !== undefined) {
axiosInstance = options.axios
}

if (options.cache) {
if (options.cache !== undefined) {
cache = options.cache
}
}

resetConfigure()
configure(configurationOptions)

function loadCache(data) {
Expand Down Expand Up @@ -140,7 +139,7 @@ export function makeUseAxios(configurationOptions) {
}

function executeRequest(config, options, dispatch) {
if (options.useCache) {
if (cache && options.useCache) {
return executeRequestWithCache(config, dispatch)
}

Expand Down

0 comments on commit b8f504a

Please sign in to comment.