84 lines
9.7 KiB
JavaScript
84 lines
9.7 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const get_testcafe_version_1 = __importDefault(require("../utils/get-testcafe-version"));
|
||
|
const get_dashboard_url_1 = __importDefault(require("./get-dashboard-url"));
|
||
|
const https_1 = __importDefault(require("https"));
|
||
|
const debug_1 = __importDefault(require("debug"));
|
||
|
const DEBUG_LOGGER = (0, debug_1.default)('testcafe:dashboard:connector');
|
||
|
class DashboardConnector {
|
||
|
constructor(baseUrl = (0, get_dashboard_url_1.default)()) {
|
||
|
this.baseUrl = baseUrl;
|
||
|
}
|
||
|
async _sendPostJsonRequest(relativeUrl, body) {
|
||
|
const url = new URL(relativeUrl, this.baseUrl);
|
||
|
const postData = JSON.stringify(body);
|
||
|
const options = {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json',
|
||
|
'Content-Length': Buffer.byteLength(postData),
|
||
|
},
|
||
|
};
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const req = https_1.default.request(url, options, res => {
|
||
|
res.setEncoding('utf8');
|
||
|
let responseBody = '';
|
||
|
res.on('data', chunk => {
|
||
|
responseBody += chunk.toString();
|
||
|
});
|
||
|
res.on('end', () => {
|
||
|
resolve({
|
||
|
statusCode: res.statusCode,
|
||
|
headers: res.headers,
|
||
|
body: responseBody,
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
req.on('error', reject);
|
||
|
req.write(postData);
|
||
|
req.end();
|
||
|
});
|
||
|
}
|
||
|
async _postRequest(relativeUrl, body) {
|
||
|
try {
|
||
|
const response = await this._sendPostJsonRequest(relativeUrl, body);
|
||
|
if (response.statusCode >= 200 && response.statusCode <= 299)
|
||
|
return { success: true };
|
||
|
return {
|
||
|
success: false,
|
||
|
errorMessage: response.body,
|
||
|
isDashboardError: true,
|
||
|
};
|
||
|
}
|
||
|
catch (err) {
|
||
|
DEBUG_LOGGER(err);
|
||
|
return {
|
||
|
success: false,
|
||
|
errorMessage: err.message,
|
||
|
isDashboardError: false,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
async sendEmail(email) {
|
||
|
return this._postRequest(DashboardConnector.API_URLS.sendMagicLinkMail, {
|
||
|
email,
|
||
|
thirdPartyRegistration: true,
|
||
|
});
|
||
|
}
|
||
|
async validateToken(token) {
|
||
|
return this._postRequest(DashboardConnector.API_URLS.validateToken, {
|
||
|
token,
|
||
|
testcafeVersion: (0, get_testcafe_version_1.default)(),
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.default = DashboardConnector;
|
||
|
DashboardConnector.API_URLS = {
|
||
|
sendMagicLinkMail: '/api/sendMagicLinkMail',
|
||
|
validateToken: '/api/validateAuthToken',
|
||
|
};
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29ubmVjdG9yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2Rhc2hib2FyZC9jb25uZWN0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSx5RkFBK0Q7QUFDL0QsNEVBQWtEO0FBQ2xELGtEQUEwQjtBQUMxQixrREFBMEI7QUFFMUIsTUFBTSxZQUFZLEdBQUcsSUFBQSxlQUFLLEVBQUMsOEJBQThCLENBQUMsQ0FBQztBQUUzRCxNQUFxQixrQkFBa0I7SUFRbkMsWUFBb0IsT0FBTyxHQUFHLElBQUEsMkJBQWUsR0FBRTtRQUMzQyxJQUFJLENBQUMsT0FBTyxHQUFHLE9BQU8sQ0FBQztJQUMzQixDQUFDO0lBRU8sS0FBSyxDQUFDLG9CQUFvQixDQUFFLFdBQW1CLEVBQUUsSUFBWTtRQUNqRSxNQUFNLEdBQUcsR0FBUSxJQUFJ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