UNPKG

polygon.io

Version:

Isomorphic Javascript client for Polygon.io Stocks, Forex, and Crypto APIs

32 lines (31 loc) 1.63 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = exports.auth = void 0; const cross_fetch_1 = require("cross-fetch"); const query_string_1 = require("query-string"); const auth = (apiKey, func, apiBase) => (...args) => func(apiKey, apiBase, ...args); exports.auth = auth; const get = (path, apiKey = "invalid", apiBase, query) => __awaiter(void 0, void 0, void 0, function* () { if (!apiKey) { throw new Error("API KEY not configured..."); } const authenticatedQuery = Object.assign(Object.assign({}, query), { apiKey }); const queryString = query_string_1.stringify(authenticatedQuery, { encode: true }); const url = `${apiBase}${path}?${queryString}`; const response = yield cross_fetch_1.default(url); if (response.status >= 400) { const message = yield response.text(); throw new Error(message); } return response.json(); }); exports.get = get;