@n1k1t/mock-server
Version:
The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations
71 lines • 3.02 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 lodash_1 = __importDefault(require("lodash"));
const utils_1 = require("../utils");
const operator_1 = require("../models/operator");
class SetExpectationOperator extends operator_1.ExpectationOperator {
constructor() {
super(...arguments);
this.compiled = {
...(this.command.$exec && {
exec: this.compileExecHandler(this.command.$exec, ['payload', 'utils']),
}),
};
}
get tags() {
const acc = (0, utils_1.compileMetaTagsAccumulator)(this.command.$location);
if (!acc) {
return {};
}
return this.command.$value !== undefined
? acc([this.command.$value])
: {};
}
async match() {
return true;
}
async manipulate(context) {
const payload = (0, utils_1.extractContextByLocation)(this.command.$location, context);
if (!payload) {
return context;
}
switch (payload.type) {
case 'number':
case 'buffer':
case 'string': {
lodash_1.default.set(payload.parent, payload.key, this.compiled.exec
? await this.compiled.exec('manipulate', context, payload.value)
: this.command.$value);
return context;
}
case 'object': {
if (this.command.$path) {
lodash_1.default.set(payload.parent, `${payload.key}.${this.command.$path}`, this.compiled.exec
? await this.compiled.exec('manipulate', context, lodash_1.default.get(payload.value, this.command.$path))
: this.command.$value);
return context;
}
if (this.command.$jsonPath && lodash_1.default.isObject(payload.value)) {
const segments = (0, utils_1.extractWithJsonPathSafe)({ path: this.command.$jsonPath, json: payload.value }).results ?? [];
for (const segment of segments) {
const target = this.compiled.exec
? await this.compiled.exec('manipulate', context, segment.value)
: this.command.$value;
lodash_1.default.set(segment.parent, segment.parentProperty, target);
}
return context;
}
lodash_1.default.set(payload.parent, payload.key, this.compiled.exec
? await this.compiled.exec('manipulate', context, payload.value)
: this.command.$value);
return context;
}
default: return context;
}
}
}
exports.default = SetExpectationOperator;
//# sourceMappingURL=set.operator.js.map