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.

76 lines (75 loc) 3.46 kB
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()); }); }; import * as fs from 'fs'; import * as util from 'util'; import { custom_maps, fileMap } from "./fileMap"; import tmp_path from './tmp_path'; import * as path from "path"; const readFileAsync = util.promisify(fs.readFile); const fileExistsAsync = util.promisify(fs.exists); const package_html_path = path.resolve(tmp_path, 'html'); // When calling app.http you need to set "route" parameter // to something like: "apidocs/{*file}". // Then your swagger ui will become available at [hostname]/api/apidocs . export default function (request, context) { return __awaiter(this, void 0, void 0, function* () { const file = !!request.params.file ? path.normalize(request.params.file) : ''; // If no file is specified, default to index.html const mapEntry = fileMap.get(file); // Check if the requested file is part of the swagger-ui-dist package or this package if (!!mapEntry) { if (yield fileExistsAsync(mapEntry.fileName)) { return { body: yield readFileAsync(mapEntry.fileName), headers: { 'Content-Type': mapEntry.contentType } }; } return { status: 404 }; } const mapCustomEntry = custom_maps[context.functionName].get(file); // Check if the requested file is part of the custom files specified in the swaggerOptions if (!!mapCustomEntry) { if (yield fileExistsAsync(mapCustomEntry.fileName)) { return { body: yield readFileAsync(mapCustomEntry.fileName), headers: { 'Content-Type': mapCustomEntry.contentType } }; } return { status: 404 }; } // Returning index.html by default, to support client routing const html = custom_maps[context.functionName].get('html'); if (html) { if (yield fileExistsAsync(html.fileName)) { return { body: yield readFileAsync(html.fileName), headers: { 'Content-Type': 'text/html; charset=UTF-8' } }; } context.error('Error reading the HTML file:', html.fileName); return { status: 404 }; } const file_path = path.normalize(`${package_html_path}/${context.functionName}.html`); if (yield fileExistsAsync(file_path)) { return { body: yield readFileAsync(file_path), headers: { 'Content-Type': 'text/html; charset=UTF-8' } }; } context.error('Error reading the HTML file:', file_path); return { status: 404 }; }); } ;