@lokalise/api-contracts
Version:
51 lines (50 loc) • 2.64 kB
TypeScript
import { z } from 'zod/v4';
import type { CommonRouteDefinitionMetadata, InferSchemaOutput, RoutePathResolver } from '../apiContracts.ts';
import type { Exactly } from '../typeUtils.ts';
import { 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;
summary?: string;
description?: string;
tags?: readonly string[];
};
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;
type TypedPathApiContract<T extends RequestPathParamsSchema> = Omit<ApiContract, 'pathResolver' | 'requestPathParamsSchema'> & {
pathResolver: RoutePathResolver<InferSchemaOutput<T>>;
requestPathParamsSchema?: T;
};
export declare const defineApiContract: <PathParamsSchema extends RequestPathParamsSchema, const Contract extends TypedPathApiContract<PathParamsSchema>>(contract: Exactly<Contract, TypedPathApiContract<PathParamsSchema>> & {
requestPathParamsSchema?: PathParamsSchema;
}) => Contract;
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;
/** @deprecated No known consumers — will be removed in a future release. */
export declare const getSuccessResponseSchema: (routeConfig: ApiContract) => z.ZodType | null;
/** @deprecated No known consumers — will be removed in a future release. */
export declare const getIsEmptyResponseExpected: (routeConfig: ApiContract) => boolean;
export {};