UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

89 lines 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var yaml = require("js-yaml"); var logger_1 = require("@bitblit/ratchet/dist/common/logger"); /** * Monitoring endpoints */ var OpenApiDocModifier = /** @class */ (function () { function OpenApiDocModifier(options) { this.options = options; } OpenApiDocModifier.prototype.modifyOpenApiDoc = function (yamlString) { var _this = this; var rval; if (!!yamlString && !!this.options) { try { var openApi_1 = yaml.load(yamlString); var removeTags_1 = this.options.removeTags ? this.options.removeTags.map(function (t) { return t.toLowerCase(); }) : []; // Apply new server path if (this.options.newServerPath) { openApi_1['servers'] = [{ url: this.options.newServerPath }]; } if (!!this.options.removeTags && openApi_1['tags']) { if (openApi_1['tags']) { openApi_1['tags'] = openApi_1.tags.filter(function (f) { var n = !!f && !!f['name'] ? f['name'].toLowerCase() : ''; var i = removeTags_1.indexOf(n); return i === -1; }); } } if (openApi_1['paths']) { var newPaths_1 = {}; Object.keys(openApi_1['paths']).forEach(function (p) { var path = openApi_1['paths'][p]; Object.keys(path).forEach(function (verb) { var entry = path[verb]; entry.tags = !!entry.tags ? entry.tags.filter(function (t) { return removeTags_1.lastIndexOf(t.toLowerCase()) == -1; }) : entry.tags; var matcher = verb.toLowerCase() + ' ' + p.toLowerCase(); if (_this.matchNone(matcher, _this.options.removeEndpoints)) { newPaths_1[p] = newPaths_1[p] || {}; newPaths_1[p][verb] = entry; } }); }); if (this.options.sortEndpoints) { var keys = Object.keys(newPaths_1).sort(); var newPaths2_1 = {}; keys.forEach(function (k) { newPaths2_1[k] = newPaths_1[k]; }); newPaths_1 = newPaths2_1; } openApi_1['paths'] = newPaths_1; } // Sort the schemas and remove any that are marked for removal var remSchemas_1 = this.options.removeSchemas || []; remSchemas_1 = remSchemas_1.map(function (s) { return s.toLowerCase(); }); if (openApi_1['components'] && openApi_1['components']['schemas']) { var keys = Object.keys(openApi_1['components']['schemas']).sort(); var newComp_1 = {}; keys.forEach(function (k) { if (remSchemas_1.indexOf(k.toLowerCase()) === -1) { newComp_1[k] = openApi_1['components']['schemas'][k]; } }); openApi_1['components']['schemas'] = newComp_1; } rval = yaml.dump(openApi_1); } catch (err) { logger_1.Logger.error('Error processing yaml: %s', err, err); } } return rval; }; OpenApiDocModifier.prototype.matchNone = function (input, regex) { var rval = true; if (!!input && !!regex) { regex.forEach(function (r) { rval = rval && !r.test(input); }); } return rval; }; return OpenApiDocModifier; }()); exports.OpenApiDocModifier = OpenApiDocModifier; //# sourceMappingURL=open-api-doc-modifier.js.map