From 3baa6c681422e90ff99f51e7c00930319b1442d8 Mon Sep 17 00:00:00 2001 From: Reed Lawrence Date: Wed, 22 Jan 2020 19:18:54 -0600 Subject: [PATCH 1/2] Added chart fn --- lib/chart.js | 22 ++++++++++++++++++++++ lib/index.js | 1 + 2 files changed, 23 insertions(+) create mode 100644 lib/chart.js diff --git a/lib/chart.js b/lib/chart.js new file mode 100644 index 0000000..3479868 --- /dev/null +++ b/lib/chart.js @@ -0,0 +1,22 @@ +var request = require('request'); + +/** + * + * @param {string} symbol + */ +function chart(symbol, cb) { + return new Promise((resolve, reject) => { + if (!symbol) { + return reject('Symbol must not be falsey value'); + } + + var url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?region=US&lang=en-US&includePrePost=false&interval=2m&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance`; + + request.get(url, function (err, res, body) { + return resolve(body); + }); + }); +} + +exports.__esModule = true; +exports.default = chart; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index a10e438..63f5cd5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ exports.historical = require('./historical'); exports.snapshot = require('./snapshot').default; exports.quote = require('./quote').default; +exports.chart = require('./chart').default; From e125e4b1bda573346a0619013a083fdb405f3ff2 Mon Sep 17 00:00:00 2001 From: Reed Lawrence Date: Wed, 22 Jan 2020 19:24:02 -0600 Subject: [PATCH 2/2] Updates to chart method --- lib/chart.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/chart.js b/lib/chart.js index 3479868..9497c49 100644 --- a/lib/chart.js +++ b/lib/chart.js @@ -1,10 +1,10 @@ var request = require('request'); /** - * + * Method for getting chart data via yahoo API * @param {string} symbol */ -function chart(symbol, cb) { +function chart(symbol) { return new Promise((resolve, reject) => { if (!symbol) { return reject('Symbol must not be falsey value'); @@ -13,7 +13,13 @@ function chart(symbol, cb) { var url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?region=US&lang=en-US&includePrePost=false&interval=2m&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance`; request.get(url, function (err, res, body) { - return resolve(body); + if (err) { + return reject(err); + } else if (body) { + return resolve(body); + } else { + return reject('Yahoo finance response not as expected'); + } }); }); }