@n1k1t/mock-server
Version:
The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations
155 lines • 7.29 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 minimatch_1 = __importDefault(require("minimatch"));
const lodash_1 = __importDefault(require("lodash"));
const operator_1 = require("../models/operator");
const utils_1 = require("../utils");
class HasExpectationOperator extends operator_1.ExpectationOperator {
constructor() {
super(...arguments);
this.compiled = {
...(this.command.$regExp && {
regExp: new RegExp(this.command.$regExp.source, this.command.$regExp.flags)
}),
...(this.command.$regExpAnyOf && {
regExpAnyOf: this.command.$regExpAnyOf.map((exp) => new RegExp(exp.source, exp.flags)),
}),
...(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 {};
}
if (this.command.$value !== undefined) {
return acc([this.command.$value]);
}
if (this.command.$valueAnyOf) {
return acc(this.command.$valueAnyOf);
}
if (this.command.$match) {
return acc([this.command.$match]);
}
if (this.command.$matchAnyOf) {
return acc(this.command.$matchAnyOf);
}
if (this.command.$regExp) {
return acc([this.command.$regExp.source]);
}
if (this.command.$regExpAnyOf) {
return acc(this.command.$regExpAnyOf.map((value) => value.source));
}
return {};
}
async match(context) {
if (!(0, utils_1.checkIsLocationInContext)(this.command.$location, context)) {
return false;
}
const payload = (0, utils_1.extractContextByLocation)(this.command.$location, context);
if (!payload) {
return false;
}
switch (payload.type) {
case 'string': {
if (this.command.$value !== undefined) {
return payload.value === this.command.$value;
}
if (this.command.$valueAnyOf) {
return this.command.$valueAnyOf.some((value) => payload.value === value);
}
if (this.compiled.regExp) {
return this.compiled.regExp.test(payload.value ?? '');
}
if (this.compiled.regExpAnyOf) {
return this.compiled.regExpAnyOf.some((regExp) => regExp.test(payload.value ?? ''));
}
if (typeof this.command.$match === 'string') {
return (0, minimatch_1.default)(payload.value ?? '', this.command.$match);
}
if (this.command.$matchAnyOf) {
return this.command.$matchAnyOf.some((pattern) => (0, minimatch_1.default)(payload.value ?? '', String(pattern)));
}
if (this.compiled.exec) {
return (await this.compiled.exec('match', context, payload.value)) === true;
}
return false;
}
case 'number': {
if (this.command.$value !== undefined) {
return payload.value === this.command.$value;
}
if (this.command.$valueAnyOf) {
return this.command.$valueAnyOf.some((value) => payload.value === value);
}
if (typeof this.command.$match === 'string') {
return (0, minimatch_1.default)(String(payload.value), this.command.$match);
}
if (this.command.$matchAnyOf) {
const value = String(payload.value);
return this.command.$matchAnyOf.some((pattern) => (0, minimatch_1.default)(value, String(pattern)));
}
if (this.compiled.exec) {
return (await this.compiled.exec('match', context, payload.value)) === true;
}
return false;
}
case 'buffer': {
if (this.compiled.exec) {
return (await this.compiled.exec('match', context, payload.value)) === true;
}
return false;
}
case 'object': {
if (this.command.$path && !lodash_1.default.has(payload.value, this.command.$path)) {
return false;
}
const values = (this.command.$path
? [lodash_1.default.get(payload.value, this.command.$path)]
: (this.command.$jsonPath && lodash_1.default.isObject(payload.value))
? (0, utils_1.extractWithJsonPathSafe)({ path: this.command.$jsonPath, json: payload.value }).results?.map(({ value }) => value)
: [payload.value]) ?? [];
if (this.command.$value !== undefined) {
return values.every((value) => lodash_1.default.isEqual(this.command.$value, value));
}
if (this.command.$valueAnyOf) {
return values.every((source) => this.command.$valueAnyOf.some((target) => lodash_1.default.isEqual(source, target)));
}
if (this.compiled.regExp) {
return values.every((value) => this.compiled.regExp.test(String(value)));
}
if (this.compiled.regExpAnyOf) {
return values.every((value) => this.compiled.regExpAnyOf.some((exp) => exp.test(String(value))));
}
if (typeof this.command.$match === 'string') {
return values.every((value) => (0, minimatch_1.default)(String(value), this.command.$match));
}
if (typeof this.command.$match === 'object') {
return values.every((value) => lodash_1.default.isMatch(value, this.command.$match));
}
if (typeof this.command.$matchAnyOf?.[0] === 'string') {
return values.every((value) => this.command.$matchAnyOf.some((pattern) => (0, minimatch_1.default)(String(value), String(pattern))));
}
if (typeof this.command.$matchAnyOf?.[0] === 'object') {
return values.every((value) => this.command.$matchAnyOf.some((target) => lodash_1.default.isMatch(value, target)));
}
if (this.compiled.exec) {
const executed = await Promise.all(values.map((value) => this.compiled.exec('match', context, value)));
return executed.every((result) => result === true);
}
return values.length !== 0;
}
default: return false;
}
}
async manipulate(context) {
return context;
}
}
exports.default = HasExpectationOperator;
//# sourceMappingURL=has.operator.js.map