@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
50 lines • 2.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeExpectationSchema = exports.introspectExpectationOperatorsSchema = void 0;
const rfdc_1 = __importDefault(require("rfdc"));
const lodash_1 = __importDefault(require("lodash"));
const utils_1 = require("../../utils");
const types_1 = require("../types");
const clone = (0, rfdc_1.default)();
const introspectExpectationOperatorsSchema = (schema, handler, location = '') => {
Object.keys(schema).forEach((key) => {
const path = location ? `${location}.${String(key)}` : String(key);
handler(key, schema, path);
if (lodash_1.default.isObject(schema[key]) && !types_1.LExpectationFlatOperator.includes(key)) {
(0, exports.introspectExpectationOperatorsSchema)(schema[key], handler, path);
}
});
};
exports.introspectExpectationOperatorsSchema = introspectExpectationOperatorsSchema;
const serializeExpectationSchema = (schema) => {
const cloned = clone(schema ?? {});
(0, exports.introspectExpectationOperatorsSchema)(schema, (key, schema, path) => {
if (key === '$exec') {
lodash_1.default.set(cloned, path, schema.$exec?.toString());
}
if (key === '$has' && schema.$has?.$regExp) {
lodash_1.default.set(cloned, `${path}.$regExp`, (0, utils_1.serializeRegExp)(schema.$has.$regExp));
}
if (key === '$has' && schema.$has?.$regExpAnyOf) {
lodash_1.default.set(cloned, `${path}.$regExpAnyOf`, schema.$has.$regExpAnyOf.map((expr) => (0, utils_1.serializeRegExp)(expr)));
}
if (key === '$has' && schema.$has?.$exec) {
lodash_1.default.set(cloned, `${path}.$exec`, schema.$has.$exec.toString());
}
if (key === '$set' && schema.$set?.$exec) {
lodash_1.default.set(cloned, `${path}.$exec`, schema.$set.$exec.toString());
}
if (key === '$merge' && schema.$merge?.$exec) {
lodash_1.default.set(cloned, `${path}.$exec`, schema.$merge.$exec.toString());
}
if (key === '$switch' && schema.$switch?.$exec) {
lodash_1.default.set(cloned, `${path}.$exec`, schema.$switch.$exec.toString());
}
});
return cloned;
};
exports.serializeExpectationSchema = serializeExpectationSchema;
//# sourceMappingURL=schema.js.map