-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
190 lines (165 loc) · 4.39 KB
/
index.ts
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
import axios from 'axios';
import * as moment from 'moment';
import * as CryptoJS from 'crypto-js';
import * as HmacSHA256 from 'crypto-js/hmac-sha256';
const DEFAULT_HEADERS = {
'Content-Type': 'application/json;charset=utf-8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
};
enum STATUS {
'OK' = 'ok',
'ERROR' = 'error'
}
/**
* 火币API调用
*
* - accessKey: 必填, 通过火币官方申请 https://huobiapi.github.io/docs/spot/v1/cn/#185368440e
* - secretKey: 必填, 通过火币官方申请 https://huobiapi.github.io/docs/spot/v1/cn/#185368440e
* - proxy: 选填, 需要代理时填入, default false
* - hostname: 选填, 调用火币的hostname, default 'api.huobi.pro'
* - timeout: 选填, 接口调用超时时间, 单位ms, default 30000
*
* @export
* @class HuobiRestAPI
*/
export class HuobiRestAPI {
private accessKey: string;
private secretKey: string;
private httpsConfig: Object;
hostname: string;
protocol: string;
proxy: {
host: string,
port: number
} | false;
constructor({ accessKey, secretKey, proxy = false, hostname = 'api.huobi.pro', timeout = 30000 }: {
accessKey: string,
secretKey: string,
proxy?: {
host: string,
port: number
} | false,
timeout?: number
hostname?: string
}) {
if (!accessKey || !secretKey) {
throw 'Params Missing: accessKey or secretKey';
}
this.accessKey = accessKey;
this.secretKey = secretKey;
this.hostname = hostname;
this.protocol = 'https';
this.proxy = proxy;
this.httpsConfig = {
timeout,
headers: DEFAULT_HEADERS
};
}
get host() {
return `${this.protocol}://${this.hostname}`;
}
get(path: string, params?: Object) {
return this.request('GET', path, params);
}
post(path: string, params?: Object) {
return this.request('POST', path, params);
}
request(method: 'GET' | 'POST', path: string, params?: Object) {
if (method !== 'GET' && method !== 'POST') {
throw 'method only be GET or POST';
}
path = this.foramtPath(path);
const { paramsStr, originalParams } = this.signParams({
path,
method,
params
});
if (method === 'GET') {
return this.fetch(`${path}?${paramsStr}`, {
method
});
}
return this.fetch(`${path}?${paramsStr}`, {
method,
data: originalParams
});
}
private signParams({
method, path, params
}: {
method: 'GET' | 'POST';
path: String;
params?: Object;
}): {
originalParams: Object,
paramsStr: string,
signature: string
} {
if (!path.startsWith('/')) {
throw 'path must starts with \/';
}
const needSignature = !path.startsWith('/market');
let originalParams;
if (needSignature) {
originalParams = {
AccessKeyId: this.accessKey,
SignatureMethod: 'HmacSHA256',
SignatureVersion: '2',
Timestamp: moment.utc().format('YYYY-MM-DDTHH:mm:ss'),
...params
};
} else {
originalParams = { ...params };
}
const paramsArr = [];
for (const item in originalParams) {
paramsArr.push(`${item}=${encodeURIComponent(originalParams[item])}`);
}
const pStr = paramsArr.sort().join('&');
if (!needSignature) {
return {
originalParams,
signature: '',
paramsStr: pStr
};
}
const meta = [method, this.hostname, path, pStr].join('\n');
const hash = HmacSHA256(meta, this.secretKey);
const signature = encodeURIComponent(CryptoJS.enc.Base64.stringify(hash));
return {
signature,
originalParams,
paramsStr: `${pStr}&Signature=${signature}`
};
}
private foramtPath(path: string): string {
path = path.trim();
if (!path.startsWith('/')) {
path = `/${ path }`;
}
if (path.endsWith('/')) {
path = path.substring(0, path.length - 1);
}
return path;
}
private fetch(path, options: Object) {
const url = `${this.host}${path}`;
return axios({
url,
...options,
...this.httpsConfig,
proxy: this.proxy
}).then((res) => {
if (res.status !== 200) {
throw res;
}
return res.data;
}).then((data) => {
const status: string = data.status.toLowerCase();
if (status !== STATUS.OK) {
throw data;
}
return data;
});
}
}