forked from singlesly/bingx-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingx-request.ts
More file actions
47 lines (42 loc) · 1.42 KB
/
Copy pathbingx-request.ts
File metadata and controls
47 lines (42 loc) · 1.42 KB
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
import { EndpointInterface } from 'bingx-api/bingx/endpoints/endpoint.interface';
import { lastValueFrom } from 'rxjs';
import { BingxRequestInterface } from 'bingx-api/bingx/endpoints/bingx-request.interface';
import { HttpService } from '@nestjs/axios';
import axios from 'axios';
import * as JSONBigNumber from 'json-bignumber';
export class BingxRequest<R> implements BingxRequestInterface<R> {
private readonly http = new HttpService(
axios.create({
baseURL: 'https://open-api.bingx.com',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
transformResponse: (res) => {
try {
return JSON.parse(JSON.stringify(JSONBigNumber.parse(res)));
} catch (e) {
console.error('BingxRequest.http.transformResponse', e, res);
return res;
}
},
}),
);
constructor(private readonly endpoint: EndpointInterface<R>) {}
async getResponse(): Promise<Readonly<R>> {
const response = await lastValueFrom(
this.http.request({
method: this.endpoint.method(),
params: {
...this.endpoint.parameters().asRecord(),
signature: this.endpoint.signature().toString(),
},
headers: this.endpoint.apiKey().asHeader(),
url: this.endpoint.path(),
}),
);
return response.data;
}
async getEndpoint(): Promise<Readonly<EndpointInterface<R>>> {
return this.endpoint;
}
}