doc-it-up
Version:
Generates automatic documentation for your code. Supports Express, Fastify, Koa, Hono, Elysia, and Hapi.
57 lines (54 loc) • 2.19 kB
JavaScript
import { _ as __awaiter } from '../chunks/tslib.es6-WQS2tr1v.js';
import { initDocsDirectory, DocItUpCore } from '../core.js';
import 'fs/promises';
import 'path';
const hapiPlugin = {
name: 'doc-it-up',
version: '1.0.0',
register: function (server, options) {
return __awaiter(this, void 0, void 0, function* () {
if (options.docsDir)
initDocsDirectory(options.docsDir);
else
initDocsDirectory();
server.ext('onPreResponse', (request, h) => {
if (request.path.startsWith('/docs'))
return h.continue;
const response = request.response;
// Hapi boom errors or standard responses
if (response && !('isBoom' in response) && response.statusCode >= 200 && response.statusCode < 300) {
DocItUpCore.recordRequest({
method: request.method.toUpperCase(),
path: request.path,
headers: request.headers,
query: request.query,
body: request.payload,
params: request.params
}, {
statusCode: response.statusCode,
headers: response.headers,
body: response.source
});
}
return h.continue;
});
server.route({
method: 'GET',
path: '/docs',
handler: (request, h) => __awaiter(this, void 0, void 0, function* () {
yield DocItUpCore.loadSpecs();
return h.response(DocItUpCore.getHtml()).type('text/html');
})
});
server.route({
method: 'GET',
path: '/docs/swagger.json',
handler: (request, h) => __awaiter(this, void 0, void 0, function* () {
yield DocItUpCore.loadSpecs();
return DocItUpCore.getSwaggerSpec();
})
});
});
}
};
export { hapiPlugin };