@slavmak2486/bx24ts 1.0.8 → 1.1.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/BX24.ts +15 -0
- package/BX24Dev.ts +16 -14
- package/BX24Server.ts +15 -7
- package/auth.ts +13 -0
- package/base/BX24.ts +163 -67
- package/batchAppOption.ts +40 -0
- package/callResult.ts +34 -20
- package/dealList.ts +2214 -0
- package/dist/BX24.js +13 -0
- package/dist/BX24Dev.js +15 -17
- package/dist/BX24Server.js +14 -5
- package/dist/auth.js +15 -0
- package/dist/base/BX24.js +117 -43
- package/dist/batchAppOption.js +42 -0
- package/dist/callResult.js +26 -5
- package/dist/dealList.js +2216 -0
- package/dist/tests/constants/authExample.js +11 -0
- package/package.json +34 -36
- package/tests/base/stringify.test.ts +55 -0
- package/tests/batchHelper.test.ts +67 -5
- package/tests/callBatch.test.ts +39 -41
- package/tests/callBatchCb.test.ts +35 -47
- package/tests/callMethod.test.ts +17 -26
- package/tests/callMethodCb.test.ts +36 -37
- package/tests/constants/authExample.ts +9 -0
- package/tests/mocks/auth.ts +13 -0
- package/tests/mocks/batchAppOption.ts +40 -0
- package/tests/mocks/dealList.ts +2314 -2212
- package/tests/mocks/dealListPage2.ts +2316 -0
package/BX24.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { reject } from "lodash";
|
|
1
2
|
import { baseBX24 } from "./base/BX24";
|
|
2
3
|
import { AuthBase } from "./types/authBase";
|
|
4
|
+
import { getAuth } from "./types/getAuth";
|
|
3
5
|
|
|
4
6
|
export class BX24 extends baseBX24{
|
|
5
7
|
constructor(cb?:(params:any)=>void){
|
|
@@ -87,6 +89,19 @@ export class BX24 extends baseBX24{
|
|
|
87
89
|
}
|
|
88
90
|
}
|
|
89
91
|
|
|
92
|
+
refreshAuthAsync(): Promise<getAuth> {
|
|
93
|
+
return new Promise((resolve, reject)=>{
|
|
94
|
+
let isExeted=false;
|
|
95
|
+
this.refreshAuth(newAuth=>{
|
|
96
|
+
isExeted=true;
|
|
97
|
+
resolve(newAuth);
|
|
98
|
+
})
|
|
99
|
+
setTimeout(() => {
|
|
100
|
+
if (!isExeted) reject('refresh auth timeout!')
|
|
101
|
+
}, 5000);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
90
105
|
refreshAuth(cb?:(params: any) => void){
|
|
91
106
|
this.sendMessage('refreshAuth', {}, (result:any)=>{
|
|
92
107
|
this.AUTH_ID=result.AUTH_ID;
|
package/BX24Dev.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { BX24Server } from "./BX24Server";
|
|
2
|
-
import qs from 'qs';
|
|
3
2
|
import { set as __set } from "lodash";
|
|
4
3
|
import { AuthBaseDev } from "./types/authBaseDev";
|
|
5
4
|
|
|
@@ -13,10 +12,12 @@ export class BX24Dev extends BX24Server{
|
|
|
13
12
|
getAppOption:['app.option.get', {}],
|
|
14
13
|
getUserOption:['user.option.get', {}]
|
|
15
14
|
}, ress=>{
|
|
16
|
-
|
|
15
|
+
let idx: keyof typeof ress;
|
|
16
|
+
let textError='';
|
|
17
|
+
for (idx in ress){
|
|
17
18
|
const res=ress[idx];
|
|
18
19
|
if (res.error()){
|
|
19
|
-
|
|
20
|
+
textError+=`Init error. Check auth data. ${res.error()} for ${idx}\n`;
|
|
20
21
|
}
|
|
21
22
|
else{
|
|
22
23
|
if (idx=='getAppOption'){
|
|
@@ -27,7 +28,9 @@ export class BX24Dev extends BX24Server{
|
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
+
if (textError)
|
|
32
|
+
console.error(textError);
|
|
33
|
+
|
|
31
34
|
if (cb){
|
|
32
35
|
cb();
|
|
33
36
|
}
|
|
@@ -36,13 +39,12 @@ export class BX24Dev extends BX24Server{
|
|
|
36
39
|
});
|
|
37
40
|
|
|
38
41
|
if (new Date().getTime()-5*60*1000>this.AUTH_EXPIRES && !param.noUpdateRefresh){
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
} catch (error) {
|
|
42
|
+
this.refreshAuthAsync().then(()=>{
|
|
43
|
+
this.doInit();
|
|
44
|
+
})
|
|
45
|
+
.catch((error:Error)=>{
|
|
44
46
|
this.logger.error(error);
|
|
45
|
-
}
|
|
47
|
+
});
|
|
46
48
|
}
|
|
47
49
|
else{
|
|
48
50
|
this.doInit();
|
|
@@ -93,10 +95,10 @@ export class BX24Dev extends BX24Server{
|
|
|
93
95
|
|
|
94
96
|
private getPlacementOptions(){
|
|
95
97
|
const result:{[key:string]:any}={};
|
|
96
|
-
const url=new URL(location.href) as any;
|
|
97
|
-
const entrity=url.searchParams.entries();
|
|
98
|
+
const url=typeof location!=='undefined'?new URL(location.href) as any:undefined;
|
|
99
|
+
const entrity=url?url.searchParams.entries():undefined;
|
|
98
100
|
const regexp=/\[([^\]]*)]/g;
|
|
99
|
-
let done=false;
|
|
101
|
+
let done=entrity?false:true;
|
|
100
102
|
while(!done){
|
|
101
103
|
const el=entrity.next();
|
|
102
104
|
done=el.done===true?true:false;
|
|
@@ -132,7 +134,7 @@ export class BX24Dev extends BX24Server{
|
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
|
|
135
|
-
window.open(`?${
|
|
137
|
+
window.open(`?${this.getHttpString(params)}`);
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
openPath(path:string, cb?:(params:any)=>void):void{
|
package/BX24Server.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { baseBX24 } from "./base/BX24";
|
|
2
2
|
import { AuthBaseServe, logger } from "./types/authBaseServe";
|
|
3
3
|
import {getAuth} from './types/getAuth';
|
|
4
|
-
import { get as __get } from "lodash";
|
|
5
4
|
import { CallResult } from "./callResult";
|
|
6
5
|
import { BitrixEvent } from "./types/bitrixEvent";
|
|
7
6
|
|
|
@@ -116,20 +115,29 @@ export class BX24Server extends baseBX24{
|
|
|
116
115
|
});
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
refreshAuthAsync():Promise<getAuth
|
|
118
|
+
refreshAuthAsync():Promise<getAuth> {
|
|
120
119
|
return new Promise((resolve, reject) => {
|
|
121
120
|
this.call("https://oauth.bitrix.info/oauth/token/", {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
method:'refresh_token',
|
|
122
|
+
data:{
|
|
123
|
+
grant_type: 'refresh_token',
|
|
124
|
+
client_id: this.CLIENT_ID,
|
|
125
|
+
client_secret: this.CLIENT_SECRET,
|
|
126
|
+
refresh_token: this.REFRESH_ID
|
|
127
|
+
}
|
|
126
128
|
})
|
|
127
129
|
.then(data=>{
|
|
128
130
|
this.AUTH_ID=data.answer.access_token;
|
|
129
131
|
this.REFRESH_ID=data.answer.refresh_token;
|
|
130
132
|
this.AUTH_EXPIRES=data.answer.expires*1000;
|
|
131
133
|
this.emitEvent('refreshAuth', this.getAuth());
|
|
132
|
-
|
|
134
|
+
const authArr=this.getAuth();
|
|
135
|
+
if (authArr===false){
|
|
136
|
+
reject('Refresh auth undefined error!');
|
|
137
|
+
}
|
|
138
|
+
else{
|
|
139
|
+
resolve(authArr);
|
|
140
|
+
}
|
|
133
141
|
})
|
|
134
142
|
.catch(err=>{
|
|
135
143
|
reject (err);
|
package/auth.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
access_token: 'a123b123c123d123e123f123g123h123a123b123c123d123e123f123g123h123456789',
|
|
3
|
+
expires: Math.ceil((new Date().valueOf()+3*60*1000)/1000),
|
|
4
|
+
expires_in: 3600,
|
|
5
|
+
scope: 'app',
|
|
6
|
+
domain: 'oauth.bitrix.info',
|
|
7
|
+
server_endpoint: 'https://oauth.bitrix.info/rest/',
|
|
8
|
+
status: 'F',
|
|
9
|
+
client_endpoint: 'https://example.bitrix24.ru/rest/',
|
|
10
|
+
member_id: 'a123b123c123d123e123f123g123h123',
|
|
11
|
+
user_id: 1,
|
|
12
|
+
refresh_token: 'abc123456789d123456789ef123456789g123456789h123456789i123456789j123456'
|
|
13
|
+
}
|
package/base/BX24.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { AuthBase } from "../types/authBase";
|
|
2
1
|
import { batchCmdElement } from "../types/batchElement";
|
|
3
2
|
import { eventElement } from "../types/eventElement";
|
|
4
3
|
|
|
5
|
-
import axios, {
|
|
4
|
+
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
6
5
|
import { CallResult } from "../callResult";
|
|
7
|
-
import
|
|
8
|
-
import {get as __get} from 'lodash'
|
|
6
|
+
import {cloneDeep, get as __get} from 'lodash'
|
|
9
7
|
import { logger } from "../types/authBaseServe";
|
|
8
|
+
import { getAuth } from "../types/getAuth";
|
|
10
9
|
|
|
11
10
|
// проблемы декларирования методов HTMLElement
|
|
12
11
|
declare global{
|
|
@@ -57,6 +56,23 @@ export abstract class baseBX24{
|
|
|
57
56
|
|
|
58
57
|
isReadyVal=false; //check
|
|
59
58
|
|
|
59
|
+
getHttpString(value:any, prefix=''):string{
|
|
60
|
+
if (value instanceof Date){
|
|
61
|
+
return prefix+'='+value.toISOString();
|
|
62
|
+
}
|
|
63
|
+
else if (typeof value=='object'){
|
|
64
|
+
const resultObj=[];
|
|
65
|
+
for (const field in value){
|
|
66
|
+
resultObj.push(this.getHttpString(value[field], prefix+`${prefix!=''?'[':""}${field}${prefix!=''?']':''}`));
|
|
67
|
+
}
|
|
68
|
+
return resultObj.join('&');
|
|
69
|
+
}
|
|
70
|
+
else if (prefix!=''){
|
|
71
|
+
return prefix+'='+value;
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
|
|
61
77
|
isFunction(item:any){
|
|
62
78
|
return item === null ? false : (typeof (item) == "function" || item instanceof Function);
|
|
@@ -104,7 +120,7 @@ export abstract class baseBX24{
|
|
|
104
120
|
}
|
|
105
121
|
|
|
106
122
|
abstract sendMessage(cmd:string, params:any, cb?:(params:any)=>void):void
|
|
107
|
-
|
|
123
|
+
|
|
108
124
|
constructor(){
|
|
109
125
|
this.DOMAIN="";
|
|
110
126
|
this.PROTOCOL=1;
|
|
@@ -193,32 +209,41 @@ export abstract class baseBX24{
|
|
|
193
209
|
return;
|
|
194
210
|
}
|
|
195
211
|
|
|
196
|
-
abstract refreshAuth(cb?:(params:any)=>void):void
|
|
212
|
+
abstract refreshAuth(cb?:(params:any)=>void):void;
|
|
213
|
+
abstract refreshAuthAsync():Promise<getAuth>;
|
|
197
214
|
|
|
198
215
|
protected callSuccess(xhr:AxiosResponse):boolean{
|
|
199
216
|
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;
|
|
200
217
|
}
|
|
201
218
|
|
|
202
|
-
protected call(url:string, config:
|
|
219
|
+
protected call(url:string, config:{
|
|
220
|
+
method:string,
|
|
221
|
+
data:any,
|
|
222
|
+
callback?:(params: any)=>void
|
|
223
|
+
}):Promise<CallResult>{
|
|
203
224
|
return new Promise((resolve, reject)=>{
|
|
204
|
-
config.
|
|
225
|
+
const params=cloneDeep(config.data);
|
|
226
|
+
params.auth=this.AUTH_ID;
|
|
205
227
|
if (this.AUTH_CONNECTOR){
|
|
206
|
-
|
|
228
|
+
params.auth_connector=this.AUTH_CONNECTOR;
|
|
207
229
|
}
|
|
208
230
|
const options:AxiosRequestConfig = {
|
|
209
231
|
method: 'POST',
|
|
210
232
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
211
|
-
data:
|
|
233
|
+
data: this.getHttpString(params),
|
|
212
234
|
url:url
|
|
213
235
|
};
|
|
214
236
|
|
|
215
237
|
axios(options).then(res=>{
|
|
216
238
|
const data = res.data;
|
|
217
|
-
const result = new CallResult(data, config, res.status);
|
|
239
|
+
const result = new CallResult(data, config, this, res.status);
|
|
218
240
|
resolve(result);
|
|
219
241
|
})
|
|
220
|
-
.catch(err=>{
|
|
221
|
-
if (
|
|
242
|
+
.catch((err:AxiosError)=>{
|
|
243
|
+
if (!err?.response?.data){
|
|
244
|
+
reject(err);
|
|
245
|
+
}
|
|
246
|
+
else if (__get(err, ['response','data','error'], undefined)=='expired_token'&&!url.includes('oauth.bitrix.info/oauth/token/')){
|
|
222
247
|
try {
|
|
223
248
|
this.refreshAuth(()=>{
|
|
224
249
|
this.call(url, config).then(resolve).catch(reject);
|
|
@@ -228,7 +253,7 @@ export abstract class baseBX24{
|
|
|
228
253
|
}
|
|
229
254
|
}
|
|
230
255
|
else{
|
|
231
|
-
reject(__get(err, ['response','data'],
|
|
256
|
+
reject(__get(err, ['response','data'], err.message));
|
|
232
257
|
}
|
|
233
258
|
});
|
|
234
259
|
});
|
|
@@ -240,25 +265,37 @@ export abstract class baseBX24{
|
|
|
240
265
|
: false;
|
|
241
266
|
}
|
|
242
267
|
|
|
243
|
-
callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
|
|
244
|
-
callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
|
|
245
|
-
callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
|
|
246
|
-
callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
|
|
247
|
-
callBatch(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
268
|
+
// callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
|
|
269
|
+
// callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
|
|
270
|
+
// callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
|
|
271
|
+
// callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
|
|
272
|
+
// callBatch(
|
|
273
|
+
// cmd:batchCmdElement,
|
|
274
|
+
// cb?:((params:{[key:string|number]:CallResult})=>void)|boolean,
|
|
275
|
+
// haltOnError=false):void|Promise<{[key:string|number]:CallResult
|
|
276
|
+
// }>
|
|
277
|
+
callBatch<T extends batchCmdElement>(cmd:T, haltOnError?:boolean):Promise<{[key in keyof T]:CallResult}>
|
|
278
|
+
callBatch<T extends batchCmdElement>(cmd:T, cb:(params:{[key in keyof T]:CallResult})=>void):void
|
|
279
|
+
callBatch<T extends batchCmdElement>(cmd:T, cb:(params:{[key in keyof T]:CallResult})=>void, haltOnError:boolean):void
|
|
280
|
+
callBatch<T extends batchCmdElement>(
|
|
281
|
+
cmd:T,
|
|
282
|
+
cb?:((params:{[key in keyof T]:CallResult})=>void)|boolean,
|
|
283
|
+
haltOnError=false
|
|
284
|
+
):void|Promise<{[key in keyof T]:CallResult}>
|
|
285
|
+
{
|
|
252
286
|
const startCb=cb;
|
|
253
287
|
const startHaltOnError=haltOnError;
|
|
254
288
|
if (typeof cb == 'boolean'){
|
|
255
289
|
haltOnError=cb;
|
|
256
290
|
cb=undefined;
|
|
257
291
|
}
|
|
292
|
+
else{
|
|
293
|
+
cb=cb as (params:{[key in keyof T]:CallResult})=>void;
|
|
294
|
+
}
|
|
258
295
|
|
|
259
|
-
const comands:{
|
|
260
|
-
[key
|
|
261
|
-
}
|
|
296
|
+
const comands:Partial<{
|
|
297
|
+
[key in keyof T]:string
|
|
298
|
+
}>={};
|
|
262
299
|
let cnt=0;
|
|
263
300
|
|
|
264
301
|
for(const idx in cmd){
|
|
@@ -274,56 +311,80 @@ export abstract class baseBX24{
|
|
|
274
311
|
if(method)
|
|
275
312
|
{
|
|
276
313
|
cnt++;
|
|
277
|
-
comands[idx] = `${method}?${
|
|
314
|
+
comands[idx] = `${method}?${this.getHttpString(params)}`;
|
|
278
315
|
}
|
|
279
316
|
}
|
|
280
317
|
|
|
281
318
|
if (cnt>0){
|
|
282
319
|
const url=`http${this.PROTOCOL?'s':''}://${this.DOMAIN}${this.PATH}/batch.json`;
|
|
283
320
|
const params={
|
|
284
|
-
cmd:comands
|
|
321
|
+
cmd:comands as {
|
|
322
|
+
[key in keyof T]:string
|
|
323
|
+
},
|
|
285
324
|
halt:haltOnError?1:0
|
|
286
325
|
};
|
|
287
326
|
|
|
288
327
|
if (!startCb){
|
|
289
328
|
if (this.AUTH_EXPIRES<(new Date()).valueOf()){
|
|
290
329
|
return new Promise((resolve, reject)=>{
|
|
291
|
-
|
|
292
|
-
this.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
} catch (error) {
|
|
330
|
+
this.refreshAuthAsync().then(()=>{
|
|
331
|
+
this.callBatch(cmd, startHaltOnError).then(res=>{
|
|
332
|
+
resolve(res);
|
|
333
|
+
})
|
|
334
|
+
.catch(err=>{
|
|
335
|
+
reject(err);
|
|
336
|
+
})
|
|
337
|
+
})
|
|
338
|
+
.catch((error:Error)=>{
|
|
301
339
|
reject(error);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
});
|
|
340
|
+
});
|
|
341
|
+
});
|
|
305
342
|
}
|
|
306
343
|
else{
|
|
307
344
|
return new Promise((resolve, reject)=>{
|
|
308
|
-
this.call(url,
|
|
309
|
-
|
|
345
|
+
this.call(url, {
|
|
346
|
+
method:'batch',
|
|
347
|
+
data: params,
|
|
348
|
+
}).then(res=>{
|
|
349
|
+
resolve(this.formatResultForBatch(res, cmd));
|
|
310
350
|
})
|
|
351
|
+
.catch(reject)
|
|
311
352
|
});
|
|
312
353
|
}
|
|
313
354
|
}
|
|
314
355
|
else if (typeof startCb=='function'){
|
|
315
356
|
if (this.AUTH_EXPIRES<(new Date()).valueOf()){
|
|
316
|
-
this.
|
|
357
|
+
this.refreshAuthAsync().then(()=>{
|
|
317
358
|
this.callBatch(cmd, startCb, startHaltOnError);
|
|
359
|
+
})
|
|
360
|
+
.catch(error=>{
|
|
361
|
+
console.error(error);
|
|
318
362
|
});
|
|
319
363
|
}
|
|
320
364
|
else{
|
|
321
|
-
this.call(url,
|
|
365
|
+
this.call(url, {
|
|
366
|
+
method:'batch',
|
|
367
|
+
data:params,
|
|
368
|
+
callback: startCb
|
|
369
|
+
}).then(res=>{
|
|
322
370
|
if (typeof cb =='function')
|
|
323
|
-
cb(this.formatResultForBatch(res,
|
|
371
|
+
cb(this.formatResultForBatch(res, cmd, cb));
|
|
324
372
|
})
|
|
325
373
|
.catch(err=>{
|
|
326
|
-
|
|
374
|
+
if (err instanceof Error){
|
|
375
|
+
throw err;
|
|
376
|
+
}
|
|
377
|
+
const result=this.formatResultForBatch(
|
|
378
|
+
new CallResult(err, {
|
|
379
|
+
method:'batch',
|
|
380
|
+
data:params
|
|
381
|
+
}, this, 500),
|
|
382
|
+
cmd,
|
|
383
|
+
cb as (params:any)=>void
|
|
384
|
+
);
|
|
385
|
+
if (typeof cb ==='function')
|
|
386
|
+
cb(result);
|
|
387
|
+
return false;
|
|
327
388
|
})
|
|
328
389
|
}
|
|
329
390
|
}
|
|
@@ -331,32 +392,54 @@ export abstract class baseBX24{
|
|
|
331
392
|
else{
|
|
332
393
|
return;
|
|
333
394
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
395
|
}
|
|
337
396
|
|
|
338
|
-
formatResultForBatch
|
|
397
|
+
formatResultForBatch<T extends CallResult, R extends batchCmdElement>(
|
|
398
|
+
res:T,
|
|
399
|
+
calls:R,
|
|
400
|
+
callback?:(params:any)=>void)
|
|
401
|
+
:{[key in keyof R]:CallResult}
|
|
402
|
+
{
|
|
339
403
|
const data = res.data();
|
|
340
|
-
const calls=params&¶ms.cmd?params.cmd:[];
|
|
341
404
|
|
|
342
|
-
const result:{[key
|
|
405
|
+
const result:Partial<{[key in keyof R]:CallResult}>={};
|
|
343
406
|
for(const idx in calls){
|
|
344
|
-
|
|
345
|
-
|
|
407
|
+
const cmd=calls[idx];
|
|
408
|
+
if (data?.result?.[idx]|| data?.result_error?.[idx]){
|
|
346
409
|
result[idx]=new CallResult({
|
|
347
|
-
result:
|
|
410
|
+
result: data.result?.[idx]||{},
|
|
348
411
|
error:data.result_error[idx]||undefined,
|
|
349
412
|
total:data.result_total[idx],
|
|
413
|
+
time:data.result_time[idx],
|
|
350
414
|
next: data.result_next[idx]
|
|
351
415
|
}, {
|
|
352
|
-
method:
|
|
353
|
-
data:
|
|
416
|
+
method:Array.isArray(cmd)?cmd[0]:cmd?.method,
|
|
417
|
+
data: Array.isArray(cmd)?cmd[1]:cmd?.params,
|
|
354
418
|
callback:callback
|
|
355
|
-
}, res.status)
|
|
419
|
+
}, this, res.status)
|
|
420
|
+
}
|
|
421
|
+
else{
|
|
422
|
+
result[idx]={
|
|
423
|
+
data:()=>({}),
|
|
424
|
+
total:()=>0,
|
|
425
|
+
error_description:()=>'Incorrect response from bitrix24',
|
|
426
|
+
answer:data?.result?.[idx],
|
|
427
|
+
query: {
|
|
428
|
+
method:Array.isArray(cmd)?cmd[0]:cmd?.method,
|
|
429
|
+
data: Array.isArray(cmd)?cmd[1]:cmd?.params,
|
|
430
|
+
callback:callback
|
|
431
|
+
},
|
|
432
|
+
next:()=>false,
|
|
433
|
+
bx24:this,
|
|
434
|
+
time:()=>({}),
|
|
435
|
+
status:res.status,
|
|
436
|
+
more:()=>false,
|
|
437
|
+
error:()=>`Incorect response from bitrix24`
|
|
438
|
+
};
|
|
356
439
|
}
|
|
357
440
|
}
|
|
358
441
|
|
|
359
|
-
return result;
|
|
442
|
+
return result as { [key in keyof R]: CallResult; };
|
|
360
443
|
}
|
|
361
444
|
|
|
362
445
|
|
|
@@ -383,27 +466,40 @@ export abstract class baseBX24{
|
|
|
383
466
|
});
|
|
384
467
|
}
|
|
385
468
|
else{
|
|
386
|
-
return this.call(url,
|
|
469
|
+
return this.call(url, {
|
|
470
|
+
method,
|
|
471
|
+
data:params
|
|
472
|
+
});
|
|
387
473
|
}
|
|
388
474
|
}
|
|
389
|
-
else
|
|
475
|
+
else {
|
|
390
476
|
if (this.AUTH_EXPIRES<(new Date()).valueOf()){
|
|
391
477
|
this.refreshAuth(()=>{
|
|
392
478
|
this.callMethod(method, params, cb);
|
|
393
479
|
});
|
|
394
480
|
}
|
|
395
481
|
else{
|
|
396
|
-
this.call(url,
|
|
397
|
-
|
|
482
|
+
this.call(url, {
|
|
483
|
+
method,
|
|
484
|
+
data:params,
|
|
485
|
+
callback:cb
|
|
486
|
+
}).then(res=>{
|
|
487
|
+
return cb(res);
|
|
398
488
|
})
|
|
399
489
|
.catch(err=>{
|
|
400
|
-
|
|
490
|
+
if (err instanceof Error){
|
|
491
|
+
throw err;
|
|
492
|
+
}
|
|
493
|
+
else{
|
|
494
|
+
cb(new CallResult(err, {
|
|
495
|
+
data:params,
|
|
496
|
+
method,
|
|
497
|
+
callback:cb
|
|
498
|
+
}, this, 500));
|
|
499
|
+
}
|
|
401
500
|
})
|
|
402
501
|
}
|
|
403
502
|
}
|
|
404
|
-
else{
|
|
405
|
-
throw new Error("Incorrect params");
|
|
406
|
-
}
|
|
407
503
|
}
|
|
408
504
|
|
|
409
505
|
//Getters
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
result: {
|
|
3
|
+
result: { getAppOption: [], getUserOption: [] },
|
|
4
|
+
result_error: [],
|
|
5
|
+
result_total: [],
|
|
6
|
+
result_next: [],
|
|
7
|
+
result_time: {
|
|
8
|
+
getAppOption: {
|
|
9
|
+
start: 1670906729.0897441,
|
|
10
|
+
finish: 1670906729.090286,
|
|
11
|
+
duration: 0.00054192543029785156,
|
|
12
|
+
processing: 5.6028366088867188e-5,
|
|
13
|
+
date_start: "2022-12-13T07:45:29+03:00",
|
|
14
|
+
date_finish: "2022-12-13T07:45:29+03:00",
|
|
15
|
+
operating_reset_at: 1670907329,
|
|
16
|
+
operating: 0,
|
|
17
|
+
},
|
|
18
|
+
getUserOption: {
|
|
19
|
+
start: 1670906729.0914669,
|
|
20
|
+
finish: 1670906729.096303,
|
|
21
|
+
duration: 0.0048360824584960938,
|
|
22
|
+
processing: 0.0002598762512270312,
|
|
23
|
+
date_start: "2022-12-13T07:45:29+03:00",
|
|
24
|
+
date_finish: "2022-12-13T07:45:29+03:00",
|
|
25
|
+
operating_reset_at: 1670907329,
|
|
26
|
+
operating: 0,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
time: {
|
|
31
|
+
start: 1670906729.028657,
|
|
32
|
+
finish: 1670906729.100661,
|
|
33
|
+
duration: 0.072004079818725586,
|
|
34
|
+
processing: 0.010973930358886719,
|
|
35
|
+
date_start: "2022-12-13T07:45:29+03:00",
|
|
36
|
+
date_finish: "2022-12-13T07:45:29+03:00",
|
|
37
|
+
operating_reset_at: 1670907329,
|
|
38
|
+
operating: 0,
|
|
39
|
+
},
|
|
40
|
+
};
|
package/callResult.ts
CHANGED
|
@@ -1,30 +1,42 @@
|
|
|
1
|
+
import { baseBX24 } from "./base/BX24";
|
|
2
|
+
import { cloneDeep } from "lodash";
|
|
1
3
|
|
|
2
4
|
|
|
3
5
|
export class CallResult{
|
|
4
6
|
answer:any;
|
|
5
|
-
query:
|
|
7
|
+
query:{
|
|
8
|
+
method:string,
|
|
9
|
+
data:any,
|
|
10
|
+
callback?:(params: any)=>void
|
|
11
|
+
};
|
|
6
12
|
status:number;
|
|
13
|
+
bx24:baseBX24;
|
|
7
14
|
|
|
8
|
-
constructor(data:any, config:
|
|
15
|
+
constructor(data:any, config:{
|
|
16
|
+
method:string,
|
|
17
|
+
data:any,
|
|
18
|
+
callback?:(params: any)=>void
|
|
19
|
+
}, bx24:baseBX24, status:number){
|
|
9
20
|
this.answer=data;
|
|
10
21
|
this.query=config;
|
|
11
22
|
this.status=status;
|
|
23
|
+
this.bx24=bx24;
|
|
12
24
|
|
|
13
|
-
if(
|
|
25
|
+
if(this.answer?.next){
|
|
14
26
|
this.answer.next = parseInt(this.answer.next);
|
|
15
27
|
}
|
|
16
28
|
|
|
17
|
-
if(
|
|
29
|
+
if(this.answer?.error){
|
|
18
30
|
this.answer.ex = new ajaxError(this.status, typeof this.answer.error == 'string' ? this.answer : this.answer.error)
|
|
19
31
|
}
|
|
20
32
|
}
|
|
21
33
|
|
|
22
34
|
data(){
|
|
23
|
-
return this.answer
|
|
35
|
+
return this.answer?.result;
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
error(){
|
|
27
|
-
return this.answer
|
|
39
|
+
return this.answer?.ex;
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
error_description(){
|
|
@@ -35,25 +47,27 @@ export class CallResult{
|
|
|
35
47
|
return !isNaN(this.answer.next);
|
|
36
48
|
}
|
|
37
49
|
|
|
50
|
+
time(){
|
|
51
|
+
return this.answer.time;
|
|
52
|
+
}
|
|
53
|
+
|
|
38
54
|
total(){
|
|
39
55
|
return parseInt(this.answer.total);
|
|
40
56
|
}
|
|
41
57
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
// {
|
|
45
|
-
// this.query.start = this.answer.next;
|
|
46
|
-
|
|
47
|
-
// if(!!cb && util.type.isFunction(cb))
|
|
48
|
-
// {
|
|
49
|
-
// this.query.callback = cb;
|
|
50
|
-
// }
|
|
51
|
-
|
|
52
|
-
// return ajax(this.query);
|
|
53
|
-
// }
|
|
58
|
+
next(){
|
|
59
|
+
if(!this.more()) return false;
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
const params=cloneDeep(this.query.data);
|
|
62
|
+
params.start=this.answer.next;
|
|
63
|
+
if (this.query.callback){
|
|
64
|
+
this.bx24.callMethod(this.query.method, params, this.query.callback);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
else{
|
|
68
|
+
return this.bx24.callMethod(this.query.method, params);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
interface exErrorInterface{
|