@gameye/sdk
Version:
Node.js SDK for Gameye
136 lines • 5.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const msecs_1 = require("msecs");
const querystring = require("querystring");
const stream_1 = require("stream");
const errors = require("../errors");
const streams = require("../streams");
const utils_1 = require("../utils");
const gameye_game_1 = require("./gameye-game");
const gameye_match_1 = require("./gameye-match");
const gameye_statistic_1 = require("./gameye-statistic");
const gameye_template_1 = require("./gameye-template");
/**
* Gameye client class for communicating with the Gameye API
*/
class GameyeClient {
constructor(config = {}) {
// #region commands
this.commandStartMatch = gameye_match_1.commandStartMatch;
this.commandStopMatch = gameye_match_1.commandStopMatch;
// #endregion
// #region queries
this.queryStatistic = gameye_statistic_1.queryStatistic;
this.queryTemplate = gameye_template_1.queryTemplate;
this.queryGame = gameye_game_1.queryGame;
this.queryMatch = gameye_match_1.queryMatch;
// #endregion
// #region subscribe
this.subscribeStatistic = gameye_statistic_1.subscribeStatistic;
this.subscribeTemplate = gameye_template_1.subscribeTemplate;
this.subscribeGame = gameye_game_1.subscribeGame;
this.subscribeMatch = gameye_match_1.subscribeMatch;
this.config = Object.freeze(Object.assign({}, GameyeClient.defaultConfig, config));
this.validateConfig();
}
async command(type, payload) {
const { endpoint, token } = this.config;
const url = new URL(`${endpoint}/action/${type}`);
const requestStream = streams.createRequestStream("POST", url, {
"content-type": "application/json",
"authorization": `Bearer ${token}`,
}, 10 * msecs_1.second);
await utils_1.writeAll(requestStream, JSON.stringify(payload));
try {
const responseStream = await streams.getResponse(requestStream);
try {
const result = await streams.readResponse(responseStream);
return result;
}
finally {
responseStream.destroy();
}
}
finally {
requestStream.abort();
requestStream.destroy();
}
}
async query(type, arg) {
const { endpoint, token } = this.config;
const url = new URL(`${endpoint}/fetch/${type}`);
url.search = querystring.stringify(arg);
const requestStream = streams.createRequestStream("GET", url, {
accept: "application/json",
authorization: `Bearer ${token}`,
}, 30 * msecs_1.second);
await utils_1.writeAll(requestStream);
try {
const responseStream = await streams.getResponse(requestStream);
try {
const result = await streams.readResponse(responseStream);
return result;
}
finally {
responseStream.destroy();
}
}
finally {
requestStream.abort();
requestStream.destroy();
}
}
async subscribe(type, arg) {
const { endpoint, token } = this.config;
const url = new URL(`${endpoint}/fetch/${type}`);
url.search = querystring.stringify(arg);
const requestStream = streams.createRequestStream("GET", url, {
accept: "application/x-ndjson",
authorization: `Bearer ${token}`,
}, 30 * msecs_1.second);
await utils_1.writeAll(requestStream);
try {
const responseStream = await streams.getResponse(requestStream);
try {
const split = new streams.SplitTransform();
const fromJson = new streams.FromJSONTransform();
const reduce = new streams.ReduceTransform(utils_1.reducePatch, {});
const pass = new stream_1.PassThrough({
objectMode: true,
autoDestroy: true,
});
stream_1.pipeline(responseStream, split, fromJson, reduce, pass, error => {
requestStream.abort();
requestStream.destroy();
pass.push(null);
pass.destroy(error || undefined);
});
return pass;
}
catch (error) {
responseStream.destroy();
throw error;
}
}
catch (error) {
requestStream.abort();
requestStream.destroy();
throw error;
}
}
validateConfig() {
const { config } = this;
const requiredFields = new Array("endpoint", "token");
for (const field of requiredFields) {
const value = config[field];
if (utils_1.isEmpty(value))
throw new errors.MissingConfigurationField(field);
}
}
}
GameyeClient.defaultConfig = Object.freeze({
endpoint: process.env.GAMEYE_API_ENDPOINT || "https://api.gameye.com",
token: process.env.GAMEYE_API_TOKEN || "",
});
exports.GameyeClient = GameyeClient;
//# sourceMappingURL=gameye.js.map