@alphabin/trx
Version:
TRX reporter for Playwright tests with Azure Blob Storage upload support
72 lines (71 loc) • 2.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiClient = void 0;
const axios_1 = __importDefault(require("axios"));
const logger_util_1 = __importDefault(require("./logger.util"));
/**
* API client for communicating with TRX server
*/
class ApiClient {
constructor(options) {
this.serverUrl = options.serverUrl.endsWith('/')
? options.serverUrl.slice(0, -1)
: options.serverUrl;
this.apiKey = options.apiKey;
this.timeout = options.timeout || 30000;
this.retries = options.retries || 3;
}
/**
* Sends test report to TRX server
*/
async sendReport(report) {
const url = `${this.serverUrl}/api/reports/playwright`;
const config = {
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey
},
timeout: this.timeout
};
try {
let retryCount = 0;
let lastError = null;
while (retryCount <= this.retries) {
try {
logger_util_1.default.debug(`Sending report to ${url} (attempt ${retryCount + 1}/${this.retries + 1})`);
const response = await axios_1.default.post(url, report, config);
if (response.status >= 200 && response.status < 300) {
logger_util_1.default.debug(`Report sent successfully: ${response.status} ${response.statusText}`);
return response.data;
}
else {
lastError = new Error(`Unexpected response: ${response.status} ${response.statusText}`);
logger_util_1.default.debug(`Unexpected response: ${response.status} ${response.statusText}`);
}
}
catch (error) {
lastError = error;
logger_util_1.default.debug(`Error sending report (attempt ${retryCount + 1}/${this.retries + 1})`, error);
}
retryCount++;
if (retryCount <= this.retries) {
// Wait before retry (exponential backoff: 1s, 2s, 4s, etc.)
const delay = Math.pow(2, retryCount - 1) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
if (lastError) {
throw lastError;
}
return { success: false };
}
catch (error) {
logger_util_1.default.error('Failed to send report to TRX server', error);
return { success: false };
}
}
}
exports.ApiClient = ApiClient;