-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
87 lines (70 loc) · 1.76 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
const RestV2 = require('./lib/transports/rest_v2')
const SUPPORTED_REST_VERSIONS = [2]
const WebSocket = require('./lib/transports/websocket')
/**
* Client interface to commnuicate with MAX exchange Rest API v2.
*/
class MAX {
/**
* @param {Object} options
* @param {string} options.accessKey
* @param {string} options.secretKey
*/
constructor (options = {
accessKey: '',
secretKey: ''
}) {
this._accessKey = options.accessKey || ''
this._secretKey = options.secretKey || ''
// init cache
this._cache = {}
}
/* Public Methods */
/**
* Returns a new Rest API class instance (cached by version)
*
* @param {number} version - 2 (default)
* @return {RestV2} transport
*/
rest (version = 2) {
if (!SUPPORTED_REST_VERSIONS.includes(version)) {
throw Error(`Version ${version} is not supported, default version is 2.`)
}
let rest = this._readCache(this._restCacheKey(version))
if (!rest) {
rest = this._writeCache(this._restCacheKey(version), new RestV2({
accessKey: this._accessKey, secretKey: this._secretKey
}))
}
return rest
}
/**
* Returns a new WebSocket API class instance
*
* @return {WebSocket} transport
*/
ws () {
let ws = this._readCache(this._wsCacheKey())
if (!ws) {
ws = this._writeCache(this._wsCacheKey(), new WebSocket({
accessKey: this._accessKey, secretKey: this._secretKey
}))
}
return ws
}
/* Private Methods */
_writeCache (key, value) {
this._cache[key] = value
return this._cache[key]
}
_readCache (key) {
return this._cache[key]
}
_restCacheKey (version) {
return `restV${version}`
}
_wsCacheKey (version = 1) {
return `wsV${version}`
}
}
module.exports = MAX