-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.js
48 lines (41 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict'
/* global fetch */
const baseUrl = 'https://min-api.cryptocompare.com/data/'
function fetchJSON (url) {
return fetch(url)
.then(res => res.json())
.then(body => {
if (body.Response === 'Error') throw body.Message
return body
})
}
function price (fsym, tsyms, tryConversion) {
let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}
function priceMulti (fsyms, tsyms, tryConversion) {
let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}
function priceFull (fsyms, tsyms, tryConversion) {
let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
// We want the RAW data, not the DISPLAY data:
return fetchJSON(url).then(result => result.RAW)
}
function priceHistorical (fsym, tsyms, time, tryConversion) {
if (!(time instanceof Date)) throw new Error('time parameter must be an instance of Date.')
time = Math.floor(time.getTime() / 1000)
let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}`
if (tryConversion === false) url += '&tryConversion=false'
// The API returns json with an extra layer of nesting, so remove it
return fetchJSON(url).then(result => result[fsym])
}
module.exports = {
price,
priceMulti,
priceFull,
priceHistorical
}