UNPKG

taapi

Version:

A wrapper and a client for the TAAPI.IO API

164 lines (163 loc) 7.13 kB
"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")); 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_1.default.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_1.default.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_1.default.get(`${endpoint}/exchange-symbols`, { headers: { "Accept-Encoding": "gzip,deflate,compress" }, params: params }).then(response => { result = response.data; }); } return result; }); } } exports.default = Taapi;