axios-hooks
Advanced tools
Comparing version 2.2.0 to 2.3.0-alpha.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.3.0-alpha.0](https://github.com/simoneb/axios-hooks/compare/v2.2.0...v2.3.0-alpha.0) (2021-01-13) | ||
### Features | ||
* default options ([b218e68](https://github.com/simoneb/axios-hooks/commit/b218e689ae3c0e69f0ff3645785bb2ad968f2696)) | ||
## [2.2.0](https://github.com/simoneb/axios-hooks/compare/v2.1.0...v2.2.0) (2020-11-18) | ||
@@ -7,0 +14,0 @@ |
@@ -25,2 +25,7 @@ "use strict"; | ||
}; | ||
var DEFAULT_OPTIONS = { | ||
manual: false, | ||
useCache: true, | ||
ssr: true | ||
}; | ||
var useAxios = makeUseAxios(); | ||
@@ -62,5 +67,6 @@ var __ssrPromises = useAxios.__ssrPromises, | ||
function makeUseAxios(configurationOptions) { | ||
function makeUseAxios(configureOptions) { | ||
var cache; | ||
var axiosInstance; | ||
var defaultOptions; | ||
var __ssrPromises = []; | ||
@@ -71,2 +77,3 @@ | ||
axiosInstance = _axios["default"]; | ||
defaultOptions = DEFAULT_OPTIONS; | ||
} | ||
@@ -86,6 +93,10 @@ | ||
} | ||
if (options.defaultOptions !== undefined) { | ||
defaultOptions = (0, _extends3["default"])({}, DEFAULT_OPTIONS, options.defaultOptions); | ||
} | ||
} | ||
resetConfigure(); | ||
configure(configurationOptions); | ||
configure(configureOptions); | ||
@@ -277,6 +288,3 @@ function loadCache(data) { | ||
options = _react["default"].useMemo(function () { | ||
return (0, _extends3["default"])({ | ||
manual: false, | ||
useCache: true | ||
}, options); | ||
return (0, _extends3["default"])({}, defaultOptions, options); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -291,3 +299,3 @@ [JSON.stringify(options)]); | ||
if (typeof window === 'undefined' && !options.manual) { | ||
if (typeof window === 'undefined' && options.ssr && !options.manual) { | ||
useAxios.__ssrPromises.push(axiosInstance(config)); | ||
@@ -294,0 +302,0 @@ } |
@@ -11,2 +11,7 @@ import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
}; | ||
var DEFAULT_OPTIONS = { | ||
manual: false, | ||
useCache: true, | ||
ssr: true | ||
}; | ||
var useAxios = makeUseAxios(); | ||
@@ -43,5 +48,6 @@ var __ssrPromises = useAxios.__ssrPromises, | ||
export function makeUseAxios(configurationOptions) { | ||
export function makeUseAxios(configureOptions) { | ||
var cache; | ||
var axiosInstance; | ||
var defaultOptions; | ||
var __ssrPromises = []; | ||
@@ -52,2 +58,3 @@ | ||
axiosInstance = StaticAxios; | ||
defaultOptions = DEFAULT_OPTIONS; | ||
} | ||
@@ -67,6 +74,10 @@ | ||
} | ||
if (options.defaultOptions !== undefined) { | ||
defaultOptions = _extends({}, DEFAULT_OPTIONS, options.defaultOptions); | ||
} | ||
} | ||
resetConfigure(); | ||
configure(configurationOptions); | ||
configure(configureOptions); | ||
@@ -260,6 +271,3 @@ function loadCache(data) { | ||
options = React.useMemo(function () { | ||
return _extends({ | ||
manual: false, | ||
useCache: true | ||
}, options); | ||
return _extends({}, defaultOptions, options); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -273,3 +281,3 @@ [JSON.stringify(options)]); | ||
if (typeof window === 'undefined' && !options.manual) { | ||
if (typeof window === 'undefined' && options.ssr && !options.manual) { | ||
useAxios.__ssrPromises.push(axiosInstance(config)); | ||
@@ -276,0 +284,0 @@ } |
{ | ||
"name": "axios-hooks", | ||
"version": "2.2.0", | ||
"version": "2.3.0-alpha.0", | ||
"description": "axios-hooks", | ||
@@ -27,8 +27,6 @@ "keywords": [ | ||
"build": "run-p build:*", | ||
"clean": "rimraf cjs es coverage", | ||
"commit": "npx git-cz", | ||
"clean": "rimraf cjs es", | ||
"format": "prettier --write \"{src,test}/**/*.{js?(x),md,ts?(x)}\"", | ||
"lint": "eslint src test", | ||
"prepare": "npm run clean && npm run build", | ||
"prepublishOnly": "npm run build", | ||
"release": "standard-version", | ||
@@ -47,31 +45,30 @@ "pretest": "cp ./test/index.test.jsx ./test/index.test.tsx && cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx", | ||
"devDependencies": { | ||
"@babel/cli": "7.12.1", | ||
"@babel/core": "7.12.3", | ||
"@babel/plugin-transform-runtime": "7.12.1", | ||
"@babel/preset-env": "7.12.1", | ||
"@babel/preset-react": "7.12.5", | ||
"@babel/cli": "7.12.10", | ||
"@babel/core": "7.12.10", | ||
"@babel/plugin-transform-runtime": "7.12.10", | ||
"@babel/preset-env": "7.12.11", | ||
"@babel/preset-react": "7.12.10", | ||
"@commitlint/cli": "11.0.0", | ||
"@commitlint/config-conventional": "11.0.0", | ||
"@testing-library/react": "11.1.2", | ||
"@testing-library/react-hooks": "3.4.2", | ||
"@types/jest": "26.0.15", | ||
"@testing-library/react": "11.2.3", | ||
"@testing-library/react-hooks": "4.0.1", | ||
"@types/jest": "26.0.20", | ||
"@types/lru-cache": "5.1.0", | ||
"@types/node": "12.12.24", | ||
"@types/react": "16.9.56", | ||
"@types/react-dom": "16.9.9", | ||
"axios": "0.21.0", | ||
"@types/react": "17.0.0", | ||
"@types/react-dom": "17.0.0", | ||
"axios": "0.21.1", | ||
"babel-eslint": "10.1.0", | ||
"cross-env": "7.0.2", | ||
"cz-conventional-changelog": "3.3.0", | ||
"eslint": "7.13.0", | ||
"eslint-config-prettier": "6.15.0", | ||
"cross-env": "7.0.3", | ||
"eslint": "7.17.0", | ||
"eslint-config-prettier": "7.1.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"eslint-plugin-prettier": "3.1.4", | ||
"eslint-plugin-react": "7.21.5", | ||
"eslint-plugin-prettier": "3.3.1", | ||
"eslint-plugin-react": "7.22.0", | ||
"eslint-plugin-react-hooks": "4.2.0", | ||
"husky": "4.3.0", | ||
"husky": "4.3.7", | ||
"jest": "26.6.3", | ||
"lint-staged": "10.5.1", | ||
"lint-staged": "10.5.3", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "2.1.2", | ||
"prettier": "2.2.1", | ||
"react": "17.0.1", | ||
@@ -81,5 +78,5 @@ "react-dom": "17.0.1", | ||
"rimraf": "3.0.2", | ||
"standard-version": "9.0.0", | ||
"standard-version": "9.1.0", | ||
"ts-jest": "26.4.4", | ||
"typescript": "4.0.5" | ||
"typescript": "4.1.3" | ||
}, | ||
@@ -94,11 +91,6 @@ "husky": { | ||
"lint-staged": { | ||
"src/**/*.{js?(x),md}": [ | ||
"{src,test}/**/*.{js?(x),md}": [ | ||
"eslint --fix" | ||
] | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} |
@@ -52,6 +52,6 @@ # axios-hooks | ||
- [useAxios](#useaxiosurlconfig-options) | ||
- [configure](#configure-cache-axios-) | ||
- [configure](#configure-cache-axios-defaultoptions) | ||
- [serializeCache](#serializeCache) | ||
- [loadCache](#loadcachecache) | ||
- [makeUseAxios](#makeuseaxios-cache-axios-) | ||
- [makeUseAxios](#makeuseaxios-cache-axios-defaultoptions) | ||
@@ -63,2 +63,3 @@ ### Examples | ||
- [Error handling](https://codesandbox.io/s/axios-hooks-error-handling-gvi41) | ||
- [Authentication and token refresh](https://codesandbox.io/s/axios-hooks-authentication-zyeyh) | ||
- [Caching](https://codesandbox.io/s/axios-hooks-caching-nm62v) | ||
@@ -96,2 +97,3 @@ - [Using makeUseAxios](https://codesandbox.io/s/axios-hooks-makeuseaxios-kfuym) | ||
- `useCache` ( `true` ) - Allows caching to be enabled/disabled for the hook. It doesn't affect the `execute` function returned by the hook. | ||
- `ssr` ( `true` ) - Enables or disables SSR support | ||
@@ -117,8 +119,9 @@ **Returns** | ||
### configure({ cache, axios }) | ||
### configure({ cache, axios, defaultOptions }) | ||
Allows to provide custom instances of cache and axios. | ||
Allows to provide custom instances of cache and axios and to override the default options. | ||
- `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) | ||
- `defaultOptions` An object overriding the default Hook options. It will be merged with the default options. | ||
@@ -139,8 +142,9 @@ ### serializeCache() | ||
### makeUseAxios({ cache, axios }) | ||
### makeUseAxios({ cache, axios, defaultOptions }) | ||
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, axios instance and default options. | ||
- `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) | ||
- `defaultOptions` An object overriding the default Hook options. It will be merged with the default options. | ||
@@ -166,2 +170,3 @@ **Returns** | ||
- `cache` - a new instance of the default `lru-cache` package export, with no arguments | ||
- `defaultOptions` - `{ manual: false, useCache: true, ssr: true }` | ||
@@ -168,0 +173,0 @@ These defaults may not suit your needs, for example: |
@@ -21,2 +21,3 @@ import { | ||
useCache?: boolean | ||
ssr?: boolean | ||
} | ||
@@ -31,8 +32,15 @@ | ||
cache?: LRUCache<any, any> | false | ||
defaultOptions?: Options | ||
} | ||
export interface UseAxios { | ||
<TResponse = any, TError = any>(config: AxiosRequestConfig | string, options?: Options): [ | ||
<TResponse = any, TError = any>( | ||
config: AxiosRequestConfig | string, | ||
options?: Options | ||
): [ | ||
ResponseValues<TResponse, TError>, | ||
(config?: AxiosRequestConfig, options?: RefetchOptions) => AxiosPromise<TResponse> | ||
( | ||
config?: AxiosRequestConfig, | ||
options?: RefetchOptions | ||
) => AxiosPromise<TResponse> | ||
] | ||
@@ -65,2 +73,2 @@ | ||
export function makeUseAxios(options: ConfigureOptions): UseAxios | ||
export function makeUseAxios(options?: ConfigureOptions): UseAxios |
@@ -10,2 +10,8 @@ import React from 'react' | ||
const DEFAULT_OPTIONS = { | ||
manual: false, | ||
useCache: true, | ||
ssr: true | ||
} | ||
const useAxios = makeUseAxios() | ||
@@ -54,5 +60,6 @@ | ||
export function makeUseAxios(configurationOptions) { | ||
export function makeUseAxios(configureOptions) { | ||
let cache | ||
let axiosInstance | ||
let defaultOptions | ||
@@ -64,2 +71,3 @@ const __ssrPromises = [] | ||
axiosInstance = StaticAxios | ||
defaultOptions = DEFAULT_OPTIONS | ||
} | ||
@@ -75,6 +83,10 @@ | ||
} | ||
if (options.defaultOptions !== undefined) { | ||
defaultOptions = { ...DEFAULT_OPTIONS, ...options.defaultOptions } | ||
} | ||
} | ||
resetConfigure() | ||
configure(configurationOptions) | ||
configure(configureOptions) | ||
@@ -200,3 +212,3 @@ function loadCache(data) { | ||
options = React.useMemo( | ||
() => ({ manual: false, useCache: true, ...options }), | ||
() => ({ ...defaultOptions, ...options }), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -213,3 +225,3 @@ [JSON.stringify(options)] | ||
if (typeof window === 'undefined' && !options.manual) { | ||
if (typeof window === 'undefined' && options.ssr && !options.manual) { | ||
useAxios.__ssrPromises.push(axiosInstance(config)) | ||
@@ -216,0 +228,0 @@ } |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51088
35
797
355
1