unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
124 lines • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const jsyaml = require("js-yaml");
const pointer = require("json-pointer");
const fspath = require("path");
const XRegExp = require("xregexp");
const constants_1 = require("./constants");
function derefIfNeeded({ schema, absPath, }) {
return (mightHaveReference) => {
const selfDeref = derefIfNeeded({ schema, absPath });
if (typeof mightHaveReference !== "object" ||
Object.keys(mightHaveReference).length === 0) {
return mightHaveReference;
}
const noReferences = Array.isArray(mightHaveReference)
? mightHaveReference.map(element => selfDeref(element))
: Object.keys(mightHaveReference).reduce((obj, childKey) => (Object.assign(Object.assign({}, obj), { [childKey]: selfDeref(mightHaveReference[childKey]) })), mightHaveReference);
const refValue = noReferences.$ref;
if (refValue === undefined) {
return noReferences;
}
const isLocal = refValue.startsWith("#");
const afterPound = refValue
.split("#")
.slice(1)
.join("#");
const resolvedSchema = isLocal
? schema
: loadSpec(fspath.join(absPath, refValue.split("#")[0]));
const ref = pointer.get(resolvedSchema, afterPound);
return selfDeref(ref);
};
}
exports.derefIfNeeded = derefIfNeeded;
const loadSpec = (path) => {
const content = fs.readFileSync(path, "utf8");
return fspath.extname(path).toLowerCase() === ".json"
? JSON.parse(content)
: jsyaml.safeLoad(content);
};
exports.getPathParametersFromPath = (path) => {
const pathParameters = [];
XRegExp.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