iracing-node-api
Version:
A nodejs wrapper for Iracing's data api.
230 lines (229 loc) • 9.98 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const axios_cookiejar_support_1 = require("axios-cookiejar-support");
const crypto_js_1 = require("crypto-js");
const http_status_codes_1 = __importDefault(require("http-status-codes"));
const tough_cookie_1 = require("tough-cookie");
const user_agents_1 = __importDefault(require("user-agents"));
class IracingClient {
constructor(email, password, options = { shouldUseInterval: true }) {
if (!email || !password) {
throw new Error(`Iracing Api: Unable to initialze client. Missing email or password`);
}
this.email = email;
this.password = password;
this.options = options;
this.authenticated = false;
this.agent = new user_agents_1.default({ platform: 'Win32' });
this.apiClient = (0, axios_cookiejar_support_1.wrapper)(axios_1.default.create({
jar: new tough_cookie_1.CookieJar(),
withCredentials: true,
baseURL: 'https://members-ng.iracing.com',
headers: {
'User-Agent': this.agent.toString()
}
}));
this.signIn();
this.interval = null;
this.handleError();
}
handleError() {
this.apiClient.interceptors.response.use((config) => __awaiter(this, void 0, void 0, function* () {
if (config.status === http_status_codes_1.default.UNAUTHORIZED ||
config.status === http_status_codes_1.default.FORBIDDEN) {
this.authenticated = false;
this.signIn();
}
return config;
}), (error) => {
if (error.status === http_status_codes_1.default.UNAUTHORIZED ||
error.status === http_status_codes_1.default.FORBIDDEN) {
this.authenticated = false;
this.signIn();
}
});
}
encodeCredentials(email, password) {
const hash = (0, crypto_js_1.SHA256)(password + email.toLowerCase());
return crypto_js_1.enc.Base64.stringify(hash);
}
signIn() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const hash = this.encodeCredentials(this.email, this.password);
try {
const res = yield this.apiClient.post('/auth', {
email: this.email,
password: hash
});
if ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.authcode) {
this.authenticated = true;
}
}
catch (error) {
throw error;
}
});
}
getResource() {
return __awaiter(this, arguments, void 0, function* (resourceLink = '') {
if (!resourceLink) {
throw new Error('Missing resource url');
}
try {
const res = yield this.apiClient.get(resourceLink);
return res.data;
}
catch (error) {
throw error;
}
});
}
getCarAssets() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
yield this.signIn();
const res = yield this.apiClient.get('/data/car/assets');
const assets = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return assets;
}
catch (error) {
throw error;
}
});
}
getTrackData() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get('/data/track/get');
const trackData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return trackData;
});
}
getTrackAssets() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get('/data/track/assets');
const data = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return data;
});
}
getCarData() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get('/data/car/get');
const carData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return carData;
});
}
getSessionResults(subsessionId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`/data/results/get?subsession_id=${subsessionId}`);
const results = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return results;
});
}
getSessionLogs(subsessionId, sessionNumber) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`/data/results/event_log?subsession_id=${subsessionId}&simsession_number=${sessionNumber}`);
const logs = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return logs;
});
}
getLapChartData(subsessionId, sessionNumber) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`/data/results/lap_chart_data?subsession_id=${subsessionId}&simsession_number=${sessionNumber}`);
const signedData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return signedData;
});
}
getMemberStatsHistory(category, chart_type, memberId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const types = {
iRating: 1,
'tt-rating': 2,
sr: 3
};
const categories = {
oval: 1,
road: 2,
'dirt-oval': 3,
dirt: 4
};
const res = yield this.apiClient.get(`/data/member/chart_data?category_id=${categories[category]}&chart_type=${types[chart_type]}&cust_id=${memberId}`);
const signedResponse = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return signedResponse;
});
}
getMember(memberId) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
yield this.signIn();
const res = yield this.apiClient.get(`data/member/get?cust_ids=${memberId}`);
const signedResponse = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return ((_b = signedResponse.members.find((member) => member.cust_id === memberId)) !== null && _b !== void 0 ? _b : null);
});
}
getLeague(leagueId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`data/league/get?league_id=${leagueId}`);
const signedData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return signedData;
});
}
getLeagueSeasons(leagueId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`data/league/seasons?league_id=${leagueId}`);
const signedData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return signedData;
});
}
getLeagueRoster(leagueId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`data/league/roster?league_id=${leagueId}`);
const signedData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.data_url);
return signedData;
});
}
getLeagueSeasonSesssions(leagueId, seasonId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.signIn();
const res = yield this.apiClient.get(`data/league/season_sessions?league_id=${leagueId}&season_id=${seasonId}`);
const signedData = yield this.getResource((_a = res.data) === null || _a === void 0 ? void 0 : _a.link);
return signedData;
});
}
}
exports.default = IracingClient;