unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
90 lines • 3.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const xregexp_1 = __importDefault(require("xregexp"));
const constants_1 = require("./constants");
exports.getPathParametersFromPath = (path) => {
const pathParameters = [];
xregexp_1.default.forEach(path, constants_1.OAS_PATH_PARAM_REGEXP, (matchArr) => {
pathParameters.push(matchArr[1]);
});
return pathParameters;
};
exports.getPathParametersFromSchema = (schema, path) => {
if (!path.includes("{") || schema[path] === undefined) {
return [];
}
const schemaPathParameters = exports.getAtLevel(schema[path], 2, (_, v) => v !== null && v.in === constants_1.OAS_PATH_PARAMS_KW);
if (schemaPathParameters.length === 0) {
throw new Error(`Found a dynamic path '${path}' but no description for path parameters!`);
}
return schemaPathParameters;
};
exports.buildPathRegexStringFromParameters = (path, schemaParameters, pathParameters) => {
if (schemaParameters.length === 0) {
return path;
}
let newPath = `${path}`;
schemaParameters.forEach((p) => {
const paramLoc = pathParameters.indexOf(p.name);
if (paramLoc !== -1) {
newPath = newPath.replace(`{${p.name}}`, `(?<${p.name}>[^/]+)`);
pathParameters.splice(paramLoc, 1);
}
});
if (pathParameters.length > 0) {
throw new Error(`Found a dynamic path '${path}' but the following path ` +
`parameters have not been described: ${pathParameters}!`);
}
return newPath;
};
exports.getAtLevel = (nestedObj, level, filterFn) => {
if (level < 0) {
throw new Error(`Not sure what should I find at nested level ${level}...`);
}
if (nestedObj === undefined || Object.keys(nestedObj).length === 0) {
throw new Error(`Empty 'nestedObj' received - ${JSON.stringify(nestedObj)}`);
}
let i = 0;
let subObjects = [];
let prevObjects = [nestedObj];
while (i < level) {
prevObjects.forEach(o => {
if (Array.isArray(o)) {
o.forEach(e => subObjects.push(e));
}
else if (typeof o === "object") {
const vals = Object.values(o);
if (vals.length === 1) {
subObjects.push(vals[0]);
}
else {
Object.values(o).forEach(v => subObjects.push(v));
}
}
});
prevObjects = subObjects;
subObjects = [];
i++;
}
prevObjects.forEach(o => {
if (Array.isArray(o)) {
o.forEach(e => {
if (filterFn === undefined || filterFn(undefined, e)) {
subObjects.push(e);
}
});
}
else if (typeof o === "object") {
Object.keys(o).forEach(k => {
if (filterFn === undefined || filterFn(k, o[k])) {
subObjects.push({ [k]: o[k] });
}
});
}
});
return subObjects;
};
//# sourceMappingURL=util.js.map