-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.js
62 lines (53 loc) · 1.85 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'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])
}
function topPairs (fsym, limit) {
let url = `${baseUrl}top/pairs?fsym=${fsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
function topExchanges (fsym, tsym, limit) {
let url = `${baseUrl}top/exchanges?fsym=${fsym}&tsym=${tsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
module.exports = {
price,
priceMulti,
priceFull,
priceHistorical,
topPairs,
topExchanges
}