UNPKG

apidog-reporter-allure

Version:
101 lines (100 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildUrl = buildUrl; exports.indexOf = indexOf; exports.joinMethods = joinMethods; exports.parseContentType = parseContentType; exports.parseTestDataRow = parseTestDataRow; exports.prettyBody = prettyBody; exports.prettyHeaders = prettyHeaders; exports.safe = safe; function buildUrl(host, path) { return new URL(path, host); } const formats = ['json', 'html', 'xml']; function joinMethods(obj) { const record = obj; const propertyNames = Object.getOwnPropertyNames(record); propertyNames.filter(propertyName => typeof record[propertyName] === "function").forEach(propertyName => record[propertyName] = record[propertyName].bind(record)); } function prettyHeaders(headers) { return headers.map(({ key, value }) => `${key} -> ${value}`).join('\n'); } function safe(callback, defaultValue) { try { return callback(); } catch (e) { return defaultValue; } } function parseContentType(contentType) { return contentType && formats.find(item => contentType.includes(item)) || 'raw'; } function prettyBody(content, contentType) { const format = parseContentType(contentType); switch (format) { case "json": return safe(() => JSON.stringify(JSON.parse(content), null, 2), content) || ''; case "html": case "xml": case "raw": default: return content; } } function isIncl(source, sub, offset) { for (let i = 0; i < sub.length; i++) { if (source[offset + i] !== sub[i]) { return false; } } return true; } function indexOf(source, sub, offset = 0) { if (offset < 0) { offset = 0; } for (let i = offset; i <= source.length - sub.length; i++) { if (isIncl(source, sub, i)) { return i; } } return -1; } function parseTestDataRow(row) { const result = []; while (row.length !== 0) { if (row.startsWith(',')) { row = row.substring(1); } if (row.startsWith('"""')) { const index = indexOf(row, '"""', 3); result.push(row.substring(3, index)); row = row.substring(index); continue; } if (row.startsWith('"')) { const index = indexOf(row, '"', 1); result.push(row.substring(1, index)); row = row.substring(index + 1); continue; } const index = row.indexOf(','); if (index < 0) { result.push(row); break; } if (index === 0) { result.push(''); continue; } result.push(row.substring(0, index)); row = row.substring(index); } return result; }