galeforce-tmp-sea
Version:
A customizable, promise-based, and command-oriented TypeScript library for the Riot Games API.
134 lines • 6.98 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Game = exports.Division = exports.Tier = exports.ValorantQueue = exports.LeagueQueue = exports.LorRegion = exports.DataDragonRegion = exports.ValorantRegion = exports.RiotRegion = exports.LeagueRegion = exports.ENDPOINTS = exports.RiotAPIModule = void 0;
const lodash_1 = __importDefault(require("lodash"));
const https_1 = __importDefault(require("https"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const debug_1 = __importDefault(require("debug"));
const chalk_1 = __importDefault(require("chalk"));
const ENDPOINTS = __importStar(require("./enums/endpoints"));
exports.ENDPOINTS = ENDPOINTS;
const regions_1 = require("./enums/regions");
Object.defineProperty(exports, "LeagueRegion", { enumerable: true, get: function () { return regions_1.LeagueRegion; } });
Object.defineProperty(exports, "RiotRegion", { enumerable: true, get: function () { return regions_1.RiotRegion; } });
Object.defineProperty(exports, "ValorantRegion", { enumerable: true, get: function () { return regions_1.ValorantRegion; } });
Object.defineProperty(exports, "DataDragonRegion", { enumerable: true, get: function () { return regions_1.DataDragonRegion; } });
Object.defineProperty(exports, "LorRegion", { enumerable: true, get: function () { return regions_1.LorRegion; } });
const queues_1 = require("./enums/queues");
Object.defineProperty(exports, "LeagueQueue", { enumerable: true, get: function () { return queues_1.LeagueQueue; } });
Object.defineProperty(exports, "ValorantQueue", { enumerable: true, get: function () { return queues_1.ValorantQueue; } });
const tiers_1 = __importDefault(require("./enums/tiers"));
exports.Tier = tiers_1.default;
const divisions_1 = __importDefault(require("./enums/divisions"));
exports.Division = divisions_1.default;
const games_1 = __importDefault(require("./enums/games"));
exports.Game = games_1.default;
const requests_1 = __importDefault(require("./requests"));
const initDebug = (0, debug_1.default)('galeforce:init');
initDebug(`${chalk_1.default.bold('attempting to load Game Client certificate chain')}`);
const httpsAgent = (fs_1.default === null || fs_1.default === void 0 ? void 0 : fs_1.default.readFileSync) ? new https_1.default.Agent({ ca: fs_1.default.readFileSync(path_1.default.join(__dirname, '..', '..', 'resource', 'riotgames.pem')) }) : new https_1.default.Agent();
class RiotAPIModule {
constructor(options) {
this.key = options.key;
}
/**
* @private
*
* @param template A list of string templates that are filled in with parameters.
* @param match Parameters to fill in variables for the string templates.
* @returns Substituted version of template with parameter values from match.
*/
static generateTemplateString(template, match) {
try {
// Encode the components of the target URL (using values from the payload)
return lodash_1.default.template(template)(lodash_1.default.mapValues(match, (v) => {
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {
return encodeURIComponent(v);
}
return v;
}));
}
catch (e) {
if (e instanceof Error) {
throw new Error(`[galeforce]: Action payload ${e.message.split(' ')[0]} is required but undefined.`);
}
else {
throw new Error('[galeforce]: Failed to generate URL from template string.');
}
}
}
/**
* Returns a general Riot API `Request` object, with the provided API key as a header.
*
* @param stringTemplate The URL string template to substitute `parameters` into.
* @param parameters The parameters to substitute into the URL `stringTemplate`.
* @param query The query to send along with the request.
* @param body The body of the request.
* @returns a `Request` object.
*/
request(stringTemplate, parameters, query = {}, body = {}) {
return new requests_1.default(RiotAPIModule.generateTemplateString(stringTemplate, parameters), body, {
params: query,
headers: this.key ? { 'X-Riot-Token': this.key } : {},
});
}
/**
* Returns a `Request` object with a custom `https.Agent` containing the Riot Game Client SSL certificate chain.
*
* @param stringTemplate The URL string template to substitute `parameters` into.
* @param parameters The parameters to substitute into the URL `stringTemplate`.
* @param query The query to send along with the request.
* @param body The body of the request.
* @returns a `Request` object.
*/
gcRequest(stringTemplate, parameters, query = {}, body = {}) {
return new requests_1.default(RiotAPIModule.generateTemplateString(stringTemplate, parameters), body, {
params: query,
httpsAgent,
});
}
/**
* Returns a `Request` object that provides the fetched data as an `ArrayBuffer`.
*
* @param stringTemplate The URL string template to substitute `parameters` into.
* @param parameters The parameters to substitute into the URL `stringTemplate`.
* @param query The query to send along with the request.
* @param body The body of the request.
* @returns a `Request` object.
*/
bufferRequest(stringTemplate, parameters, query = {}, body = {}) {
return new requests_1.default(RiotAPIModule.generateTemplateString(stringTemplate, parameters), body, {
params: query,
responseType: 'arraybuffer',
});
}
}
exports.RiotAPIModule = RiotAPIModule;
//# sourceMappingURL=index.js.map