-
Notifications
You must be signed in to change notification settings - Fork 6
/
ApiTransport.ts
54 lines (45 loc) · 1.23 KB
/
ApiTransport.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
import { hmac } from 'https://deno.land/x/hmac@v2.0.1/mod.ts';
export type APIResponse = {
status: boolean;
response: any;
};
export class ApiTransport {
protected apiEndPointURL: string;
protected apiKey: string;
protected apiSecret: string;
constructor(apiEndPointURL: string, apiKey: string, apiSecret: string) {
this.apiEndPointURL = apiEndPointURL;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}
public sendRequest = async (path: string, body: any) => {
const output: APIResponse = {
status: false,
response: undefined,
};
const b = JSON.stringify(body);
const signature = hmac('sha256', this.apiSecret, b, 'utf8', 'hex');
const headers: HeadersInit = {
'Content-Type': 'application/json',
'API-KEY': this.apiKey,
'HASH-SIGNATURE': signature,
};
try {
const req = await fetch(`${this.apiEndPointURL}${path}`, {
method: 'POST',
headers,
body: b,
});
const res = await req.json();
if (req.ok) {
output.status = true;
output.response = res;
} else {
output.response = res.msg;
}
} catch (error) {
output.response = error.message;
}
return output;
};
}