-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvk_transport_connect.js
61 lines (55 loc) · 1.83 KB
/
vk_transport_connect.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
import bridge from "@vkontakte/vk-bridge";
import { VkApiError } from "./vk_api.js";
class VkRequestError extends Error {
constructor(data) {
super(JSON.stringify(data));
this.name = 'VkRequestError';
this.data = data;
}
}
export const vkSendInitRequest = () => {
bridge.send('VKWebAppInit', {});
};
export const vkSendRequest = (method, params) => {
return new Promise((resolve, reject) => {
bridge.send(method, params)
.then(resolve)
.catch((data) => reject(new VkRequestError(data)));
});
};
export class Transport {
constructor() {
this._accessToken = null;
}
setAccessToken(accessToken) {
this._accessToken = accessToken;
}
async callAPI(method, params) {
if (this._accessToken === null)
throw new Error('access token was not set for this Transport instance');
let result;
try {
result = await vkSendRequest(
'VKWebAppCallAPIMethod',
{
method: method,
params: {...params, access_token: this._accessToken},
}
);
} catch (err) {
if (!(err instanceof VkRequestError))
throw err;
//if (err.data.error_type === 'client_error' && err.data.error_data.error_code === 1) {
// const reason = err.data.error_data.error_reason;
// throw new VkApiError(reason.error_code, reason.error_msg);
//} else {
// throw err;
//}
const error_data = err.data.error_data;
throw new VkApiError(error_data.error_code, error_data.error_msg);
}
if (result.error)
throw new VkApiError(result.error.error_code, result.error.error_msg);
return result;
}
}