@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
162 lines • 7.58 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 utils_1 = require("../utils");
const operator_1 = require("../models/operator");
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() {
if (this.command.$location !== 'path' && this.command.$location !== 'method') {
return [];
}
if (this.command.$value) {
return [{ location: this.command.$location, value: String(this.command.$value) }];
}
if (this.command.$valueAnyOf) {
return this.command.$valueAnyOf.map((value) => ({
location: this.command.$location,
value: String(value)
}));
}
if (this.command.$match) {
return [{ location: this.command.$location, value: String(this.command.$match) }];
}
if (this.command.$matchAnyOf) {
return this.command.$matchAnyOf.map((value) => ({
location: this.command.$location,
value: String(value)
}));
}
if (this.command.$regExp) {
return [{ location: this.command.$location, value: this.command.$regExp.source }];
}
if (this.command.$regExpAnyOf) {
return this.command.$regExpAnyOf.map((value) => ({
location: this.command.$location,
value: value.source,
}));
}
return [];
}
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 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 this.compiled.exec('match', context, payload.value) === true;
}
return false;
}
case 'buffer': {
if (this.compiled.exec) {
return 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) {
return values.every((value) => this.compiled.exec('match', context, value) === true);
}
return values.length !== 0;
}
default: return false;
}
}
manipulate(context) {
return context;
}
}
exports.default = HasExpectationOperator;
//# sourceMappingURL=has.operator.js.map