@apvee/azure-functions-openapi
Version:
An extension for Azure Functions V4 that provides support for exporting OpenAPI spec files from annotated Azure Functions.
64 lines (63 loc) • 3.9 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerOpenAPIHandler = registerOpenAPIHandler;
const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
const functions_1 = require("@azure/functions");
const yaml_1 = require("yaml");
const openAPI3ToSwagger2_1 = __importDefault(require("../core/openAPI3ToSwagger2"));
const registry_1 = require("../core/registry");
/**
* Registers an OpenAPI handler for an Azure Function.
*
* @param {'anonymous' | 'function' | 'admin'} authLevel - The authorization level required to access the function.
* @param {OpenAPIObjectConfig} configuration - The OpenAPI configuration object containing information about the API.
* @param {'2.0' | '3.0.3' | '3.1.0'} version - The OpenAPI version to use.
* @param {'json' | 'yaml'} format - The format of the OpenAPI document.
* @param {string} [route] - Optional. The route at which the OpenAPI document will be served. If not provided, a default route will be used based on the format and version.
* @returns {OpenAPIDocumentInfo} An object containing the title and URL of the registered OpenAPI document.
*/
function registerOpenAPIHandler(authLevel, configuration, version, format, route) {
const finalRoute = route || ((format === "json") ? `openapi-${version}.json` : `openapi-${version}.yaml`);
const functionName = `X_OpenAPI_${version.split('.').join('_')}_${format === 'json' ? 'Json' : 'Yaml'}Handler`;
functions_1.app.http(functionName, {
methods: ['GET'],
authLevel: authLevel,
handler: (request, context) => __awaiter(this, void 0, void 0, function* () {
context.log(`Invoking OpenAPI ${version} ${format} definition handler for url "${request.url}"`);
const openApiGenerator = version === '3.1.0' ? zod_to_openapi_1.OpenApiGeneratorV31 : zod_to_openapi_1.OpenApiGeneratorV3;
let openAPIDefinition = new openApiGenerator(registry_1.registry.definitions)
.generateDocument({
openapi: version,
info: configuration.info,
security: configuration.security,
servers: configuration.servers || [{ url: `${new URL(request.url).origin}` }],
externalDocs: configuration.externalDocs,
tags: configuration.tags
});
if (version === '2.0') {
const converter = new openAPI3ToSwagger2_1.default(openAPIDefinition);
openAPIDefinition = converter.convert();
}
return {
status: 200,
headers: { 'Content-Type': format === 'json' ? 'application/json' : 'application/x-yaml' },
body: (format === 'yaml') ? (0, yaml_1.stringify)(openAPIDefinition) : undefined,
jsonBody: (format === 'json') ? openAPIDefinition : undefined,
};
}),
route: finalRoute
});
return { title: `${configuration.info.title} (${format === 'json' ? 'Json' : 'Yaml'} - OpenAPI ${version})`, url: finalRoute };
}
;