UNPKG

@contract-case/case-core-plugin-http

Version:

ContractCase core HTTP plugin, providing HTTP matchers and mocks

34 lines 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateCodes = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const validateCodeNumber = (code, matchContext) => { if (code < 100 || code >= 600) { throw new case_plugin_base_1.CaseConfigurationError(`HTTP status code '${code}' is outside the valid range (100-599) for status codes`, matchContext); } if (Math.abs(code) !== code) { throw new case_plugin_base_1.CaseConfigurationError(`HTTP status code '${code}' must be a whole integer`, matchContext); } return code; }; const validateCode = (code, matchContext) => { switch (typeof code) { case 'number': return validateCodeNumber(code, matchContext); case 'string': return validateCodeNumber(Number.parseInt(code.replace(/X|x/g, '0'), 10), matchContext); default: throw new case_plugin_base_1.CaseConfigurationError(`'${typeof code}' is not a valid type for an HTTP status code`, matchContext); } }; const validateCodes = (codes, matchContext) => { if (Array.isArray(codes)) { if (codes.length === 0) { throw new case_plugin_base_1.CaseConfigurationError(`An empty array isn't a valid list of HTTP status codes`, matchContext); } return codes.map((c) => validateCode(c, matchContext))[0]; } return validateCode(codes, matchContext); }; exports.validateCodes = validateCodes; //# sourceMappingURL=codeValidator.js.map