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.
115 lines (114 loc) • 5.15 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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.default = default_1;
const fs = __importStar(require("fs"));
const util = __importStar(require("util"));
const fileMap_1 = require("./fileMap");
const tmp_path_1 = __importDefault(require("./tmp_path"));
const path = __importStar(require("path"));
const readFileAsync = util.promisify(fs.readFile);
const fileExistsAsync = util.promisify(fs.exists);
const package_html_path = path.resolve(tmp_path_1.default, '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 .
function default_1(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_1.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 = fileMap_1.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 = fileMap_1.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
};
});
}
;