UNPKG

@n1k1t/mock-server

Version:

The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations

43 lines 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpectationsStorage = void 0; const expectation_1 = require("./expectation"); const logger_1 = require("../../logger"); const logger = logger_1.Logger.build('Expectations.Storage'); class ExpectationsStorage extends Map { constructor(configuration) { super(); this.configuration = configuration; } /** Extends this storage with another */ extend(storage) { for (const [name, expectation] of storage.entries()) { this.set(name, expectation); } return this; } register(configuration) { const expectation = expectation_1.Expectation.build(configuration); const errors = expectation.validate(); if (errors.length) { return { status: 'ERROR', reasons: errors }; } logger.info(`Expectation [${expectation.name}] has registered in group [${expectation.group}]`); this.set(expectation.id, expectation); return { status: 'REGISTERED', expectation }; } async match(context) { for (const expectation of this.values()) { if (!expectation.isEnabled) { continue; } const hasSameTransport = expectation.transports?.includes(context.transport) ?? true; if (hasSameTransport && (await expectation.request.match(context))) { return expectation; } } return null; } } exports.ExpectationsStorage = ExpectationsStorage; //# sourceMappingURL=storage.js.map