kypi
Version:
Type-safe, ergonomic API client builder for TypeScript & React based on ky.
98 lines (97 loc) • 6.13 kB
TypeScript
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 { type AfterResponseHook, ApiClientOptions, type BeforeErrorHook, type BeforeRequestHook, type BeforeRetryHook, type BeforeRetryState, ClientOf, Endpoint, EndpointGroup, HTTPError as HTTPError$1, type Hooks, HttpMethod, type Input, type KyInstance, type KyRequest, type KyResponse, type NormalizedOptions, type Options, type Progress, type ResponsePromise, type RetryOptions, type SearchParamsOption, TimeoutError as TimeoutError$1, adel as adel$1, aget as aget$1, ahead as ahead$1, apatch as apatch$1, apost as apost$1, aput as aput$1, authed as authed$1, client as client$1, del as del$1, endpoint as endpoint$1, get as get$1, head as head$1, ky as ky$1, patch as patch$1, post as post$1, put as put$1 };