UNPKG

azure-functions-swagger-ui

Version:

A simple npm module that integrates Swagger UI into Azure Functions, providing a user-friendly interface to visualize and interact with your API endpoints. Perfect for documenting and testing Azure Functions with minimal setup.

58 lines (55 loc) 3.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.swagger_ui_handler = void 0; exports.default = default_1; const functions_1 = require("@azure/functions"); const handler_1 = __importDefault(require("./src/handler")); exports.swagger_ui_handler = handler_1.default; const makeHtml_1 = require("./src/makeHtml"); const fileMap_1 = require("./src/fileMap"); const jsdoc_1 = __importDefault(require("./src/jsdoc")); const tmp_path_1 = require("./src/tmp_path"); /** * * @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions`, default is 'swagger_ui' * @param swaggerOptions SwaggerOptions, { doc_path, title?, favicon16?, favicon32?, css_path?, display_topbar? : 0 | 1 | 2 | 3} * @param httpFunctionOptions HttpFunctionOptions, azure function options * @param is_swagger_jsdoc_object boolean | Array<boolean>, set to true if the doc_path is a swagger-jsdoc object or boolean array of the same length as doc_path specifying which doc_path is a swagger-jsdoc object * * This function creates a new http function with the specified name and options. * * Set options.route to the route where you want to access the Swagger UI without parameters. For example: 'apidocs', and '/{file?}' will be added automatically. * https://github.com/Le-Roi-du-village/azure-functions-swagger-ui#other-example */ function default_1(name = 'swagger_ui', swaggerOptions, httpFunctionOptions, is_swagger_jsdoc_object = false) { if (!swaggerOptions.doc_path) throw new Error('doc_path is required in swaggerOptions see https://github.com/Le-Roi-du-village/azure-functions-swagger-ui#other-example'); if (Array.isArray(swaggerOptions.doc_path) && is_swagger_jsdoc_object && (!Array.isArray(is_swagger_jsdoc_object) || (swaggerOptions.doc_path.length !== is_swagger_jsdoc_object.length))) throw new Error('If doc_path is an array and is_swagger_jsdoc_object is define, it must be an array of the same length , see https://github.com/Le-Roi-du-village/azure-functions-swagger-ui#multiple-swaggeropenapi-files-and-swagger-jsdoc.'); if (httpFunctionOptions) { if (httpFunctionOptions.route) { httpFunctionOptions.route = httpFunctionOptions.route.endsWith('/') ? `${httpFunctionOptions.route}{*file}` : `${httpFunctionOptions.route}/{*file}`; } else { httpFunctionOptions.route = `${name}/{*file}`; } if (!httpFunctionOptions.methods) httpFunctionOptions.methods = ['GET']; httpFunctionOptions.handler = handler_1.default; } else { httpFunctionOptions = { route: `${name}/{*file}`, handler: handler_1.default, methods: ['GET'] }; } if (is_swagger_jsdoc_object) (0, jsdoc_1.default)(name, swaggerOptions, is_swagger_jsdoc_object); (0, fileMap_1.updateCustomMap)(name, swaggerOptions, is_swagger_jsdoc_object); if (!swaggerOptions.html_path) (0, makeHtml_1.makeHtml)(name, swaggerOptions, httpFunctionOptions.route); functions_1.app.http(name, httpFunctionOptions); [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach((eventType) => { process.on(eventType, tmp_path_1.deleteTmpDir.bind(null)); }); }