@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
53 lines • 2.6 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const deepmerge_1 = __importDefault(require("deepmerge"));
const lodash_1 = __importDefault(require("lodash"));
const utils_1 = require("../utils");
const operator_1 = require("../models/operator");
class MergeExpectationOperator extends operator_1.ExpectationOperator {
constructor() {
super(...arguments);
this.compiled = {
...(this.command.$exec && {
exec: this.compileExecHandler(this.command.$exec, ['payload', 'utils']),
}),
};
}
get tags() {
return [];
}
match() {
return true;
}
manipulate(context) {
const payload = (0, utils_1.extractContextByLocation)(this.command.$location, context);
if (payload?.type !== 'object' || !lodash_1.default.isObject(payload.value)) {
return context;
}
if (this.command.$path) {
const value = lodash_1.default.get(payload.value, this.command.$path);
lodash_1.default.set(payload.parent, `${payload.key}.${this.command.$path}`, this.compiled.exec
? (0, deepmerge_1.default)(value, this.compiled.exec('manipulate', context, value), { arrayMerge: (target, source) => source })
: (0, deepmerge_1.default)(value, this.command.$value ?? {}, { arrayMerge: (target, source) => source }));
return context;
}
if (this.command.$jsonPath) {
(0, utils_1.extractWithJsonPathSafe)({ path: this.command.$jsonPath, json: payload.value }).results?.forEach((segment) => {
const value = lodash_1.default.get(segment.parent, segment.parentProperty);
lodash_1.default.set(payload.value, segment.pointer.substring(1).replace(/\//g, '.'), this.compiled.exec
? (0, deepmerge_1.default)(value, this.compiled.exec('manipulate', context, value))
: (0, deepmerge_1.default)(value, this.command.$value ?? {}));
});
return context;
}
lodash_1.default.set(payload.parent, payload.key, this.compiled.exec
? (0, deepmerge_1.default)(payload.value, this.compiled.exec('manipulate', context, payload.value))
: (0, deepmerge_1.default)(payload.value, this.command.$value ?? {}));
return context;
}
}
exports.default = MergeExpectationOperator;
//# sourceMappingURL=merge.operator.js.map