feat(199): add ability to disable cache

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
This commit is contained in:
Simone Busoli 2020-04-11 12:25:56 +02:00
parent 32e43037b9
commit b8f504a1e8
7 changed files with 559 additions and 529 deletions

View File

@ -1,4 +1,6 @@
{
"semi": false,
"singleQuote": true
"singleQuote": true,
"trailingComma": "none",
"arrowParens": "avoid"
}

View File

@ -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()
@ -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:
@ -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.

View File

@ -88,8 +88,7 @@
},
"lint-staged": {
"src/**/*.{js,md}": [
"prettier --write",
"git add"
"eslint --fix"
]
},
"config": {

2
src/index.d.ts vendored
View File

@ -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 {

View File

@ -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) {
@ -140,7 +139,7 @@ export function makeUseAxios(configurationOptions) {
}
function executeRequest(config, options, dispatch) {
if (options.useCache) {
if (cache && options.useCache) {
return executeRequestWithCache(config, dispatch)
}

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true,
"jsx": "react"
"jsx": "react",
"outDir": "$$dummy$$"
}
}