diff --git a/src/lazy_request_hook.ts b/src/lazy_request_hook.ts index a45ba52..e079d60 100644 --- a/src/lazy_request_hook.ts +++ b/src/lazy_request_hook.ts @@ -104,8 +104,8 @@ export function useLazyRequest( return Promise.resolve(state.data); } - const onComplete = handlerConfig?.onComplete ?? config?.onComplete; - const onFailure = handlerConfig?.onFailure ?? config?.onFailure; + const onCompletes = [config?.onComplete, handlerConfig?.onComplete].filter(isFunction); + const onFailures = [config?.onFailure, handlerConfig?.onFailure].filter(isFunction); dispatch({ type: 'call', headers, variables, params }); @@ -123,17 +123,15 @@ export function useLazyRequest( (response) => { dispatch({ type: 'success', response }); - if (isFunction(onComplete)) { - onComplete(response.data); - } + onCompletes.forEach(cb => cb(response.data)); return response.data; }, (response: ClientResponse>) => { dispatch({ type: 'failure', response }); - if (!response.canceled && isFunction(onFailure)) { - onFailure(response); + if (!response.canceled) { + onFailures.forEach(cb => cb(response)); } return null; @@ -172,7 +170,7 @@ export function useLazyRequest( loading: state.loading, isCalled: state.isCalled, isCanceled: state.isCanceled, - error: state.error, + fetchError: state.fetchError, refetch, cancel: client.cancelRequest.bind(client), }, diff --git a/src/reducer.ts b/src/reducer.ts index 03ad523..51ca12c 100644 --- a/src/reducer.ts +++ b/src/reducer.ts @@ -5,7 +5,8 @@ export type PublicRequestState = Readonly<{ loading: boolean; isCalled: boolean; isCanceled?: boolean; - error?: Error; + response?: ClientResponse; + fetchError?: Error; }>; export type RequestState = PublicRequestState & Readonly<{ @@ -35,12 +36,13 @@ export type RequestAction = export type RequestReducer = React.Reducer, RequestAction> -export function requestReducer(state: RequestState, action: RequestAction) { +export function requestReducer(state: RequestState, action: RequestAction): RequestState { switch (action.type) { case 'call': { return { ...state, - error: undefined, + response: undefined, + fetchError: undefined, isCanceled: false, loading: true, isCalled: true, @@ -53,6 +55,7 @@ export function requestReducer(state: RequestState, action: RequestAction< return { ...state, loading: false, + response: action.response, data: action.response.data, }; } @@ -60,8 +63,9 @@ export function requestReducer(state: RequestState, action: RequestAction< return { ...state, loading: false, + response: action.response, data: null, - error: action.response.error, + fetchError: action.response.error, isCanceled: action.response.canceled, }; } @@ -69,7 +73,7 @@ export function requestReducer(state: RequestState, action: RequestAction< return { ...state, isCanceled: false, - error: undefined, + fetchError: undefined, }; } } diff --git a/target/lazy_request_hook.js b/target/lazy_request_hook.js index ac98849..893a708 100644 --- a/target/lazy_request_hook.js +++ b/target/lazy_request_hook.js @@ -20,7 +20,7 @@ export function useLazyRequest(endpoint, config) { : data; }, [endpoint]); const handler = React.useCallback((handlerConfig) => { - var _a, _b, _c; + var _a; if ((state === null || state === void 0 ? void 0 : state.loading) || (state === null || state === void 0 ? void 0 : state.isCanceled)) { return Promise.resolve(null); } @@ -45,8 +45,8 @@ export function useLazyRequest(endpoint, config) { && !(handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.force)) { return Promise.resolve(state.data); } - const onComplete = (_b = handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.onComplete) !== null && _b !== void 0 ? _b : config === null || config === void 0 ? void 0 : config.onComplete; - const onFailure = (_c = handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.onFailure) !== null && _c !== void 0 ? _c : config === null || config === void 0 ? void 0 : config.onFailure; + const onCompletes = [config === null || config === void 0 ? void 0 : config.onComplete, handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.onComplete].filter(isFunction); + const onFailures = [config === null || config === void 0 ? void 0 : config.onFailure, handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.onFailure].filter(isFunction); dispatch({ type: 'call', headers, variables, params }); setPrevHandlerConfig(handlerConfig !== null && handlerConfig !== void 0 ? handlerConfig : {}); return client @@ -55,14 +55,12 @@ export function useLazyRequest(endpoint, config) { transformResponseData })) .then((response) => { dispatch({ type: 'success', response }); - if (isFunction(onComplete)) { - onComplete(response.data); - } + onCompletes.forEach(cb => cb(response.data)); return response.data; }, (response) => { dispatch({ type: 'failure', response }); - if (!response.canceled && isFunction(onFailure)) { - onFailure(response); + if (!response.canceled) { + onFailures.forEach(cb => cb(response)); } return null; }); @@ -85,7 +83,7 @@ export function useLazyRequest(endpoint, config) { loading: state.loading, isCalled: state.isCalled, isCanceled: state.isCanceled, - error: state.error, + fetchError: state.fetchError, refetch, cancel: client.cancelRequest.bind(client), }, diff --git a/target/reducer.d.ts b/target/reducer.d.ts index 3cec641..ee44c80 100644 --- a/target/reducer.d.ts +++ b/target/reducer.d.ts @@ -5,7 +5,8 @@ export declare type PublicRequestState = Readonly<{ loading: boolean; isCalled: boolean; isCanceled?: boolean; - error?: Error; + response?: ClientResponse; + fetchError?: Error; }>; export declare type RequestState = PublicRequestState & Readonly<{ prevHeaders?: Record; @@ -27,31 +28,4 @@ export declare type RequestAction = { type: 'cancel'; }; export declare type RequestReducer = React.Reducer, RequestAction>; -export declare function requestReducer(state: RequestState, action: RequestAction): { - loading: boolean; - data: R; - isCalled: boolean; - isCanceled?: boolean | undefined; - error?: Error | undefined; - prevHeaders?: Record | undefined; - prevVariables?: Record | undefined; - prevParams?: Record | undefined; -} | { - loading: boolean; - data: null; - error: Error | undefined; - isCanceled: boolean | undefined; - isCalled: boolean; - prevHeaders?: Record | undefined; - prevVariables?: Record | undefined; - prevParams?: Record | undefined; -} | { - isCanceled: boolean; - error: undefined; - data: R | null; - loading: boolean; - isCalled: boolean; - prevHeaders?: Record | undefined; - prevVariables?: Record | undefined; - prevParams?: Record | undefined; -}; +export declare function requestReducer(state: RequestState, action: RequestAction): RequestState; diff --git a/target/reducer.js b/target/reducer.js index 3697cdc..778d2c0 100644 --- a/target/reducer.js +++ b/target/reducer.js @@ -1,16 +1,16 @@ export function requestReducer(state, action) { switch (action.type) { case 'call': { - return Object.assign(Object.assign({}, state), { error: undefined, isCanceled: false, loading: true, isCalled: true, prevHeaders: action.headers, prevVariables: action.variables, prevParams: action.params }); + return Object.assign(Object.assign({}, state), { response: undefined, fetchError: undefined, isCanceled: false, loading: true, isCalled: true, prevHeaders: action.headers, prevVariables: action.variables, prevParams: action.params }); } case 'success': { - return Object.assign(Object.assign({}, state), { loading: false, data: action.response.data }); + return Object.assign(Object.assign({}, state), { loading: false, response: action.response, data: action.response.data }); } case 'failure': { - return Object.assign(Object.assign({}, state), { loading: false, data: null, error: action.response.error, isCanceled: action.response.canceled }); + return Object.assign(Object.assign({}, state), { loading: false, response: action.response, data: null, fetchError: action.response.error, isCanceled: action.response.canceled }); } case 'cancel': { - return Object.assign(Object.assign({}, state), { isCanceled: false, error: undefined }); + return Object.assign(Object.assign({}, state), { isCanceled: false, fetchError: undefined }); } } }