-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
215 lines (187 loc) · 5.45 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
209
210
211
212
213
214
215
import fetchPonyfill from 'fetch-ponyfill'
import {
DEFAULT_ENDPOINTS,
HEADER_API_KEY_KEY,
HEADER_API_SIGNATURE_KEY,
HEADER_USER_AGENT_KEY,
HEADER_USER_AGENT_VALUE,
PRIVATE,
} from '../constants'
import {
buildRequest,
createBuildApiSignature,
createBuildEndpointUrl,
createBuildHeadersSpecification,
createGetEndpointSpecification,
createGetConfig,
joinUrlParts,
} from '../utils'
const { fetch: DEFAULT_FETCH } = fetchPonyfill()
/* eslint-disable no-undef */
const isBrowser = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
)
/* eslint-enable no-undef */
const defaultHeadersSpecification = {
'Content-Type': 'application/json',
[HEADER_USER_AGENT_KEY]: HEADER_USER_AGENT_VALUE,
}
export const defaultConfigOptions = {
apiUrl: 'https://api.kraken.com',
timeout: 60 * 1000,
version: 0,
}
class KrakenClient {
constructor(
{
apiCredentialsForbidden = false,
endpointSpecifications = DEFAULT_ENDPOINTS,
key = '',
OVERRIDE_BROWSER_CHECK_ONLY_FOR_TESTING = false,
secret = '',
...options
} = {},
) {
if (!apiCredentialsForbidden) {
if (isBrowser && !OVERRIDE_BROWSER_CHECK_ONLY_FOR_TESTING) {
throw new Error(
`It seems this is used in a browser environment and then it is only
allowed to use the public client, which does not require any API
credentials. It is exported as a named export.`,
)
}
if (!key) {
throw new Error(
'"key" is missing: constructor must be called with Kraken API key',
)
}
if (!secret) {
throw new Error(
`"secret" is missing: constructor must be called with Kraken API
secret`,
)
}
}
this.getConfig = createGetConfig({
...defaultConfigOptions,
...options,
apiCredentialsForbidden,
key,
secret,
})
const { apiUrl, version } = this.getConfig()
const baseUrl = joinUrlParts([apiUrl, version])
this.buildApiSignature = apiCredentialsForbidden
? null
: createBuildApiSignature(secret)
this.buildEndpointUrl = createBuildEndpointUrl(baseUrl)
this.buildHeadersSpecification = createBuildHeadersSpecification(
defaultHeadersSpecification,
)
this.getEndpointSpecification = createGetEndpointSpecification(
endpointSpecifications,
)
}
getHeadersSpecification({ body, nonce, path, type: endpointType }) {
if (endpointType === PRIVATE) {
const { key } = this.getConfig()
return this.buildHeadersSpecification({
[HEADER_API_KEY_KEY]: key,
[HEADER_API_SIGNATURE_KEY]: this.buildApiSignature({
body,
nonce,
path,
}),
})
}
return this.buildHeadersSpecification()
}
request(
endpointKey,
{ body, fetch = DEFAULT_FETCH, nonce = Number(`${Date.now()}000`) } = {},
) {
if (!endpointKey) {
throw new Error(
`"endpointKey" is missing: publicGet requires an endpointKey, according
to this pattern: https://api.kraken.com/{version}/{type}/{endpointKey}`,
)
}
const endpointSpecification = this.getEndpointSpecification(endpointKey)
if (!endpointSpecification) {
throw new Error(
`"endpointKey" is invalid: publicGet requires a case-sensitive
endpointKey, according to this pattern:
https://api.kraken.com/{version}/{type}/{endpointKey}
Check the correct endpointKey on https://www.kraken.com/help/api and
raise an issue on GitHub if the endpointKey is missing from
constants/endpoints.`,
)
}
const { method, path, type } = endpointSpecification
const { apiCredentialsForbidden, timeout } = this.getConfig()
if (apiCredentialsForbidden && type === PRIVATE) {
throw new Error(
`${endpointKey} is a private method and cannot be called with the public
client. Please use the default client instead.`,
)
}
const methodDependentBody =
method === 'GET'
? undefined
: {
...body,
nonce,
}
const url = this.buildEndpointUrl(path)
const headers = this.getHeadersSpecification({
body: methodDependentBody,
nonce,
path: `/0${path}`,
type,
})
const request = buildRequest(url, {
body: methodDependentBody,
headers,
method,
timeout,
})
/*
* Responses are JSON encoded in the form of:
* error = array of error messages in the format of:
* <char-severity code><string-error category>:<string-error type>[:
* <string-extra info>]
* severity code can be E for error or W for warning
* result = result of API call (may not be present if errors occur)
*/
return fetch(url, request)
.then(response => {
return response.json().then(({ error, result }) => {
if (error && error.length) {
throw error.join('; ')
}
return result
})
})
.catch(error => {
if (!error) {
throw new Error('Kraken API rejected the request with unknown error')
}
throw error
})
}
}
export const createPublicKrakenClient = (
{
endpointSpecifications = DEFAULT_ENDPOINTS,
KrakenClientOrMock = KrakenClient,
...options
} = {},
) =>
new KrakenClientOrMock({
apiCredentialsForbidden: true,
endpointSpecifications,
...options,
})
export default KrakenClient