stocks.js
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -125,2 +125,3 @@ /*! | ||
API_KEY_URL: 'https://www.alphavantage.co/support/#api-key', | ||
INTERVALS: [ | ||
@@ -147,5 +148,3 @@ '1min', '5min', '15min', '30min', '60min', 'daily', 'weekly', 'monthly' | ||
if (typeof this.apiKey === 'undefined') { | ||
throw new Error( | ||
`You must first claim your API Key at ${this.API_KEY_URL}` | ||
); | ||
this._throw(0, 'error'); | ||
} | ||
@@ -160,7 +159,3 @@ | ||
if (typeof data['Error Message'] !== 'undefined') { | ||
throw new Error( | ||
'An error occured. Please create an issue at ' + | ||
'https://github.com/wagenaartje/stocks/issues with your code, ' + | ||
`and provide the following message: ${data['Error Message']}` | ||
); | ||
this._throw(9, 'error'); | ||
} | ||
@@ -173,30 +168,57 @@ | ||
_throw: function (code, type) { | ||
const MESSAGES = { | ||
0: `You must first claim your API Key at ${this.API_KEY_URL}`, | ||
1: 'No options specified!', | ||
2: 'No `symbol` option specified!', | ||
3: `No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.INTERVALS.join(', ')}`, | ||
4: `Only 'start'-'end' OR 'amount' can be specified!`, | ||
5: `No 'indicator' option specified!`, | ||
6: `No 'time_period' option specified!`, | ||
7: `No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.PERFORMANCES.join(', ')}`, | ||
8: `No 'amount' option specified, returning maximum amount of datapoints`, | ||
9: 'An error occured during the API request. Please create an issue at ' + | ||
'https://github.com/wagenaartje/stocks/issues with your code', | ||
10: `'start' specified, but 'end' not specified. Using today's date as ` + | ||
`end date!` | ||
}; | ||
if (type === 'error') { | ||
throw new Error(`${code}: ${MESSAGES[code]}`); | ||
} else if (type === 'warning') { | ||
console.warn(`${code}: ${MESSAGES[code]}`); | ||
} | ||
}, | ||
_checkOptions: function (options, type) { | ||
if (typeof options === 'undefined') { | ||
throw new Error('No options specified!'); | ||
this._throw(1, 'error'); | ||
} else if (typeof options.symbol === 'undefined') { | ||
throw new Error('No `symbol` option specified!'); | ||
this._throw(2, 'error'); | ||
} else if (typeof options.interval === 'undefined' || | ||
this.INTERVALS.indexOf(options.interval) === -1) { | ||
throw new Error( | ||
`No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.INTERVALS.join(', ')}` | ||
); | ||
!this.INTERVALS.includes(options.interval)) { | ||
this._throw(3, 'error'); | ||
} else if (typeof options.start !== 'undefined' && | ||
typeof options.amount !== 'undefined') { | ||
throw new Error(`Only 'start'-'end' OR 'amount' can be specified!`); | ||
this._throw(4, 'error'); | ||
} | ||
if (typeof options.amount === 'undefined' && | ||
options.start === 'undefined') { | ||
console.warn( | ||
`No 'amount' option specified, returning maximum amount of datapoints` | ||
); | ||
typeof options.start === 'undefined') { | ||
this._throw(8, 'warning'); | ||
} | ||
if (typeof options.start === 'object' && | ||
typeof options.end === 'undefined') { | ||
this._throw(10, 'warning'); | ||
options.end = Date.now(); | ||
} | ||
if (type === 'technical') { | ||
if (typeof options.indicator === 'undefined') { | ||
throw new Error(`No 'indicator' option specified!`); | ||
this._throw(5, 'error'); | ||
} else if (typeof options.time_period === 'undefined') { | ||
throw new Error(`No 'time_period' option specified!`); | ||
this._throw(6, 'error'); | ||
} | ||
@@ -209,3 +231,4 @@ } | ||
var key = Object.keys(data).find( | ||
key => key.indexOf('Time Series') !== -1 || key.indexOf('Technical') !== -1 | ||
key => key.indexOf('Time Series') !== -1 || | ||
key.indexOf('Technical') !== -1 | ||
); | ||
@@ -246,3 +269,3 @@ data = data[key]; | ||
if (this.INTERVALS.slice(0, 5).indexOf(options.interval) > -1) { | ||
if (this.INTERVALS.slice(0, 5).includes(options.interval)) { | ||
var interval = options.interval; | ||
@@ -300,6 +323,4 @@ options.interval = 'intraday'; | ||
if (typeof options.timespan === 'undefined' || | ||
this.PERFORMANCES.indexOf(options.timespan) === -1) { | ||
throw new Error(`No (correct) 'interval' option specified, please set ` + | ||
`to any of the following: ${this.PERFORMANCES.join(', ')}` | ||
); | ||
!this.PERFORMANCES.includes(options.timespan)) { | ||
this._throw(7, 'error'); | ||
} | ||
@@ -315,3 +336,3 @@ | ||
let noSpace = key.replace(/ /g, '').toLowerCase(); | ||
return noSpace.indexOf(options.timespan) !== -1; | ||
return noSpace.includes(options.timespan); | ||
}); | ||
@@ -318,0 +339,0 @@ |
{ | ||
"name": "stocks.js", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "stocks.js offers an easy to use stock market data API", | ||
@@ -5,0 +5,0 @@ "homepage" : "https://github.com/wagenaartje/stocks.js", |
@@ -22,2 +22,3 @@ /** Import */ | ||
API_KEY_URL: 'https://www.alphavantage.co/support/#api-key', | ||
INTERVALS: [ | ||
@@ -44,5 +45,3 @@ '1min', '5min', '15min', '30min', '60min', 'daily', 'weekly', 'monthly' | ||
if (typeof this.apiKey === 'undefined') { | ||
throw new Error( | ||
`You must first claim your API Key at ${this.API_KEY_URL}` | ||
); | ||
this._throw(0, 'error'); | ||
} | ||
@@ -57,7 +56,3 @@ | ||
if (typeof data['Error Message'] !== 'undefined') { | ||
throw new Error( | ||
'An error occured. Please create an issue at ' + | ||
'https://github.com/wagenaartje/stocks/issues with your code, ' + | ||
`and provide the following message: ${data['Error Message']}` | ||
); | ||
this._throw(9, 'error'); | ||
} | ||
@@ -70,30 +65,57 @@ | ||
_throw: function (code, type) { | ||
const MESSAGES = { | ||
0: `You must first claim your API Key at ${this.API_KEY_URL}`, | ||
1: 'No options specified!', | ||
2: 'No `symbol` option specified!', | ||
3: `No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.INTERVALS.join(', ')}`, | ||
4: `Only 'start'-'end' OR 'amount' can be specified!`, | ||
5: `No 'indicator' option specified!`, | ||
6: `No 'time_period' option specified!`, | ||
7: `No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.PERFORMANCES.join(', ')}`, | ||
8: `No 'amount' option specified, returning maximum amount of datapoints`, | ||
9: 'An error occured during the API request. Please create an issue at ' + | ||
'https://github.com/wagenaartje/stocks/issues with your code', | ||
10: `'start' specified, but 'end' not specified. Using today's date as ` + | ||
`end date!` | ||
}; | ||
if (type === 'error') { | ||
throw new Error(`${code}: ${MESSAGES[code]}`); | ||
} else if (type === 'warning') { | ||
console.warn(`${code}: ${MESSAGES[code]}`); | ||
} | ||
}, | ||
_checkOptions: function (options, type) { | ||
if (typeof options === 'undefined') { | ||
throw new Error('No options specified!'); | ||
this._throw(1, 'error'); | ||
} else if (typeof options.symbol === 'undefined') { | ||
throw new Error('No `symbol` option specified!'); | ||
this._throw(2, 'error'); | ||
} else if (typeof options.interval === 'undefined' || | ||
this.INTERVALS.indexOf(options.interval) === -1) { | ||
throw new Error( | ||
`No (correct) 'interval' option specified, please set to any of the ` + | ||
`following: ${this.INTERVALS.join(', ')}` | ||
); | ||
!this.INTERVALS.includes(options.interval)) { | ||
this._throw(3, 'error'); | ||
} else if (typeof options.start !== 'undefined' && | ||
typeof options.amount !== 'undefined') { | ||
throw new Error(`Only 'start'-'end' OR 'amount' can be specified!`); | ||
this._throw(4, 'error'); | ||
} | ||
if (typeof options.amount === 'undefined' && | ||
options.start === 'undefined') { | ||
console.warn( | ||
`No 'amount' option specified, returning maximum amount of datapoints` | ||
); | ||
typeof options.start === 'undefined') { | ||
this._throw(8, 'warning'); | ||
} | ||
if (typeof options.start === 'object' && | ||
typeof options.end === 'undefined') { | ||
this._throw(10, 'warning'); | ||
options.end = Date.now(); | ||
} | ||
if (type === 'technical') { | ||
if (typeof options.indicator === 'undefined') { | ||
throw new Error(`No 'indicator' option specified!`); | ||
this._throw(5, 'error'); | ||
} else if (typeof options.time_period === 'undefined') { | ||
throw new Error(`No 'time_period' option specified!`); | ||
this._throw(6, 'error'); | ||
} | ||
@@ -106,3 +128,4 @@ } | ||
var key = Object.keys(data).find( | ||
key => key.indexOf('Time Series') !== -1 || key.indexOf('Technical') !== -1 | ||
key => key.indexOf('Time Series') !== -1 || | ||
key.indexOf('Technical') !== -1 | ||
); | ||
@@ -143,3 +166,3 @@ data = data[key]; | ||
if (this.INTERVALS.slice(0, 5).indexOf(options.interval) > -1) { | ||
if (this.INTERVALS.slice(0, 5).includes(options.interval)) { | ||
var interval = options.interval; | ||
@@ -197,6 +220,4 @@ options.interval = 'intraday'; | ||
if (typeof options.timespan === 'undefined' || | ||
this.PERFORMANCES.indexOf(options.timespan) === -1) { | ||
throw new Error(`No (correct) 'interval' option specified, please set ` + | ||
`to any of the following: ${this.PERFORMANCES.join(', ')}` | ||
); | ||
!this.PERFORMANCES.includes(options.timespan)) { | ||
this._throw(7, 'error'); | ||
} | ||
@@ -212,3 +233,3 @@ | ||
let noSpace = key.replace(/ /g, '').toLowerCase(); | ||
return noSpace.indexOf(options.timespan) !== -1; | ||
return noSpace.includes(options.timespan); | ||
}); | ||
@@ -215,0 +236,0 @@ |
37122
669