@pactflow/pact-msw-adapter
Version:
> Generate pact contracts from the recorded mock service worker interactions.
92 lines (91 loc) • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addTimeout = exports.checkUrlFilters = exports.createWriter = exports.logGroup = exports.log = void 0;
var path = require("path");
let fs; // dynamic import
const logPrefix = '[pact-msw-adapter]';
const logColors = {
debug: 'gray',
info: 'forestgreen',
warn: 'gold',
error: 'coral'
};
const log = (message, options) => {
const group = (options === null || options === void 0 ? void 0 : options.group) || false;
const mode = (options === null || options === void 0 ? void 0 : options.mode) || 'info';
const color = logColors[mode];
const logFunction = group ? options.logger.groupCollapsed : options.logger[mode];
logFunction(`%c${logPrefix} %c${message}`, `color:${color}`, 'color:inherit');
};
exports.log = log;
const logGroup = (message, options) => {
const isArray = message instanceof Array;
if (isArray) {
const [label, ...content] = message;
log(label, { group: true, mode: options.mode, logger: options.logger });
content.forEach((c) => options.logger[(options === null || options === void 0 ? void 0 : options.mode) || 'info'](c));
}
else {
log(message, { group: true, mode: options.mode, logger: options.logger });
}
if (options === null || options === void 0 ? void 0 : options.endGroup) {
options.logger.groupEnd();
}
};
exports.logGroup = logGroup;
const ensureDirExists = (filePath) => {
var _a, _b;
var dirname = path.dirname(filePath);
if ((_a = fs.existsSync) === null || _a === void 0 ? void 0 : _a.call(fs, dirname)) {
return true;
}
(_b = fs.mkdirSync) === null || _b === void 0 ? void 0 : _b.call(fs, dirname);
};
const createWriter = (options) => (filePath, data) => {
var _a;
if (!fs) {
try {
fs = require('fs');
}
catch (e) { }
}
if (!(fs === null || fs === void 0 ? void 0 : fs.existsSync)) {
log('You need a node environment to save files.', { mode: 'warn', group: true, logger: options.logger });
options.logger.info('filePath:', filePath);
options.logger.info('contents:', data);
options.logger.groupEnd();
}
else {
ensureDirExists(filePath);
(_a = fs.writeFileSync) === null || _a === void 0 ? void 0 : _a.call(fs, filePath, JSON.stringify(data));
}
};
exports.createWriter = createWriter;
const hasProvider = (pending, options) => {
var _a;
if (typeof options.providers === 'function') {
return options.providers(pending) !== null;
}
return (_a = Object.values(options.providers)) === null || _a === void 0 ? void 0 : _a.some(validPaths => validPaths.some(path => pending.request.url.includes(path)));
};
const checkUrlFilters = (pending, options) => {
const urlString = pending.request.url.toString();
const providerFilter = hasProvider(pending, options);
const includeFilter = !options.includeUrl || options.includeUrl.some(inc => urlString.includes(inc));
const excludeFilter = !options.excludeUrl || !options.excludeUrl.some(exc => urlString.includes(exc));
const matchIsAllowed = includeFilter && excludeFilter && providerFilter;
if (options.debug) {
logGroup(['Checking request against url filters', { urlString, providerFilter, includeFilter, excludeFilter, matchIsAllowed }], { logger: options.logger });
}
return matchIsAllowed;
};
exports.checkUrlFilters = checkUrlFilters;
const addTimeout = async (promise, label, timeout) => {
const asyncTimeout = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`[pact-msw-adapter] ${label} timed out after ${timeout}ms`));
}, timeout).unref();
});
return Promise.race([promise, asyncTimeout]);
};
exports.addTimeout = addTimeout;