UNPKG

tspace-spear

Version:

tspace-spear is a lightweight, high-performance API framework for Node.js that leverages the native HTTP server and supports uWebSockets.js (C++) for maximum speed and efficiency.

53 lines (52 loc) 2.36 kB
import type { AnyRoutes, RoutesWithMethod, ResponseType, RequestInput, OptionalIfEmpty, ApiResponse } from "./types"; /** * Type-safe HTTP client built on top of the native Fetch API. * * `ApiClient` provides end-to-end type safety for your API routes, * including: * * - `params` typing * - `query` typing * - `body` typing * - typed file uploads * - fully inferred response types * * Route types are inferred from your server route definitions, * giving you autocomplete and compile-time validation across * the entire request lifecycle. * * @template TRoutes Application route definitions. * * @example * ```ts * import app from '../server/app'; * * const client = new ApiClient<typeof app.contract>() * * const res = await client.get("/cats", { * query: { * id: "1", * }, * }) * * // fully typed response * if(res.ok) * console.log(res.cats) * ``` */ declare class ApiClient<TRoutes extends AnyRoutes> { private baseURL; constructor(baseURL: string); private request; get<TPath extends RoutesWithMethod<TRoutes, "GET">>(path: TPath, ...args: OptionalIfEmpty<RequestInput<TRoutes, TPath, "GET">>): Promise<ApiResponse<ResponseType<TRoutes, TPath, "GET">>>; post<TPath extends RoutesWithMethod<TRoutes, "POST">>(path: TPath, ...args: OptionalIfEmpty<RequestInput<TRoutes, TPath, "POST">>): Promise<ApiResponse<ResponseType<TRoutes, TPath, "POST">>>; put<TPath extends RoutesWithMethod<TRoutes, "PUT">>(path: TPath, ...args: OptionalIfEmpty<RequestInput<TRoutes, TPath, "PUT">>): Promise<ApiResponse<ResponseType<TRoutes, TPath, "PUT">>>; patch<TPath extends RoutesWithMethod<TRoutes, "PATCH">>(path: TPath, ...args: OptionalIfEmpty<RequestInput<TRoutes, TPath, "PATCH">>): Promise<ApiResponse<ResponseType<TRoutes, TPath, "PATCH">>>; delete<TPath extends RoutesWithMethod<TRoutes, "DELETE">>(path: TPath, ...args: OptionalIfEmpty<RequestInput<TRoutes, TPath, "DELETE">>): Promise<ApiResponse<ResponseType<TRoutes, TPath, "DELETE">>>; upload<TMethod extends "POST" | "PUT" | "PATCH" = "POST", TPath extends RoutesWithMethod<TRoutes, TMethod> = RoutesWithMethod<TRoutes, TMethod>>(path: TPath, options: { method?: TMethod; formdata: FormData; }): Promise<ApiResponse<ResponseType<TRoutes, TPath, TMethod>>>; } export { ApiClient }; export default ApiClient;