UNPKG

api-morph

Version:

A modern TypeScript-first OpenAPI document generator that analyzes your code and JSDoc comments to automatically generate comprehensive and accurate API documentation.

57 lines (54 loc) 1.6 kB
import { SchemaRegistry, generateSwaggerUIHTML, getCallLocation, getSwaggerUIAssetDir } from "./swagger-CArZTgxc.js"; import { relative } from "node:path"; import { zValidator } from "@hono/zod-validator"; import { serveStatic } from "@hono/node-server/serve-static"; //#region src/hono/middlewares/zodValidator.ts /** * 包装官方的 `@hono/zod-validator`,行为完全与官方 `zValidator` 一致, * 只是会额外收集 schema 信息用于 OpenAPI 文档生成 */ const zodValidator = (target, schema, hook, options) => { const location = getCallLocation(); const registry = SchemaRegistry.getInstance(); const schemaInfo = {}; switch (target) { case "json": schemaInfo.body = schema; break; case "query": schemaInfo.query = schema; break; case "param": schemaInfo.params = schema; break; case "header": schemaInfo.headers = schema; break; case "form": schemaInfo.body = schema; break; } registry.register(location, schemaInfo); return zValidator(target, schema, hook, options); }; //#endregion //#region src/hono/swagger.ts /** * 设置 SwaggerUI 的静态资源和路由 * @param path SwaggerUI 路径 * @param app Hono 应用实例 * @param options 选项 * * @category Hono */ function setupSwaggerUI(path$1, app, options = {}) { const assetDir = getSwaggerUIAssetDir(); const relativePath = relative(process.cwd(), assetDir); app.use("/*", serveStatic({ root: relativePath })); app.get(path$1, (c) => { const html = generateSwaggerUIHTML(options); return c.html(html); }); } //#endregion export { setupSwaggerUI, zodValidator };