UNPKG

@lokalise/api-contracts

Version:
54 lines (53 loc) 2.67 kB
import type { z } from 'zod/v4'; import type { CommonRouteDefinitionMetadata, InferSchemaOutput, RoutePathResolver, RouteVisibility } from '../apiContracts.ts'; import type { DistributiveOmit, Exactly } from '../typeUtils.ts'; import type { ContractNoBody } from './constants.ts'; import { type ResponsesByStatusCode, type SseSchemaByEventName } from './contractResponse.ts'; export type RequestPathParamsSchema = z.ZodObject; export type RequestQuerySchema = z.ZodObject; export type RequestHeaderSchema = z.ZodObject; export type ResponseHeaderSchema = z.ZodObject; export type CommonApiContract = { pathResolver: RoutePathResolver<any>; requestPathParamsSchema?: RequestPathParamsSchema; requestQuerySchema?: RequestQuerySchema; requestHeaderSchema?: RequestHeaderSchema; responseHeaderSchema?: ResponseHeaderSchema; responsesByStatusCode: ResponsesByStatusCode; metadata?: CommonRouteDefinitionMetadata; /** Human-readable summary of the route. */ summary: string; description?: string; tags?: readonly string[]; visibility: RouteVisibility; }; export type GetApiContract = CommonApiContract & { method: 'get'; requestBodySchema?: never; }; export type DeleteApiContract = CommonApiContract & { method: 'delete'; requestBodySchema?: never; }; export type PayloadApiContract = CommonApiContract & { method: 'post' | 'put' | 'patch'; requestBodySchema: typeof ContractNoBody | z.ZodType; }; export type ApiContract = GetApiContract | DeleteApiContract | PayloadApiContract; /** * Input shape accepted by `defineApiContract`. */ type ApiContractConfig<TPathParamsSchema extends RequestPathParamsSchema | undefined> = DistributiveOmit<ApiContract, 'pathResolver' | 'requestPathParamsSchema'> & { pathResolver: RoutePathResolver<InferSchemaOutput<TPathParamsSchema>>; requestPathParamsSchema?: TPathParamsSchema; }; export declare const defineApiContract: <TPathParamsSchema extends RequestPathParamsSchema | undefined = undefined, const TContract extends ApiContractConfig<TPathParamsSchema> = ApiContractConfig<TPathParamsSchema>>(contract: Exactly<TContract, ApiContractConfig<TPathParamsSchema>> & { requestPathParamsSchema?: TPathParamsSchema; }) => Omit<TContract, 'visibility'> & { visibility: RouteVisibility; }; export declare const mapApiContractToPath: (routeConfig: ApiContract) => string; export declare const describeApiContract: (routeConfig: ApiContract) => string; export declare const getSseSchemaByEventName: (routeConfig: ApiContract) => SseSchemaByEventName | null; export declare const hasAnySuccessSseResponse: (apiContract: ApiContract) => boolean; export {};