UNPKG

@pact-foundation/pact

Version:
106 lines 4.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setResponseDetails = exports.setRequestDetails = exports.setHeaders = exports.setBody = exports.setQuery = exports.setRequestMethodAndPath = exports.contentTypeFromHeaders = void 0; const ramda_1 = require("ramda"); const logger_1 = __importDefault(require("../common/logger")); const matchers_1 = require("../dsl/matchers"); var InteractionPart; (function (InteractionPart) { InteractionPart[InteractionPart["REQUEST"] = 1] = "REQUEST"; InteractionPart[InteractionPart["RESPONSE"] = 2] = "RESPONSE"; })(InteractionPart || (InteractionPart = {})); const CONTENT_TYPE_HEADER = 'content-type'; const CONTENT_TYPE_JSON = 'application/json'; const contentTypeFromHeaders = (headers, defaultContentType) => { let contentType = defaultContentType; (0, ramda_1.forEachObjIndexed)((v, k) => { if (`${k}`.toLowerCase() === CONTENT_TYPE_HEADER) { contentType = (0, matchers_1.matcherValueOrString)(v); } }, headers || {}); return contentType; }; exports.contentTypeFromHeaders = contentTypeFromHeaders; const setRequestMethodAndPath = (interaction, req) => { if (req?.method && req?.path) { const method = req?.method; const reqPath = (0, matchers_1.matcherValueOrString)(req?.path); interaction.withRequest(method, reqPath); } }; exports.setRequestMethodAndPath = setRequestMethodAndPath; const setQuery = (interaction, query) => { (0, ramda_1.forEachObjIndexed)((v, k) => { if (Array.isArray(v)) { v.forEach((vv, i) => { interaction.withQuery(k, i, (0, matchers_1.matcherValueOrString)(vv)); }); } else { interaction.withQuery(k, 0, (0, matchers_1.matcherValueOrString)(v)); } }, query); }; exports.setQuery = setQuery; const setBody = (part, interaction, headers, body) => { if (body) { const matcher = (0, matchers_1.matcherValueOrString)(body); const contentType = (0, exports.contentTypeFromHeaders)(headers, CONTENT_TYPE_JSON); switch (part) { case InteractionPart.REQUEST: interaction.withRequestBody(matcher, contentType); break; case InteractionPart.RESPONSE: interaction.withResponseBody(matcher, contentType); break; default: break; } } }; exports.setBody = setBody; const setHeaders = (part, interaction, headers) => { Object.entries(headers || {}).forEach(([k, v]) => { let values = []; if (Array.isArray(v)) { values = v; } else { values = [v]; } switch (part) { case InteractionPart.REQUEST: values.forEach((h, i) => { logger_1.default.debug(`setting header request value for ${k} at index ${i} to ${JSON.stringify((0, matchers_1.matcherValueOrString)(h))}`); interaction.withRequestHeader(k, i, (0, matchers_1.matcherValueOrString)(h)); }); break; case InteractionPart.RESPONSE: values.forEach((h, i) => { logger_1.default.debug(`setting header response value for ${h} at index ${i} to ${JSON.stringify((0, matchers_1.matcherValueOrString)(h))}`); interaction.withResponseHeader(k, i, (0, matchers_1.matcherValueOrString)(h)); }); break; default: break; } }); }; exports.setHeaders = setHeaders; const setRequestDetails = (interaction, req) => { (0, exports.setRequestMethodAndPath)(interaction, req); (0, exports.setBody)(InteractionPart.REQUEST, interaction, req.headers, req.body); (0, exports.setHeaders)(InteractionPart.REQUEST, interaction, req.headers); (0, exports.setQuery)(interaction, req.query); }; exports.setRequestDetails = setRequestDetails; const setResponseDetails = (interaction, res) => { interaction.withStatus(res.status); (0, exports.setBody)(InteractionPart.RESPONSE, interaction, res.headers, res.body); (0, exports.setHeaders)(InteractionPart.RESPONSE, interaction, res.headers); }; exports.setResponseDetails = setResponseDetails; //# sourceMappingURL=ffi.js.map