import { ApiResponse } from "./runtime"; /** * Type to access a response's data property for a given status. */ declare type DataType = T extends { status: S; } ? T["data"] : never; /** * Object with methods to handle possible status codes of an ApiResponse. */ declare type ResponseHandler = { [P in T["status"]]: (res: DataType) => any; } & { default?: (status: number, data: any) => any; }; /** * Utility function to handle different status codes. * * Example: * * const userId = await handle(api.register({ email, password }), { * 200: (user: User) => user.id, * 400: (err: string) => console.log(err), * }) **/ export declare function handle>(promise: Promise, handler: H): Promise>; declare const SUCCESS_CODES: readonly [200, 201, 202, 204]; declare type SuccessCodes = typeof SUCCESS_CODES[number]; declare type SuccessResponse = DataType; /** * Utility function to directly return any successful response * and throw a HttpError otherwise. * * Example: * * try { * const userId = await ok(api.register({ email, password })); * } * catch (err) { * console.log(err.status) * } */ export declare function ok(promise: Promise): Promise>; export declare type Args = T extends (...args: infer U) => any ? U : any; export declare type ApiFunction = (...args: any[]) => Promise; export declare type AsyncReturnType = T extends (...args: any[]) => Promise ? V : never; export declare type OkResponse = SuccessResponse>; export declare type Okify = (...args: Args) => Promise>; /** * Utility function to wrap an API function with `ok(...)`. */ export declare function okify(fn: T): Okify; declare type OptimisticApi = { [K in keyof T]: T[K] extends ApiFunction ? Okify : T[K]; }; /** * Utility to `okify` each function of an API. */ export declare function optimistic>(api: T): OptimisticApi; export declare class HttpError extends Error { status: number; data?: any; constructor(status: number, data: any); } export {};