|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | +}; |
| 14 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | +exports.CoinCodex = void 0; |
| 16 | +const https_1 = __importDefault(require("https")); |
| 17 | +const utilities_1 = __importDefault(require("./helpers/utilities")); |
| 18 | +const constants_1 = __importDefault(require("./helpers/constants")); |
| 19 | +/** |
| 20 | + * @description A Node.js wrapper for the CoinCodex API with no dependencies. For more information, visit: https://coincodex.com/page/api/ |
| 21 | + * @example |
| 22 | + * const CoinCodex = require('coincodex-api'); |
| 23 | + * const CoinCodexClient = new CoinCodex(); |
| 24 | + */ |
| 25 | +class CoinCodex { |
| 26 | + /** Returns historic data for coins needed to draw the graph on the frontpage */ |
| 27 | + firstPageHistory(params) { |
| 28 | + return __awaiter(this, void 0, void 0, function* () { |
| 29 | + //Must be object |
| 30 | + if (!utilities_1.default.isObject(params)) |
| 31 | + utilities_1.default._WARN_('Invalid parameter', 'params must be of type: Object'); |
| 32 | + if (!params['days']) |
| 33 | + utilities_1.default._WARN_('Missing parameter', 'params must include `days` and be a string or number'); |
| 34 | + if (!params['coins_limit']) |
| 35 | + utilities_1.default._WARN_('Missing parameter', 'params must include `coins_limit` and be a string or number'); |
| 36 | + //If no 'samples', just set to default: 1 |
| 37 | + if (utilities_1.default.isNumber(params['samples'])) |
| 38 | + params['samples'] = `${params['samples']}`; |
| 39 | + if (!utilities_1.default.isString(params['samples']) || utilities_1.default.isStringEmpty(params['samples'])) { |
| 40 | + params['samples'] = 1; |
| 41 | + } |
| 42 | + //Get values |
| 43 | + const { days, samples, coins_limit } = params; |
| 44 | + const path = `/api/coincodex/get_firstpage_history/${days}/${samples}/${coins_limit}`; |
| 45 | + const options = this._buildRequestOptions(path); |
| 46 | + return this._request(options); |
| 47 | + }); |
| 48 | + } |
| 49 | + ; |
| 50 | + /** Calls related to coins */ |
| 51 | + get coins() { |
| 52 | + return { |
| 53 | + /** Returns all coins on the platform with properties that are needed to display them on the frontpage */ |
| 54 | + all: () => __awaiter(this, void 0, void 0, function* () { |
| 55 | + const path = `/apps/coincodex/cache/all_coins.json`; |
| 56 | + const options = this._buildRequestOptions(path); |
| 57 | + return this._request(options); |
| 58 | + }), |
| 59 | + /** Returns all properties for the coin needed to display the coin details page */ |
| 60 | + fetch: (coinId) => __awaiter(this, void 0, void 0, function* () { |
| 61 | + //Must have coinId |
| 62 | + if (!utilities_1.default.isString(coinId) || utilities_1.default.isStringEmpty(coinId)) |
| 63 | + utilities_1.default._WARN_('Invalid parameter', 'coinId must be of type: String and greater than 0 characters.'); |
| 64 | + const path = `/api/coincodex/get_coin/${coinId}`; |
| 65 | + const options = this._buildRequestOptions(path); |
| 66 | + return this._request(options); |
| 67 | + }), |
| 68 | + /** Returns historical price data for a single coin */ |
| 69 | + fetchHistory: (coinId, params) => __awaiter(this, void 0, void 0, function* () { |
| 70 | + //Must have coinId |
| 71 | + if (!utilities_1.default.isString(coinId) || utilities_1.default.isStringEmpty(coinId)) |
| 72 | + utilities_1.default._WARN_('Invalid parameter', 'coinId must be of type: String and greater than 0 characters.'); |
| 73 | + //Must be object |
| 74 | + if (!utilities_1.default.isObject(params)) |
| 75 | + utilities_1.default._WARN_('Invalid parameter', 'params must be of type: Object'); |
| 76 | + //If no params.start_date, params.end_date... Warn |
| 77 | + if (!utilities_1.default.isString(params['start_date']) || utilities_1.default.isStringEmpty(params['start_date'])) |
| 78 | + utilities_1.default._WARN_('Missing parameter', 'params must include `start_date` and be a string in format: `YYYY-MM-DD`'); |
| 79 | + if (!utilities_1.default.isString(params['end_date']) || utilities_1.default.isStringEmpty(params['end_date'])) |
| 80 | + utilities_1.default._WARN_('Missing parameter', 'params must include `end_date` and be a string in format: `YYYY-MM-DD`'); |
| 81 | + //If no 'samples', just set to default: 1 |
| 82 | + if (utilities_1.default.isNumber(params['samples'])) |
| 83 | + params['samples'] = `${params['samples']}`; |
| 84 | + if (!utilities_1.default.isString(params['samples']) || utilities_1.default.isStringEmpty(params['samples'])) { |
| 85 | + params['samples'] = 1; |
| 86 | + } |
| 87 | + //Get values |
| 88 | + const { start_date, end_date, samples } = params; |
| 89 | + const path = `/api/coincodex/get_coin_history/${coinId}/${start_date}/${end_date}/${samples}`; |
| 90 | + const options = this._buildRequestOptions(path); |
| 91 | + return this._request(options); |
| 92 | + }), |
| 93 | + /** Returns exchanges and markets for a coin */ |
| 94 | + markets: (coinId) => __awaiter(this, void 0, void 0, function* () { |
| 95 | + //Must have coinId |
| 96 | + if (!utilities_1.default.isString(coinId) || utilities_1.default.isStringEmpty(coinId)) |
| 97 | + utilities_1.default._WARN_('Invalid parameter', 'coinId must be of type: String and greater than 0 characters.'); |
| 98 | + const path = `/api/exchange/get_markets_by_coin/${coinId}`; |
| 99 | + const options = this._buildRequestOptions(path); |
| 100 | + return this._request(options); |
| 101 | + }), |
| 102 | + /** Returns min and max ranges of given coinId(s) for 1 hour, 1 day, 7 day, 30 day, 90 day, 180 day, year to date, last 365 days and all time */ |
| 103 | + ranges: (coinIds) => __awaiter(this, void 0, void 0, function* () { |
| 104 | + if (utilities_1.default.isString(coinIds) && utilities_1.default.isStringEmpty(coinIds)) |
| 105 | + utilities_1.default._WARN_('Invalid parameter', 'coinId must be of type: String or Array and greater than 0 characters, items.'); |
| 106 | + if (Array.isArray(coinIds)) { |
| 107 | + if (coinIds.length === 0) |
| 108 | + utilities_1.default._WARN_('Invalid parameter', 'coinId must be of type: String or Array and greater than 0 characters, items.'); |
| 109 | + //If are arrays, convert to string |
| 110 | + coinIds = coinIds.join(','); |
| 111 | + } |
| 112 | + const path = `/api/coincodex/get_coin_ranges/${coinIds}`; |
| 113 | + const options = this._buildRequestOptions(path); |
| 114 | + return this._request(options); |
| 115 | + }), |
| 116 | + }; |
| 117 | + } |
| 118 | + ; |
| 119 | + /** Build options for https.request */ |
| 120 | + _buildRequestOptions(path) { |
| 121 | + return { |
| 122 | + path, |
| 123 | + method: 'GET', |
| 124 | + host: constants_1.default.HOST, |
| 125 | + port: 443, |
| 126 | + }; |
| 127 | + } |
| 128 | + ; |
| 129 | + /** Perform https request */ |
| 130 | + _request(options) { |
| 131 | + return new Promise((resolve, reject) => { |
| 132 | + //Perform request |
| 133 | + let req = https_1.default.request(options, (res) => { |
| 134 | + let body = []; |
| 135 | + //Set body on data |
| 136 | + res.on('data', (chunk) => { |
| 137 | + body.push(chunk); |
| 138 | + }); |
| 139 | + //On end, end the Promise |
| 140 | + res.on('end', () => { |
| 141 | + try { |
| 142 | + body = Buffer.concat(body); |
| 143 | + body = body.toString(); |
| 144 | + //Check if page is returned instead of JSON |
| 145 | + if (body.startsWith('<!DOCTYPE html>')) |
| 146 | + utilities_1.default._WARN_('Invalid request', 'There was a problem with your request. The parameter(s) you gave are missing or incorrect.'); |
| 147 | + //Attempt to parse |
| 148 | + body = JSON.parse(body); |
| 149 | + } |
| 150 | + catch (error) { |
| 151 | + reject(error); |
| 152 | + } |
| 153 | + ; |
| 154 | + //Create return object |
| 155 | + resolve({ |
| 156 | + success: !(res.statusCode < 200 || res.statusCode >= 300), |
| 157 | + message: res.statusMessage, |
| 158 | + code: res.statusCode, |
| 159 | + data: body |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + //On error, reject the Promise |
| 164 | + req.on('error', (error) => reject(error)); |
| 165 | + //End request |
| 166 | + req.end(); |
| 167 | + }); |
| 168 | + } |
| 169 | +} |
| 170 | +exports.CoinCodex = CoinCodex; |
0 commit comments