mirror of
https://github.com/simoneb/axios-hooks.git
synced 2025-12-08 21:25:56 +00:00
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:
parent
32e43037b9
commit
b8f504a1e8
@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
"semi": false,
|
"semi": false,
|
||||||
"singleQuote": true
|
"singleQuote": true,
|
||||||
|
"trailingComma": "none",
|
||||||
|
"arrowParens": "avoid"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -93,7 +93,7 @@ Returns:
|
|||||||
|
|
||||||
Allows to provide custom instances of cache and axios.
|
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)
|
- `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)
|
||||||
|
|
||||||
### serializeCache()
|
### 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.
|
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)
|
- `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -137,10 +137,11 @@ Unless provided via the `configure` function, `axios-hooks` uses as defaults:
|
|||||||
- `axios` - the default `axios` package export
|
- `axios` - the default `axios` package export
|
||||||
- `cache` - a new instance of the default `lru-cache` package export, with no arguments
|
- `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
|
- you may want a common base url for axios requests
|
||||||
- the default (Infinite) cache size may not be a sensible default
|
- 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.
|
In such cases you can use the `configure` function to provide your custom implementation of both.
|
||||||
|
|
||||||
|
|||||||
@ -88,8 +88,7 @@
|
|||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"src/**/*.{js,md}": [
|
"src/**/*.{js,md}": [
|
||||||
"prettier --write",
|
"eslint --fix"
|
||||||
"git add"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
|||||||
2
src/index.d.ts
vendored
2
src/index.d.ts
vendored
@ -26,7 +26,7 @@ export interface RefetchOptions {
|
|||||||
|
|
||||||
export interface ConfigureOptions {
|
export interface ConfigureOptions {
|
||||||
axios?: AxiosInstance | AxiosStatic | any
|
axios?: AxiosInstance | AxiosStatic | any
|
||||||
cache?: LRUCache<any, any>
|
cache?: LRUCache<any, any> | false
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UseAxios {
|
export interface UseAxios {
|
||||||
|
|||||||
@ -32,18 +32,17 @@ export function makeUseAxios(configurationOptions) {
|
|||||||
axiosInstance = StaticAxios
|
axiosInstance = StaticAxios
|
||||||
}
|
}
|
||||||
|
|
||||||
resetConfigure()
|
|
||||||
|
|
||||||
function configure(options = {}) {
|
function configure(options = {}) {
|
||||||
if (options.axios) {
|
if (options.axios !== undefined) {
|
||||||
axiosInstance = options.axios
|
axiosInstance = options.axios
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.cache) {
|
if (options.cache !== undefined) {
|
||||||
cache = options.cache
|
cache = options.cache
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetConfigure()
|
||||||
configure(configurationOptions)
|
configure(configurationOptions)
|
||||||
|
|
||||||
function loadCache(data) {
|
function loadCache(data) {
|
||||||
@ -140,7 +139,7 @@ export function makeUseAxios(configurationOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function executeRequest(config, options, dispatch) {
|
function executeRequest(config, options, dispatch) {
|
||||||
if (options.useCache) {
|
if (cache && options.useCache) {
|
||||||
return executeRequestWithCache(config, dispatch)
|
return executeRequestWithCache(config, dispatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1060
test/index.test.js
1060
test/index.test.js
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,7 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"jsx": "react"
|
"jsx": "react",
|
||||||
|
"outDir": "$$dummy$$"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user