doc-it-up
Version:
Generates automatic documentation for your code. Supports Express, Fastify, Koa, Hono, Elysia, and Hapi.
66 lines (63 loc) • 2.15 kB
JavaScript
import { _ as __awaiter } from '../chunks/tslib.es6-WQS2tr1v.js';
import { initDocsDirectory, DocItUpCore } from '../core.js';
import 'fs/promises';
import 'path';
const honoMiddleware = (options = {}) => {
if (options.docsDir)
initDocsDirectory(options.docsDir);
else
initDocsDirectory();
return (c, next) => __awaiter(void 0, void 0, void 0, function* () {
if (c.req.path.startsWith('/docs'))
return next();
yield next();
if (c.res.status >= 200 && c.res.status < 300) {
// Cloning response is required to read body without consuming it for the client
const resClone = c.res.clone();
let resBody;
try {
resBody = yield resClone.json();
}
catch (_a) {
try {
resBody = yield resClone.text();
}
catch (_b) { }
}
let reqBody;
try {
reqBody = yield c.req.parseBody();
}
catch (_c) { }
if (!reqBody) {
try {
reqBody = yield c.req.json();
}
catch (_d) { }
}
DocItUpCore.recordRequest({
method: c.req.method,
path: c.req.path,
headers: c.req.header(),
query: c.req.query(),
body: reqBody,
params: c.req.param()
}, {
statusCode: c.res.status,
headers: Object.fromEntries(c.res.headers.entries()),
body: resBody
});
}
});
};
const registerHonoDocs = (app) => {
app.get('/docs', (c) => __awaiter(void 0, void 0, void 0, function* () {
yield DocItUpCore.loadSpecs();
return c.html(DocItUpCore.getHtml());
}));
app.get('/docs/swagger.json', (c) => __awaiter(void 0, void 0, void 0, function* () {
yield DocItUpCore.loadSpecs();
return c.json(DocItUpCore.getSwaggerSpec());
}));
};
export { honoMiddleware, registerHonoDocs };