UNPKG

kypi

Version:

Type-safe, ergonomic API client builder for TypeScript & React based on ky.

98 lines (97 loc) 6.05 kB
import ky, { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, BeforeRetryHook, BeforeRetryState, HTTPError, Hooks, Input, KyInstance, KyRequest, KyResponse, NormalizedOptions, Options, Progress, ResponsePromise, RetryOptions, SearchParamsOption, TimeoutError } from "ky"; //#region src/index.d.ts type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'; type Endpoint<In = unknown, Out = unknown, Params = any, Query = any> = { method: HttpMethod; url: string; auth?: boolean; }; interface EndpointGroup { [k: string]: Endpoint | EndpointGroup; } type InputFor<In, Params, Query> = Params extends undefined ? Query extends undefined ? In extends void | undefined ? undefined : In : In extends void | undefined ? { query: Query; } : { query: Query; body: In; } : Query extends undefined ? In extends void | undefined ? { params: Params; } : { params: Params; body: In; } : In extends void | undefined ? { params: Params; query: Query; } : { params: Params; query: Query; body: In; }; type CallSig<In, Out, Params, Query> = InputFor<In, Params, Query> extends undefined ? (kyOptions?: Options) => ResponsePromise<Out> : (input: InputFor<In, Params, Query>, kyOptions?: Options) => ResponsePromise<Out>; type ClientOf<G extends EndpointGroup> = { [K in keyof G]: G[K] extends Endpoint<infer I, infer O, infer P, infer Q> ? CallSig<I, O, P, Q> : G[K] extends EndpointGroup ? ClientOf<G[K]> : never }; interface ApiClientOptions<E extends EndpointGroup> { baseUrl: string; getToken?: () => string | null | Promise<string | null>; endpoints: E; onError?: (error: HTTPError) => void; kyInstance?: KyInstance; } type EpOpts = Omit<Endpoint, 'method' | 'url' | '__params'>; /** * Creates an endpoint definition. * * @param method - HTTP method (e.g., 'get', 'post', etc.) * @param url - URL path for the endpoint * @param opts - Optional parameters, including `auth` to indicate if authentication is required */ declare const endpoint: <In, Out, Params = undefined, Query = undefined>(method: HttpMethod, url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** * Same as `endpoint`, but it creates an endpoint that requires authentication. * * Will use the provided `getToken` function to retrieve the token and add it to the `Authorization` * header. */ declare const authed: <In, Out, Params = undefined, Query = undefined>(method: HttpMethod, url: string) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP GET endpoint. */ declare const get: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP POST endpoint. */ declare const post: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP PUT endpoint. */ declare const put: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP PATCH endpoint. */ declare const patch: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP HEAD endpoint. */ declare const head: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an HTTP DELETE endpoint. */ declare const del: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string, opts?: EpOpts) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP GET endpoint. */ declare const aget: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP POST endpoint. */ declare const apost: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP PUT endpoint. */ declare const aput: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP PATCH endpoint. */ declare const apatch: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP HEAD endpoint. */ declare const ahead: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** Creates an authed HTTP DELETE endpoint. */ declare const adel: <In = undefined, Out = unknown, Params = undefined, Query = undefined>(url: string) => Endpoint<In, Out, Params, Query>; /** * Creates a client for the given endpoint group. * * @param options - Options for the API client. * @param options.baseUrl - The base URL for the API * @param options.getToken - Optional function to retrieve an authentication token * @param options.endpoints - The endpoint group definition * @param options.onError - Optional onError hook, will be called if the request throws an error * @param options.kyInstance - Optional custom Ky instance to use, defaults to the default instance */ declare function client<E extends EndpointGroup>({ baseUrl, getToken, onError, endpoints, kyInstance }: ApiClientOptions<E>): ClientOf<E>; //#endregion export { authed as A, TimeoutError as C, apatch as D, ahead as E, head as F, ky as I, patch as L, del as M, endpoint as N, apost as O, get as P, post as R, SearchParamsOption as S, aget as T, NormalizedOptions as _, BeforeRetryHook as a, ResponsePromise as b, Endpoint as c, Hooks as d, HttpMethod as f, KyResponse as g, KyRequest as h, BeforeRequestHook as i, client as j, aput as k, EndpointGroup as l, KyInstance as m, ApiClientOptions as n, BeforeRetryState as o, Input as p, BeforeErrorHook as r, ClientOf as s, AfterResponseHook as t, HTTPError as u, Options as v, adel as w, RetryOptions as x, Progress as y, put as z };