diff --git a/package.json b/package.json index 03295e1..b1d0cd7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-rest-request", - "version": "0.5.2", + "version": "0.5.3", "description": "Minimalistic REST API client for React inspired by Apollo", "readme": "README.md", "main": "./target/index.js", diff --git a/src/endpoint.ts b/src/endpoint.ts index 16570a5..520cf9f 100644 --- a/src/endpoint.ts +++ b/src/endpoint.ts @@ -8,7 +8,7 @@ export enum Method { DELETE = 'DELETE', } -export type Endpoint = Readonly<{ +export type Endpoint = Readonly<{ _?: V; // Temporary hack to extract the type of variables. Do not use it in real endpoints. method: Method; url: string | ((params: P) => string); @@ -16,7 +16,7 @@ export type Endpoint = Readonly<{ transformResponseData?: (data: any) => R; }> -export type AnyEndpoint = Endpoint; +export type AnyEndpoint = Endpoint export type ExtractEndpointResponse = E extends Endpoint ? R : E extends Endpoint ? R : never; export type ExtractEndpointVariables = E extends Endpoint ? V : E extends Endpoint ? V : never; diff --git a/target/endpoint.d.ts b/target/endpoint.d.ts index 3fb8732..54e2b09 100644 --- a/target/endpoint.d.ts +++ b/target/endpoint.d.ts @@ -6,12 +6,14 @@ export declare enum Method { PATCH = "PATCH", DELETE = "DELETE" } -export declare type Endpoint = Readonly<{ +export declare type Endpoint = Readonly<{ + _?: V; method: Method; url: string | ((params: P) => string); headers?: Record; transformResponseData?: (data: any) => R; }>; +export declare type AnyEndpoint = Endpoint; export declare type ExtractEndpointResponse = E extends Endpoint ? R : E extends Endpoint ? R : never; export declare type ExtractEndpointVariables = E extends Endpoint ? V : E extends Endpoint ? V : never; export declare type ExtractEndpointParams = E extends Endpoint ? P : never; diff --git a/target/lazy-request-hook.d.ts b/target/lazy-request-hook.d.ts index 34e8248..85eb71d 100644 --- a/target/lazy-request-hook.d.ts +++ b/target/lazy-request-hook.d.ts @@ -1,19 +1,20 @@ -import { Endpoint, ExtractEndpointParams, ExtractEndpointResponse, ExtractEndpointVariables } from './endpoint'; +import { AnyEndpoint, ExtractEndpointParams, ExtractEndpointResponse, ExtractEndpointVariables } from './endpoint'; import { PublicRequestState } from './reducer'; import { ClientResponse } from './client'; -export declare type LazyRequestConfig = Readonly<{ +export declare type LazyRequestConfig = Readonly<{ variables?: V; params?: P; headers?: Record; onComplete?: (data: R) => unknown; onFailure?: (res: ClientResponse) => unknown; }>; -export declare type LazyRequestHandlerConfig = Readonly & { +export declare type LazyRequestConfigFromEndpoint = LazyRequestConfig, ExtractEndpointVariables, ExtractEndpointParams>; +export declare type LazyRequestHandlerConfig = Readonly & { force?: boolean; }>; -export declare type RequestHandler = (config?: LazyRequestHandlerConfig) => Promise; +export declare type RequestHandler = (config?: LazyRequestHandlerConfig) => Promise | null>; export declare type RefetchRequestHandler = () => void; -export declare type PublicRequestStateWithRefetch = PublicRequestState & { +export declare type PublicRequestStateWithRefetch = PublicRequestState> & { refetch: RefetchRequestHandler; }; -export declare function useLazyRequest, R = ExtractEndpointResponse, V = ExtractEndpointVariables, P = ExtractEndpointParams>(endpoint: E, config?: LazyRequestConfig): [RequestHandler, PublicRequestStateWithRefetch]; +export declare function useLazyRequest(endpoint: E, config?: LazyRequestConfigFromEndpoint): [RequestHandler, PublicRequestStateWithRefetch]; diff --git a/target/reducer.d.ts b/target/reducer.d.ts index 0b0892f..bf94a2c 100644 --- a/target/reducer.d.ts +++ b/target/reducer.d.ts @@ -1,3 +1,4 @@ +/// import { ClientResponse } from './client'; export declare type PublicRequestState = Readonly<{ data: R | null; @@ -21,6 +22,7 @@ export declare type RequestAction = { type: 'failure'; response: ClientResponse; }; +export declare type RequestReducer = React.Reducer, RequestAction>; export declare function requestReducer(state: RequestState, action: RequestAction): { loading: boolean; isCalled: boolean; diff --git a/target/request-hook.d.ts b/target/request-hook.d.ts index 0644b01..23106ca 100644 --- a/target/request-hook.d.ts +++ b/target/request-hook.d.ts @@ -1,6 +1,6 @@ -import { Endpoint, ExtractEndpointParams, ExtractEndpointResponse, ExtractEndpointVariables } from './endpoint'; -import { LazyRequestConfig } from './lazy-request-hook'; -export declare type RequestConfig = Readonly & { +import { AnyEndpoint } from './endpoint'; +import { LazyRequestConfigFromEndpoint } from './lazy-request-hook'; +export declare type RequestConfigFromEndpoint = Readonly & { skip?: boolean; }>; -export declare function useRequest, R = ExtractEndpointResponse, V = ExtractEndpointVariables, P = ExtractEndpointParams>(endpoint: E, config?: RequestConfig): import("./lazy-request-hook").PublicRequestStateWithRefetch; +export declare function useRequest(endpoint: E, config?: RequestConfigFromEndpoint): import("./lazy-request-hook").PublicRequestStateWithRefetch;