unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
58 lines • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(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) => {
return Object.keys(state)
.filter((key) => interfaces_2.TopLevelDSLKeys[key] !== undefined &&
interfaces_2.TopLevelDSLKeys[key] === typeof state[key])
.reduce((a, b) => (Object.assign({}, a, { [b]: state[b] })), {});
};
exports.filterTopLevelDSL = (state) => {
return Object.keys(state)
.filter((key) => interfaces_2.TopLevelDSLKeys[key] === undefined ||
interfaces_2.TopLevelDSLKeys[key] !== typeof state[key])
.reduce((a, b) => (Object.assign({}, a, { [b]: state[b] })), {});
};
exports.throwOnErrorIfStrict = (fn) => {
try {
fn();
}
catch (e) {
if (dsl_1.DSL.STRICT_MODE) {
throw e;
}
}
};
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({}, 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