import { z, ZodType, ZodSchema, ZodError } from 'zod'; /** * Re-export HttpError for backward compatibility * @deprecated Use ApiError from '@/lib/api/types' instead */ export type { ApiError as HttpError } from '../api/types'; /** * Supported HTTP methods */ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; /** * Endpoint definition with full type safety */ export interface EndpointDefinition { /** HTTP method */ method: HttpMethod; /** URL path with optional :param placeholders */ path: string; /** Path parameters schema */ params?: TParams; /** Query parameters schema */ query?: TQuery; /** Request body schema */ body?: TBody; /** Response data schema */ response: TResponse; /** Additional headers */ headers?: Record; /** Request timeout (ms) */ timeout?: number; /** Tags for categorization and cache invalidation */ tags?: string[]; /** Description for documentation */ description?: string; /** Whether this endpoint requires authentication */ requiresAuth?: boolean; } /** * API contract - collection of endpoint definitions */ export type ApiContract = Record; /** * Infer request types from endpoint definition */ export type InferRequest = { params: E['params'] extends ZodType ? z.infer : never; query: E['query'] extends ZodType ? z.infer : never; body: E['body'] extends ZodType ? z.infer : never; }; /** * Infer response type from endpoint definition */ export type InferResponse = z.infer; /** * Build request options type based on endpoint requirements */ export type RequestOptions = (E['params'] extends ZodType ? { params: z.infer; } : object) & (E['query'] extends ZodType ? { query: z.infer; } : object) & (E['body'] extends ZodType ? { body: z.infer; } : object) & { /** Abort signal for cancellation */ signal?: AbortSignal; /** Skip request/response validation */ skipValidation?: boolean; /** Additional headers for this request */ headers?: Record; /** Override timeout for this request */ timeout?: number; }; /** * Type-safe API client methods generated from contract */ export type TypedApiClient = { [K in keyof T]: (options: RequestOptions) => Promise>; }; /** * API client configuration */ export interface TypedApiClientConfig { /** Base URL for API requests */ baseUrl: string; /** API version prefix (e.g., 'v1') */ version?: string; /** Default headers for all requests */ defaultHeaders?: Record; /** Callback for validation errors */ onValidationError?: (error: ZodError, endpoint: string, direction: 'request' | 'response') => void; /** Custom response transformer */ transformResponse?: (data: unknown, schema: ZodSchema) => T; /** Enable strict mode (throw on extra properties) */ strictMode?: boolean; /** Default timeout (ms) */ timeout?: number; /** Auth token getter */ getAuthToken?: () => string | null | Promise; } /** * API validation error with detailed information */ export declare class ApiValidationError extends Error { readonly zodError: ZodError; readonly isValidationError = true; readonly endpoint: string; readonly direction: 'request' | 'response'; readonly issues: z.ZodIssue[]; constructor(message: string, zodError: ZodError, endpoint: string, direction: 'request' | 'response'); /** * Get formatted error messages */ getFormattedErrors(): string[]; /** * Get field-level errors */ getFieldErrors(): Record; } /** * API contract error for invalid endpoint definitions */ export declare class ApiContractError extends Error { readonly isContractError = true; readonly endpoint: string; constructor(message: string, endpoint: string); } /** * Create a type-safe API client from contract definition */ export declare function createTypedApiClient(contract: T, config: TypedApiClientConfig): TypedApiClient & { /** Get contract definition */ getContract: () => T; /** Get configuration */ getConfig: () => TypedApiClientConfig; /** Invalidate cache by tags */ invalidateByTags: (tags: string[]) => void; }; /** * Define an API endpoint with type inference */ export declare function defineEndpoint(definition: EndpointDefinition): EndpointDefinition; /** * Define a GET endpoint */ export declare function defineGet(path: string, response: TResponse, options?: Omit, 'method' | 'path' | 'response' | 'body'>): EndpointDefinition; /** * Define a POST endpoint */ export declare function definePost(path: string, response: TResponse, options?: Omit, 'method' | 'path' | 'response'>): EndpointDefinition; /** * Define a PUT endpoint */ export declare function definePut(path: string, response: TResponse, options?: Omit, 'method' | 'path' | 'response'>): EndpointDefinition; /** * Define a PATCH endpoint */ export declare function definePatch(path: string, response: TResponse, options?: Omit, 'method' | 'path' | 'response'>): EndpointDefinition; /** * Define a DELETE endpoint */ export declare function defineDelete(path: string, response: TResponse, options?: Omit, 'method' | 'path' | 'response' | 'body'>): EndpointDefinition; /** * Common pagination schema */ export declare const paginationSchema: z.ZodObject<{ page: z.ZodDefault>; pageSize: z.ZodDefault>; sortBy: z.ZodOptional; sortOrder: z.ZodDefault>>; }, z.core.$strip>; export type PaginationParams = z.infer; /** * Common paginated response schema factory */ export declare function createPaginatedResponseSchema(itemSchema: T): z.ZodObject<{ items: z.ZodArray; total: z.ZodNumber; page: z.ZodNumber; pageSize: z.ZodNumber; totalPages: z.ZodNumber; hasNextPage: z.ZodOptional; hasPreviousPage: z.ZodOptional; }>; /** * Common ID parameter schema */ export declare const idParamSchema: z.ZodObject<{ id: z.ZodString; }, z.core.$strip>; /** * Common error response schema */ export declare const errorResponseSchema: z.ZodObject<{ error: z.ZodObject<{ code: z.ZodString; message: z.ZodString; details: z.ZodOptional>; traceId: z.ZodOptional; }, z.core.$strip>; }, z.core.$strip>; /** * Common success response schema */ export declare const successResponseSchema: z.ZodObject<{ success: z.ZodLiteral; message: z.ZodOptional; }, z.core.$strip>; /** * User schema */ export declare const userSchema: z.ZodObject<{ id: z.ZodString; email: z.ZodString; name: z.ZodString; role: z.ZodEnum<{ admin: "admin"; user: "user"; viewer: "viewer"; }>; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; createdAt: z.ZodString; updatedAt: z.ZodString; }, z.core.$strip>; export type User = z.infer; /** * Example user API contract */ export declare const userApiContract: { readonly getUsers: EndpointDefinition>; pageSize: z.ZodDefault>; sortBy: z.ZodOptional; sortOrder: z.ZodDefault>>; search: z.ZodOptional; role: z.ZodOptional>; status: z.ZodOptional>; }, z.core.$strip>, never, z.ZodObject<{ items: z.ZodArray; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; createdAt: z.ZodString; updatedAt: z.ZodString; }, z.core.$strip>>; total: z.ZodNumber; page: z.ZodNumber; pageSize: z.ZodNumber; totalPages: z.ZodNumber; hasNextPage: z.ZodOptional; hasPreviousPage: z.ZodOptional; }, z.core.$strip>>; readonly getUserById: EndpointDefinition, never, never, z.ZodObject<{ id: z.ZodString; email: z.ZodString; name: z.ZodString; role: z.ZodEnum<{ admin: "admin"; user: "user"; viewer: "viewer"; }>; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; createdAt: z.ZodString; updatedAt: z.ZodString; }, z.core.$strip>>; readonly createUser: EndpointDefinition; password: z.ZodString; }, z.core.$strip>, z.ZodObject<{ id: z.ZodString; email: z.ZodString; name: z.ZodString; role: z.ZodEnum<{ admin: "admin"; user: "user"; viewer: "viewer"; }>; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; createdAt: z.ZodString; updatedAt: z.ZodString; }, z.core.$strip>>; readonly updateUser: EndpointDefinition, never, z.ZodObject<{ name: z.ZodOptional; role: z.ZodOptional>; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; }, z.core.$strip>, z.ZodObject<{ id: z.ZodString; email: z.ZodString; name: z.ZodString; role: z.ZodEnum<{ admin: "admin"; user: "user"; viewer: "viewer"; }>; status: z.ZodOptional>; profile: z.ZodOptional; bio: z.ZodOptional; }, z.core.$strip>>; createdAt: z.ZodString; updatedAt: z.ZodString; }, z.core.$strip>>; readonly deleteUser: EndpointDefinition, never, never, z.ZodObject<{ success: z.ZodLiteral; message: z.ZodOptional; }, z.core.$strip>>; }; /** * Create typed user API client */ export declare function createUserApiClient(config?: Partial): TypedApiClient;