@apvee/azure-functions-openapi
Version:
An extension for Azure Functions V4 that provides support for exporting OpenAPI spec files from annotated Azure Functions.
77 lines (76 loc) • 3.53 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerSwaggerUIHandler = registerSwaggerUIHandler;
const functions_1 = require("@azure/functions");
/**
* Registers a Swagger UI handler for an Azure Function.
*
* @param {'anonymous' | 'function' | 'admin'} authLevel - The authorization level required to access the Swagger UI.
* @param {string} azureFunctionRoutePrefix - The route prefix for the Azure Function. Defaults to 'api'.
* @param {OpenAPIDocumentInfo[]} openAPIDocuments - An array of OpenAPI document information objects to be included in the Swagger UI.
*
* This function sets up an HTTP GET handler that serves a Swagger UI page, which lists the provided OpenAPI documents.
* The Swagger UI is configured to use the provided URLs and titles for the OpenAPI documents.
*/
function registerSwaggerUIHandler(authLevel = 'anonymous', azureFunctionRoutePrefix = 'api', openAPIDocuments) {
const fxHandler = (request, context) => __awaiter(this, void 0, void 0, function* () {
context.log(`Invoking SwaggerUI handler for url "${request.url}"`);
const urls = openAPIDocuments.map(doc => {
return {
url: `/${azureFunctionRoutePrefix}/${doc.url}`,
name: doc.title
};
});
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js" crossorigin></script>
</head>
<body>
<div id="swagger-ui"></div>
<div id="swagger-ui"></div>
<script>
window.swaggerUI = SwaggerUIBundle({
urls: ${JSON.stringify(urls)},
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
});
</script>
</body>
</html>`;
context.log(`SwaggerUI generated successfully`);
return {
status: 200,
body: html,
headers: {
"Content-Type": "text/html"
}
};
});
functions_1.app.http('X_HandlerSwaggerUI', {
methods: ['GET'],
authLevel: authLevel,
handler: fxHandler,
route: `swagger-ui.html`
});
}
;