unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
50 lines • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("debug");
const interfaces_1 = require("../interfaces");
const constants_1 = require("./constants");
const dsl_1 = require("./dsl");
const interfaces_2 = require("./interfaces");
const debugLog = debug_1.default("unmock:dsl:utils");
exports.getTopLevelDSL = (state) => Object.keys(state)
.filter((key) => interfaces_2.TopLevelDSLKeys[key] !== undefined && interfaces_2.TopLevelDSLKeys[key](state[key]))
.reduce((a, b) => (Object.assign(Object.assign({}, a), { [b]: state[b] })), {});
exports.filterTopLevelDSL = (state) => Object.keys(state)
.filter((key) => interfaces_2.TopLevelDSLKeys[key] === undefined || !interfaces_2.TopLevelDSLKeys[key](state[key]))
.reduce((a, b) => (Object.assign(Object.assign({}, a), { [b]: state[b] })), {});
exports.throwOnErrorIfStrict = (fn) => {
try {
return fn();
}
catch (e) {
if (dsl_1.DSL.STRICT_MODE) {
throw e;
}
return undefined;
}
};
exports.buildUnmockPropety = (name, value) => ({
[name]: { type: constants_1.UNMOCK_TYPE, default: value },
});
exports.injectUnmockProperty = (responses, unmockProperty) => {
Object.values(responses).forEach((response) => {
Object.values(response).forEach((schema) => {
schema.properties = Object.assign(Object.assign({}, schema.properties), unmockProperty);
});
});
};
exports.hasUnmockProperty = (schema, name) => {
const log = (found) => debugLog(`${found ? "Found" : "Can't find"} '${name}' in ${JSON.stringify(schema)}`);
if (schema.properties === undefined) {
log(false);
return false;
}
const prop = schema.properties[name];
if (prop === undefined || interfaces_1.isReference(prop)) {
log(false);
return false;
}
log(prop.type === constants_1.UNMOCK_TYPE);
return prop.type === constants_1.UNMOCK_TYPE;
};
//# sourceMappingURL=utils.js.map