UNPKG

@n1k1t/mock-server

Version:

Powerful util to setup mocks over HTTP APIs

67 lines 2.99 kB
"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() { if (this.command.$location === 'outgoing.status') { return this.command.$value ? [{ location: 'outgoing.status', value: Number(this.command.$value) }] : []; } if (this.command.$location === 'error') { return this.command.$value ? [{ location: 'error', value: String(this.command.$value) }] : []; } return []; } match() { return true; } 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 ? 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 ? 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)) { (0, utils_1.extractWithJsonPathSafe)({ path: this.command.$jsonPath, json: payload.value }).results?.forEach(({ parent, parentProperty, value }) => this.compiled.exec ? lodash_1.default.set(parent, parentProperty, this.compiled.exec('manipulate', context, value)) : lodash_1.default.set(parent, parentProperty, this.command.$value)); return context; } lodash_1.default.set(payload.parent, payload.key, this.compiled.exec ? this.compiled.exec('manipulate', context, payload.value) : this.command.$value); return context; } default: return context; } } } exports.default = SetExpectationOperator; //# sourceMappingURL=set.operator.js.map