UNPKG

erest

Version:

Easy to build api server depend on @leizm/web and express.

95 lines (94 loc) 2.84 kB
/** * @file API Docs * 参考 hojs * @author Yourtion Guo <yourtion@gmail.com> */ import type { ZodType } from "zod"; import type ERest from ".."; import type { IApiOptionInfo, IDocOptions } from ".."; import type { APIOption } from "../api"; import type { ErrorManager } from "../manager"; /** 文档输出写入方法 */ export type IDocWritter = (path: string, data: string) => void; /** 文档生成器插件 */ export type IDocGeneratePlugin = (data: IDocData, dir: string, options: IDocOptions, writter: IDocWritter) => void; /** 文档数据 */ export interface IDocData { /** API信息 */ info: IApiOptionInfo; /** 生成时间 */ genTime: string; /** 分组信息 */ group: Record<string, string>; /** 基础数据类型 */ types: Record<string, IDocTypes>; /** API */ apis: Record<string, APIOption<unknown>>; /** 文档Schema */ schema: unknown; /** 类型管理器 */ typeManager: unknown; /** 错误信息 */ errorManager: ErrorManager; /** API统计信息 */ apiInfo: { count: number; tested: number; untest: string[]; }; /** ERest实例引用(用于访问内部属性) */ erest?: ERest<unknown> & { typeRegistry?: Map<string, ZodType>; schemaRegistry?: Map<string, ZodType>; }; } export interface IDocTypes { /** 数据类型名称 */ name: string; /** 检查方法 */ checker?: string; /** 格式化方法 */ formatter?: string; /** 解析方法 */ parser?: string; /** 类型动态参数检查器 */ paramsChecker?: string; /** 说明信息 */ description: string; /** 是否为系统内置的类型 */ isBuiltin?: boolean; /** 对应的TypeScript类型 */ tsType?: string; /** 是否默认自动格式化 */ isDefaultFormat?: boolean; /** 类型动态参数是否必须 */ isParamsRequired: boolean; } export default class IAPIDoc { private erest; private info; private groups; private docsOptions; private plugins; private writer; private docDataCache; constructor(erestIns: ERest<unknown>); /** 生成类型文档 - 支持新的 Zod 实现 */ private generateTypeDocumentation; /** 从Zod Schema中提取描述信息 */ private extractZodSchemaDescription; /** 从Zod Schema中提取TypeScript类型 */ private extractTypeScriptType; /** 获取文档数据 */ buildDocData(): IDocData; /** 设置文档输出函数 */ setWritter(writer: IDocWritter): void; /** 生成文档 */ genDocs(): this; getSwaggerInfo(): unknown; registerPlugin(name: string, plugin: IDocGeneratePlugin): void; /** 保存文档 */ save(dir: string): this; /** 当进程退出时存储文档 */ saveOnExit(dir: string): this; }