@gameye/sdk
Version:
Node.js SDK for Gameye
133 lines • 5.04 kB
JavaScript
import { second } from "msecs";
import * as querystring from "querystring";
import { PassThrough, pipeline } from "stream";
import * as errors from "../errors";
import * as streams from "../streams";
import { isEmpty, reducePatch, writeAll } from "../utils";
import { queryGame, subscribeGame } from "./gameye-game";
import { commandStartMatch, commandStopMatch, queryMatch, subscribeMatch } from "./gameye-match";
import { queryStatistic, subscribeStatistic } from "./gameye-statistic";
import { queryTemplate, subscribeTemplate } from "./gameye-template";
/**
* Gameye client class for communicating with the Gameye API
*/
export class GameyeClient {
constructor(config = {}) {
// #region commands
this.commandStartMatch = commandStartMatch;
this.commandStopMatch = commandStopMatch;
// #endregion
// #region queries
this.queryStatistic = queryStatistic;
this.queryTemplate = queryTemplate;
this.queryGame = queryGame;
this.queryMatch = queryMatch;
// #endregion
// #region subscribe
this.subscribeStatistic = subscribeStatistic;
this.subscribeTemplate = subscribeTemplate;
this.subscribeGame = subscribeGame;
this.subscribeMatch = 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 * second);
await 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 * second);
await 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 * second);
await 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(reducePatch, {});
const pass = new PassThrough({
objectMode: true,
autoDestroy: true,
});
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 (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 || "",
});
//# sourceMappingURL=gameye.js.map