@axios-use/react
Advanced tools
Comparing version 6.5.0 to 6.6.0
@@ -0,1 +1,10 @@ | ||
## [6.6.0](https://github.com/axios-use/react/compare/v6.5.0...v6.6.0) (2024-03-10) | ||
### Features | ||
* **useRequest:** `getResponseItem` option ([#22](https://github.com/axios-use/axios-use-react/pull/22)) | ||
## [6.5.0](https://github.com/axios-use/react/compare/v6.4.2...v6.5.0) (2023-10-21) | ||
@@ -2,0 +11,0 @@ |
@@ -12,2 +12,4 @@ import React from "react"; | ||
customCreateReqError?: (err: any) => RequestError<T, any, E>; | ||
/** custom `data` value. @default response['data'] */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -14,0 +16,0 @@ export type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; |
@@ -30,2 +30,4 @@ var __assign = (this && this.__assign) || function () { | ||
cacheKey: defaultCacheKeyGenerator, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
getResponseItem: function (res) { return res === null || res === void 0 ? void 0 : res.data; }, | ||
}; | ||
@@ -35,4 +37,18 @@ export var RequestContext = createContext(defaultConfig); | ||
export var RequestProvider = function (props) { | ||
var children = props.children, instance = props.instance, cache = props.cache, cacheKey = props.cacheKey, cacheFilter = props.cacheFilter, customCreateReqError = props.customCreateReqError, rest = __rest(props, ["children", "instance", "cache", "cacheKey", "cacheFilter", "customCreateReqError"]); | ||
var providerValue = useMemo(function () { return ({ instance: instance, cache: cache, cacheKey: cacheKey, cacheFilter: cacheFilter, customCreateReqError: customCreateReqError }); }, [cache, cacheFilter, cacheKey, customCreateReqError, instance]); | ||
var children = props.children, instance = props.instance, cache = props.cache, cacheKey = props.cacheKey, cacheFilter = props.cacheFilter, customCreateReqError = props.customCreateReqError, getResponseItem = props.getResponseItem, rest = __rest(props, ["children", "instance", "cache", "cacheKey", "cacheFilter", "customCreateReqError", "getResponseItem"]); | ||
var providerValue = useMemo(function () { return ({ | ||
instance: instance, | ||
cache: cache, | ||
cacheKey: cacheKey, | ||
cacheFilter: cacheFilter, | ||
customCreateReqError: customCreateReqError, | ||
getResponseItem: getResponseItem, | ||
}); }, [ | ||
cache, | ||
cacheFilter, | ||
cacheKey, | ||
customCreateReqError, | ||
getResponseItem, | ||
instance, | ||
]); | ||
return (React.createElement(RequestContext.Provider, __assign({ value: providerValue }, rest), children)); | ||
@@ -39,0 +55,0 @@ }; |
@@ -5,2 +5,4 @@ import type { Canceler, AxiosInstance } from "axios"; | ||
instance?: AxiosInstance; | ||
/** custom returns the value of `data`. @default (r) => r?.data */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -7,0 +9,0 @@ export type UseRequestResult<TRequest extends Request> = [ |
@@ -57,6 +57,9 @@ var __assign = (this && this.__assign) || function () { | ||
.then(function (response) { | ||
var _a; | ||
var _a, _b; | ||
removeCancelToken(source.token); | ||
(_a = onCompletedRef.current) === null || _a === void 0 ? void 0 : _a.call(onCompletedRef, response.data, response); | ||
return [response.data, response]; | ||
var _data = ((options === null || options === void 0 ? void 0 : options.getResponseItem) | ||
? options.getResponseItem(response) | ||
: (_a = RequestConfig.getResponseItem) === null || _a === void 0 ? void 0 : _a.call(RequestConfig, response)); | ||
(_b = onCompletedRef.current) === null || _b === void 0 ? void 0 : _b.call(onCompletedRef, _data, response); | ||
return [_data, response]; | ||
}) | ||
@@ -63,0 +66,0 @@ .catch(function (err) { |
@@ -20,3 +20,3 @@ import type { Canceler } from "axios"; | ||
]; | ||
export type UseResourceOptions<T extends Request> = Pick<RequestContextConfig<Payload<T>>, "cache" | "cacheFilter" | "instance"> & RequestCallbackFn<T> & { | ||
export type UseResourceOptions<T extends Request> = Pick<RequestContextConfig<Payload<T>>, "cache" | "cacheFilter" | "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
cacheKey?: CacheKey | CacheKeyFn<T>; | ||
@@ -23,0 +23,0 @@ /** Conditional Fetching */ |
@@ -127,2 +127,3 @@ var __assign = (this && this.__assign) || function () { | ||
instance: options === null || options === void 0 ? void 0 : options.instance, | ||
getResponseItem: options === null || options === void 0 ? void 0 : options.getResponseItem, | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
@@ -129,0 +130,0 @@ var _b = useReducer(getNextState, __assign({ data: cacheData !== null && cacheData !== void 0 ? cacheData : undefined, isLoading: getDefaultStateLoading(requestParams, options === null || options === void 0 ? void 0 : options.filter) }, options === null || options === void 0 ? void 0 : options.defaultState)), state = _b[0], dispatch = _b[1]; |
@@ -12,2 +12,4 @@ import React from "react"; | ||
customCreateReqError?: (err: any) => RequestError<T, any, E>; | ||
/** custom `data` value. @default response['data'] */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -14,0 +16,0 @@ export type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; |
@@ -56,2 +56,4 @@ "use strict"; | ||
cacheKey: cache_1.defaultCacheKeyGenerator, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
getResponseItem: function (res) { return res === null || res === void 0 ? void 0 : res.data; }, | ||
}; | ||
@@ -61,4 +63,18 @@ exports.RequestContext = (0, react_1.createContext)(defaultConfig); | ||
var RequestProvider = function (props) { | ||
var children = props.children, instance = props.instance, cache = props.cache, cacheKey = props.cacheKey, cacheFilter = props.cacheFilter, customCreateReqError = props.customCreateReqError, rest = __rest(props, ["children", "instance", "cache", "cacheKey", "cacheFilter", "customCreateReqError"]); | ||
var providerValue = (0, react_1.useMemo)(function () { return ({ instance: instance, cache: cache, cacheKey: cacheKey, cacheFilter: cacheFilter, customCreateReqError: customCreateReqError }); }, [cache, cacheFilter, cacheKey, customCreateReqError, instance]); | ||
var children = props.children, instance = props.instance, cache = props.cache, cacheKey = props.cacheKey, cacheFilter = props.cacheFilter, customCreateReqError = props.customCreateReqError, getResponseItem = props.getResponseItem, rest = __rest(props, ["children", "instance", "cache", "cacheKey", "cacheFilter", "customCreateReqError", "getResponseItem"]); | ||
var providerValue = (0, react_1.useMemo)(function () { return ({ | ||
instance: instance, | ||
cache: cache, | ||
cacheKey: cacheKey, | ||
cacheFilter: cacheFilter, | ||
customCreateReqError: customCreateReqError, | ||
getResponseItem: getResponseItem, | ||
}); }, [ | ||
cache, | ||
cacheFilter, | ||
cacheKey, | ||
customCreateReqError, | ||
getResponseItem, | ||
instance, | ||
]); | ||
return (react_1.default.createElement(exports.RequestContext.Provider, __assign({ value: providerValue }, rest), children)); | ||
@@ -65,0 +81,0 @@ }; |
@@ -5,2 +5,4 @@ import type { Canceler, AxiosInstance } from "axios"; | ||
instance?: AxiosInstance; | ||
/** custom returns the value of `data`. @default (r) => r?.data */ | ||
getResponseItem?: (res?: any) => unknown; | ||
}; | ||
@@ -7,0 +9,0 @@ export type UseRequestResult<TRequest extends Request> = [ |
@@ -63,6 +63,9 @@ "use strict"; | ||
.then(function (response) { | ||
var _a; | ||
var _a, _b; | ||
removeCancelToken(source.token); | ||
(_a = onCompletedRef.current) === null || _a === void 0 ? void 0 : _a.call(onCompletedRef, response.data, response); | ||
return [response.data, response]; | ||
var _data = ((options === null || options === void 0 ? void 0 : options.getResponseItem) | ||
? options.getResponseItem(response) | ||
: (_a = RequestConfig.getResponseItem) === null || _a === void 0 ? void 0 : _a.call(RequestConfig, response)); | ||
(_b = onCompletedRef.current) === null || _b === void 0 ? void 0 : _b.call(onCompletedRef, _data, response); | ||
return [_data, response]; | ||
}) | ||
@@ -69,0 +72,0 @@ .catch(function (err) { |
@@ -20,3 +20,3 @@ import type { Canceler } from "axios"; | ||
]; | ||
export type UseResourceOptions<T extends Request> = Pick<RequestContextConfig<Payload<T>>, "cache" | "cacheFilter" | "instance"> & RequestCallbackFn<T> & { | ||
export type UseResourceOptions<T extends Request> = Pick<RequestContextConfig<Payload<T>>, "cache" | "cacheFilter" | "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
cacheKey?: CacheKey | CacheKeyFn<T>; | ||
@@ -23,0 +23,0 @@ /** Conditional Fetching */ |
@@ -130,2 +130,3 @@ "use strict"; | ||
instance: options === null || options === void 0 ? void 0 : options.instance, | ||
getResponseItem: options === null || options === void 0 ? void 0 : options.getResponseItem, | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
@@ -132,0 +133,0 @@ var _b = (0, react_1.useReducer)(getNextState, __assign({ data: cacheData !== null && cacheData !== void 0 ? cacheData : undefined, isLoading: getDefaultStateLoading(requestParams, options === null || options === void 0 ? void 0 : options.filter) }, options === null || options === void 0 ? void 0 : options.defaultState)), state = _b[0], dispatch = _b[1]; |
{ | ||
"name": "@axios-use/react", | ||
"version": "6.5.0", | ||
"version": "6.6.0", | ||
"description": "A React hook plugin for Axios. Lightweight, cancelable and less change", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -74,9 +74,10 @@ English | [简体中文](./README.zh-CN.md) | ||
| config | type | explain | | ||
| -------------------- | --------------- | ---------------------------------------------------------- | | ||
| instance | object | axios instance | | ||
| cache | object \| false | Customized cache collections. Or close. (**Default on**) | | ||
| cacheKey | function | Global custom formatted cache keys | | ||
| cacheFilter | function | Global callback function to decide whether to cache or not | | ||
| customCreateReqError | function | Custom format error data | | ||
| config | type | default | explain | | ||
| -------------------- | --------------- | ------------------------ | ----------------------------------------------------------- | | ||
| instance | object | axios | axios instance | | ||
| cache | object \| false | \_ttlcache | Customized cache collections. Or close. (**Default on**) | | ||
| cacheKey | function | defaultCacheKeyGenerator | Global custom formatted cache keys | | ||
| cacheFilter | function | - | Global callback function to decide whether to cache or not | | ||
| customCreateReqError | function | - | Custom format error data | | ||
| getResponseItem | function | `(r) => r.data` | custom `data` value. The default value is response['data']. | | ||
@@ -83,0 +84,0 @@ ### useRequest |
99471
1431
324