UNPKG

doc-it-up

Version:

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

187 lines (154 loc) 5.37 kB
import { _ as __awaiter } from '../chunks/tslib.es6-WQS2tr1v.js'; import { initDocsDirectory, DocItUpCore } from '../core.js'; import 'fs/promises'; import 'path'; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var plugin = {exports: {}}; var getPluginName = {exports: {}}; var hasRequiredGetPluginName; function requireGetPluginName () { if (hasRequiredGetPluginName) return getPluginName.exports; hasRequiredGetPluginName = 1; const fpStackTracePattern = /at\s(?:.*\.)?plugin\s.*\n\s*(.*)/; const fileNamePattern = /(\w*(\.\w*)*)\..*/; getPluginName.exports = function getPluginName (fn) { if (fn.name.length > 0) return fn.name const stackTraceLimit = Error.stackTraceLimit; Error.stackTraceLimit = 10; try { throw new Error('anonymous function') } catch (e) { Error.stackTraceLimit = stackTraceLimit; return extractPluginName(e.stack) } }; function extractPluginName (stack) { const m = stack.match(fpStackTracePattern); // get last section of path and match for filename return m ? m[1].split(/[/\\]/).slice(-1)[0].match(fileNamePattern)[1] : 'anonymous' } getPluginName.exports.extractPluginName = extractPluginName; return getPluginName.exports; } var toCamelCase; var hasRequiredToCamelCase; function requireToCamelCase () { if (hasRequiredToCamelCase) return toCamelCase; hasRequiredToCamelCase = 1; toCamelCase = function toCamelCase (name) { if (name[0] === '@') { name = name.slice(1).replace('/', '-'); } return name.replace(/-(.)/g, function (match, g1) { return g1.toUpperCase() }) }; return toCamelCase; } var hasRequiredPlugin; function requirePlugin () { if (hasRequiredPlugin) return plugin.exports; hasRequiredPlugin = 1; const getPluginName = requireGetPluginName(); const toCamelCase = requireToCamelCase(); let count = 0; function plugin$1 (fn, options = {}) { let autoName = false; if (fn.default !== undefined) { // Support for 'export default' behaviour in transpiled ECMAScript module fn = fn.default; } if (typeof fn !== 'function') { throw new TypeError( `fastify-plugin expects a function, instead got a '${typeof fn}'` ) } if (typeof options === 'string') { options = { fastify: options }; } if ( typeof options !== 'object' || Array.isArray(options) || options === null ) { throw new TypeError('The options object should be an object') } if (options.fastify !== undefined && typeof options.fastify !== 'string') { throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`) } if (!options.name) { autoName = true; options.name = getPluginName(fn) + '-auto-' + count++; } fn[Symbol.for('skip-override')] = options.encapsulate !== true; fn[Symbol.for('fastify.display-name')] = options.name; fn[Symbol.for('plugin-meta')] = options; // Faux modules support if (!fn.default) { fn.default = fn; } // TypeScript support for named imports // See https://github.com/fastify/fastify/issues/2404 for more details // The type definitions would have to be update to match this. const camelCase = toCamelCase(options.name); if (!autoName && !fn[camelCase]) { fn[camelCase] = fn; } return fn } plugin.exports = plugin$1; plugin.exports.default = plugin$1; plugin.exports.fastifyPlugin = plugin$1; return plugin.exports; } var pluginExports = requirePlugin(); var fp = /*@__PURE__*/getDefaultExportFromCjs(pluginExports); const fastifyDocItUp = (fastify, options) => __awaiter(void 0, void 0, void 0, function* () { if (options.docsDir) initDocsDirectory(options.docsDir); else initDocsDirectory(); fastify.addHook('onSend', (request, reply, payload) => __awaiter(void 0, void 0, void 0, function* () { if (request.url.startsWith('/docs')) return payload; if (reply.statusCode >= 200 && reply.statusCode < 300) { let parsedBody = payload; try { if (typeof payload === 'string') parsedBody = JSON.parse(payload); } catch (_a) { } const pathOnly = request.url.split('?')[0]; DocItUpCore.recordRequest({ method: request.method, path: pathOnly, headers: request.headers, query: request.query, body: request.body, params: request.params, files: request.files }, { statusCode: reply.statusCode, headers: reply.getHeaders(), body: parsedBody }); } return payload; })); fastify.get('/docs', (req, reply) => __awaiter(void 0, void 0, void 0, function* () { yield DocItUpCore.loadSpecs(); reply.type('text/html').send(DocItUpCore.getHtml()); })); fastify.get('/docs/swagger.json', (req, reply) => __awaiter(void 0, void 0, void 0, function* () { yield DocItUpCore.loadSpecs(); reply.send(DocItUpCore.getSwaggerSpec()); })); }); var fastify = fp(fastifyDocItUp, { name: 'doc-it-up' }); export { fastify as default };