@pact-foundation/pact
Version:
Pact for all things Javascript
100 lines • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertRulesToFFI = exports.validateRules = exports.convertStatusMatcherToFFI = exports.convertMatcherToFFI = void 0;
/**
* Converts a matcher to the FFI format expected by pact-core
* @param matcher The matcher to convert
* @returns The matcher in FFI format
*/
const convertMatcherToFFI = (matcher) => {
const result = {};
if (matcher['pact:matcher:type']) {
result.match = matcher['pact:matcher:type'];
}
Object.keys(matcher).forEach((key) => {
if (key !== 'pact:matcher:type' && key !== 'pact:generator:type') {
result[key] = matcher[key];
}
});
return result;
};
exports.convertMatcherToFFI = convertMatcherToFFI;
/**
* Converts a StatusCodeMatcher to the FFI matching rules format expected by pact-core
* @param matcher The status code matcher
* @returns The matching rules in FFI format
*/
const convertStatusMatcherToFFI = (matcher) => {
return {
status: {
$: {
matchers: [
{
match: 'statusCode',
status: matcher.status,
},
],
},
},
};
};
exports.convertStatusMatcherToFFI = convertStatusMatcherToFFI;
/**
* Validates that the rules parameter is of type Rules
* @param rules The rules to validate
* @throws Error if rules is not a valid Rules object
*/
const validateRules = (rules) => {
if (!rules || typeof rules !== 'object' || Array.isArray(rules)) {
throw new Error('rules must be an object');
}
const validParts = ['path', 'body', 'header', 'query', 'status'];
const ruleKeys = Object.keys(rules);
ruleKeys.forEach((key) => {
if (!validParts.includes(key)) {
throw new Error(`Invalid part "${key}" in rules. Valid parts are: ${validParts.join(', ')}`);
}
const partRules = rules[key];
if (!partRules)
return;
const rulesArray = Array.isArray(partRules) ? partRules : [partRules];
rulesArray.forEach((rule, index) => {
if (!rule || typeof rule !== 'object') {
throw new Error(`Rule at ${key}[${index}] must be an object`);
}
const ruleObj = rule;
if (!('rules' in ruleObj) || !Array.isArray(ruleObj.rules)) {
throw new Error(`Rule at ${key}[${index}] must have a "rule" property that is an array`);
}
if ('path' in ruleObj && typeof ruleObj.path !== 'string') {
throw new Error(`Rule at ${key}[${index}] has a "path" property that is not a string`);
}
});
});
};
exports.validateRules = validateRules;
/**
* Converts Rules format to FFI matching rules format
* @param rules Rules in the user-friendly format
* @returns Rules in FFI format
*/
const convertRulesToFFI = (rules) => {
const ffiRules = {};
Object.keys(rules).forEach((part) => {
const partRules = rules[part];
if (!partRules)
return;
const rulesArray = Array.isArray(partRules) ? partRules : [partRules];
ffiRules[part] = {};
rulesArray.forEach((rule) => {
if (rule.path) {
ffiRules[part][rule.path] = {
matchers: rule.rules.map(exports.convertMatcherToFFI),
};
}
});
});
return ffiRules;
};
exports.convertRulesToFFI = convertRulesToFFI;
//# sourceMappingURL=matchingRules.js.map