-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
executable file
·208 lines (184 loc) · 5.74 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
const fetch = require('node-fetch')
const qs = require('qs')
const BASE_URL = 'https://api.coinpaprika.com'
class CoinpaprikaAPI {
/**
*
* @param {Object=} Options Options for the CoinpaprikaAPI instance
*
* @param {String=} options.version Version of API. Defaults to 'v1'
* @param {Object=} options.config = Configuration for fetch request
*
*/
constructor ({ version = 'v1', config = {} } = {}) {
this.config = Object.assign({}, {
method: 'GET',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Accept-Encoding': 'deflate, gzip'
}
}, config)
this.fetcher = fetch
this.url = `${BASE_URL}/${version}`
}
/**
* Get global information
*
* @example
* const client = new CoinpaprikaAPI()
* client.getGlobal().then(console.log).catch(console.error)
*/
getGlobal () {
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/global`,
config: this.config
})
}
/**
* Get information on all tickers or specifed ticker.
* DEPRECATED
*
* @param {Object=} options Options for the request
*
* @example
* const client = new CoinpaprikaAPI()
* client.getTicker({coinId: 3}).then(console.log).catch(console.error)
* client.getTicker().then(console.log).catch(console.error)
*/
getTicker (args = {}) {
if (Object.prototype.toString.call(args) !== '[object Object]') {
throw Error('Please pass object as arg.')
}
const { coinId } = args
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/ticker${(coinId) ? `/${coinId}` : ''}`,
config: this.config
})
}
/**
* Get tickers for all coins
* @param {Object=} options for the request consistent to https://api.coinpaprika.com/#tag/Tickers
* @param coinId: string
* @param quotes: array of strings
* @param historical: object
* @example
* const client = new CoinpaprikaAPI()
* client.getAllTickers({
* coinId:'btc-bitcoin',
* quotes: ['BTC', 'ETH']
* })
* .then(console.log)
* .catch(console.error)
*
* client.getAllTickers({
* coinId:'btc-bitcoin',
* historical: {
* start: '2018-02-15',
* end: '2018-02-16',
* limit: 2000,
* quote: 'btc',
* interval: '30m'
* }
* })
* .then(console.log)
* .catch(console.error)
*/
getAllTickers (params = {}) {
if (Object.prototype.toString.call(params) !== '[object Object]') {
throw Error('Please pass object as arg.')
}
const { coinId, quotes, historical } = params
if ((historical && typeof coinId === 'undefined') || (coinId && historical && typeof historical.start === 'undefined')) {
throw Error('required param was not pass, please check CoinpaprikaAPI client usage')
}
const coinIdParam = coinId ? `/${coinId}` : ''
const quotesParam = quotes ? `?quotes=${quotes.join(',')}` : ''
let historicalParam = ''
if (historical && coinId) {
historicalParam = ((historicalArgs = {}) => {
const { start, end, limit, quote, interval } = historicalArgs
const startParam = `start=${start}`
const endParam = end ? `&end=${end}` : ''
const limitParam = limit ? `&limit=${limit}` : ''
const quoteParam = quote ? `"e=${quote}` : ''
const intervalParam = interval ? `&interval=${interval}` : ''
return `/historical?${startParam}${endParam}${limitParam}${quoteParam}${intervalParam}`
})(historical)
}
const query = `${coinIdParam}${historicalParam}${quotesParam}`
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/tickers/${query}`,
config: this.config
})
}
/**
* Get a list of all cryptocurrencies available on coinpaprika.com.
*
* @example
* const client = new CoinpaprikaAPI()
* client.getCoins().then(console.log).catch(console.error)
*/
getCoins () {
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/coins`,
config: this.config
})
}
/**
* Get particular coin by coinId available on coinpaprika.com.
*
* @example
* const client = new CoinpaprikaAPI()
* client.getCoin('btc-bitcoin').then(console.log).catch(console.error)
*/
getCoin (coinId) {
if (!coinId) throw new Error('Can not be called without coinId')
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/coins/${coinId}`
})
}
/**
* Get the coin OHLCV historical
* @example
* const client = new CoinpaprikaAPI()
* client.getCoinsOHLCVHistorical({
* coinId: "btc-bitcoin",
* quote: "usd",
* start: "2020-01-01",
* end: "2020-01-02"
* }).then(console.log).catch(console.error)
*/
getCoinsOHLCVHistorical (params = {}) {
if (Object.prototype.toString.call(params) !== '[object Object]') {
throw Error('Please pass object as arg.')
}
const { coinId, quote, start, end } = params
if (typeof coinId !== 'string' || typeof start !== 'string' || !coinId || !start) {
throw Error('required param was not pass, please check CoinpaprikaAPI client usage')
}
const reqparams = {
coinId,
start: `?start=${start}`,
quote: quote ? `"e=${quote}` : '',
end: end ? `&end=${end}` : ''
}
const query = `${reqparams.start}${reqparams.end}${reqparams.quote}`
return createRequest({
fetcher: this.fetcher,
url: `${this.url}/coins/${reqparams.coinId}/ohlcv/historical${query}`,
config: this.config
})
}
}
const createRequest = (args = {}) => {
const { url, config, query, fetcher } = args
return fetcher(`${url}${query ? `?${qs.stringify(query)}` : ''}`, config).then(res => res.json())
}
module.exports = CoinpaprikaAPI