/** * @file API Scheme * @author Yourtion Guo */ import { type ZodTypeAny, z } from "zod"; import type ERest from "."; import type { ISchemaType, SchemaType } from "./params"; import { type SourceResult } from "./utils"; export type TYPE_RESPONSE = string | SchemaType | ISchemaType | Record; export interface IExample { name?: string | undefined; path?: string; headers?: Record; input?: Record; output?: Record; } export type DEFAULT_HANDLER = (...args: unknown[]) => unknown; export declare const SUPPORT_METHOD: readonly ["get", "post", "put", "delete", "patch"]; export type SUPPORT_METHODS = (typeof SUPPORT_METHOD)[number]; export interface APICommon { method: SUPPORT_METHODS; path: string; title: string; description?: string; handler?: T; response?: TYPE_RESPONSE; } export interface APIDefine extends APICommon { group?: string; headers?: Record; query?: Record; body?: Record; params?: Record; required?: string[]; requiredOneOf?: string[]; before?: Array; middlewares?: Array; handler?: T; mock?: Record; } export interface APIOption extends Record { group: string; realPath: string; examples: IExample[]; beforeHooks: Set; middlewares: Set; required: Set; requiredOneOf: string[][]; _allParams: Map; mock?: Record; tested: boolean; response?: TYPE_RESPONSE; responseSchema?: SchemaType | ISchemaType; querySchema?: z.ZodObject; bodySchema?: z.ZodObject; paramsSchema?: z.ZodObject; headersSchema?: z.ZodObject; } export default class API { key: string; pathTestRegExp: RegExp; inited: boolean; options: APIOption; /** * 构造函数 */ constructor(method: SUPPORT_METHODS, path: string, sourceFile: SourceResult, group?: string, prefix?: string); static define(options: APIDefine, sourceFile: SourceResult, group?: string, prefix?: string): API; /** * 检查是否已经完成初始化,如果是则报错 */ private checkInited; /** * 检查URL是否符合API规则 */ pathTest(method: SUPPORT_METHODS, path: string): boolean; /** * API标题 */ title(title: string): this; /** * API描述 */ description(description: string): this; /** * API分组 */ group(group: string): this; private addExample; /** * API使用例子 */ example(example: IExample): this; /** * 输出结果对象 */ response(response: TYPE_RESPONSE): this; /** * 输入参数 */ private setParam; /** * 输入参数 */ private setParams; /** * 检测混合使用并设置 Zod Schema */ private setZodSchema; /** * 检测混合使用并设置 ISchemaType 参数 */ private checkMixedUsage; /** * Body 参数 - 支持 ISchemaType 和原生 Zod Schema */ body(obj: Record | ZodTypeAny): this; /** * Query 参数 - 支持 ISchemaType 和原生 Zod Schema */ query(obj: Record | ZodTypeAny): this; /** * Param 参数 - 支持 ISchemaType 和原生 Zod Schema */ params(obj: Record | ZodTypeAny): this; /** * Headers 参数 - 支持 ISchemaType 和原生 Zod Schema */ headers(obj: Record | ZodTypeAny): this; /** * 必填参数 */ required(list: string[]): this; /** * 多选一必填参数 */ requiredOneOf(list: string[]): this; /** * 中间件 */ middlewares(...list: Array): this; /** * 注册执行之前的钩子 */ before(...list: Array): this; /** * 注册处理函数 */ register(fn: T): this; /** * 注册强类型处理函数 (基于 zod schema) */ registerTyped, TBody extends z.ZodRawShape = Record, TParams extends z.ZodRawShape = Record, THeaders extends z.ZodRawShape = Record, TResponse extends z.ZodTypeAny = z.ZodAny>(schemas: { query?: z.ZodObject; body?: z.ZodObject; params?: z.ZodObject; headers?: z.ZodObject; response?: TResponse; }, handler: (req: { query: z.infer>; body: z.infer>; params: z.infer>; headers: z.infer>; }, res: unknown) => z.infer | Promise>): this; mock(data?: Record): void; init(parent: ERest): void; }