@axios-use/react
Advanced tools
Comparing version 6.4.2 to 6.5.0
@@ -0,1 +1,10 @@ | ||
## [6.5.0](https://github.com/axios-use/react/compare/v6.4.2...v6.5.0) (2023-10-21) | ||
### Features | ||
* **request:** export `_request` to custom response type ([#21](https://github.com/axios-use/axios-use-react/pull/21)) | ||
## [6.4.2](https://github.com/axios-use/react/compare/v6.4.1...v6.4.2) (2023-05-04) | ||
@@ -2,0 +11,0 @@ |
import type { Resource } from "./request"; | ||
export declare type CacheKey = string | number; | ||
export declare type CacheKeyFn<T = any, D = any> = (config: Resource<T, D>) => CacheKey; | ||
export declare type CacheFilter<T = any, D = any> = (config: Resource<T, D>) => boolean; | ||
export type CacheKey = string | number; | ||
export type CacheKeyFn<T = any, D = any> = (config: Resource<T, D>) => CacheKey; | ||
export type CacheFilter<T = any, D = any> = (config: Resource<T, D>) => boolean; | ||
export interface Cache<T = any> { | ||
@@ -11,5 +11,5 @@ get(key: CacheKey): T | null | undefined; | ||
} | ||
export declare const defaultCacheKeyGenerator: <T = any, D = any>(config: Resource<T, D>) => CacheKey; | ||
export declare const defaultCacheKeyGenerator: <T = any, D = any>(config: Resource<T, D, never, never, never>) => CacheKey; | ||
/** @deprecated Use `defaultCacheKeyGenerator` instead */ | ||
export declare const createCacheKey: null; | ||
export declare function wrapCache<T = any>(provider: Cache<T>): Cache; |
import { hash } from "object-code"; | ||
var SLASHES_REGEX = /^\/|\/$/g; | ||
var SLASHES_REGEX = /(?:^\/)|(?:\/$)/g; | ||
export var defaultCacheKeyGenerator = function (config) { | ||
@@ -4,0 +4,0 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
import type { AxiosRequestConfig, AxiosResponse, AxiosError, Canceler } from "axios"; | ||
/** @deprecated No longer use. Use `AxiosResponse` instead */ | ||
export declare type AxiosRestResponse<D = any> = Omit<AxiosResponse<unknown, D>, "data">; | ||
export interface Resource<TPayload, D = any> extends AxiosRequestConfig<D> { | ||
payload?: TPayload; | ||
export type AxiosRestResponse<D = any> = Omit<AxiosResponse<unknown, D>, "data">; | ||
export interface Resource<T = AxiosResponse, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never> extends AxiosRequestConfig<D> { | ||
_payload?: T; | ||
_payload_item?: [K3] extends [never] ? [K2] extends [never] ? [K1] extends [never] ? T extends AxiosResponse<infer DD> | { | ||
data?: infer DD; | ||
} ? DD : undefined : T[K1] : T[K1][K2] : T[K1][K2][K3]; | ||
} | ||
export declare type Request<T = any, D = any> = (...args: any[]) => Resource<T, D>; | ||
export declare type Payload<T extends Request> = ReturnType<T>["payload"]; | ||
export declare type BodyData<T extends Request> = ReturnType<T>["data"]; | ||
export type Request<T = any, D = any, K1 extends keyof T = any, K2 extends keyof T[K1] = any, K3 extends keyof T[K1][K2] = any> = (...args: any[]) => Resource<T, D, K1, K2, K3>; | ||
type _AnyKeyValue<T, K> = K extends keyof T ? T[K] : any; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? _AnyKeyValue<ReturnType<T>, "_payload_item"> : T extends Request<AxiosResponse> ? Exclude<_AnyKeyValue<ReturnType<T>, "_payload">, undefined> : _AnyKeyValue<ReturnType<T>, "_payload">; | ||
export type BodyData<T extends Request> = _AnyKeyValue<ReturnType<T>, "data">; | ||
/** @deprecated No longer use. Use `BodyData` instead */ | ||
export declare type CData<T extends Request> = BodyData<T>; | ||
export interface RequestFactory<T extends Request> { | ||
(...args: Parameters<T>): { | ||
cancel: Canceler; | ||
ready: () => Promise<[Payload<T>, AxiosResponse<Payload<T>, BodyData<T>>]>; | ||
}; | ||
} | ||
export interface RequestDispatcher<T extends Request> { | ||
(...args: Parameters<T>): Canceler; | ||
} | ||
export type CData<T extends Request> = BodyData<T>; | ||
export type RequestFactory<T extends Request> = (...args: Parameters<T>) => { | ||
cancel: Canceler; | ||
ready: () => Promise<readonly [Payload<T, true>, Payload<T>]>; | ||
}; | ||
export type RequestDispatcher<T extends Request> = (...args: Parameters<T>) => Canceler; | ||
/** | ||
* Normalize the error response returned from `@axios-use/vue` | ||
*/ | ||
export interface RequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>> { | ||
@@ -28,7 +31,23 @@ data?: T; | ||
} | ||
export declare type RequestCallbackFn<T extends Request> = { | ||
onCompleted?: (data: Payload<T>, response: AxiosResponse<Payload<T>, BodyData<T>>) => void; | ||
onError?: (err?: RequestError<Payload<T>, BodyData<T>>) => void; | ||
export type RequestCallbackFn<T extends Request> = { | ||
/** | ||
* A callback function that's called when your request successfully completes with zero errors. | ||
* This function is passed the request's result `data` and `response`. | ||
*/ | ||
onCompleted?: (data: Payload<T, true>, response: Payload<T>) => void; | ||
/** | ||
* A callback function that's called when the request encounters one or more errors. | ||
* This function is passed an `RequestError` object that contains either a networkError object or a `AxiosError`, depending on the error(s) that occurred. | ||
*/ | ||
onError?: (err: RequestError<Payload<T>, BodyData<T>>) => void; | ||
}; | ||
export declare function request<T, D = any>(config: AxiosRequestConfig<D>): Resource<T, D>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare function _request<T, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never>(config: AxiosRequestConfig<D>): Resource<T, D, K1, K2, K3>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare const request: <T = any, D = any>(config: AxiosRequestConfig<D>) => Resource<AxiosResponse<T, D>, D, never, never, never>; | ||
export declare function createRequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>>(error: E): RequestError<T, D, E>; | ||
export {}; |
import axios from "axios"; | ||
export function request(config) { | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export function _request(config) { | ||
return config; | ||
} | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export var request = function (config) { | ||
return _request(config); | ||
}; | ||
export function createRequestError(error) { | ||
@@ -6,0 +15,0 @@ var _a, _b, _c, _d, _e, _f, _g; |
@@ -6,3 +6,3 @@ import React from "react"; | ||
import type { Cache, CacheKeyFn, CacheFilter } from "./cache"; | ||
export declare type RequestContextConfig<T = any, E = any> = { | ||
export type RequestContextConfig<T = any, E = any> = { | ||
instance?: AxiosInstance; | ||
@@ -14,8 +14,8 @@ cache?: Cache<T> | false; | ||
}; | ||
export declare type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; | ||
export type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; | ||
export declare const RequestContext: React.Context<RequestContextValue<any, any>>; | ||
export declare const RequestProvider: { | ||
<T>(props: React.PropsWithChildren<RequestContextConfig<T, any>>): JSX.Element; | ||
<T>(props: React.PropsWithChildren<RequestContextConfig<T, any>>): React.JSX.Element; | ||
defaultProps: RequestContextConfig<any, any>; | ||
}; | ||
export declare const RequestConsumer: React.Consumer<RequestContextValue<any, any>>; |
@@ -23,4 +23,3 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import React from "react"; | ||
import { createContext } from "react"; | ||
import React, { createContext, useMemo } from "react"; | ||
import { defaultCacheKeyGenerator, wrapCache } from "./cache"; | ||
@@ -37,11 +36,6 @@ import { _ttlcache } from "./cachettl"; | ||
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"]); | ||
return (React.createElement(RequestContext.Provider, __assign({ value: { | ||
instance: instance, | ||
cache: cache, | ||
cacheKey: cacheKey, | ||
cacheFilter: cacheFilter, | ||
customCreateReqError: customCreateReqError, | ||
} }, rest), children)); | ||
var providerValue = useMemo(function () { return ({ instance: instance, cache: cache, cacheKey: cacheKey, cacheFilter: cacheFilter, customCreateReqError: customCreateReqError }); }, [cache, cacheFilter, cacheKey, customCreateReqError, instance]); | ||
return (React.createElement(RequestContext.Provider, __assign({ value: providerValue }, rest), children)); | ||
}; | ||
RequestProvider.defaultProps = defaultConfig; | ||
export var RequestConsumer = RequestContext.Consumer; |
import type { Canceler, AxiosInstance } from "axios"; | ||
import type { RequestFactory, RequestCallbackFn, Request } from "./request"; | ||
export declare type UseRequestOptions<TRequest extends Request> = RequestCallbackFn<TRequest> & { | ||
export type UseRequestOptions<TRequest extends Request> = RequestCallbackFn<TRequest> & { | ||
instance?: AxiosInstance; | ||
}; | ||
export declare type UseRequestResult<TRequest extends Request> = [ | ||
export type UseRequestResult<TRequest extends Request> = [ | ||
RequestFactory<TRequest>, | ||
@@ -8,0 +8,0 @@ { |
@@ -1,21 +0,21 @@ | ||
import type { Canceler, AxiosResponse } from "axios"; | ||
import type { Canceler } from "axios"; | ||
import type { Payload, BodyData, RequestError, Request, RequestDispatcher, RequestCallbackFn } from "./request"; | ||
import type { RequestContextConfig } from "./requestContext"; | ||
import type { CacheKey, CacheKeyFn } from "./cache"; | ||
declare type RequestState<TRequest extends Request> = { | ||
data?: Payload<TRequest>; | ||
response?: AxiosResponse<BodyData<TRequest>>; | ||
error?: RequestError<Payload<TRequest>, BodyData<TRequest>>; | ||
type RequestState<T extends Request> = { | ||
data?: Payload<T, true>; | ||
response?: Payload<T>; | ||
error?: RequestError<Payload<T>, BodyData<T>>; | ||
isLoading?: boolean; | ||
/** @deprecated Use `response` instead */ | ||
other?: AxiosResponse<BodyData<TRequest>>; | ||
other?: Payload<T>; | ||
}; | ||
export declare type UseResourceResult<TRequest extends Request> = [ | ||
RequestState<TRequest> & { | ||
export type UseResourceResult<T extends Request> = [ | ||
RequestState<T> & { | ||
cancel: Canceler; | ||
}, | ||
RequestDispatcher<TRequest>, | ||
RequestDispatcher<T>, | ||
() => Canceler | undefined | ||
]; | ||
export declare 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"> & RequestCallbackFn<T> & { | ||
cacheKey?: CacheKey | CacheKeyFn<T>; | ||
@@ -22,0 +22,0 @@ /** Conditional Fetching */ |
@@ -27,3 +27,3 @@ var __assign = (this && this.__assign) || function () { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
@@ -69,3 +69,3 @@ if (y = 0, t) op = [op[0] & 2, t.value]; | ||
error: action.type === "error" ? action.error : undefined, | ||
isLoading: action.type === "start" ? true : false, | ||
isLoading: action.type === "start", | ||
// will be deleted | ||
@@ -84,3 +84,3 @@ other: response, | ||
catch (error) { | ||
return undefined; | ||
return null; | ||
} | ||
@@ -90,16 +90,17 @@ // eslint-disable-next-line react-hooks/exhaustive-deps | ||
var requestCache = useMemo(function () { | ||
var _a, _b; | ||
var _a; | ||
var filter = (options === null || options === void 0 ? void 0 : options.cacheFilter) || RequestConfig.cacheFilter; | ||
var _cache = (_a = options === null || options === void 0 ? void 0 : options.cache) !== null && _a !== void 0 ? _a : RequestConfig.cache; | ||
if (_cache == undefined) { | ||
return null; | ||
} | ||
if (filter && typeof filter === "function") { | ||
if (fnOptions && filter(fnOptions)) { | ||
return (_a = options === null || options === void 0 ? void 0 : options.cache) !== null && _a !== void 0 ? _a : RequestConfig.cache; | ||
return _cache; | ||
} | ||
return undefined; | ||
} | ||
if ((fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) === null || | ||
(fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) === undefined || | ||
/^get$/i.test(fnOptions.method)) { | ||
return (_b = options === null || options === void 0 ? void 0 : options.cache) !== null && _b !== void 0 ? _b : RequestConfig.cache; | ||
if ((fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) == null || /^get$/i.test(fnOptions.method)) { | ||
return _cache; | ||
} | ||
return undefined; | ||
return null; | ||
}, [ | ||
@@ -117,9 +118,10 @@ RequestConfig.cache, | ||
((_a = getStrByFn(options === null || options === void 0 ? void 0 : options.cacheKey, fnOptions)) !== null && _a !== void 0 ? _a : getStrByFn(RequestConfig.cacheKey, fnOptions))) || | ||
undefined); | ||
null); | ||
}, [RequestConfig.cacheKey, fnOptions, options === null || options === void 0 ? void 0 : options.cacheKey, requestCache]); | ||
var cacheData = useMemo(function () { | ||
var _a; | ||
return requestCache && cacheKey && typeof requestCache.get === "function" | ||
? (_a = requestCache.get(cacheKey)) !== null && _a !== void 0 ? _a : undefined | ||
: undefined; | ||
if (requestCache && cacheKey && typeof requestCache.get === "function") { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return requestCache.get(cacheKey); | ||
} | ||
return null; | ||
}, [cacheKey, requestCache]); | ||
@@ -131,3 +133,3 @@ var _a = useRequest(fn, { | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
var _b = useReducer(getNextState, __assign({ data: cacheData, 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]; | ||
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]; | ||
var request = useCallback(function () { | ||
@@ -187,4 +189,3 @@ var args = []; | ||
useEffect(function () { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
var canceller = function () { }; | ||
var canceller = function () { return undefined; }; | ||
if (requestParams) { | ||
@@ -191,0 +192,0 @@ var _c = refreshRefFn.current(); |
import type { Resource } from "./request"; | ||
export declare type CacheKey = string | number; | ||
export declare type CacheKeyFn<T = any, D = any> = (config: Resource<T, D>) => CacheKey; | ||
export declare type CacheFilter<T = any, D = any> = (config: Resource<T, D>) => boolean; | ||
export type CacheKey = string | number; | ||
export type CacheKeyFn<T = any, D = any> = (config: Resource<T, D>) => CacheKey; | ||
export type CacheFilter<T = any, D = any> = (config: Resource<T, D>) => boolean; | ||
export interface Cache<T = any> { | ||
@@ -11,5 +11,5 @@ get(key: CacheKey): T | null | undefined; | ||
} | ||
export declare const defaultCacheKeyGenerator: <T = any, D = any>(config: Resource<T, D>) => CacheKey; | ||
export declare const defaultCacheKeyGenerator: <T = any, D = any>(config: Resource<T, D, never, never, never>) => CacheKey; | ||
/** @deprecated Use `defaultCacheKeyGenerator` instead */ | ||
export declare const createCacheKey: null; | ||
export declare function wrapCache<T = any>(provider: Cache<T>): Cache; |
@@ -5,3 +5,3 @@ "use strict"; | ||
var object_code_1 = require("object-code"); | ||
var SLASHES_REGEX = /^\/|\/$/g; | ||
var SLASHES_REGEX = /(?:^\/)|(?:\/$)/g; | ||
var defaultCacheKeyGenerator = function (config) { | ||
@@ -8,0 +8,0 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
import type { AxiosRequestConfig, AxiosResponse, AxiosError, Canceler } from "axios"; | ||
/** @deprecated No longer use. Use `AxiosResponse` instead */ | ||
export declare type AxiosRestResponse<D = any> = Omit<AxiosResponse<unknown, D>, "data">; | ||
export interface Resource<TPayload, D = any> extends AxiosRequestConfig<D> { | ||
payload?: TPayload; | ||
export type AxiosRestResponse<D = any> = Omit<AxiosResponse<unknown, D>, "data">; | ||
export interface Resource<T = AxiosResponse, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never> extends AxiosRequestConfig<D> { | ||
_payload?: T; | ||
_payload_item?: [K3] extends [never] ? [K2] extends [never] ? [K1] extends [never] ? T extends AxiosResponse<infer DD> | { | ||
data?: infer DD; | ||
} ? DD : undefined : T[K1] : T[K1][K2] : T[K1][K2][K3]; | ||
} | ||
export declare type Request<T = any, D = any> = (...args: any[]) => Resource<T, D>; | ||
export declare type Payload<T extends Request> = ReturnType<T>["payload"]; | ||
export declare type BodyData<T extends Request> = ReturnType<T>["data"]; | ||
export type Request<T = any, D = any, K1 extends keyof T = any, K2 extends keyof T[K1] = any, K3 extends keyof T[K1][K2] = any> = (...args: any[]) => Resource<T, D, K1, K2, K3>; | ||
type _AnyKeyValue<T, K> = K extends keyof T ? T[K] : any; | ||
export type Payload<T extends Request, Check = false> = Check extends true ? _AnyKeyValue<ReturnType<T>, "_payload_item"> : T extends Request<AxiosResponse> ? Exclude<_AnyKeyValue<ReturnType<T>, "_payload">, undefined> : _AnyKeyValue<ReturnType<T>, "_payload">; | ||
export type BodyData<T extends Request> = _AnyKeyValue<ReturnType<T>, "data">; | ||
/** @deprecated No longer use. Use `BodyData` instead */ | ||
export declare type CData<T extends Request> = BodyData<T>; | ||
export interface RequestFactory<T extends Request> { | ||
(...args: Parameters<T>): { | ||
cancel: Canceler; | ||
ready: () => Promise<[Payload<T>, AxiosResponse<Payload<T>, BodyData<T>>]>; | ||
}; | ||
} | ||
export interface RequestDispatcher<T extends Request> { | ||
(...args: Parameters<T>): Canceler; | ||
} | ||
export type CData<T extends Request> = BodyData<T>; | ||
export type RequestFactory<T extends Request> = (...args: Parameters<T>) => { | ||
cancel: Canceler; | ||
ready: () => Promise<readonly [Payload<T, true>, Payload<T>]>; | ||
}; | ||
export type RequestDispatcher<T extends Request> = (...args: Parameters<T>) => Canceler; | ||
/** | ||
* Normalize the error response returned from `@axios-use/vue` | ||
*/ | ||
export interface RequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>> { | ||
@@ -28,7 +31,23 @@ data?: T; | ||
} | ||
export declare type RequestCallbackFn<T extends Request> = { | ||
onCompleted?: (data: Payload<T>, response: AxiosResponse<Payload<T>, BodyData<T>>) => void; | ||
onError?: (err?: RequestError<Payload<T>, BodyData<T>>) => void; | ||
export type RequestCallbackFn<T extends Request> = { | ||
/** | ||
* A callback function that's called when your request successfully completes with zero errors. | ||
* This function is passed the request's result `data` and `response`. | ||
*/ | ||
onCompleted?: (data: Payload<T, true>, response: Payload<T>) => void; | ||
/** | ||
* A callback function that's called when the request encounters one or more errors. | ||
* This function is passed an `RequestError` object that contains either a networkError object or a `AxiosError`, depending on the error(s) that occurred. | ||
*/ | ||
onError?: (err: RequestError<Payload<T>, BodyData<T>>) => void; | ||
}; | ||
export declare function request<T, D = any>(config: AxiosRequestConfig<D>): Resource<T, D>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare function _request<T, D = any, K1 extends keyof T = never, K2 extends keyof T[K1] = never, K3 extends keyof T[K1][K2] = never>(config: AxiosRequestConfig<D>): Resource<T, D, K1, K2, K3>; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export declare const request: <T = any, D = any>(config: AxiosRequestConfig<D>) => Resource<AxiosResponse<T, D>, D, never, never, never>; | ||
export declare function createRequestError<T = any, D = any, E = AxiosError<T, D> | AxiosResponse<T, D>>(error: E): RequestError<T, D, E>; | ||
export {}; |
@@ -6,7 +6,17 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRequestError = exports.request = void 0; | ||
exports.createRequestError = exports.request = exports._request = void 0; | ||
var axios_1 = __importDefault(require("axios")); | ||
function request(config) { | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
function _request(config) { | ||
return config; | ||
} | ||
exports._request = _request; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
var request = function (config) { | ||
return _request(config); | ||
}; | ||
exports.request = request; | ||
@@ -13,0 +23,0 @@ function createRequestError(error) { |
@@ -6,3 +6,3 @@ import React from "react"; | ||
import type { Cache, CacheKeyFn, CacheFilter } from "./cache"; | ||
export declare type RequestContextConfig<T = any, E = any> = { | ||
export type RequestContextConfig<T = any, E = any> = { | ||
instance?: AxiosInstance; | ||
@@ -14,8 +14,8 @@ cache?: Cache<T> | false; | ||
}; | ||
export declare type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; | ||
export type RequestContextValue<T = any, E = any> = RequestContextConfig<T, E>; | ||
export declare const RequestContext: React.Context<RequestContextValue<any, any>>; | ||
export declare const RequestProvider: { | ||
<T>(props: React.PropsWithChildren<RequestContextConfig<T, any>>): JSX.Element; | ||
<T>(props: React.PropsWithChildren<RequestContextConfig<T, any>>): React.JSX.Element; | ||
defaultProps: RequestContextConfig<any, any>; | ||
}; | ||
export declare const RequestConsumer: React.Consumer<RequestContextValue<any, any>>; |
@@ -13,2 +13,25 @@ "use strict"; | ||
}; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
@@ -25,9 +48,5 @@ var t = {}; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RequestConsumer = exports.RequestProvider = exports.RequestContext = void 0; | ||
var react_1 = __importDefault(require("react")); | ||
var react_2 = require("react"); | ||
var react_1 = __importStar(require("react")); | ||
var cache_1 = require("./cache"); | ||
@@ -40,13 +59,8 @@ var cachettl_1 = require("./cachettl"); | ||
}; | ||
exports.RequestContext = (0, react_2.createContext)(defaultConfig); | ||
exports.RequestContext = (0, react_1.createContext)(defaultConfig); | ||
exports.RequestContext.displayName = "RequestHookConfig"; | ||
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"]); | ||
return (react_1.default.createElement(exports.RequestContext.Provider, __assign({ value: { | ||
instance: instance, | ||
cache: cache, | ||
cacheKey: cacheKey, | ||
cacheFilter: cacheFilter, | ||
customCreateReqError: customCreateReqError, | ||
} }, rest), children)); | ||
var providerValue = (0, react_1.useMemo)(function () { return ({ instance: instance, cache: cache, cacheKey: cacheKey, cacheFilter: cacheFilter, customCreateReqError: customCreateReqError }); }, [cache, cacheFilter, cacheKey, customCreateReqError, instance]); | ||
return (react_1.default.createElement(exports.RequestContext.Provider, __assign({ value: providerValue }, rest), children)); | ||
}; | ||
@@ -53,0 +67,0 @@ exports.RequestProvider = RequestProvider; |
import type { Canceler, AxiosInstance } from "axios"; | ||
import type { RequestFactory, RequestCallbackFn, Request } from "./request"; | ||
export declare type UseRequestOptions<TRequest extends Request> = RequestCallbackFn<TRequest> & { | ||
export type UseRequestOptions<TRequest extends Request> = RequestCallbackFn<TRequest> & { | ||
instance?: AxiosInstance; | ||
}; | ||
export declare type UseRequestResult<TRequest extends Request> = [ | ||
export type UseRequestResult<TRequest extends Request> = [ | ||
RequestFactory<TRequest>, | ||
@@ -8,0 +8,0 @@ { |
@@ -1,21 +0,21 @@ | ||
import type { Canceler, AxiosResponse } from "axios"; | ||
import type { Canceler } from "axios"; | ||
import type { Payload, BodyData, RequestError, Request, RequestDispatcher, RequestCallbackFn } from "./request"; | ||
import type { RequestContextConfig } from "./requestContext"; | ||
import type { CacheKey, CacheKeyFn } from "./cache"; | ||
declare type RequestState<TRequest extends Request> = { | ||
data?: Payload<TRequest>; | ||
response?: AxiosResponse<BodyData<TRequest>>; | ||
error?: RequestError<Payload<TRequest>, BodyData<TRequest>>; | ||
type RequestState<T extends Request> = { | ||
data?: Payload<T, true>; | ||
response?: Payload<T>; | ||
error?: RequestError<Payload<T>, BodyData<T>>; | ||
isLoading?: boolean; | ||
/** @deprecated Use `response` instead */ | ||
other?: AxiosResponse<BodyData<TRequest>>; | ||
other?: Payload<T>; | ||
}; | ||
export declare type UseResourceResult<TRequest extends Request> = [ | ||
RequestState<TRequest> & { | ||
export type UseResourceResult<T extends Request> = [ | ||
RequestState<T> & { | ||
cancel: Canceler; | ||
}, | ||
RequestDispatcher<TRequest>, | ||
RequestDispatcher<T>, | ||
() => Canceler | undefined | ||
]; | ||
export declare 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"> & RequestCallbackFn<T> & { | ||
cacheKey?: CacheKey | CacheKeyFn<T>; | ||
@@ -22,0 +22,0 @@ /** Conditional Fetching */ |
@@ -28,3 +28,3 @@ "use strict"; | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
@@ -72,3 +72,3 @@ if (y = 0, t) op = [op[0] & 2, t.value]; | ||
error: action.type === "error" ? action.error : undefined, | ||
isLoading: action.type === "start" ? true : false, | ||
isLoading: action.type === "start", | ||
// will be deleted | ||
@@ -87,3 +87,3 @@ other: response, | ||
catch (error) { | ||
return undefined; | ||
return null; | ||
} | ||
@@ -93,16 +93,17 @@ // eslint-disable-next-line react-hooks/exhaustive-deps | ||
var requestCache = (0, react_1.useMemo)(function () { | ||
var _a, _b; | ||
var _a; | ||
var filter = (options === null || options === void 0 ? void 0 : options.cacheFilter) || RequestConfig.cacheFilter; | ||
var _cache = (_a = options === null || options === void 0 ? void 0 : options.cache) !== null && _a !== void 0 ? _a : RequestConfig.cache; | ||
if (_cache == undefined) { | ||
return null; | ||
} | ||
if (filter && typeof filter === "function") { | ||
if (fnOptions && filter(fnOptions)) { | ||
return (_a = options === null || options === void 0 ? void 0 : options.cache) !== null && _a !== void 0 ? _a : RequestConfig.cache; | ||
return _cache; | ||
} | ||
return undefined; | ||
} | ||
if ((fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) === null || | ||
(fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) === undefined || | ||
/^get$/i.test(fnOptions.method)) { | ||
return (_b = options === null || options === void 0 ? void 0 : options.cache) !== null && _b !== void 0 ? _b : RequestConfig.cache; | ||
if ((fnOptions === null || fnOptions === void 0 ? void 0 : fnOptions.method) == null || /^get$/i.test(fnOptions.method)) { | ||
return _cache; | ||
} | ||
return undefined; | ||
return null; | ||
}, [ | ||
@@ -120,9 +121,10 @@ RequestConfig.cache, | ||
((_a = (0, utils_1.getStrByFn)(options === null || options === void 0 ? void 0 : options.cacheKey, fnOptions)) !== null && _a !== void 0 ? _a : (0, utils_1.getStrByFn)(RequestConfig.cacheKey, fnOptions))) || | ||
undefined); | ||
null); | ||
}, [RequestConfig.cacheKey, fnOptions, options === null || options === void 0 ? void 0 : options.cacheKey, requestCache]); | ||
var cacheData = (0, react_1.useMemo)(function () { | ||
var _a; | ||
return requestCache && cacheKey && typeof requestCache.get === "function" | ||
? (_a = requestCache.get(cacheKey)) !== null && _a !== void 0 ? _a : undefined | ||
: undefined; | ||
if (requestCache && cacheKey && typeof requestCache.get === "function") { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return requestCache.get(cacheKey); | ||
} | ||
return null; | ||
}, [cacheKey, requestCache]); | ||
@@ -134,3 +136,3 @@ var _a = (0, useRequest_1.useRequest)(fn, { | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
var _b = (0, react_1.useReducer)(getNextState, __assign({ data: cacheData, 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]; | ||
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]; | ||
var request = (0, react_1.useCallback)(function () { | ||
@@ -190,4 +192,3 @@ var args = []; | ||
(0, react_1.useEffect)(function () { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
var canceller = function () { }; | ||
var canceller = function () { return undefined; }; | ||
if (requestParams) { | ||
@@ -194,0 +195,0 @@ var _c = refreshRefFn.current(); |
{ | ||
"name": "@axios-use/react", | ||
"version": "6.4.2", | ||
"version": "6.5.0", | ||
"description": "A React hook plugin for Axios. Lightweight, cancelable and less change", | ||
@@ -43,4 +43,3 @@ "contributors": [ | ||
"devDependencies": { | ||
"@testing-library/react": "^13.2.0", | ||
"@testing-library/react-hooks": "^8.0.0", | ||
"@testing-library/react": "^14.0.0", | ||
"@types/jest": "^27.4.0", | ||
@@ -47,0 +46,0 @@ "@types/react": "^18.0.9", |
@@ -288,2 +288,20 @@ English | [简体中文](./README.zh-CN.md) | ||
custom response type. (if you change the response's return value. like axios.interceptors.response) | ||
```ts | ||
import { request, _request } from "@axios-use/react"; | ||
const [reqState] = useResource(() => request<DataType>({ url: `/users` }), []); | ||
// AxiosResponse<DataType> | ||
reqState.response; | ||
// DataType | ||
reqState.data; | ||
// custom response type | ||
const [reqState] = useResource(() => _request<MyWrapper<DataType>>({ url: `/users` }), []); | ||
// MyWrapper<DataType> | ||
reqState.response; | ||
// MyWrapper<DataType>["data"]. maybe `undefined` type. | ||
reqState.data; | ||
``` | ||
#### createRequestError | ||
@@ -290,0 +308,0 @@ |
@@ -288,2 +288,20 @@ 简体中文 | [English](./README.md) | ||
自定义 response 类型. (如果你有手动修改 response 数据的需求。 axios.interceptors.response) | ||
```ts | ||
import { request, _request } from "@axios-use/react"; | ||
const [reqState] = useResource(() => request<DataType>({ url: `/users` }), []); | ||
// AxiosResponse<DataType> | ||
reqState.response; | ||
// DataType | ||
reqState.data; | ||
// 自定义 response 类型 | ||
const [reqState] = useResource(() => _request<MyWrapper<DataType>>({ url: `/users` }), []); | ||
// MyWrapper<DataType> | ||
reqState.response; | ||
// MyWrapper<DataType>["data"]. maybe `undefined` type. | ||
reqState.data; | ||
``` | ||
#### createRequestError | ||
@@ -290,0 +308,0 @@ |
96919
22
1383
323