UNPKG

@n1k1t/mock-server

Version:

The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations

57 lines 2.72 kB
"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 {}; } async match() { return true; } async 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); const target = this.compiled.exec ? (await this.compiled.exec('manipulate', context, value)) : this.command.$value ?? {}; lodash_1.default.set(payload.parent, `${payload.key}.${this.command.$path}`, (0, deepmerge_1.default)(value, target, { arrayMerge: (target, source) => source })); return context; } if (this.command.$jsonPath) { const segments = (0, utils_1.extractWithJsonPathSafe)({ path: this.command.$jsonPath, json: payload.value }).results ?? []; for (const segment of segments) { const value = lodash_1.default.get(segment.parent, segment.parentProperty); const target = this.compiled.exec ? (await this.compiled.exec('manipulate', context, value)) : this.command.$value ?? {}; lodash_1.default.set(payload.value, segment.pointer.substring(1).replace(/\//g, '.'), (0, deepmerge_1.default)(value, target, { arrayMerge: (target, source) => source })); } return context; } const target = this.compiled.exec ? (await this.compiled.exec('manipulate', context, payload.value)) : this.command.$value ?? {}; lodash_1.default.set(payload.parent, payload.key, (0, deepmerge_1.default)(payload.value, target, { arrayMerge: (target, source) => source })); return context; } } exports.default = MergeExpectationOperator; //# sourceMappingURL=merge.operator.js.map