This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
react-rest-request/src/endpoint.ts

32 lines
1.1 KiB
TypeScript

export enum Method {
HEAD = 'HEAD',
GET = 'GET',
PUT = 'PUT',
POST = 'POST',
PATCH = 'PATCH',
DELETE = 'DELETE',
}
export function methodCanContainBody(method: Method) {
return [Method.POST, Method.PATCH, Method.PUT].includes(method);
}
export function methodWithoutEffects(method: Method) {
return [Method.HEAD, Method.GET].includes(method);
}
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);
headers?: Record<string, string>;
transformResponseData?: (data: any) => R;
}>
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;
export type ExtractEndpointParams<E> = E extends Endpoint<any, any, infer P> ? P : never;