@slavmak2486/bx24ts 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +1 -0
- package/.eslintrc.json +22 -0
- package/BX24.ts +131 -0
- package/BX24Dev.ts +128 -0
- package/BX24Server.ts +161 -0
- package/BatchHelper.ts +58 -0
- package/base/BX24.ts +556 -0
- package/bin/test_.ts +2 -0
- package/callResult.ts +87 -0
- package/dist/BX24.js +107 -0
- package/dist/BX24Dev.js +117 -0
- package/dist/BX24Server.js +120 -0
- package/dist/BatchHelper.js +43 -0
- package/dist/base/BX24.js +418 -0
- package/dist/bin/test_.js +3 -0
- package/dist/callResult.js +49 -0
- package/dist/index.js +11 -0
- package/dist/types/authBase.js +2 -0
- package/dist/types/authBaseDev.js +2 -0
- package/dist/types/authBaseServe.js +2 -0
- package/dist/types/batchElement.js +2 -0
- package/dist/types/bitrixEvent.js +2 -0
- package/dist/types/eventElement.js +2 -0
- package/dist/types/getAuth.js +2 -0
- package/index.ts +4 -0
- package/jest.config.js +5 -0
- package/package.json +42 -0
- package/tests/batchHelper.test.ts +47 -0
- package/tests/callBatch.test.ts +69 -0
- package/tests/callBatchCb.test.ts +63 -0
- package/tests/callMethod.test.ts +49 -0
- package/tests/callMethodCb.test.ts +55 -0
- package/tests/mocks/batch.ts +3779 -0
- package/tests/mocks/dealList.ts +2214 -0
- package/tests/mocks/leadList.ts +1543 -0
- package/tsconfig.json +106 -0
- package/types/authBase.ts +11 -0
- package/types/authBaseServe.ts +22 -0
- package/types/batchElement.ts +3 -0
- package/types/bitrixEvent.ts +10 -0
- package/types/eventElement.ts +4 -0
- package/types/getAuth.ts +7 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.baseBX24 = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const callResult_1 = require("../callResult");
|
|
9
|
+
const qs_1 = __importDefault(require("qs"));
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
11
|
+
class baseBX24 {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.cbArray = [];
|
|
14
|
+
this.AUTH_CONNECTOR = "";
|
|
15
|
+
this.CLIENT_ID = "";
|
|
16
|
+
this.CLIENT_SECRET = "";
|
|
17
|
+
this.isInit = false;
|
|
18
|
+
this.APP_SID = false;
|
|
19
|
+
this.PATH = "/rest";
|
|
20
|
+
this.LANG = "";
|
|
21
|
+
this.AUTH_ID = "";
|
|
22
|
+
this.REFRESH_ID = "";
|
|
23
|
+
this.MEMBER_ID = "";
|
|
24
|
+
this.PLACEMENT = "";
|
|
25
|
+
this.IS_ADMIN = false;
|
|
26
|
+
this.AUTH_EXPIRES = 0;
|
|
27
|
+
this.arrEvents = [];
|
|
28
|
+
this.logger = console;
|
|
29
|
+
this.isReadyVal = false;
|
|
30
|
+
this.userOption = {
|
|
31
|
+
get: (name) => {
|
|
32
|
+
return this.USER_OPTIONS[name];
|
|
33
|
+
},
|
|
34
|
+
set: (name, value, cb) => {
|
|
35
|
+
this.USER_OPTIONS[name] = value;
|
|
36
|
+
this.sendMessage('setUserOption', { name: name, value: value }, cb);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
this.appOption = {
|
|
40
|
+
get: (name) => {
|
|
41
|
+
return this.APP_OPTIONS[name];
|
|
42
|
+
},
|
|
43
|
+
set: (name, value, cb) => {
|
|
44
|
+
if (this.isAdmin()) {
|
|
45
|
+
this.APP_OPTIONS[name] = value;
|
|
46
|
+
this.sendMessage('setAppOption', { name: name, value: value }, cb);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
console.error('Access denied!');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
this.selectAccess = (title, value, cb) => {
|
|
54
|
+
if (typeof (value) === 'function') {
|
|
55
|
+
cb = value;
|
|
56
|
+
value = [];
|
|
57
|
+
}
|
|
58
|
+
this.sendMessage('selectAccess', { value: value }, cb);
|
|
59
|
+
};
|
|
60
|
+
this.selectUser = (title, cb) => {
|
|
61
|
+
if (typeof (title) === 'function') {
|
|
62
|
+
cb = title;
|
|
63
|
+
title = '';
|
|
64
|
+
}
|
|
65
|
+
this.sendMessage('selectUser', { title: title, mult: false }, cb);
|
|
66
|
+
};
|
|
67
|
+
this.selectUsers = (title, cb) => {
|
|
68
|
+
if (typeof (title) !== 'string') {
|
|
69
|
+
cb = title;
|
|
70
|
+
title = '';
|
|
71
|
+
}
|
|
72
|
+
this.sendMessage('selectUser', { title: title, mult: true }, cb);
|
|
73
|
+
};
|
|
74
|
+
this.selectCRM = (params, cb) => {
|
|
75
|
+
this.sendMessage('selectCRM', {
|
|
76
|
+
entityType: params.entityType,
|
|
77
|
+
multiple: params.multiple,
|
|
78
|
+
value: params.value,
|
|
79
|
+
}, cb);
|
|
80
|
+
};
|
|
81
|
+
this.placement = {
|
|
82
|
+
info: () => ({
|
|
83
|
+
placement: this.PLACEMENT,
|
|
84
|
+
options: this.PLACEMENT_OPTIONS
|
|
85
|
+
}),
|
|
86
|
+
getInterface: (cb) => {
|
|
87
|
+
this.sendMessage('getInterface', {}, cb);
|
|
88
|
+
},
|
|
89
|
+
call: (cmd, params, cb) => {
|
|
90
|
+
this.sendMessage(cmd, params, cb);
|
|
91
|
+
},
|
|
92
|
+
bindEvent: (eventName, cb) => {
|
|
93
|
+
this.sendMessage('placementBindEvent', { event: eventName }, cb);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
this.DOMAIN = "";
|
|
97
|
+
this.PROTOCOL = 1;
|
|
98
|
+
}
|
|
99
|
+
isFunction(item) {
|
|
100
|
+
return item === null ? false : (typeof (item) == "function" || item instanceof Function);
|
|
101
|
+
}
|
|
102
|
+
addEvent(event, handler) {
|
|
103
|
+
this.arrEvents.push({
|
|
104
|
+
event: event,
|
|
105
|
+
handler: handler
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
emitEvent(event, params) {
|
|
109
|
+
const arrHandler = this.arrEvents.filter(el => { return el.event == event; });
|
|
110
|
+
for (const idx in arrHandler) {
|
|
111
|
+
setTimeout(() => {
|
|
112
|
+
arrHandler[idx].handler.call(this, params);
|
|
113
|
+
}, 10);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
uniqid() {
|
|
117
|
+
const charsList = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
118
|
+
let s = '';
|
|
119
|
+
for (let i = 0; i < 32; i++)
|
|
120
|
+
s += charsList[Math.round(Math.random() * (charsList.length - 1))];
|
|
121
|
+
return s;
|
|
122
|
+
}
|
|
123
|
+
setCallback(cb) {
|
|
124
|
+
const cbId = this.uniqid();
|
|
125
|
+
if (cb) {
|
|
126
|
+
this.cbArray.push({ uid: cbId, cb: cb });
|
|
127
|
+
}
|
|
128
|
+
return cbId;
|
|
129
|
+
}
|
|
130
|
+
doInit() {
|
|
131
|
+
this.emitEvent('init', this);
|
|
132
|
+
}
|
|
133
|
+
utilReady() {
|
|
134
|
+
if (document.readyState === "complete") {
|
|
135
|
+
return this.runReady();
|
|
136
|
+
}
|
|
137
|
+
let __readyHandler;
|
|
138
|
+
if (document.addEventListener) {
|
|
139
|
+
__readyHandler = () => {
|
|
140
|
+
document.removeEventListener("DOMContentLoaded", __readyHandler, false);
|
|
141
|
+
this.runReady();
|
|
142
|
+
};
|
|
143
|
+
document.addEventListener("DOMContentLoaded", __readyHandler, false);
|
|
144
|
+
window.addEventListener("load", () => { this.runReady(); }, false);
|
|
145
|
+
}
|
|
146
|
+
else if (document.attachEvent) {
|
|
147
|
+
__readyHandler = () => {
|
|
148
|
+
if (document.readyState === "complete") {
|
|
149
|
+
document.detachEvent("onreadystatechange", __readyHandler);
|
|
150
|
+
this.runReady();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
document.attachEvent("onreadystatechange", __readyHandler);
|
|
154
|
+
window.attachEvent("onload", () => { this.runReady(); });
|
|
155
|
+
}
|
|
156
|
+
this.utilReady = () => null;
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
runReady() {
|
|
160
|
+
if (!this.isReadyVal) {
|
|
161
|
+
if (!document.body) {
|
|
162
|
+
setTimeout(this.runReady, 15);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.isReadyVal = true;
|
|
166
|
+
this.ready = (handler) => {
|
|
167
|
+
if (typeof handler == 'function') {
|
|
168
|
+
setTimeout(handler, 10);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
this.emitEvent('ready');
|
|
172
|
+
}
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
callSuccess(xhr) {
|
|
176
|
+
return typeof xhr.status == 'undefined' || (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || xhr.status >= 400 && xhr.status < 500 || xhr.status === 1223 || xhr.status === 0;
|
|
177
|
+
}
|
|
178
|
+
call(url, config) {
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
config.auth = this.AUTH_ID;
|
|
181
|
+
if (this.AUTH_CONNECTOR) {
|
|
182
|
+
config.auth_connector = this.AUTH_CONNECTOR;
|
|
183
|
+
}
|
|
184
|
+
const options = {
|
|
185
|
+
method: 'POST',
|
|
186
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
187
|
+
data: qs_1.default.stringify(config),
|
|
188
|
+
url: url
|
|
189
|
+
};
|
|
190
|
+
(0, axios_1.default)(options).then(res => {
|
|
191
|
+
const data = res.data;
|
|
192
|
+
const result = new callResult_1.CallResult(data, config, res.status);
|
|
193
|
+
resolve(result);
|
|
194
|
+
})
|
|
195
|
+
.catch(err => {
|
|
196
|
+
if ((0, lodash_1.get)(err, ['response', 'data', 'error'], undefined) == 'expired_token' && !url.includes('oauth.bitrix.info/oauth/token/')) {
|
|
197
|
+
this.refreshAuth(() => {
|
|
198
|
+
this.call(url, config);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
reject((0, lodash_1.get)(err, ['response', 'data'], 'unknown error'));
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
getAuth() {
|
|
208
|
+
return (this.isInit && this.AUTH_EXPIRES > (new Date()).valueOf())
|
|
209
|
+
? { access_token: this.AUTH_ID, refresh_token: this.REFRESH_ID, expires_in: this.AUTH_EXPIRES, domain: this.DOMAIN, member_id: this.MEMBER_ID }
|
|
210
|
+
: false;
|
|
211
|
+
}
|
|
212
|
+
callBatch(cmd, cb, haltOnError = false) {
|
|
213
|
+
const startCb = cb;
|
|
214
|
+
const startHaltOnError = haltOnError;
|
|
215
|
+
if (typeof cb == 'boolean') {
|
|
216
|
+
haltOnError = cb;
|
|
217
|
+
cb = undefined;
|
|
218
|
+
}
|
|
219
|
+
const comands = {};
|
|
220
|
+
let cnt = 0;
|
|
221
|
+
for (const idx in cmd) {
|
|
222
|
+
let method = "", params = null;
|
|
223
|
+
if (Array.isArray(cmd[idx])) {
|
|
224
|
+
method = (0, lodash_1.get)(cmd[idx], 0, undefined);
|
|
225
|
+
params = (0, lodash_1.get)(cmd[idx], 1, {});
|
|
226
|
+
}
|
|
227
|
+
else if ((0, lodash_1.get)(cmd[idx], 'method', undefined) != undefined) {
|
|
228
|
+
method = (0, lodash_1.get)(cmd[idx], 'method', undefined);
|
|
229
|
+
params = (0, lodash_1.get)(cmd[idx], 'params', {});
|
|
230
|
+
}
|
|
231
|
+
if (method) {
|
|
232
|
+
cnt++;
|
|
233
|
+
comands[idx] = `${method}?${qs_1.default.stringify(params)}`;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (cnt > 0) {
|
|
237
|
+
const url = `http${this.PROTOCOL ? 's' : ''}://${this.DOMAIN}${this.PATH}/batch.json`;
|
|
238
|
+
const params = {
|
|
239
|
+
cmd: comands,
|
|
240
|
+
halt: haltOnError ? 1 : 0
|
|
241
|
+
};
|
|
242
|
+
if (!startCb) {
|
|
243
|
+
if (this.AUTH_EXPIRES < (new Date()).valueOf()) {
|
|
244
|
+
return new Promise((resolve, reject) => {
|
|
245
|
+
this.refreshAuth(() => {
|
|
246
|
+
this.callBatch(cmd, startHaltOnError).then(res => {
|
|
247
|
+
resolve(res);
|
|
248
|
+
})
|
|
249
|
+
.catch(err => {
|
|
250
|
+
reject(err);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
return new Promise((resolve, reject) => {
|
|
257
|
+
this.call(url, params).then(res => {
|
|
258
|
+
resolve(this.formatResultForBatch(res, params));
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
else if (typeof startCb == 'function') {
|
|
264
|
+
if (this.AUTH_EXPIRES < (new Date()).valueOf()) {
|
|
265
|
+
this.refreshAuth(() => {
|
|
266
|
+
this.callBatch(cmd, startCb, startHaltOnError);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
this.call(url, params).then(res => {
|
|
271
|
+
if (typeof cb == 'function')
|
|
272
|
+
cb(this.formatResultForBatch(res, params, cb));
|
|
273
|
+
})
|
|
274
|
+
.catch(err => {
|
|
275
|
+
throw err;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
formatResultForBatch(res, params, callback) {
|
|
285
|
+
const data = res.data();
|
|
286
|
+
const calls = params && params.cmd ? params.cmd : [];
|
|
287
|
+
const result = {};
|
|
288
|
+
for (const idx in calls) {
|
|
289
|
+
if (typeof data.result[idx] !== 'undefined' || typeof data.result_error[idx] !== 'undefined') {
|
|
290
|
+
const query = calls[idx].split('?');
|
|
291
|
+
result[idx] = new callResult_1.CallResult({
|
|
292
|
+
result: typeof data.result[idx] !== 'undefined' ? data.result[idx] : {},
|
|
293
|
+
error: data.result_error[idx] || undefined,
|
|
294
|
+
total: data.result_total[idx],
|
|
295
|
+
next: data.result_next[idx]
|
|
296
|
+
}, {
|
|
297
|
+
method: query[0],
|
|
298
|
+
data: query[1],
|
|
299
|
+
callback: callback
|
|
300
|
+
}, res.status);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
callMethod(method, params, cb) {
|
|
306
|
+
const url = `http${this.PROTOCOL ? 's' : ''}://${this.DOMAIN}${this.PATH}/${method}.json`;
|
|
307
|
+
if (!cb) {
|
|
308
|
+
if (this.AUTH_EXPIRES < (new Date()).valueOf()) {
|
|
309
|
+
return new Promise((resolve, reject) => {
|
|
310
|
+
this.refreshAuth(() => {
|
|
311
|
+
var _a;
|
|
312
|
+
(_a = this.callMethod(method, params)) === null || _a === void 0 ? void 0 : _a.then(res => {
|
|
313
|
+
resolve(res);
|
|
314
|
+
}).catch(err => {
|
|
315
|
+
reject(err);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
return this.call(url, params);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (cb) {
|
|
325
|
+
if (this.AUTH_EXPIRES < (new Date()).valueOf()) {
|
|
326
|
+
this.refreshAuth(() => {
|
|
327
|
+
this.callMethod(method, params, cb);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
this.call(url, params).then(res => {
|
|
332
|
+
cb(res);
|
|
333
|
+
})
|
|
334
|
+
.catch(err => {
|
|
335
|
+
cb(new callResult_1.CallResult(err, params, 500));
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
throw new Error("Incorrect params");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
isAdmin() {
|
|
344
|
+
return this.IS_ADMIN;
|
|
345
|
+
}
|
|
346
|
+
getLang() {
|
|
347
|
+
return this.LANG;
|
|
348
|
+
}
|
|
349
|
+
getDomain() {
|
|
350
|
+
return this.DOMAIN;
|
|
351
|
+
}
|
|
352
|
+
isReady() {
|
|
353
|
+
return this.isReadyVal;
|
|
354
|
+
}
|
|
355
|
+
ready(handler) {
|
|
356
|
+
this.addEvent('ready', handler);
|
|
357
|
+
}
|
|
358
|
+
getScrollSize() {
|
|
359
|
+
return {
|
|
360
|
+
scrollWidth: Math.max(document.documentElement.scrollWidth, document.documentElement.offsetWidth),
|
|
361
|
+
scrollHeight: Math.max(document.documentElement.scrollHeight, document.documentElement.offsetHeight)
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
init(callback) {
|
|
365
|
+
if (callback) {
|
|
366
|
+
this.addEvent('init', callback);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
install(callback) {
|
|
370
|
+
if (callback) {
|
|
371
|
+
this.addEvent('install', callback);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
callBind(event, handler, auth_type, callback) {
|
|
375
|
+
if (!this.isInit) {
|
|
376
|
+
this.init(() => {
|
|
377
|
+
this.callBind(event, handler, auth_type, callback);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
else if (this.isAdmin()) {
|
|
381
|
+
const params = {
|
|
382
|
+
event: event || '',
|
|
383
|
+
handler: handler || '',
|
|
384
|
+
auth_type: (typeof auth_type == 'undefined') ? 0 : auth_type
|
|
385
|
+
};
|
|
386
|
+
return this.callMethod('event.bind', params, callback);
|
|
387
|
+
}
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
installFinish() {
|
|
391
|
+
this.sendMessage('setInstallFinish', {});
|
|
392
|
+
}
|
|
393
|
+
resizeWindow(width, height, cb) {
|
|
394
|
+
if (width > 0 && height > 0) {
|
|
395
|
+
this.sendMessage('resizeWindow', { width: width, height: height }, cb);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
fitWindow(cb) {
|
|
399
|
+
this.sendMessage('resizeWindow', {
|
|
400
|
+
width: '100%', height: this.getScrollSize().scrollHeight
|
|
401
|
+
}, cb);
|
|
402
|
+
}
|
|
403
|
+
closeApplication(cb) {
|
|
404
|
+
this.sendMessage('closeApplication', {}, cb);
|
|
405
|
+
}
|
|
406
|
+
reloadWindow(cb) {
|
|
407
|
+
this.sendMessage('reloadWindow', {}, cb);
|
|
408
|
+
}
|
|
409
|
+
setTitle(title, cb) {
|
|
410
|
+
this.sendMessage('setTitle', { title: title }, cb);
|
|
411
|
+
}
|
|
412
|
+
scrollParentWindow(scroll, cb) {
|
|
413
|
+
if (scroll > 0) {
|
|
414
|
+
this.sendMessage('setScroll', { scroll: scroll }, cb);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
exports.baseBX24 = baseBX24;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CallResult = void 0;
|
|
4
|
+
class CallResult {
|
|
5
|
+
constructor(data, config, status) {
|
|
6
|
+
this.answer = data;
|
|
7
|
+
this.query = config;
|
|
8
|
+
this.status = status;
|
|
9
|
+
if (typeof this.answer.next != 'undefined') {
|
|
10
|
+
this.answer.next = parseInt(this.answer.next);
|
|
11
|
+
}
|
|
12
|
+
if (typeof this.answer.error != 'undefined') {
|
|
13
|
+
this.answer.ex = new ajaxError(this.status, typeof this.answer.error == 'string' ? this.answer : this.answer.error);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
data() {
|
|
17
|
+
return this.answer.result;
|
|
18
|
+
}
|
|
19
|
+
error() {
|
|
20
|
+
return this.answer.ex;
|
|
21
|
+
}
|
|
22
|
+
error_description() {
|
|
23
|
+
return this.answer.error_description;
|
|
24
|
+
}
|
|
25
|
+
more() {
|
|
26
|
+
return !isNaN(this.answer.next);
|
|
27
|
+
}
|
|
28
|
+
total() {
|
|
29
|
+
return parseInt(this.answer.total);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.CallResult = CallResult;
|
|
33
|
+
class ajaxError {
|
|
34
|
+
constructor(status, ex) {
|
|
35
|
+
this.status = status;
|
|
36
|
+
this.ex = ex;
|
|
37
|
+
}
|
|
38
|
+
getError() {
|
|
39
|
+
return this.ex;
|
|
40
|
+
}
|
|
41
|
+
getStatus() {
|
|
42
|
+
return this.status;
|
|
43
|
+
}
|
|
44
|
+
toString() {
|
|
45
|
+
return this.ex.error +
|
|
46
|
+
(this.ex.error_description ? ': ' + this.ex.error_description : '') +
|
|
47
|
+
' (' + this.status + ')';
|
|
48
|
+
}
|
|
49
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BatchHelper = exports.BX24Dev = exports.BX24Server = exports.BX24 = void 0;
|
|
4
|
+
var BX24_1 = require("./BX24");
|
|
5
|
+
Object.defineProperty(exports, "BX24", { enumerable: true, get: function () { return BX24_1.BX24; } });
|
|
6
|
+
var BX24Server_1 = require("./BX24Server");
|
|
7
|
+
Object.defineProperty(exports, "BX24Server", { enumerable: true, get: function () { return BX24Server_1.BX24Server; } });
|
|
8
|
+
var BX24Dev_1 = require("./BX24Dev");
|
|
9
|
+
Object.defineProperty(exports, "BX24Dev", { enumerable: true, get: function () { return BX24Dev_1.BX24Dev; } });
|
|
10
|
+
var BatchHelper_1 = require("./BatchHelper");
|
|
11
|
+
Object.defineProperty(exports, "BatchHelper", { enumerable: true, get: function () { return BatchHelper_1.BatchHelper; } });
|
package/index.ts
ADDED
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slavmak2486/bx24ts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Library for bitrix24",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "ts-node index.ts",
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "jest"
|
|
11
|
+
},
|
|
12
|
+
"bin": {
|
|
13
|
+
"github-pages-commit": "bin/test.js"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"bitrix24",
|
|
17
|
+
"b24",
|
|
18
|
+
"rest"
|
|
19
|
+
],
|
|
20
|
+
"author": "dc",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/jest": "^27.4.1",
|
|
24
|
+
"@types/lodash": "^4.14.179",
|
|
25
|
+
"@types/qs": "^6.9.7",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^5.13.0",
|
|
27
|
+
"@typescript-eslint/parser": "^5.13.0",
|
|
28
|
+
"axios-mock-adapter": "^1.20.0",
|
|
29
|
+
"eslint": "^8.10.0",
|
|
30
|
+
"jest": "^27.5.1",
|
|
31
|
+
"nodemon": "^2.0.15",
|
|
32
|
+
"ts-jest": "^27.1.3",
|
|
33
|
+
"ts-node": "^10.6.0",
|
|
34
|
+
"typescript": "^4.6.2"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"axios": "^0.26.0",
|
|
38
|
+
"lodash": "^4.17.21",
|
|
39
|
+
"primeflex": "^3.1.3",
|
|
40
|
+
"qs": "^6.10.3"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {BatchHelper} from '../BatchHelper';
|
|
2
|
+
import {batchCmdElement} from '../types/batchElement';
|
|
3
|
+
|
|
4
|
+
test('batchHelper test 50', async () => {
|
|
5
|
+
const testBatch:batchCmdElement={};
|
|
6
|
+
for (let idx=0; idx<50; idx++){
|
|
7
|
+
testBatch[`test_${idx}`]=['method', {params:true}];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let helper=new BatchHelper();
|
|
11
|
+
const arrBatch=helper.addToBatch(testBatch).getArrBatches();
|
|
12
|
+
expect(Array.isArray(arrBatch)).toBe(true);
|
|
13
|
+
expect(JSON.stringify(arrBatch)).toBe(JSON.stringify([testBatch]));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('batchHelper test 100', async () => {
|
|
17
|
+
let helper=new BatchHelper();
|
|
18
|
+
let testBatch:batchCmdElement={};
|
|
19
|
+
for (let idx=0; idx<50; idx++){
|
|
20
|
+
testBatch[`test_${idx}`]=['method', {params:true}];
|
|
21
|
+
}
|
|
22
|
+
helper.addToBatch(testBatch);
|
|
23
|
+
let testBatch2:batchCmdElement={};
|
|
24
|
+
for (let idx=0; idx<50; idx++){
|
|
25
|
+
testBatch2[`test2_${idx}`]=['method', {params:true}];
|
|
26
|
+
}
|
|
27
|
+
const arrBatch=helper.addToBatch(testBatch2).getArrBatches();
|
|
28
|
+
expect(Array.isArray(arrBatch)).toBe(true);
|
|
29
|
+
expect(JSON.stringify(arrBatch)).toBe(JSON.stringify([testBatch, testBatch2]));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('batchHelper test 3', async () => {
|
|
33
|
+
let helper=new BatchHelper();
|
|
34
|
+
helper.addToBatch({test_1:['method', {params:true}]});
|
|
35
|
+
helper.addToBatch({test_2:['method', {params:true}]});
|
|
36
|
+
helper.addToBatch({test_3:['method', {params:true}]});
|
|
37
|
+
const arrBatch=helper.getArrBatches();
|
|
38
|
+
|
|
39
|
+
const result=[{
|
|
40
|
+
test_1:['method', {params:true}],
|
|
41
|
+
test_2:['method', {params:true}],
|
|
42
|
+
test_3:['method', {params:true}]
|
|
43
|
+
}];
|
|
44
|
+
|
|
45
|
+
expect(Array.isArray(arrBatch)).toBe(true);
|
|
46
|
+
expect(JSON.stringify(arrBatch)).toBe(JSON.stringify(result));
|
|
47
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {BX24Dev} from '../index';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import MockAdapter from "axios-mock-adapter";
|
|
4
|
+
import batch from './mocks/batch'
|
|
5
|
+
import dealList from './mocks/dealList';
|
|
6
|
+
import leadList from './mocks/leadList';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
let mock:MockAdapter;
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
mock = new MockAdapter(axios);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
mock.reset();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('Test callMethod Promise', async () => {
|
|
20
|
+
mock.onPost('https://oauth.bitrix.info/oauth/token/').reply(201, {
|
|
21
|
+
access_token: 'fd3228620053e0720050316c00000035403803bb4a6d911ce347871d1b0234b40e5ddf',
|
|
22
|
+
expires: Math.ceil((new Date().valueOf()+3*60*1000)/1000),
|
|
23
|
+
expires_in: 3600,
|
|
24
|
+
scope: 'app',
|
|
25
|
+
domain: 'oauth.bitrix.info',
|
|
26
|
+
server_endpoint: 'https://oauth.bitrix.info/rest/',
|
|
27
|
+
status: 'F',
|
|
28
|
+
client_endpoint: 'https://d-clouds.bitrix24.ru/rest/',
|
|
29
|
+
member_id: 'a76802b13260c2689c0bf5ac801b714c',
|
|
30
|
+
user_id: 53,
|
|
31
|
+
refresh_token: 'edb14f620053e0720050316c000000354038034cf993a3d9379c29b612936b7112a9e2'
|
|
32
|
+
});
|
|
33
|
+
mock.onPost('https://d-clouds.bitrix24.ru/rest/batch.json').reply(200, batch);
|
|
34
|
+
|
|
35
|
+
const bx24=new BX24Dev({
|
|
36
|
+
access_token: "b0a727620053e0720050316c00000035403803b931cc9602df09ed4f1eb241c4a94505",
|
|
37
|
+
domain: "d-clouds.bitrix24.ru",
|
|
38
|
+
expires_in: 1614674265580,
|
|
39
|
+
member_id: "a76802b13260c2689c0bf5ac801b714c",
|
|
40
|
+
refresh_token: "a0264f620053e0720050316c00000035403803d6bcb8f3b4d91a311957f0b24c3b3b2e",
|
|
41
|
+
client_id:'app.60791f505183d4.54860773',
|
|
42
|
+
client_secret:'FtMCVDUy5NE28jk0AgqykSlddlAFymlGI6jY0ALbJqlORY0t2m'
|
|
43
|
+
});
|
|
44
|
+
const promise= bx24.callBatch({
|
|
45
|
+
getDeals:['crm.deal.list', {}],
|
|
46
|
+
getLeads:['crm.lead.list', {}],
|
|
47
|
+
getByID:['crm.deal.get', {id:123123}]
|
|
48
|
+
});
|
|
49
|
+
// .data()).toEqual();
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const result=await promise;
|
|
53
|
+
expect(result['getDeals'].data()).toEqual(dealList.result);
|
|
54
|
+
expect(result['getLeads'].data()).toEqual(leadList.result);
|
|
55
|
+
|
|
56
|
+
expect(result['getDeals'].total()).toEqual(dealList.total);
|
|
57
|
+
expect(result['getLeads'].total()).toEqual(leadList.total);
|
|
58
|
+
|
|
59
|
+
expect(result['getDeals'].error()).toBeUndefined();
|
|
60
|
+
expect(result['getLeads'].error()).toBeUndefined();
|
|
61
|
+
expect(result.getByID.error()).toEqual({
|
|
62
|
+
status: 200,
|
|
63
|
+
ex: { error: '', error_description: 'Not found' }
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
// expect(result).toEqual(batchFormated);
|
|
68
|
+
// expect(result.total()).toEqual(dealList.total);
|
|
69
|
+
});
|