import { type ContractLike, type HttpContractConfig, type ResolveContract, type StandardErrorResponseBody, type StandardSchemaV1 } from "../contracts/index.js"; import type { CallArgs, ClientConfig, EndpointResult, InferEndpointErrorResponse, InferEndpointErrorResponseByStatus, InferEndpointErrorStatus, InferSuccessResponse } from "./types.js"; /** * Source category for a `ContractError`. */ export type ContractErrorSource = "http" | "client" | "network" | "contract"; /** * Narrow a contract error union by source. */ export type ContractErrorWithSource = Extract; /** * Narrow a contract error union by HTTP status. */ export type ContractErrorWithStatus = Extract; /** * Narrow a contract error union by Beignet error code. */ export type ContractErrorWithCode = Extract; /** * Error for a non-2xx HTTP response. */ export type HttpContractError = ContractError & { readonly source: "http"; readonly status: TStatus; readonly response: Response; }; /** * Error created by the client before a network request is made. */ export type ClientContractError = ContractError & { readonly source: "client"; readonly status: undefined; readonly response: undefined; }; /** * Error created when the network request itself fails. */ export type NetworkContractError = ContractError & { readonly source: "network"; readonly status: undefined; readonly response: undefined; }; /** * Error created when a response violates the contract. */ export type ResponseContractError = ContractError & { readonly source: "contract"; readonly status: TStatus; }; /** * Union of all Beignet client error variants. */ export type AnyContractError = HttpContractError | ClientContractError | NetworkContractError | ResponseContractError; type EndpointCatalogErrorDefinition = TContract["metadata"] extends { errors: infer TErrors; } ? TErrors extends Record ? TErrors[keyof TErrors] : never : never; type InferErrorDefinitionDetails = TDef extends { details: StandardSchemaV1; } ? StandardSchemaV1.InferOutput : unknown; type StandardErrorBodyForDefinition = StandardErrorResponseBody & { code: TDef["code"]; details?: InferErrorDefinitionDetails; }; type EndpointCatalogContractError = EndpointCatalogErrorDefinition extends infer TDef ? TDef extends { code: string; status: number; } ? HttpContractError, TDef["status"]> & { readonly code: TDef["code"]; readonly details?: InferErrorDefinitionDetails; } : never : never; /** * Infer route-owned error catalog codes declared by a contract. */ export type InferEndpointErrorCode = EndpointCatalogErrorDefinition["code"]; /** * Infer the full typed error union for a contract endpoint. */ export type InferEndpointContractError = EndpointCatalogContractError | { [TStatus in InferEndpointErrorStatus]: HttpContractError, TStatus>; }[InferEndpointErrorStatus] | HttpContractError, number> | ClientContractError | NetworkContractError | ResponseContractError, number | undefined>; /** * Error thrown by Beignet contract clients. * * `source` distinguishes HTTP error responses, client-side request mistakes, * network failures, and contract drift such as response validation failures. */ export declare class ContractError extends Error { /** * Error source category. */ readonly source: TSource; /** * HTTP status when a response was available. */ readonly status: TStatus; /** * Stable error code. */ readonly code?: string; /** * Parsed response body when available. */ readonly body?: TBody; /** * Structured error details when available. */ readonly details?: unknown; /** * Native fetch response when available. */ readonly response?: Response; cause?: unknown; constructor(args: { source: TSource; status?: TStatus; code?: string; message: string; body?: TBody; details?: unknown; response?: Response; cause?: unknown; }); /** * Check whether this error has a specific HTTP status code. */ hasStatus(status: S): this is this & { readonly status: S; }; /** * Check whether this error came from a specific source. */ hasSource(source: S): this is this & { readonly source: S; }; /** * Check whether this error has a specific error code. */ hasCode(code: C): this is this & { code: C; }; } /** * Type guard to check if an unknown error is a ContractError, * optionally narrowing by HTTP status code. * * @example * ```ts * try { await endpoint.call(...) } * catch (err) { * if (isContractError(err, 404)) { * // err.status is 404 * } * if (isContractError(err)) { * // err is ContractError * } * } * ``` */ export declare function isContractError(err: unknown): err is AnyContractError; export declare function isContractError(err: unknown, status: S): err is HttpContractError; export declare function isContractError(err: TError, criteria: { status: S; }): err is ContractErrorWithStatus; export declare function isContractError(err: TError, criteria: { source: S; }): err is ContractErrorWithSource; export declare function isContractError(err: TError, criteria: { status: Status; source: Source; }): err is ContractErrorWithSource, Source>; export declare function isContractError(err: TError, criteria: { code: Code; }): err is ContractErrorWithCode; export declare function isContractError(err: TError, criteria: { status: Status; code: Code; }): err is ContractErrorWithCode, Code>; export declare function isContractError(err: TError, criteria: { source: Source; code: Code; }): err is ContractErrorWithCode, Code>; export declare function isContractError(err: TError, criteria: { status: Status; source: Source; code: Code; }): err is ContractErrorWithCode, Source>, Code>; export declare function isContractError(err: unknown, criteria: { status: S; }): err is ContractError; export declare function isContractError(err: unknown, criteria: { source: S; }): err is ContractError; export declare function isContractError(err: unknown, criteria: { status: Status; source: Source; }): err is ContractErrorWithSource, Source>; export declare function isContractError(err: unknown, criteria: { code: Code; }): err is ContractError & { readonly code: Code; }; /** * Typed client endpoint for one contract. */ export declare class Endpoint { private contract; private config; constructor(contract: TContract, config: ClientConfig); /** * Check whether an unknown error is a `ContractError` for this endpoint. */ isError(err: unknown): err is InferEndpointContractError; isError>(err: unknown, status: S): err is ContractErrorWithStatus, S>; isError(err: unknown, status: S): err is HttpContractError; isError>(err: unknown, criteria: { status: S; }): err is ContractErrorWithStatus, S>; isError(err: unknown, criteria: { source: S; }): err is ContractErrorWithSource, S>; isError>(err: unknown, criteria: { code: C; }): err is ContractErrorWithCode, C>; isError(err: unknown, criteria: { code: C; }): err is InferEndpointContractError & { readonly code: C; }; isError, Source extends ContractErrorSource>(err: unknown, criteria: { status: Status; source: Source; }): err is ContractErrorWithSource, Status>, Source>; isError, C extends InferEndpointErrorCode>(err: unknown, criteria: { status: Status; code: C; }): err is ContractErrorWithCode, Status>, C>; isError>(err: unknown, criteria: { source: Source; code: C; }): err is ContractErrorWithCode, Source>, C>; isError, Source extends ContractErrorSource, C extends InferEndpointErrorCode>(err: unknown, criteria: { status: Status; source: Source; code: C; }): err is ContractErrorWithCode, Status>, Source>, C>; /** * Call the endpoint and return the parsed success body. * * Throws `ContractError` when the request fails or the response violates the * contract. */ call(...callArgs: CallArgs): Promise>; /** * Call the endpoint and return a typed result instead of throwing * `ContractError`. */ safeCall(...callArgs: CallArgs): Promise>>; /** * Build the full URL with path and query parameters */ private buildUrl; /** * Build request headers */ private buildHeaders; /** * Create a contract error */ private createError; } /** * Client for making contract-based requests. */ export declare class Client { private config; constructor(config: ClientConfig); /** * Create an endpoint wrapper for a contract. * * Accepts either a plain `HttpContractConfig` or a builder with a `.config` * property. */ endpoint(contract: TContractLike): Endpoint, TProvidedHeaders>; } /** * Create a configured Beignet client. */ export declare function createClient(config?: ClientConfig): Client; export {}; //# sourceMappingURL=client.d.ts.map