UNPKG

node-iex-cloud

Version:
395 lines (394 loc) 20.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var IEXCloudClient = /** @class */ (function () { function IEXCloudClient(fetchFunc, _a) { var _this = this; var publishable = _a.publishable, _b = _a.sandbox, sandbox = _b === void 0 ? false : _b, _c = _a.version, version = _c === void 0 ? "beta" : _c; /** * Data points are available per symbol and return individual plain text values. Retrieving individual data points is useful for Excel and Google Sheet users, and applications where a single, lightweight value is needed. */ this.dataPoints = function (key) { if (key === void 0) { key = ""; } _this.datatype = "data-points"; return _this.request(key); }; /** TOPS provides IEX’s aggregated best quoted bid and offer position in near real time for all securities on IEX’s displayed limit order book. TOPS is ideal for developers needing both quote and trade data. */ this.tops = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } _this.datatype = "tops/last"; return _this.request("?symbols=" + params); }; /** Returns daily stats for a given time frame */ this.historicalStats = function (params, date) { _this.datatype = "stats"; return _this.request("" + (params ? params : "") + (date ? "/" + date : "")); }; this.market = function (params) { if (params === void 0) { params = ""; } _this.datatype = "stock/market"; return _this.request(params); }; this.collection = function (_a) { var param = _a.param, collectionName = _a.collectionName; return _this.request("collection/" + param + "?collectionName=" + collectionName); }; /** Time series is the most common type of data available, and consists of a collection of data points over a period of time. Time series data is indexed by a single date field, and can be retrieved by any portion of time. To use this endpoint, you’ll first make a free call to get an inventory of available time series data. */ this.timeSeries = function (id, subkey) { if (id === void 0) { id = ""; } _this.datatype = "time-series/" + id; return _this.request("" + subkey); }; /** DEEP is used to receive real-time depth of book quotations direct from IEX. The depth of book quotations received via DEEP provide an aggregated size of resting displayed orders at a price and side, and do not indicate the size or number of individual orders at any price level. Non-displayed orders and non-displayed portions of reserve orders are not represented in DEEP. */ this.deep = function (params) { if (params === void 0) { params = ""; } _this.datatype = "deep"; return _this.request(params); }; /** Takes in a stock symbol, a unique series of letters assigned to a security */ this.symbol = function (symbol) { _this.stockSymbol = symbol; return _this; }; /** Takes in multiple stock symbols, and batches them to a single request */ this.symbols = function () { var symbols = []; for (var _i = 0; _i < arguments.length; _i++) { symbols[_i] = arguments[_i]; } _this.datatype = "stock/market/batch"; _this.stockSymbols = symbols; return _this; }; this.setToken = function (token) { return _this.sandbox && token[0] !== "T" ? "T" + token : token; }; this.params = function (params) { if (params === void 0) { params = ""; } var env = _this.sandbox ? "sandbox" : "cloud"; var url = "https://" + env + ".iexapis.com/" + _this.version + "/" + _this.datatype; var operand = params.match(new RegExp("\\?", "g")); var q = operand && operand[0] === "?" ? "&" : "?"; var request = url + "/" + _this.stockSymbol + "/" + params + q + "token=" + _this.setToken(_this.publishable); if (_this.datatype === "deep") { var request_1 = url + "/" + params + "?symbols=" + _this.stockSymbol + "&token=" + _this.setToken(_this.publishable); _this.datatype = "stock"; _this.sandbox && console.log(request_1); return request_1; } if (_this.datatype === "stock/market/batch") { var request_2 = url + "?symbols=" + _this.stockSymbols.map(function (symbol) { return symbol; }) + "&types=" + params + "&token=" + _this.setToken(_this.publishable); _this.datatype = "stock"; _this.sandbox && console.log(request_2); return request_2; } if (_this.datatype === "stats") { var request_3 = url + "/" + params + q + "token=" + _this.setToken(_this.publishable); _this.datatype = "stock"; _this.sandbox && console.log(request_3); return request_3; } if (_this.datatype === "tops/last" || _this.datatype === "stock/market") { var request_4 = url + "/" + params + q + "token=" + _this.setToken(_this.publishable); _this.datatype = "stock"; _this.sandbox && console.log(request_4); return request_4; } _this.sandbox && console.log(request); return request; }; this.batchParams = function () { var types = []; for (var _i = 0; _i < arguments.length; _i++) { types[_i] = arguments[_i]; } var env = _this.sandbox ? "sandbox" : "cloud"; var url = "https://" + env + ".iexapis.com/" + _this.version + "/" + _this.datatype; if (_this.datatype === "stock/market/batch") { var request_5 = url + "/batch?symbols=" + _this.stockSymbols.map(function (symbol) { return symbol; }) + "&types=" + types.map(function (type) { return type; }) + "&token=" + _this.setToken(_this.publishable); _this.datatype = "stock"; _this.sandbox && console.log(request_5); return request_5; } var request = url + "/" + _this.stockSymbol + "/batch?types=" + types.map(function (type) { return type; }) + "&token=" + _this.setToken(_this.publishable); _this.sandbox && console.log(request); return request; }; this.request = function (params) { return __awaiter(_this, void 0, void 0, function () { var res, contentType, _a, err_1; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 6, , 7]); return [4 /*yield*/, this.fetchFunc(this.params(params))]; case 1: res = _b.sent(); contentType = res.headers.get("content-type"); if (!(contentType === "application/json; charset=utf-8")) return [3 /*break*/, 3]; return [4 /*yield*/, res.json()]; case 2: return [2 /*return*/, _b.sent()]; case 3: if (!(res.status >= 400)) return [3 /*break*/, 5]; _a = Error.bind; return [4 /*yield*/, res.text()]; case 4: throw new (_a.apply(Error, [void 0, _b.sent()]))(); case 5: return [3 /*break*/, 7]; case 6: err_1 = _b.sent(); console.error(err_1); return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }; /** batch returns multipe data-types for a give stock symbol */ this.batch = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(_this, void 0, void 0, function () { var res, contentType, _a, err_2; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 6, , 7]); return [4 /*yield*/, this.fetchFunc(this.batchParams(params))]; case 1: res = _b.sent(); contentType = res.headers.get("content-type"); if (!(contentType === "application/json; charset=utf-8")) return [3 /*break*/, 3]; return [4 /*yield*/, res.json()]; case 2: return [2 /*return*/, _b.sent()]; case 3: if (!(res.status >= 400)) return [3 /*break*/, 5]; _a = Error.bind; return [4 /*yield*/, res.text()]; case 4: throw new (_a.apply(Error, [void 0, _b.sent()]))(); case 5: return [3 /*break*/, 7]; case 6: err_2 = _b.sent(); console.error(err_2); return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }; /** returns balance sheet data. Available quarterly or annually with the default being the last available quarter * `Data Weight: 3000` */ this.balanceSheet = function (period, last) { return _this.request("balance-sheet" + (period ? "?period=" + period : "") + (last ? "&last=" + last : "")); }; /** * returns book value for a given stock * `Data Weight: 1 per quote returned` */ this.book = function () { return _this.request("book"); }; /** * Returns adjusted and unadjusted historical data for up to 15 years. * `Data Weight: 1,000 per symbol per period` */ this.chart = function (range, params) { var values = params && Object.entries(params); return _this.request("chart/" + range + (params ? "?" + values.map(function (v) { return v[0] + "=" + v[1]; }).join("&") : "")); }; /** * returns cash flow data. Available quarterly or annually, with the default being the last available quarter. * * `Data Weight: 1,000 per symbol per period` */ this.cashFlow = function (period, _a) { if (period === void 0) { period = "quarterly"; } var _b = _a.last, last = _b === void 0 ? 1 : _b; return _this.request("cash-flow?period=" + period + "&last=" + last); }; /** returns Ceo Compensation */ this.ceoCompensation = function () { return _this.request("ceo-compensation"); }; /** returns data on a given company * `Data Weight: 1 per symbol` */ this.company = function () { return _this.request("company"); }; /** * `Data Weight: 1 per symbol per quote` */ this.delayedQuote = function () { return _this.request("delayed-quote"); }; /** * `Data Weight: 10 per symbol per period returned` */ this.dividends = function (range) { return _this.request("dividends" + (range ? "/" + range : "")); }; /** Returns earnings data for a given company including the actual EPS, consensus, and fiscal period. Earnings are available quarterly (last 4 quarters). * `Data Weight: 1000 per symbol per period` */ this.earnings = function (last, field) { return _this.request("earnings" + (last ? "/" + last : "") + (field ? "/" + field : "")); }; /** Provides the latest consensus estimate for the next fiscal period */ this.estimates = function () { return _this.request("estimates"); }; /** Pulls income statement, balance sheet, and cash flow data from the most recent reported quarter. */ this.financials = function (period) { if (period === void 0) { period = "quarterly"; } return _this.request("financials?period=" + period); }; /** Returns latest news for a give stock symbol */ this.news = function (last) { if (last === void 0) { last = 10; } return _this.request("news/last/" + last); }; /** * Returns the top 10 fund holders, meaning any firm not defined as buy-side or sell-side such as mutual funds, pension funds, endowments, investment firms, and other large entities that manage funds on behalf of others. */ this.fundOwnership = function () { return _this.request("fund-ownership"); }; /** Pulls income statement data. Available quarterly or annually with the default being the last available quarter. */ this.income = function (period, last) { return _this.request("income" + (period ? "?period=" + period : "") + (last ? "&last=" + last : "")); }; /** Returns the top 10 insiders, with the most recent information. */ this.insiderRoster = function () { return _this.request("insider-roster"); }; /** Returns aggregated insiders summary data for the last 6 months. */ this.insiderSummary = function () { return _this.request("insider-summary"); }; /** Returns insider transactions. */ this.insiderTransactions = function () { return _this.request("insider-transactions"); }; /** Returns the top 10 institutional holders, defined as buy-side or sell-side firms. */ this.institutionalOwnership = function () { return _this.request("institutional-ownership"); }; /** This endpoint will return aggregated intraday prices in one minute buckets */ this.intradayPrices = function () { return _this.request("intraday-prices"); }; /** This is a helper function, but the google APIs url is standardized. */ this.logo = function () { return _this.request("logo"); }; /** This returns 15 minute delayed, last sale eligible trades. */ this.largestTrades = function () { return _this.request("largest-trades"); }; /** Returns end of day options data */ this.options = function (expiration, optionSide) { if (expiration === void 0) { expiration = ""; } return _this.request("options" + (expiration ? "/" + expiration : "") + (optionSide ? "/" + optionSide : "")); }; /** Returns peer group */ this.peers = function () { return _this.request("peers"); }; /** Returns previous day adjusted price data for one or more stocks. */ this.previous = function () { return _this.request("previous"); }; /** Returns price of a stock */ this.price = function () { return _this.request("price"); }; /** Provides the latest avg, high, and low analyst price target for a symbol. */ this.priceTarget = function () { return _this.request("price-target"); }; /** Returns the official open and close for a give symbol. The official open is available as soon as 9:45am ET and the official close as soon as 4:15pm ET. Some stocks can report late open or close prices. */ this.ohlc = function () { return _this.request("ohlc"); }; /** This endpoint provides social sentiment data from StockTwits. Data can be viewed as a daily value, or by minute for a given date. */ this.sentiment = function (type, date) { if (type === void 0) { type = "daily"; } if (date === void 0) { date = null; } return _this.request("sentiment/" + type + (date ? "/" + date : "")); }; this.quote = function (field) { if (field === void 0) { field = ""; } return _this.request("quote/" + (field ? field : "")); }; /** Pulls data from the last four months. */ this.recommendationTrends = function () { return _this.request("recommendation-trends"); }; this.stats = function (stat) { if (stat === void 0) { stat = ""; } return _this.request("stats/" + stat); }; this.splits = function (range) { if (range === void 0) { range = "1m"; } return _this.request("splits/" + range); }; this.shortInterest = function (date) { if (date === void 0) { date = ""; } return _this.request("short-interest/" + date); }; /** This returns 15 minute delayed and 30 day average consolidated volume percentage of a stock, by market. This call will always return 13 values, and will be sorted in ascending order by current day trading volume percentage. */ this.volumeByVenue = function () { return _this.request("volume-by-venue"); }; (this.fetchFunc = fetchFunc), (this.publishable = publishable), (this.version = version), (this.sandbox = sandbox), (this.datatype = "stock"), (this.stockSymbol = ""), (this.stockSymbols = []), (this.request = this.request); } return IEXCloudClient; }()); exports.default = IEXCloudClient;