axios-hooks
Advanced tools
Comparing version 4.0.0 to 5.0.0
205
cjs/index.js
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -9,17 +8,11 @@ exports.loadCache = exports["default"] = exports.configure = exports.clearCache = exports.__ssrPromises = void 0; | ||
exports.serializeCache = exports.resetConfigure = void 0; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var _extends3 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _react = _interopRequireDefault(require("react")); | ||
var _axios = _interopRequireDefault(require("axios")); | ||
var _lruCache = _interopRequireDefault(require("lru-cache")); | ||
var _axios = _interopRequireWildcard(require("axios")); | ||
var _lruCache = require("lru-cache"); | ||
var _lite = require("dequal/lite"); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
var actions = { | ||
@@ -37,7 +30,7 @@ REQUEST_START: 'REQUEST_START', | ||
var __ssrPromises = useAxios.__ssrPromises, | ||
resetConfigure = useAxios.resetConfigure, | ||
configure = useAxios.configure, | ||
loadCache = useAxios.loadCache, | ||
serializeCache = useAxios.serializeCache, | ||
clearCache = useAxios.clearCache; | ||
resetConfigure = useAxios.resetConfigure, | ||
configure = useAxios.configure, | ||
loadCache = useAxios.loadCache, | ||
serializeCache = useAxios.serializeCache, | ||
clearCache = useAxios.clearCache; | ||
exports.clearCache = clearCache; | ||
@@ -51,7 +44,5 @@ exports.serializeCache = serializeCache; | ||
exports["default"] = _default; | ||
function isReactEvent(obj) { | ||
return obj && obj.nativeEvent && obj.nativeEvent instanceof Event; | ||
} | ||
function createCacheKey(config) { | ||
@@ -62,3 +53,2 @@ var cleanedConfig = (0, _extends3["default"])({}, config); | ||
} | ||
function configToObject(config) { | ||
@@ -70,6 +60,4 @@ if (typeof config === 'string') { | ||
} | ||
return Object.assign({}, config); | ||
} | ||
function makeUseAxios(configureOptions) { | ||
@@ -83,5 +71,4 @@ /** | ||
var __ssrPromises = []; | ||
function resetConfigure() { | ||
cache = new _lruCache["default"]({ | ||
cache = new _lruCache.LRUCache({ | ||
max: 500 | ||
@@ -92,3 +79,2 @@ }); | ||
} | ||
function configure(options) { | ||
@@ -98,11 +84,8 @@ if (options === void 0) { | ||
} | ||
if (options.axios !== undefined) { | ||
axiosInstance = options.axios; | ||
} | ||
if (options.cache !== undefined) { | ||
cache = options.cache; | ||
} | ||
if (options.defaultOptions !== undefined) { | ||
@@ -112,14 +95,10 @@ defaultOptions = (0, _extends3["default"])({}, DEFAULT_OPTIONS, options.defaultOptions); | ||
} | ||
resetConfigure(); | ||
configure(configureOptions); | ||
function loadCache(data) { | ||
cache.load(data); | ||
} | ||
function serializeCache() { | ||
return _serializeCache.apply(this, arguments); | ||
} | ||
function _serializeCache() { | ||
@@ -129,17 +108,13 @@ _serializeCache = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
ssrPromisesCopy = [].concat(__ssrPromises); | ||
__ssrPromises.length = 0; | ||
_context.next = 4; | ||
return Promise.all(ssrPromisesCopy); | ||
case 4: | ||
return _context.abrupt("return", cache.dump()); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
ssrPromisesCopy = [].concat(__ssrPromises); | ||
__ssrPromises.length = 0; | ||
_context.next = 4; | ||
return Promise.all(ssrPromisesCopy); | ||
case 4: | ||
return _context.abrupt("return", cache.dump()); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
@@ -150,7 +125,5 @@ }, _callee); | ||
} | ||
function clearCache() { | ||
cache.clear(); | ||
} | ||
return Object.assign(useAxios, { | ||
@@ -164,3 +137,2 @@ __ssrPromises: __ssrPromises, | ||
}); | ||
function tryStoreInCache(config, response) { | ||
@@ -170,3 +142,2 @@ if (!cache) { | ||
} | ||
var cacheKey = createCacheKey(config); | ||
@@ -178,3 +149,2 @@ var responseForCache = (0, _extends3["default"])({}, response); | ||
} | ||
function createInitialState(config, options) { | ||
@@ -190,6 +160,4 @@ var response = !options.manual && tryGetFromCache(config, options); | ||
} | ||
function reducer(state, action) { | ||
var _extends2; | ||
switch (action.type) { | ||
@@ -201,3 +169,2 @@ case actions.REQUEST_START: | ||
}); | ||
case actions.REQUEST_END: | ||
@@ -212,3 +179,2 @@ return (0, _extends3["default"])({}, state, { | ||
} | ||
function tryGetFromCache(config, options, dispatch) { | ||
@@ -218,6 +184,4 @@ if (!cache || !options.useCache) { | ||
} | ||
var cacheKey = createCacheKey(config); | ||
var response = cache.get(cacheKey); | ||
if (response && dispatch) { | ||
@@ -229,10 +193,7 @@ dispatch({ | ||
} | ||
return response; | ||
} | ||
function executeRequest(_x, _x2) { | ||
return _executeRequest.apply(this, arguments); | ||
} | ||
function _executeRequest() { | ||
@@ -242,39 +203,32 @@ _executeRequest = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(config, dispatch) { | ||
return _regenerator["default"].wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
dispatch({ | ||
type: actions.REQUEST_START | ||
}); | ||
_context2.next = 4; | ||
return axiosInstance(config); | ||
case 4: | ||
response = _context2.sent; | ||
tryStoreInCache(config, response); | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: response | ||
}); | ||
return _context2.abrupt("return", response); | ||
case 10: | ||
_context2.prev = 10; | ||
_context2.t0 = _context2["catch"](0); | ||
if (!(0, _axios.isCancel)(_context2.t0)) { | ||
dispatch({ | ||
type: actions.REQUEST_START | ||
}); | ||
_context2.next = 4; | ||
return axiosInstance(config); | ||
case 4: | ||
response = _context2.sent; | ||
tryStoreInCache(config, response); | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: response | ||
payload: _context2.t0, | ||
error: true | ||
}); | ||
return _context2.abrupt("return", response); | ||
case 10: | ||
_context2.prev = 10; | ||
_context2.t0 = _context2["catch"](0); | ||
if (!_axios["default"].isCancel(_context2.t0)) { | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: _context2.t0, | ||
error: true | ||
}); | ||
} | ||
throw _context2.t0; | ||
case 14: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
throw _context2.t0; | ||
case 14: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
@@ -285,19 +239,14 @@ }, _callee2, null, [[0, 10]]); | ||
} | ||
function request(_x3, _x4, _x5) { | ||
return _request.apply(this, arguments); | ||
} | ||
function _request() { | ||
_request = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(config, options, dispatch) { | ||
return _regenerator["default"].wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt("return", tryGetFromCache(config, options, dispatch) || executeRequest(config, dispatch)); | ||
case 1: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt("return", tryGetFromCache(config, options, dispatch) || executeRequest(config, dispatch)); | ||
case 1: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
@@ -308,45 +257,37 @@ }, _callee3); | ||
} | ||
function useAxios(_config, _options) { | ||
var config = _react["default"].useMemo(function () { | ||
return configToObject(_config); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useDeepCompareMemoize(_config)); | ||
var options = _react["default"].useMemo(function () { | ||
return (0, _extends3["default"])({}, defaultOptions, _options); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useDeepCompareMemoize(_options)); | ||
var cancelSourceRef = _react["default"].useRef(); | ||
var abortControllerRef = _react["default"].useRef(); | ||
var _React$useReducer = _react["default"].useReducer(reducer, createInitialState(config, options)), | ||
state = _React$useReducer[0], | ||
dispatch = _React$useReducer[1]; | ||
state = _React$useReducer[0], | ||
dispatch = _React$useReducer[1]; | ||
if (typeof window === 'undefined' && options.ssr && !options.manual) { | ||
useAxios.__ssrPromises.push(axiosInstance(config)); | ||
} | ||
var cancelOutstandingRequest = _react["default"].useCallback(function () { | ||
if (cancelSourceRef.current) { | ||
cancelSourceRef.current.cancel(); | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort(); | ||
} | ||
}, []); | ||
var withCancelToken = _react["default"].useCallback(function (config) { | ||
var withAbortSignal = _react["default"].useCallback(function (config) { | ||
if (options.autoCancel) { | ||
cancelOutstandingRequest(); | ||
} | ||
cancelSourceRef.current = _axios["default"].CancelToken.source(); | ||
config.cancelToken = cancelSourceRef.current.token; | ||
abortControllerRef.current = new AbortController(); | ||
config.signal = abortControllerRef.current.signal; | ||
return config; | ||
}, [cancelOutstandingRequest, options.autoCancel]); | ||
_react["default"].useEffect(function () { | ||
if (!options.manual) { | ||
request(withCancelToken(config), options, dispatch)["catch"](function () {}); | ||
request(withAbortSignal(config), options, dispatch)["catch"](function () {}); | ||
} | ||
return function () { | ||
@@ -357,20 +298,15 @@ if (options.autoCancel) { | ||
}; | ||
}, [config, options, withCancelToken, cancelOutstandingRequest]); | ||
}, [config, options, withAbortSignal, cancelOutstandingRequest]); | ||
var refetch = _react["default"].useCallback(function (configOverride, options) { | ||
configOverride = configToObject(configOverride); | ||
return request(withCancelToken((0, _extends3["default"])({}, config, isReactEvent(configOverride) ? null : configOverride)), (0, _extends3["default"])({ | ||
return request(withAbortSignal((0, _extends3["default"])({}, config, isReactEvent(configOverride) ? null : configOverride)), (0, _extends3["default"])({ | ||
useCache: false | ||
}, options), dispatch); | ||
}, [config, withCancelToken]); | ||
}, [config, withAbortSignal]); | ||
return [state, refetch, cancelOutstandingRequest]; | ||
} | ||
} | ||
function useDeepCompareMemoize(value) { | ||
var ref = _react["default"].useRef(); | ||
var signalRef = _react["default"].useRef(0); | ||
if (!(0, _lite.dequal)(value, ref.current)) { | ||
@@ -380,4 +316,3 @@ ref.current = value; | ||
} | ||
return [signalRef.current]; | ||
} |
190
es/index.js
@@ -5,4 +5,4 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
import React from 'react'; | ||
import StaticAxios from 'axios'; | ||
import LRU from 'lru-cache'; | ||
import StaticAxios, { isCancel } from 'axios'; | ||
import { LRUCache } from 'lru-cache'; | ||
import { dequal as deepEqual } from 'dequal/lite'; | ||
@@ -21,21 +21,17 @@ var actions = { | ||
var __ssrPromises = useAxios.__ssrPromises, | ||
resetConfigure = useAxios.resetConfigure, | ||
configure = useAxios.configure, | ||
loadCache = useAxios.loadCache, | ||
serializeCache = useAxios.serializeCache, | ||
clearCache = useAxios.clearCache; | ||
resetConfigure = useAxios.resetConfigure, | ||
configure = useAxios.configure, | ||
loadCache = useAxios.loadCache, | ||
serializeCache = useAxios.serializeCache, | ||
clearCache = useAxios.clearCache; | ||
export default useAxios; | ||
export { __ssrPromises, resetConfigure, configure, loadCache, serializeCache, clearCache }; | ||
function isReactEvent(obj) { | ||
return obj && obj.nativeEvent && obj.nativeEvent instanceof Event; | ||
} | ||
function createCacheKey(config) { | ||
var cleanedConfig = _extends({}, config); | ||
delete cleanedConfig.cancelToken; | ||
return JSON.stringify(cleanedConfig); | ||
} | ||
function configToObject(config) { | ||
@@ -47,6 +43,4 @@ if (typeof config === 'string') { | ||
} | ||
return Object.assign({}, config); | ||
} | ||
export function makeUseAxios(configureOptions) { | ||
@@ -60,5 +54,4 @@ /** | ||
var __ssrPromises = []; | ||
function resetConfigure() { | ||
cache = new LRU({ | ||
cache = new LRUCache({ | ||
max: 500 | ||
@@ -69,3 +62,2 @@ }); | ||
} | ||
function configure(options) { | ||
@@ -75,11 +67,8 @@ if (options === void 0) { | ||
} | ||
if (options.axios !== undefined) { | ||
axiosInstance = options.axios; | ||
} | ||
if (options.cache !== undefined) { | ||
cache = options.cache; | ||
} | ||
if (options.defaultOptions !== undefined) { | ||
@@ -89,14 +78,10 @@ defaultOptions = _extends({}, DEFAULT_OPTIONS, options.defaultOptions); | ||
} | ||
resetConfigure(); | ||
configure(configureOptions); | ||
function loadCache(data) { | ||
cache.load(data); | ||
} | ||
function serializeCache() { | ||
return _serializeCache.apply(this, arguments); | ||
} | ||
function _serializeCache() { | ||
@@ -106,17 +91,13 @@ _serializeCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
ssrPromisesCopy = [].concat(__ssrPromises); | ||
__ssrPromises.length = 0; | ||
_context.next = 4; | ||
return Promise.all(ssrPromisesCopy); | ||
case 4: | ||
return _context.abrupt("return", cache.dump()); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
ssrPromisesCopy = [].concat(__ssrPromises); | ||
__ssrPromises.length = 0; | ||
_context.next = 4; | ||
return Promise.all(ssrPromisesCopy); | ||
case 4: | ||
return _context.abrupt("return", cache.dump()); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
@@ -127,7 +108,5 @@ }, _callee); | ||
} | ||
function clearCache() { | ||
cache.clear(); | ||
} | ||
return Object.assign(useAxios, { | ||
@@ -141,3 +120,2 @@ __ssrPromises: __ssrPromises, | ||
}); | ||
function tryStoreInCache(config, response) { | ||
@@ -147,7 +125,4 @@ if (!cache) { | ||
} | ||
var cacheKey = createCacheKey(config); | ||
var responseForCache = _extends({}, response); | ||
delete responseForCache.config; | ||
@@ -157,3 +132,2 @@ delete responseForCache.request; | ||
} | ||
function createInitialState(config, options) { | ||
@@ -169,6 +143,4 @@ var response = !options.manual && tryGetFromCache(config, options); | ||
} | ||
function reducer(state, action) { | ||
var _extends2; | ||
switch (action.type) { | ||
@@ -180,3 +152,2 @@ case actions.REQUEST_START: | ||
}); | ||
case actions.REQUEST_END: | ||
@@ -191,3 +162,2 @@ return _extends({}, state, { | ||
} | ||
function tryGetFromCache(config, options, dispatch) { | ||
@@ -197,6 +167,4 @@ if (!cache || !options.useCache) { | ||
} | ||
var cacheKey = createCacheKey(config); | ||
var response = cache.get(cacheKey); | ||
if (response && dispatch) { | ||
@@ -208,10 +176,7 @@ dispatch({ | ||
} | ||
return response; | ||
} | ||
function executeRequest(_x, _x2) { | ||
return _executeRequest.apply(this, arguments); | ||
} | ||
function _executeRequest() { | ||
@@ -221,39 +186,32 @@ _executeRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(config, dispatch) { | ||
return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
dispatch({ | ||
type: actions.REQUEST_START | ||
}); | ||
_context2.next = 4; | ||
return axiosInstance(config); | ||
case 4: | ||
response = _context2.sent; | ||
tryStoreInCache(config, response); | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: response | ||
}); | ||
return _context2.abrupt("return", response); | ||
case 10: | ||
_context2.prev = 10; | ||
_context2.t0 = _context2["catch"](0); | ||
if (!isCancel(_context2.t0)) { | ||
dispatch({ | ||
type: actions.REQUEST_START | ||
}); | ||
_context2.next = 4; | ||
return axiosInstance(config); | ||
case 4: | ||
response = _context2.sent; | ||
tryStoreInCache(config, response); | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: response | ||
payload: _context2.t0, | ||
error: true | ||
}); | ||
return _context2.abrupt("return", response); | ||
case 10: | ||
_context2.prev = 10; | ||
_context2.t0 = _context2["catch"](0); | ||
if (!StaticAxios.isCancel(_context2.t0)) { | ||
dispatch({ | ||
type: actions.REQUEST_END, | ||
payload: _context2.t0, | ||
error: true | ||
}); | ||
} | ||
throw _context2.t0; | ||
case 14: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
throw _context2.t0; | ||
case 14: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
@@ -264,19 +222,14 @@ }, _callee2, null, [[0, 10]]); | ||
} | ||
function request(_x3, _x4, _x5) { | ||
return _request.apply(this, arguments); | ||
} | ||
function _request() { | ||
_request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(config, options, dispatch) { | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt("return", tryGetFromCache(config, options, dispatch) || executeRequest(config, dispatch)); | ||
case 1: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt("return", tryGetFromCache(config, options, dispatch) || executeRequest(config, dispatch)); | ||
case 1: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
@@ -287,34 +240,31 @@ }, _callee3); | ||
} | ||
function useAxios(_config, _options) { | ||
var config = React.useMemo(function () { | ||
return configToObject(_config); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useDeepCompareMemoize(_config)); | ||
var options = React.useMemo(function () { | ||
return _extends({}, defaultOptions, _options); | ||
}, // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useDeepCompareMemoize(_options)); | ||
var cancelSourceRef = React.useRef(); | ||
var abortControllerRef = React.useRef(); | ||
var _React$useReducer = React.useReducer(reducer, createInitialState(config, options)), | ||
state = _React$useReducer[0], | ||
dispatch = _React$useReducer[1]; | ||
state = _React$useReducer[0], | ||
dispatch = _React$useReducer[1]; | ||
if (typeof window === 'undefined' && options.ssr && !options.manual) { | ||
useAxios.__ssrPromises.push(axiosInstance(config)); | ||
} | ||
var cancelOutstandingRequest = React.useCallback(function () { | ||
if (cancelSourceRef.current) { | ||
cancelSourceRef.current.cancel(); | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort(); | ||
} | ||
}, []); | ||
var withCancelToken = React.useCallback(function (config) { | ||
var withAbortSignal = React.useCallback(function (config) { | ||
if (options.autoCancel) { | ||
cancelOutstandingRequest(); | ||
} | ||
cancelSourceRef.current = StaticAxios.CancelToken.source(); | ||
config.cancelToken = cancelSourceRef.current.token; | ||
abortControllerRef.current = new AbortController(); | ||
config.signal = abortControllerRef.current.signal; | ||
return config; | ||
@@ -324,5 +274,4 @@ }, [cancelOutstandingRequest, options.autoCancel]); | ||
if (!options.manual) { | ||
request(withCancelToken(config), options, dispatch)["catch"](function () {}); | ||
request(withAbortSignal(config), options, dispatch)["catch"](function () {}); | ||
} | ||
return function () { | ||
@@ -333,17 +282,15 @@ if (options.autoCancel) { | ||
}; | ||
}, [config, options, withCancelToken, cancelOutstandingRequest]); | ||
}, [config, options, withAbortSignal, cancelOutstandingRequest]); | ||
var refetch = React.useCallback(function (configOverride, options) { | ||
configOverride = configToObject(configOverride); | ||
return request(withCancelToken(_extends({}, config, isReactEvent(configOverride) ? null : configOverride)), _extends({ | ||
return request(withAbortSignal(_extends({}, config, isReactEvent(configOverride) ? null : configOverride)), _extends({ | ||
useCache: false | ||
}, options), dispatch); | ||
}, [config, withCancelToken]); | ||
}, [config, withAbortSignal]); | ||
return [state, refetch, cancelOutstandingRequest]; | ||
} | ||
} | ||
function useDeepCompareMemoize(value) { | ||
var ref = React.useRef(); | ||
var signalRef = React.useRef(0); | ||
if (!deepEqual(value, ref.current)) { | ||
@@ -353,4 +300,3 @@ ref.current = value; | ||
} | ||
return [signalRef.current]; | ||
} |
{ | ||
"name": "axios-hooks", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "axios-hooks", | ||
@@ -32,49 +32,50 @@ "keywords": [ | ||
"release": "standard-version", | ||
"pretest": "cp ./test/index.test.jsx ./test/index.test.tsx && cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx", | ||
"pretest": "shx cp ./test/index.test.jsx ./test/index.test.tsx && shx cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx", | ||
"test": "tsd && jest --no-cache" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "7.19.0", | ||
"@babel/runtime": "7.22.15", | ||
"dequal": "2.0.3", | ||
"lru-cache": "^7.14.0" | ||
"lru-cache": "^10.0.1" | ||
}, | ||
"peerDependencies": { | ||
"axios": ">=0.24.0", | ||
"axios": ">=1.0.0", | ||
"react": "^16.8.0-0 || ^17.0.0 || ^18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "7.19.3", | ||
"@babel/core": "7.19.3", | ||
"@babel/plugin-transform-runtime": "7.19.1", | ||
"@babel/preset-env": "7.19.3", | ||
"@babel/preset-react": "7.18.6", | ||
"@commitlint/cli": "17.1.2", | ||
"@commitlint/config-conventional": "17.1.0", | ||
"@babel/cli": "7.22.15", | ||
"@babel/core": "7.22.19", | ||
"@babel/plugin-transform-runtime": "7.22.15", | ||
"@babel/preset-env": "7.22.15", | ||
"@babel/preset-react": "7.22.15", | ||
"@commitlint/cli": "17.7.1", | ||
"@commitlint/config-conventional": "17.7.0", | ||
"@testing-library/react": "12.1.5", | ||
"@testing-library/react-hooks": "7.0.2", | ||
"@types/jest": "29.0.3", | ||
"@types/node": "18.7.23", | ||
"@types/react": "18.0.21", | ||
"@types/react-dom": "18.0.6", | ||
"axios": "0.27.2", | ||
"@types/jest": "29.5.4", | ||
"@types/node": "20.6.1", | ||
"@types/react": "18.2.20", | ||
"@types/react-dom": "18.2.7", | ||
"axios": "1.5.0", | ||
"cross-env": "7.0.3", | ||
"eslint": "8.24.0", | ||
"eslint-config-prettier": "8.5.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"eslint-plugin-prettier": "4.2.1", | ||
"eslint-plugin-react": "7.31.8", | ||
"eslint": "8.49.0", | ||
"eslint-config-prettier": "9.0.0", | ||
"eslint-plugin-import": "2.28.1", | ||
"eslint-plugin-prettier": "5.0.0", | ||
"eslint-plugin-react": "7.33.2", | ||
"eslint-plugin-react-hooks": "4.6.0", | ||
"husky": "^8.0.1", | ||
"jest": "26.6.3", | ||
"lint-staged": "13.0.3", | ||
"jest": "29.7.0", | ||
"jest-environment-jsdom": "^29.6.4", | ||
"lint-staged": "14.0.1", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "2.7.1", | ||
"prettier": "3.0.3", | ||
"react": "17.0.2", | ||
"react-dom": "17.0.2", | ||
"react-test-renderer": "17.0.2", | ||
"rimraf": "3.0.2", | ||
"rimraf": "5.0.1", | ||
"shx": "0.3.4", | ||
"standard-version": "9.5.0", | ||
"ts-jest": "26.5.6", | ||
"tsd": "^0.24.1", | ||
"typescript": "4.8.4" | ||
"ts-jest": "29.1.1", | ||
"tsd": "^0.29.0", | ||
"typescript": "5.2.2" | ||
}, | ||
@@ -81,0 +82,0 @@ "lint-staged": { |
@@ -25,2 +25,7 @@ > The license of this software has changed to AWISC - Anti War ISC license | ||
## Version information | ||
- `axios-hooks@5.x` is compatible with `axios@1.x` | ||
- `axios-hooks@4.x` and below are compatible with `axios@0.x` | ||
## Quick Start | ||
@@ -111,2 +116,5 @@ | ||
> [!IMPORTANT] | ||
> Default caching behavior can interfere with test isolation. Read the [testing](#testing) section for more information. | ||
**Returns** | ||
@@ -240,13 +248,11 @@ | ||
const [ | ||
{ data: putData, loading: putLoading, error: putError }, | ||
executePut | ||
] = useAxios( | ||
{ | ||
url: 'https://reqres.in/api/users/1', | ||
method: 'PUT' | ||
}, | ||
{ manual: true } | ||
) | ||
const [{ data: putData, loading: putLoading, error: putError }, executePut] = | ||
useAxios( | ||
{ | ||
url: 'https://reqres.in/api/users/1', | ||
method: 'PUT' | ||
}, | ||
{ manual: true } | ||
) | ||
function updateData() { | ||
@@ -410,2 +416,14 @@ executePut({ | ||
## Testing | ||
Testing components that make use of the `useAxios` hook are susceptible to test isolation leakage because of default caching behavior. The following snippets can be used to disable caching while testing: | ||
### react-testing-library | ||
```js | ||
beforeAll(() => { | ||
useAxios.configure({ cache: false }) | ||
}) | ||
``` | ||
## Promises | ||
@@ -412,0 +430,0 @@ |
@@ -9,3 +9,3 @@ import { | ||
} from 'axios' | ||
import LRUCache from 'lru-cache' | ||
import { LRUCache } from 'lru-cache' | ||
@@ -12,0 +12,0 @@ export interface ResponseValues<TResponse, TBody, TError> { |
import React from 'react' | ||
import StaticAxios from 'axios' | ||
import LRU from 'lru-cache' | ||
import StaticAxios, { isCancel } from 'axios' | ||
import { LRUCache } from 'lru-cache' | ||
import { dequal as deepEqual } from 'dequal/lite' | ||
@@ -72,3 +72,3 @@ | ||
function resetConfigure() { | ||
cache = new LRU({ max: 500 }) | ||
cache = new LRUCache({ max: 500 }) | ||
axiosInstance = StaticAxios | ||
@@ -193,3 +193,3 @@ defaultOptions = DEFAULT_OPTIONS | ||
} catch (err) { | ||
if (!StaticAxios.isCancel(err)) { | ||
if (!isCancel(err)) { | ||
dispatch({ type: actions.REQUEST_END, payload: err, error: true }) | ||
@@ -222,3 +222,3 @@ } | ||
const cancelSourceRef = React.useRef() | ||
const abortControllerRef = React.useRef() | ||
@@ -235,8 +235,8 @@ const [state, dispatch] = React.useReducer( | ||
const cancelOutstandingRequest = React.useCallback(() => { | ||
if (cancelSourceRef.current) { | ||
cancelSourceRef.current.cancel() | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort() | ||
} | ||
}, []) | ||
const withCancelToken = React.useCallback( | ||
const withAbortSignal = React.useCallback( | ||
config => { | ||
@@ -247,5 +247,5 @@ if (options.autoCancel) { | ||
cancelSourceRef.current = StaticAxios.CancelToken.source() | ||
abortControllerRef.current = new AbortController() | ||
config.cancelToken = cancelSourceRef.current.token | ||
config.signal = abortControllerRef.current.signal | ||
@@ -259,3 +259,3 @@ return config | ||
if (!options.manual) { | ||
request(withCancelToken(config), options, dispatch).catch(() => {}) | ||
request(withAbortSignal(config), options, dispatch).catch(() => {}) | ||
} | ||
@@ -268,3 +268,3 @@ | ||
} | ||
}, [config, options, withCancelToken, cancelOutstandingRequest]) | ||
}, [config, options, withAbortSignal, cancelOutstandingRequest]) | ||
@@ -276,3 +276,3 @@ const refetch = React.useCallback( | ||
return request( | ||
withCancelToken({ | ||
withAbortSignal({ | ||
...config, | ||
@@ -285,3 +285,3 @@ ...(isReactEvent(configOverride) ? null : configOverride) | ||
}, | ||
[config, withCancelToken] | ||
[config, withAbortSignal] | ||
) | ||
@@ -288,0 +288,0 @@ |
47458
440
35
870
+ Added@babel/runtime@7.22.15(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removed@babel/runtime@7.19.0(transitive)
- Removedlru-cache@7.18.3(transitive)
- Removedregenerator-runtime@0.13.11(transitive)
Updated@babel/runtime@7.22.15
Updatedlru-cache@^10.0.1