testrail-modern-client
Version:
A modern TypeScript client for TestRail API
133 lines (132 loc) • 6.26 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestRailClient = exports.TestRailMaintenanceError = exports.TestRailEnterpriseError = exports.TestRailRateLimitError = exports.TestRailAPIError = exports.TestRailError = void 0;
const axios_1 = __importDefault(require("axios"));
const attachments_1 = require("./services/attachments");
const bdd_1 = require("./services/bdd");
const case_fields_1 = require("./services/case-fields");
const case_types_1 = require("./services/case-types");
const cases_1 = require("./services/cases");
const configurations_1 = require("./services/configurations");
const datasets_1 = require("./services/datasets");
const groups_1 = require("./services/groups");
const milestones_1 = require("./services/milestones");
const plans_1 = require("./services/plans");
const priorities_1 = require("./services/priorities");
const projects_1 = require("./services/projects");
const reports_1 = require("./services/reports");
const result_fields_1 = require("./services/result-fields");
const results_1 = require("./services/results");
const roles_1 = require("./services/roles");
const runs_1 = require("./services/runs");
const sections_1 = require("./services/sections");
const shared_steps_1 = require("./services/shared-steps");
const statuses_1 = require("./services/statuses");
const suites_1 = require("./services/suites");
const templates_1 = require("./services/templates");
const tests_1 = require("./services/tests");
const users_1 = require("./services/users");
const variables_1 = require("./services/variables");
class TestRailError extends Error {
constructor(message) {
super(message);
this.name = 'TestRailError';
}
}
exports.TestRailError = TestRailError;
class TestRailAPIError extends TestRailError {
constructor(message, status, data) {
super(message);
this.name = 'TestRailAPIError';
this.status = status;
this.data = data;
}
}
exports.TestRailAPIError = TestRailAPIError;
class TestRailRateLimitError extends TestRailError {
constructor(retryAfter) {
super('API rate limit exceeded');
this.name = 'TestRailRateLimitError';
this.retryAfter = retryAfter;
}
}
exports.TestRailRateLimitError = TestRailRateLimitError;
class TestRailEnterpriseError extends TestRailError {
constructor() {
super('Enterprise license/subscription required');
this.name = 'TestRailEnterpriseError';
}
}
exports.TestRailEnterpriseError = TestRailEnterpriseError;
// https://www.techmatrix.co.jp/secure/quality/testrail/cloud_maintenance.html
class TestRailMaintenanceError extends TestRailError {
constructor() {
super('TestRail is currently under maintenance');
this.name = 'TestRailMaintenanceError';
}
}
exports.TestRailMaintenanceError = TestRailMaintenanceError;
class TestRailClient {
constructor(config) {
this.client = axios_1.default.create({
baseURL: `${config.host}/index.php?/api/v2`,
auth: {
username: config.email,
password: config.password,
},
headers: {
'Content-Type': 'application/json',
},
});
// Initialize services
this.projects = new projects_1.ProjectService(this.client);
this.cases = new cases_1.CaseService(this.client);
this.suites = new suites_1.SuiteService(this.client);
this.sections = new sections_1.SectionService(this.client);
this.runs = new runs_1.RunService(this.client);
this.results = new results_1.ResultService(this.client);
this.resultFields = new result_fields_1.ResultFieldService(this.client);
this.roles = new roles_1.RoleService(this.client);
this.milestones = new milestones_1.MilestoneService(this.client);
this.attachments = new attachments_1.AttachmentService(this.client);
this.bdd = new bdd_1.BDDService(this.client);
this.configurations = new configurations_1.ConfigurationService(this.client);
this.datasets = new datasets_1.DatasetService(this.client);
this.groups = new groups_1.GroupService(this.client);
this.priorities = new priorities_1.PrioritiesService(this.client);
this.reports = new reports_1.ReportService(this.client);
this.sharedSteps = new shared_steps_1.SharedStepService(this.client);
this.statuses = new statuses_1.StatusService(this.client);
this.templates = new templates_1.TemplateService(this.client);
this.tests = new tests_1.TestService(this.client);
this.users = new users_1.UserService(this.client);
this.variables = new variables_1.VariableService(this.client);
this.plans = new plans_1.PlanService(this.client);
this.caseFields = new case_fields_1.CaseFieldService(this.client);
this.caseTypes = new case_types_1.CaseTypeService(this.client);
// Add response interceptor for error handling
this.client.interceptors.response.use((response) => response, (error) => {
var _a;
if (!error.response) {
throw new TestRailError('Network error or no response from TestRail');
}
const { status, data } = error.response;
if (status === 429) {
const retryAfter = parseInt(error.response.headers['retry-after'] || '60', 10);
throw new TestRailRateLimitError(retryAfter);
}
if (status === 503) {
throw new TestRailMaintenanceError();
}
if (status === 403 && ((_a = data === null || data === void 0 ? void 0 : data.error) === null || _a === void 0 ? void 0 : _a.includes('Enterprise'))) {
throw new TestRailEnterpriseError();
}
const errorMessage = (data === null || data === void 0 ? void 0 : data.error) || error.message || 'Unknown TestRail API error';
throw new TestRailAPIError(errorMessage, status, data);
});
}
}
exports.TestRailClient = TestRailClient;