@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
91 lines • 4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenApiDocModifier = void 0;
const js_yaml_1 = __importDefault(require("js-yaml"));
const logger_1 = require("@bitblit/ratchet/common/logger");
/**
* Monitoring endpoints
*/
class OpenApiDocModifier {
constructor(options) {
this.options = options;
}
modifyOpenApiDoc(yamlString) {
let rval;
if (!!yamlString && !!this.options) {
try {
const openApi = js_yaml_1.default.load(yamlString);
const removeTags = this.options.removeTags ? this.options.removeTags.map((t) => t.toLowerCase()) : [];
// Apply new server path
if (this.options.newServerPath) {
openApi['servers'] = [{ url: this.options.newServerPath }];
}
if (!!this.options.removeTags && openApi['tags']) {
if (openApi['tags']) {
openApi['tags'] = openApi.tags.filter((f) => {
const n = !!f && !!f['name'] ? f['name'].toLowerCase() : '';
const i = removeTags.indexOf(n);
return i === -1;
});
}
}
if (openApi['paths']) {
let newPaths = {};
Object.keys(openApi['paths']).forEach((p) => {
const path = openApi['paths'][p];
Object.keys(path).forEach((verb) => {
const entry = path[verb];
entry.tags = !!entry.tags ? entry.tags.filter((t) => removeTags.lastIndexOf(t.toLowerCase()) == -1) : entry.tags;
const matcher = verb.toLowerCase() + ' ' + p.toLowerCase();
if (this.matchNone(matcher, this.options.removeEndpoints)) {
newPaths[p] = newPaths[p] || {};
newPaths[p][verb] = entry;
}
});
});
if (this.options.sortEndpoints) {
const keys = Object.keys(newPaths).sort();
const newPaths2 = {};
keys.forEach((k) => {
newPaths2[k] = newPaths[k];
});
newPaths = newPaths2;
}
openApi['paths'] = newPaths;
}
// Sort the schemas and remove any that are marked for removal
let remSchemas = this.options.removeSchemas || [];
remSchemas = remSchemas.map((s) => s.toLowerCase());
if (openApi['components'] && openApi['components']['schemas']) {
const keys = Object.keys(openApi['components']['schemas']).sort();
const newComp = {};
keys.forEach((k) => {
if (remSchemas.indexOf(k.toLowerCase()) === -1) {
newComp[k] = openApi['components']['schemas'][k];
}
});
openApi['components']['schemas'] = newComp;
}
rval = js_yaml_1.default.dump(openApi);
}
catch (err) {
logger_1.Logger.error('Error processing yaml: %s', err, err);
}
}
return rval;
}
matchNone(input, regex) {
let rval = true;
if (!!input && !!regex) {
regex.forEach((r) => {
rval = rval && !r.test(input);
});
}
return rval;
}
}
exports.OpenApiDocModifier = OpenApiDocModifier;
//# sourceMappingURL=open-api-doc-modifier.js.map