UNPKG

doc-it-up

Version:

Generates automatic documentation for your code. Supports Express, Fastify, Koa, Hono, Elysia, and Hapi.

46 lines (43 loc) 1.44 kB
import { _ as __awaiter } from '../chunks/tslib.es6-WQS2tr1v.js'; import { initDocsDirectory, DocItUpCore } from '../core.js'; import 'fs/promises'; import 'path'; const koaMiddleware = (options = {}) => { if (options.docsDir) initDocsDirectory(options.docsDir); else initDocsDirectory(); return (ctx, next) => __awaiter(void 0, void 0, void 0, function* () { if (ctx.path.startsWith('/docs')) return next(); yield next(); if (ctx.status >= 200 && ctx.status < 300) { DocItUpCore.recordRequest({ method: ctx.method, path: ctx.path, headers: ctx.headers, query: ctx.query, body: ctx.request.body, // Requires koa-bodyparser params: ctx.params || {}, files: ctx.request.files }, { statusCode: ctx.status, headers: ctx.response.header, body: ctx.body }); } }); }; const koaHandler = () => { return (ctx) => __awaiter(void 0, void 0, void 0, function* () { yield DocItUpCore.loadSpecs(); if (ctx.path.endsWith('/swagger.json')) { ctx.body = DocItUpCore.getSwaggerSpec(); } else { ctx.type = 'text/html'; ctx.body = DocItUpCore.getHtml(); } }); }; export { koaHandler, koaMiddleware };