UNPKG

request-mocking-protocol

Version:
63 lines 2.29 kB
"use strict"; /** * RequestMatcher class. * The same as URLPattern, but for matching the whole HTTP request. * See: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API * See: https://developer.chrome.com/docs/web-platform/urlpattern * * todo: move to a separate package. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestMatcher = void 0; const url_1 = require("./matchers/url"); const body_1 = require("./matchers/body"); const context_1 = require("./context"); const method_1 = require("./matchers/method"); const query_1 = require("./matchers/query"); const headers_1 = require("./matchers/headers"); class RequestMatcher { schema; debug; methodMatcher; urlMatcher; queryMatcher; headersMatcher; bodyMatcher; constructor(schema, debug) { this.schema = schema; this.debug = debug; this.methodMatcher = new method_1.MethodMatcher(this.schema); this.urlMatcher = new url_1.UrlMatcher(this.schema); this.queryMatcher = new query_1.QueryMatcher(this.schema); this.headersMatcher = new headers_1.HeadersMatcher(this.schema); this.bodyMatcher = new body_1.BodyMatcher(this.schema); if (schema.debug) this.debug = true; this.ensureSingleQuerySource(); } // eslint-disable-next-line visual/complexity async match(req) { const ctx = new context_1.MatchingContext(req); try { const matched = this.methodMatcher.match(ctx) && this.urlMatcher.match(ctx) && this.queryMatcher.match(ctx) && this.headersMatcher.match(ctx) && (await this.bodyMatcher.match(ctx)); ctx.logDone(matched); return matched ? ctx.params : null; } finally { // eslint-disable-next-line no-console if (this.debug) console.log(ctx.logs.join('\n')); } } ensureSingleQuerySource() { if (this.urlMatcher.hasQuery && this.queryMatcher.hasQuery) { throw new Error(`Query parameters should be defined either in the URL pattern or in the 'query' field.`); } } } exports.RequestMatcher = RequestMatcher; //# sourceMappingURL=index.js.map