fix: change default params type to unknown
This commit is contained in:
parent
e762303d04
commit
edc88a64ff
6 changed files with 19 additions and 14 deletions
|
@ -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",
|
||||
|
|
|
@ -8,7 +8,7 @@ export enum Method {
|
|||
DELETE = 'DELETE',
|
||||
}
|
||||
|
||||
export type Endpoint<R, V, P = never> = Readonly<{
|
||||
export type Endpoint<R, V, P = unknown> = 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<R, V, P = never> = Readonly<{
|
|||
transformResponseData?: (data: any) => R;
|
||||
}>
|
||||
|
||||
export type AnyEndpoint = Endpoint<any, any, any>;
|
||||
export type AnyEndpoint = Endpoint<any, any, any>
|
||||
|
||||
export type ExtractEndpointResponse<E> = E extends Endpoint<infer R, any, any> ? R : E extends Endpoint<infer R, any> ? R : never;
|
||||
export type ExtractEndpointVariables<E> = E extends Endpoint<any, infer V, any> ? V : E extends Endpoint<any, infer V> ? V : never;
|
||||
|
|
4
target/endpoint.d.ts
vendored
4
target/endpoint.d.ts
vendored
|
@ -6,12 +6,14 @@ export declare enum Method {
|
|||
PATCH = "PATCH",
|
||||
DELETE = "DELETE"
|
||||
}
|
||||
export declare type Endpoint<R, _V, P = never> = Readonly<{
|
||||
export declare type Endpoint<R, V, P = unknown> = Readonly<{
|
||||
_?: V;
|
||||
method: Method;
|
||||
url: string | ((params: P) => string);
|
||||
headers?: Record<string, string>;
|
||||
transformResponseData?: (data: any) => R;
|
||||
}>;
|
||||
export declare type AnyEndpoint = Endpoint<any, any, any>;
|
||||
export declare type ExtractEndpointResponse<E> = E extends Endpoint<infer R, any, any> ? R : E extends Endpoint<infer R, any> ? R : never;
|
||||
export declare type ExtractEndpointVariables<E> = E extends Endpoint<any, infer V, any> ? V : E extends Endpoint<any, infer V> ? V : never;
|
||||
export declare type ExtractEndpointParams<E> = E extends Endpoint<any, any, infer P> ? P : never;
|
||||
|
|
13
target/lazy-request-hook.d.ts
vendored
13
target/lazy-request-hook.d.ts
vendored
|
@ -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<R, V, P = never> = Readonly<{
|
||||
export declare type LazyRequestConfig<R, V, P> = Readonly<{
|
||||
variables?: V;
|
||||
params?: P;
|
||||
headers?: Record<string, string>;
|
||||
onComplete?: (data: R) => unknown;
|
||||
onFailure?: (res: ClientResponse<R>) => unknown;
|
||||
}>;
|
||||
export declare type LazyRequestHandlerConfig<R, V, P> = Readonly<LazyRequestConfig<R, V, P> & {
|
||||
export declare type LazyRequestConfigFromEndpoint<E extends AnyEndpoint> = LazyRequestConfig<ExtractEndpointResponse<E>, ExtractEndpointVariables<E>, ExtractEndpointParams<E>>;
|
||||
export declare type LazyRequestHandlerConfig<E extends AnyEndpoint> = Readonly<LazyRequestConfigFromEndpoint<E> & {
|
||||
force?: boolean;
|
||||
}>;
|
||||
export declare type RequestHandler<R, V, P> = (config?: LazyRequestHandlerConfig<R, V, P>) => Promise<R | null>;
|
||||
export declare type RequestHandler<E extends AnyEndpoint> = (config?: LazyRequestHandlerConfig<E>) => Promise<ExtractEndpointResponse<E> | null>;
|
||||
export declare type RefetchRequestHandler = () => void;
|
||||
export declare type PublicRequestStateWithRefetch<R> = PublicRequestState<R> & {
|
||||
export declare type PublicRequestStateWithRefetch<E extends AnyEndpoint> = PublicRequestState<ExtractEndpointResponse<E>> & {
|
||||
refetch: RefetchRequestHandler;
|
||||
};
|
||||
export declare function useLazyRequest<E extends Endpoint<R, V, P>, R = ExtractEndpointResponse<E>, V = ExtractEndpointVariables<E>, P = ExtractEndpointParams<E>>(endpoint: E, config?: LazyRequestConfig<R, V, P>): [RequestHandler<R, V, P>, PublicRequestStateWithRefetch<R>];
|
||||
export declare function useLazyRequest<E extends AnyEndpoint>(endpoint: E, config?: LazyRequestConfigFromEndpoint<E>): [RequestHandler<E>, PublicRequestStateWithRefetch<E>];
|
||||
|
|
2
target/reducer.d.ts
vendored
2
target/reducer.d.ts
vendored
|
@ -1,3 +1,4 @@
|
|||
/// <reference types="react" />
|
||||
import { ClientResponse } from './client';
|
||||
export declare type PublicRequestState<R> = Readonly<{
|
||||
data: R | null;
|
||||
|
@ -21,6 +22,7 @@ export declare type RequestAction<R> = {
|
|||
type: 'failure';
|
||||
response: ClientResponse<R>;
|
||||
};
|
||||
export declare type RequestReducer<R> = React.Reducer<RequestState<R>, RequestAction<R>>;
|
||||
export declare function requestReducer<R>(state: RequestState<R>, action: RequestAction<R>): {
|
||||
loading: boolean;
|
||||
isCalled: boolean;
|
||||
|
|
8
target/request-hook.d.ts
vendored
8
target/request-hook.d.ts
vendored
|
@ -1,6 +1,6 @@
|
|||
import { Endpoint, ExtractEndpointParams, ExtractEndpointResponse, ExtractEndpointVariables } from './endpoint';
|
||||
import { LazyRequestConfig } from './lazy-request-hook';
|
||||
export declare type RequestConfig<R, V, P> = Readonly<LazyRequestConfig<R, V, P> & {
|
||||
import { AnyEndpoint } from './endpoint';
|
||||
import { LazyRequestConfigFromEndpoint } from './lazy-request-hook';
|
||||
export declare type RequestConfigFromEndpoint<E extends AnyEndpoint> = Readonly<LazyRequestConfigFromEndpoint<E> & {
|
||||
skip?: boolean;
|
||||
}>;
|
||||
export declare function useRequest<E extends Endpoint<R, V, P>, R = ExtractEndpointResponse<E>, V = ExtractEndpointVariables<E>, P = ExtractEndpointParams<E>>(endpoint: E, config?: RequestConfig<R, V, P>): import("./lazy-request-hook").PublicRequestStateWithRefetch<R>;
|
||||
export declare function useRequest<E extends AnyEndpoint>(endpoint: E, config?: RequestConfigFromEndpoint<E>): import("./lazy-request-hook").PublicRequestStateWithRefetch<E>;
|
||||
|
|
Reference in a new issue