diff --git a/src/client.ts b/src/client.ts index bc68391..1bf4ecb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -105,7 +105,7 @@ export class Client { error: err, canceled, } as ResponseWithError, - canceled ? {} : err.json() + {} ]); } ) diff --git a/target/client-hook.d.ts b/target/client-hook.d.ts deleted file mode 100644 index fdc1ea1..0000000 --- a/target/client-hook.d.ts +++ /dev/null @@ -1 +0,0 @@ -export declare function useClient(): import("./client").Client[]; diff --git a/target/client-hook.js b/target/client-hook.js deleted file mode 100644 index cb3d958..0000000 --- a/target/client-hook.js +++ /dev/null @@ -1,5 +0,0 @@ -import { useRequestContext } from './request-context'; -export function useClient() { - const { client } = useRequestContext(); - return [client]; -} diff --git a/target/client.js b/target/client.js index ff0b078..daf2d90 100644 --- a/target/client.js +++ b/target/client.js @@ -65,7 +65,7 @@ export class Client { error: err, canceled, }, - canceled ? {} : err.json() + {} ]); }) .then(([res, data]) => { diff --git a/target/lazy-request-hook.d.ts b/target/lazy-request-hook.d.ts deleted file mode 100644 index 4d1fb1f..0000000 --- a/target/lazy-request-hook.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { AnyEndpoint, ExtractEndpointParams, ExtractEndpointResponse, ExtractEndpointVariables } from './endpoint'; -import { PublicRequestState } from './reducer'; -import { ClientResponse } from './client'; -export declare type LazyRequestConfig = Readonly<{ - variables?: V; - params?: P; - headers?: Record; - onComplete?: (data: R) => unknown; - onFailure?: (res: ClientResponse) => unknown; -}>; -export declare type LazyRequestConfigFromEndpoint = LazyRequestConfig, ExtractEndpointVariables, ExtractEndpointParams>; -export declare type LazyRequestHandlerConfig = Readonly & { - force?: boolean; -}>; -export declare type RequestHandler = (config?: LazyRequestHandlerConfig) => Promise | null>; -export declare type PublicRequestStateWithActions = PublicRequestState> & { - refetch: () => void; - cancel: () => void; -}; -export declare function useLazyRequest(endpoint: E, config?: LazyRequestConfigFromEndpoint): [RequestHandler, PublicRequestStateWithActions]; diff --git a/target/lazy-request-hook.js b/target/lazy-request-hook.js deleted file mode 100644 index 3d95644..0000000 --- a/target/lazy-request-hook.js +++ /dev/null @@ -1,93 +0,0 @@ -import React from 'react'; -import invariant from 'tiny-invariant'; -import isEqual from 'lodash.isequal'; -import { useClient } from './client-hook'; -import { requestReducer } from './reducer'; -import { useRequestContext } from './request-context'; -import { isFunction } from './misc'; -export function useLazyRequest(endpoint, config) { - const [client] = useClient(); - const { defaultHeaders } = useRequestContext(); - const [state, dispatch] = React.useReducer(requestReducer, { - data: null, - loading: false, - isCalled: false, - }); - const [prevHandlerConfig, setPrevHandlerConfig] = React.useState(null); - const transformResponseData = React.useCallback((data) => { - return isFunction(endpoint.transformResponseData) ? - endpoint.transformResponseData(data) - : data; - }, [endpoint]); - const handler = React.useCallback((handlerConfig) => { - var _a, _b, _c; - if ((state === null || state === void 0 ? void 0 : state.loading) || (state === null || state === void 0 ? void 0 : state.isCanceled)) { - return Promise.resolve(null); - } - let params; - let endpointUrl; - let isSameRequest = true; - if (isFunction(endpoint.url)) { - params = (_a = handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.params) !== null && _a !== void 0 ? _a : config === null || config === void 0 ? void 0 : config.params; - invariant(params, 'Endpoind required params'); - endpointUrl = endpoint.url(params); - isSameRequest = !!(state === null || state === void 0 ? void 0 : state.prevParams) && isEqual(state.prevParams, params); - } - else { - endpointUrl = endpoint.url; - } - const variables = Object.assign(Object.assign({}, config === null || config === void 0 ? void 0 : config.variables), handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.variables); - const headers = Object.assign(Object.assign(Object.assign(Object.assign({}, defaultHeaders), endpoint.headers), config === null || config === void 0 ? void 0 : config.headers), handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.headers); - if (state.isCalled - && isSameRequest - && (state === null || state === void 0 ? void 0 : state.prevVariables) && isEqual(state.prevVariables, variables) - && (state === null || state === void 0 ? void 0 : state.prevHeaders) && isEqual(state.prevHeaders, headers) - && !(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; - dispatch({ type: 'call', headers, variables, params }); - setPrevHandlerConfig(handlerConfig !== null && handlerConfig !== void 0 ? handlerConfig : {}); - return client - .request(Object.assign(Object.assign({}, endpoint), { url: endpointUrl, headers, - variables, - transformResponseData })) - .then((response) => { - dispatch({ type: 'success', response }); - if (isFunction(onComplete)) { - onComplete(response.data); - } - return response.data; - }, (response) => { - dispatch({ type: 'failure', response }); - if (!response.canceled && isFunction(onFailure)) { - onFailure(response); - } - return null; - }); - }, [state, config, client, endpoint, defaultHeaders, transformResponseData]); - const refetch = React.useCallback(() => { - if (prevHandlerConfig != null) { - handler(Object.assign(Object.assign({}, prevHandlerConfig), { force: true })); - } - }, [handler, prevHandlerConfig]); - React.useEffect(() => { - return () => { - dispatch({ type: 'cancel' }); - client.cancelRequest(); - }; - }, [client]); - return [ - handler, - { - data: state.data, - loading: state.loading, - isCalled: state.isCalled, - isCanceled: state.isCanceled, - error: state.error, - refetch, - cancel: client.cancelRequest.bind(client), - }, - ]; -} diff --git a/target/request-context.d.ts b/target/request-context.d.ts deleted file mode 100644 index 41f0e8f..0000000 --- a/target/request-context.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import React from 'react'; -import { Client } from './client'; -export declare type RequestContextData = Readonly<{ - client: Client; - defaultHeaders?: Record; -}>; -export declare type RequestProviderProps = Readonly>; -export declare function RequestProvider({ client, defaultHeaders, children }: RequestProviderProps): JSX.Element; -export declare function useRequestContext(): Readonly<{ - client: Client; - defaultHeaders?: Record | undefined; -}>; diff --git a/target/request-context.js b/target/request-context.js deleted file mode 100644 index 6b254db..0000000 --- a/target/request-context.js +++ /dev/null @@ -1,11 +0,0 @@ -import React from 'react'; -import invariant from 'tiny-invariant'; -const RequestContext = React.createContext(null); -export function RequestProvider({ client, defaultHeaders, children }) { - return (React.createElement(RequestContext.Provider, { value: { client, defaultHeaders } }, children)); -} -export function useRequestContext() { - const context = React.useContext(RequestContext); - invariant(context, 'useRequestContext() must be a child of '); - return context; -} diff --git a/target/request-hook.d.ts b/target/request-hook.d.ts deleted file mode 100644 index c6a53d2..0000000 --- a/target/request-hook.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { AnyEndpoint } from './endpoint'; -import { LazyRequestConfigFromEndpoint } from './lazy-request-hook'; -export declare type RequestConfigFromEndpoint = Readonly & { - skip?: boolean; -}>; -export declare function useRequest(endpoint: E, config?: RequestConfigFromEndpoint): import("./lazy-request-hook").PublicRequestStateWithActions; diff --git a/target/request-hook.js b/target/request-hook.js deleted file mode 100644 index f850d24..0000000 --- a/target/request-hook.js +++ /dev/null @@ -1,15 +0,0 @@ -import React from 'react'; -import invariant from 'tiny-invariant'; -import { Method } from './endpoint'; -import { useLazyRequest } from './lazy-request-hook'; -export function useRequest(endpoint, config) { - invariant(endpoint.method !== Method.DELETE, `You cannot use useRequest with ${endpoint.method} method`); - const [handler, state] = useLazyRequest(endpoint, config); - const skip = React.useMemo(() => { var _a; return (_a = config === null || config === void 0 ? void 0 : config.skip) !== null && _a !== void 0 ? _a : false; }, [config]); - React.useEffect(() => { - if (!skip) { - handler(); - } - }, [skip, handler]); - return state; -}