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.
178 lines (177 loc) • 7.43 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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.clear_custom_maps = exports.custom_maps = exports.fileMap = void 0;
exports.updateCustomMap = updateCustomMap;
const path = __importStar(require("path"));
const tmp_path_1 = require("./tmp_path");
// Path where the swagger-ui-dist package is installed
const swagger_ui_dist_path = require("swagger-ui-dist").getAbsoluteFSPath();
/*
* Map of the files that are part of the swagger-ui-dist or this package
*/
const fileMap = new Map([
[
'favicon-32x32.png', {
fileName: path.resolve(swagger_ui_dist_path, 'favicon-32x32.png'),
contentType: 'image/png'
}
],
[
'favicon-16x16.png', {
fileName: path.resolve(swagger_ui_dist_path, 'favicon-16x16.png'),
contentType: 'image/png'
}
],
[
'swagger-initializer.js', {
fileName: path.resolve(tmp_path_1.package_path, '../swagger-initializer.js'),
contentType: 'application/javascript; charset=UTF-8'
}
],
[
'index.css', {
fileName: path.resolve(swagger_ui_dist_path, 'index.css'),
contentType: 'text/css; charset=utf-8'
}
],
[
'swagger-ui.css', {
fileName: path.resolve(swagger_ui_dist_path, 'swagger-ui.css'),
contentType: 'text/css; charset=utf-8'
}
],
[
'swagger-ui-bundle.js', {
fileName: path.resolve(swagger_ui_dist_path, 'swagger-ui-bundle.js'),
contentType: 'application/javascript; charset=UTF-8'
}
],
[
'swagger-ui-standalone-preset.js', {
fileName: path.resolve(swagger_ui_dist_path, 'swagger-ui-standalone-preset.js'),
contentType: 'application/javascript; charset=UTF-8'
}
],
[
'swagger-ui.css.map', {
fileName: path.resolve(swagger_ui_dist_path, 'swagger-ui.css.map'),
contentType: 'application/json; charset=UTF-8'
}
],
]);
exports.fileMap = fileMap;
// Maps of custom files specified in SwaggerOptions
const custom_maps = {};
exports.custom_maps = custom_maps;
// Function to empty the custom_map
const clear_custom_maps = function (key) {
if (key) {
custom_maps[key].clear();
return;
}
Object.assign(custom_maps, {});
};
exports.clear_custom_maps = clear_custom_maps;
/**
*
* @param name string
* @param swaggerOptions SwaggerOptions, { doc_path, title?, favicon16?, favicon32?, css_path?, display_topbar? : 0 | 1 | 2 | 3}
* @param is_swagger_jsdoc_object boolean | Array<boolean>, set to true if the doc_path is a jsdoc object or boolean array of the same length as doc_path specifying which element of doc_path is a jsdoc object
*
*
* This function updates the custom_map with the custom files specified in the swaggerOptions
*/
function updateCustomMap(name, swaggerOptions, is_swagger_jsdoc_object) {
custom_maps[name] = new Map();
const custom_map = custom_maps[name];
if (Array.isArray(swaggerOptions.doc_path)) {
is_swagger_jsdoc_object = is_swagger_jsdoc_object || new Array(swaggerOptions.doc_path.length).fill(false);
for (let i = 0; i < swaggerOptions.doc_path.length; i++) {
const extention = path.extname(swaggerOptions.doc_path[i].url);
if (is_swagger_jsdoc_object[i]) {
const base = path.basename(swaggerOptions.doc_path[i].url);
custom_map.set(path.normalize(base), {
fileName: path.normalize(swaggerOptions.doc_path[i].url),
contentType: extention === '.json' ? 'application/json; charset=UTF-8' : (extention === '.yaml') || (extention === '.yml') ? 'application/x-yaml; charset=UTF-8' : ''
});
swaggerOptions.doc_path[i].url = base;
}
else {
custom_map.set(path.normalize(swaggerOptions.doc_path[i].url), {
fileName: path.normalize(swaggerOptions.doc_path[i].url),
contentType: extention === '.json' ? 'application/json; charset=UTF-8' : (extention === '.yaml') || (extention === '.yml') ? 'application/x-yaml; charset=UTF-8' : ''
});
}
}
}
else {
const extention = path.extname(swaggerOptions.doc_path);
if (is_swagger_jsdoc_object) {
const base = path.normalize(path.basename(swaggerOptions.doc_path));
custom_map.set(base, {
fileName: path.normalize(swaggerOptions.doc_path),
contentType: extention === '.json' ? 'application/json; charset=UTF-8' : (extention === '.yaml') || (extention === '.yml') ? 'application/x-yaml; charset=UTF-8' : ''
});
swaggerOptions.doc_path = base;
}
else {
custom_map.set(path.normalize(swaggerOptions.doc_path), {
fileName: path.normalize(swaggerOptions.doc_path),
contentType: extention === '.json' ? 'application/json; charset=UTF-8' : (extention === '.yaml') || (extention === '.yml') ? 'application/x-yaml; charset=UTF-8' : ''
});
}
}
if (swaggerOptions.favicon16)
custom_map.set(path.normalize(swaggerOptions.favicon16), {
fileName: path.normalize(swaggerOptions.favicon16),
contentType: 'image/png'
});
if (swaggerOptions.favicon32)
custom_map.set(path.normalize(swaggerOptions.favicon32), {
fileName: path.normalize(swaggerOptions.favicon32),
contentType: 'image/png'
});
if (swaggerOptions.css_path)
custom_map.set(path.normalize(swaggerOptions.css_path), {
fileName: path.normalize(swaggerOptions.css_path),
contentType: 'text/css; charset=UTF-8'
});
if (swaggerOptions.html_path)
custom_map.set('html', {
fileName: path.normalize(swaggerOptions.html_path),
contentType: 'text/html; charset=UTF-8'
});
}