taapi
Version:
A wrapper and a client for the TAAPI.IO API
159 lines (158 loc) • 6.86 kB
JavaScript
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());
});
};
import axios from 'axios';
class Taapi {
constructor(secret) {
this.provider = "";
this.providerSecret = "";
this.defaultExchange = "binance";
this.constructs = {};
this.calculationsCount = 0;
this.secret = secret;
}
setDefaultExchange(exchange) {
this.defaultExchange = exchange;
}
setProvider(provider, providerSecret) {
this.provider = provider;
this.providerSecret = providerSecret;
}
getIndicator(indicator, symbol, interval, params = {}, exchange = this.defaultExchange) {
return __awaiter(this, void 0, void 0, function* () {
let result = null;
params["secret"] = this.secret;
params["exchange"] = exchange;
params["symbol"] = symbol;
params["interval"] = interval;
let endpoint = "https://api.taapi.io";
let isRequestValid = true;
if (params["type"] == "stocks" || params["type"] == "forex") {
if (this.provider && this.providerSecret) {
endpoint = "https://us-east.taapi.io";
params["provider"] = this.provider;
params["providerSecret"] = this.providerSecret;
}
else {
isRequestValid = false;
console.log("'provider' and 'provider secret' not set. Please use setProvider(provider: string, providerSecret: string) to set your forex provider.");
}
}
if (isRequestValid) {
yield axios.get(`${endpoint}/${indicator}`, {
params: params
}).then(response => {
result = response.data;
});
}
return result;
});
}
resetBulkConstructs() {
this.constructs = {};
this.calculationsCount = 0;
}
addCalculation(indicator, symbol, interval, id, params = {}, exchange = this.defaultExchange) {
if (this.calculationsCount < 20) {
let constructKey = `${exchange}_${symbol}_${interval}`;
let construct = this.constructs[constructKey];
if (!construct) {
construct = {
"exchange": exchange,
"symbol": symbol,
"interval": interval,
"indicators": []
};
this.constructs[constructKey] = construct;
}
let indicatorDef = params || {};
indicatorDef["indicator"] = indicator;
indicatorDef["id"] = id;
construct.indicators.push(indicatorDef);
this.calculationsCount++;
}
else {
console.error("Cannot set more than 20 bulk calls!");
}
}
executeBulk(type = "crypto", params = {}) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = "https://api.taapi.io";
let constructs = [];
let isRequestValid = true;
if (type == "forex") {
if (this.provider && this.providerSecret) {
endpoint = "https://us-east.taapi.io";
params["provider"] = this.provider;
params["providerSecret"] = this.providerSecret;
}
else {
isRequestValid = false;
console.log("'provider' and 'provider secret' not set. Please use setProvider(provider: string, providerSecret: string) to set your forex provider.");
}
}
let result = null;
if (isRequestValid) {
for (let c in this.constructs) {
let construct = this.constructs[c];
construct["type"] = type;
if (type == "forex") {
construct["provider"] = this.provider;
construct["providerSecret"] = this.providerSecret;
}
constructs.push(construct);
}
params["secret"] = this.secret;
params["construct"] = constructs;
params["outputFormat"] = params["outputFormat"] || "objects";
yield axios.post(`${endpoint}/bulk`, params).then(response => {
result = response.data;
});
}
return result;
});
}
getExchangeSymbols(type, exchange = "", quoteAsset = "") {
return __awaiter(this, void 0, void 0, function* () {
let result = null;
let params = {};
params["secret"] = this.secret;
params["type"] = type;
params["exchange"] = exchange;
params["quoteAsset"] = quoteAsset;
let endpoint = "https://api.taapi.io";
let isRequestValid = true;
if (type == "crypto" && (!exchange || exchange == "")) {
console.log("Exchange is required for this type of request.");
isRequestValid = false;
}
if (params["type"] == "forex" || params["type"] == "options" || params["type"] == "otc") {
if (this.provider && this.providerSecret) {
endpoint = "https://us-east.taapi.io";
params["provider"] = this.provider;
params["providerSecret"] = this.providerSecret;
}
else {
isRequestValid = false;
console.log("'provider' and 'provider secret' not set. Please use setProvider(provider: string, providerSecret: string) to set your forex provider.");
}
}
if (isRequestValid) {
yield axios.get(`${endpoint}/exchange-symbols`, {
headers: { "Accept-Encoding": "gzip,deflate,compress" },
params: params
}).then(response => {
result = response.data;
});
}
return result;
});
}
}
export default Taapi;