@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
101 lines • 4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Expectation = void 0;
const random_animal_name_1 = __importDefault(require("random-animal-name"));
const lodash_1 = __importDefault(require("lodash"));
const uuid_1 = require("uuid");
const utils_1 = require("../utils");
const operators = __importStar(require("../operators"));
class Expectation {
get forward() {
return this.schema.forward;
}
constructor(configuration) {
this.configuration = configuration;
this.id = this.configuration.id ?? (0, uuid_1.v4)();
this.name = this.configuration.name ?? (0, random_animal_name_1.default)().split(' ').map(lodash_1.default.capitalize).join('');
this.group = this.configuration.group ?? 'unknown';
this.transports = this.configuration.transports;
this.schema = this.configuration.schema;
this.isEnabled = this.configuration.isEnabled ?? true;
this.request = this.schema.request
? new operators.root(operators, this.schema.request)
: new operators.root(operators, { $exec: () => true });
this.response = this.schema.response
? new operators.root(operators, this.schema.response)
: null;
this.meta = {
executionsCount: this.configuration.meta?.executionsCount ?? 0,
tags: (this.request?.tags ?? []).concat(this.response?.tags ?? []),
};
this.serialized = {
schema: {
request: this.schema.request ? (0, utils_1.serializeExpectationSchema)(this.schema.request) : undefined,
response: this.schema.response ? (0, utils_1.serializeExpectationSchema)(this.schema.response) : undefined,
},
};
}
increaseExecutionsCounter() {
this.meta.executionsCount += 1;
return this;
}
validate() {
return [];
}
toPlain() {
return {
id: this.id,
group: this.group,
name: this.name,
transports: this.transports,
meta: this.meta,
isEnabled: this.isEnabled,
schema: {
...this.schema,
request: this.serialized.schema.request,
response: this.serialized.schema.response,
},
};
}
static build(configuration) {
return new Expectation(configuration);
}
}
exports.Expectation = Expectation;
//# sourceMappingURL=expectation.js.map