-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbx24-rest-client.js
93 lines (84 loc) · 2.11 KB
/
bx24-rest-client.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
// @ts-check
const axios = require('axios');
const {requestLogger, responseLogger} = require('axios-logger');
class BX24RestClient {
/**
* @param {string} domain
* @param {string} auth
* @param {object} options
*/
constructor(domain, auth, options = null) {
// this.domain = domain;
// this.auth = auth;
// @ts-ignore
this.axios = axios.create({
baseURL: 'https://' + domain,
params: {
auth,
},
});
if (options?.debug) {
const config = {
prefixText: 'BX24RestClient',
// status: true,
headers: false,
params: true,
};
this.axios.interceptors.request.use((request) => {
return requestLogger(request, config);
});
this.axios.interceptors.response.use((response) => {
return responseLogger(response, config);
});
}
}
/**
*
* @param {string} method
* @param {object} params
* @returns
*/
async callMethod(method, params = null) {
const url = `/rest/${method}.json`;
const response = await this.axios.post(url, params);
return response.data;
}
}
class BX24AuthService {
constructor({clientId, clientSecret, redirectUri}, options) {
// @ts-ignore
this.axios = axios.create({
baseURL: 'https://oauth.bitrix.info',
params: {
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri,
grant_type: 'refresh_token',
},
},
);
if (options?.debug) {
const config = {
prefixText: 'BX24AuthService',
// status: true,
headers: false,
params: true,
};
this.axios.interceptors.request.use((request) => {
return requestLogger(request, config);
});
this.axios.interceptors.response.use((response) => {
return responseLogger(response, config);
});
}
}
async refreshToken(refreshToken) {
const response = await this.axios.get('/oauth/token', {
params: {
refresh_token: refreshToken,
},
});
return response.data;
}
}
module.exports = {BX24RestClient, BX24AuthService};