import { z } from 'zod'; /** * Request to be executed by the HTTP executor */ interface ExecuteRequest { method: string; url: string; headers: Record; body?: string; timeout?: number; } /** * Response from the HTTP executor */ interface ExecuteResponse { status: number; statusText: string; data: unknown; raw: TRawResponse; } /** * Interface for HTTP executors (fetch, ky, etc.) */ interface HttpExecutor { execute(request: ExecuteRequest): Promise>; } type ApiRequestSchema = Record>; type ApiResponseSchema = Record>>; type LogLevel = "error" | "warn" | "info" | "debug"; type LogMeta = Record; type LogHandler = (message: string, meta?: LogMeta) => void; /** * Optional structured logger for internal client logging. */ type Logger = Partial>; interface ApiClientLoggingOptions { logger?: Logger; } /** * Extracts path parameters from an endpoint string * @example ExtractPathParams<'/users/{id}/posts/{postId}'> → 'id' | 'postId' */ type ExtractPathParams = T extends `${string}{${infer Param}}${infer Rest}` ? Param extends `${infer Key}` ? Key | ExtractPathParams : ExtractPathParams : never; /** * Extracts params type from Request object */ type ExtractRequestParams = TRequest[TEndpoint][TMethod] extends { params: infer P; } ? P extends z.ZodTypeAny ? z.input

: never : ExtractPathParams extends never ? never : Record & string, string>; /** * Extracts query type from Request object */ type ExtractRequestQuery = TRequest[TEndpoint][TMethod] extends { query: infer Q; } ? Q extends z.ZodTypeAny ? z.input : never : never; /** * Extracts body type from Request object */ type ExtractRequestBody = TRequest[TEndpoint][TMethod] extends { body: infer B; } ? B extends z.ZodTypeAny ? z.input : never : never; /** * Helper to determine if params are required */ type ParamsRequired = TRequest[TEndpoint][TMethod] extends { params: z.ZodTypeAny; } ? true : ExtractPathParams extends never ? false : true; /** * Helper to determine if body is required */ type BodyRequired = TRequest[TEndpoint][TMethod] extends { body: z.ZodTypeAny; } ? true : false; /** * Extracts endpoints that have a specific HTTP method */ type EndpointsWithMethod = { [K in keyof TRequest]: TMethod extends keyof TRequest[K] ? K : never; }[keyof TRequest] & string; /** * Extracts all status codes from Response object for an endpoint/method */ type ExtractStatusCodes = TResponse[TEndpoint][TMethod] extends Record ? keyof TResponse[TEndpoint][TMethod] & string : never; /** * Extracts success status codes (2xx) from Response object */ type ExtractSuccessCodes = ExtractStatusCodes extends infer Codes ? Codes extends string ? Codes extends `2${string}` ? Codes : never : never : never; /** * Extracts error status codes (non-2xx) from Response object */ type ExtractErrorCodes = ExtractStatusCodes extends infer Codes ? Codes extends string ? Codes extends `2${string}` ? never : Codes : never : never; /** * Extracts response schema for a specific status code */ type ExtractResponseSchema = TResponse[TEndpoint][TMethod] extends Record ? TCode extends keyof TResponse[TEndpoint][TMethod] ? TResponse[TEndpoint][TMethod][TCode] : never : never; /** * Infers the success response body type (uses first success code, typically 200) */ type ExtractSuccessBody = ExtractSuccessCodes extends infer SuccessCode ? SuccessCode extends string ? ExtractResponseSchema extends z.ZodTypeAny ? z.infer> : never : never : never; /** * Creates a discriminated union member for a success response */ type SuccessResponse = { success: true; body: TSuccessBody; code: TCode; raw: TRawResponse; }; /** * Creates a discriminated union member for an error response with specific code */ type ErrorResponse = { success: false; error: TError; code: TCode; raw: TRawResponse; }; /** * Unexpected error response (for errors not in the spec) */ type UnexpectedErrorResponse = { success: false; error: unknown; code: number; raw?: TRawResponse; }; /** * Builds the complete discriminated union response type */ type ApiResponse = TEndpoint extends keyof TResponse ? TMethod extends keyof TResponse[TEndpoint] ? (ExtractSuccessCodes extends infer SuccessCode ? SuccessCode extends string ? ExtractResponseSchema extends z.ZodTypeAny ? SuccessResponse>, SuccessCode, TRawResponse> : never : never : never) | (ExtractErrorCodes extends infer ErrorCodes ? ErrorCodes extends string ? ExtractResponseSchema extends z.ZodTypeAny ? ErrorResponse>, ErrorCodes, TRawResponse> : never : never : never) | UnexpectedErrorResponse : UnexpectedErrorResponse : UnexpectedErrorResponse; /** * Context passed to shouldRetry callback */ type RetryContext = { /** Current attempt number (0-indexed) */ attempt: number; /** Error thrown during request (network errors, timeouts, etc.) */ error?: unknown; /** HTTP response received (for retrying based on status codes) */ response?: { status: number; statusText: string; data: unknown; }; }; /** * Request options with conditional required fields */ type RequestOptions = { timeout?: number; retries?: number; headers?: Record; /** Custom retry logic - return true to retry, false to stop */ shouldRetry?: (context: RetryContext) => boolean; } & (ExtractRequestQuery extends never ? { query?: never; } : { query?: ExtractRequestQuery; }) & (ParamsRequired extends true ? { params: ExtractRequestParams; } : ExtractRequestParams extends never ? { params?: never; } : { params?: ExtractRequestParams; }) & (BodyRequired extends true ? { body: ExtractRequestBody; } : { body?: ExtractRequestBody; }); interface ApiClientOptions extends ApiClientLoggingOptions { baseUrl: string; headers?: Record; Request: TRequest; Response: TResponse; /** * Called when Zod validation fails for request or response data. * This is only invoked for schema parse failures (i.e. when Zod would throw), * not for other validation errors like missing path params. */ onValidationError?: ApiClientValidationErrorHandler; executor: HttpExecutor; } type ApiClientValidationErrorLocation = "params" | "query" | "body" | "response"; type ApiClientValidationErrorContext = { kind: "request" | "response"; location: ApiClientValidationErrorLocation; endpoint: string; method: string; /** Human-readable context string (also used as the thrown ValidationError message). */ message: string; /** The data that failed validation. */ data: unknown; /** Zod issues produced by the schema. */ issues: z.ZodIssue[]; /** The underlying ZodError object. */ zodError: z.ZodError; /** Response-only context. */ status?: number; statusCode?: string; statusText?: string; raw?: TRawResponse; }; type ApiClientValidationErrorHandler = (context: ApiClientValidationErrorContext) => void; declare class ApiClient { readonly options: ApiClientOptions; private readonly internalLog; constructor(options: ApiClientOptions); private log; private safeInvokeOnValidationError; private validateSchema; private throwValidationError; private validateAndEncodeSchema; /** * Makes a GET request */ get>(endpoint: TEndpoint, options: RequestOptions): Promise>; /** * Makes a POST request */ post>(endpoint: TEndpoint, options: RequestOptions & { body: ExtractRequestBody; }): Promise>; /** * Makes a PUT request */ put>(endpoint: TEndpoint, options: RequestOptions & { body: ExtractRequestBody; }): Promise>; /** * Makes a PATCH request */ patch>(endpoint: TEndpoint, options: RequestOptions & { body: ExtractRequestBody; }): Promise>; /** * Makes a DELETE request */ delete>(endpoint: TEndpoint, options: RequestOptions): Promise>; /** * Internal request method */ private request; /** * Validates path parameters against Request schema */ private validatePathParams; /** * Validates query parameters against Request schema */ private validateQuery; /** * Validates body against Request schema */ private validateBody; /** * Gets path parameter names from endpoint string */ private getPathParamNames; /** * Handles the response and returns discriminated union */ private handleResponse; } declare const HTTP_CLIENT_DEBUG_NAMESPACE = "alt-stack:http-client"; /** * Base error class for API client errors */ declare class ApiClientError extends Error { readonly endpoint?: string | undefined; readonly method?: string | undefined; readonly cause?: unknown | undefined; constructor(message: string, endpoint?: string | undefined, method?: string | undefined, cause?: unknown | undefined); } /** * Unexpected error that doesn't match any defined error schema * Used in the discriminated union for errors not in the OpenAPI spec */ declare class UnexpectedApiClientError extends ApiClientError { readonly code?: number | undefined; constructor(message: string, code?: number | undefined, endpoint?: string, method?: string, cause?: unknown); } /** * Validation error for request/response schema validation failures */ declare class ValidationError extends ApiClientError { readonly validationErrors: unknown; constructor(message: string, validationErrors: unknown, endpoint?: string, method?: string); } /** * Timeout error when request exceeds configured timeout */ declare class TimeoutError extends ApiClientError { readonly timeout: number; constructor(timeout: number, endpoint?: string, method?: string, cause?: unknown); } export { ApiClient, ApiClientError, type ApiClientLoggingOptions, type ApiClientOptions, type ApiClientValidationErrorContext, type ApiClientValidationErrorHandler, type ApiClientValidationErrorLocation, type ApiRequestSchema, type ApiResponse, type ApiResponseSchema, type BodyRequired, type EndpointsWithMethod, type ErrorResponse, type ExecuteRequest, type ExecuteResponse, type ExtractErrorCodes, type ExtractPathParams, type ExtractRequestBody, type ExtractRequestParams, type ExtractRequestQuery, type ExtractResponseSchema, type ExtractStatusCodes, type ExtractSuccessBody, type ExtractSuccessCodes, HTTP_CLIENT_DEBUG_NAMESPACE, type HttpExecutor, type LogHandler, type LogLevel, type LogMeta, type Logger, type ParamsRequired, type RequestOptions, type RetryContext, type SuccessResponse, TimeoutError, UnexpectedApiClientError, type UnexpectedErrorResponse, ValidationError };