@slavmak2486/bx24ts 1.2.19 → 1.2.21

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/BX24Server.ts CHANGED
@@ -159,6 +159,7 @@ export class BX24Server extends baseBX24{
159
159
  })
160
160
  .catch(err=>{
161
161
  if (cb) cb(err);
162
+ else throw new Error(String(err));
162
163
  });
163
164
  }
164
165
 
package/BatchHelper.ts CHANGED
@@ -26,7 +26,7 @@ export class BatchHelper{
26
26
  if (!dependencies[parent]) {
27
27
  dependencies[parent] = [];
28
28
  }
29
- dependencies[parent].push(key);
29
+ dependencies[parent]!.push(key);
30
30
  })
31
31
  }
32
32
  }
@@ -66,8 +66,9 @@ export class BatchHelper{
66
66
 
67
67
  for(const dep of deps){
68
68
  alradyInBatch.push(dep);
69
- if (!obj[dep]) continue;
70
- currentChunk[dep] = obj[dep];
69
+ const depValue = obj[dep];
70
+ if (!depValue) continue;
71
+ currentChunk[dep] = depValue;
71
72
  currentSize++;
72
73
  }
73
74
 
package/base/BX24.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type batchCmdElement } from "../types/batchElement";
2
2
  import {type eventElement } from "../types/eventElement";
3
3
 
4
- import axios, { AxiosError, type AxiosRequestConfig, type AxiosResponse } from 'axios';
4
+ import axios, { AxiosError, type AxiosRequestConfig } from 'axios';
5
5
  import { CallResult } from "../callResult";
6
6
  import {cloneDeep, get as __get} from 'lodash'
7
7
  import { type Logger } from "../types/authBaseServe";
@@ -99,7 +99,7 @@ export abstract class baseBX24{
99
99
  for (const idx in arrHandler){
100
100
  setTimeout(()=>{
101
101
  if (!arrHandler[idx]?.handler) return;
102
- arrHandler[idx].handler.call(this, params);
102
+ arrHandler[idx]!.handler.call(this, params);
103
103
  }, 10);
104
104
  }
105
105
  }
@@ -222,11 +222,7 @@ export abstract class baseBX24{
222
222
  abstract refreshAuth(cb?:(params:any)=>void):void;
223
223
  abstract refreshAuthAsync():Promise<getAuth>;
224
224
 
225
- protected callSuccess(xhr:AxiosResponse):boolean{
226
- 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;
227
- }
228
-
229
- protected call(url:string, config:{
225
+ public call(url:string, config:{
230
226
  method:string,
231
227
  data:any,
232
228
  callback?:(params: any)=>void
@@ -246,12 +242,24 @@ export abstract class baseBX24{
246
242
  if (this.AUTH_CONNECTOR&&!params.auth_connector){
247
243
  params.auth_connector=this.AUTH_CONNECTOR;
248
244
  }
249
- const options:AxiosRequestConfig = {
250
- method: 'POST',
251
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
252
- data: this.getHttpString(params),
253
- url:url
254
- };
245
+ let options:AxiosRequestConfig;
246
+
247
+ if (config.method.startsWith('api/')){
248
+ options=options={
249
+ method: 'POST',
250
+ headers: { 'content-type': 'application/json' },
251
+ data: JSON.stringify(params),
252
+ url:url
253
+ };
254
+ }
255
+ else{
256
+ options={
257
+ method: 'POST',
258
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
259
+ data: this.getHttpString(params),
260
+ url:url
261
+ };
262
+ }
255
263
 
256
264
  if (this.timeoutCall){
257
265
  options.timeout=this.timeoutCall;
@@ -288,15 +296,11 @@ export abstract class baseBX24{
288
296
  : false;
289
297
  }
290
298
 
291
- // callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
292
- // callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
293
- // callBatch(cmd:batchCmdElement, haltOnError?:boolean):Promise<{[key:string|number]:CallResult}>;
294
- // callBatch(cmd:batchCmdElement, cb:(params:{[key:string|number]:CallResult})=>void|boolean, haltOnError?:boolean):void;
295
- // callBatch(
296
- // cmd:batchCmdElement,
297
- // cb?:((params:{[key:string|number]:CallResult})=>void)|boolean,
298
- // haltOnError=false):void|Promise<{[key:string|number]:CallResult
299
- // }>
299
+ public setProxyUrl(url:string){
300
+ if (!url.startsWith('http')) throw new Error('URL INCORRECT. HOT HAVE PROTOCOL');
301
+ this.url=`${url}/`
302
+ }
303
+
300
304
  callBatch<T extends batchCmdElement>(cmd:T, haltOnError?:boolean):Promise<{[key in keyof T]:CallResult}>
301
305
  callBatch<T extends batchCmdElement>(cmd:T, cb:(params:{[key in keyof T]:CallResult})=>void):void
302
306
  callBatch<T extends batchCmdElement>(cmd:T, cb:(params:{[key in keyof T]:CallResult})=>void, haltOnError:boolean):void
@@ -466,7 +470,8 @@ export abstract class baseBX24{
466
470
  callMethod(method:string, params:any):Promise<CallResult>;
467
471
  callMethod(method:string, params:any, cb?:(params:CallResult)=>void):void;
468
472
  callMethod(method:string, params:any, cb?:(params:CallResult)=>void):void|Promise<CallResult>{
469
- const url=this.url?`${this.url}${method}.json`:`http${this.PROTOCOL?'s':''}://${this.DOMAIN}${this.PATH}/${method}.json`;
473
+ const methodToCall=method.startsWith('api/')?method:`${method}.json`;
474
+ const url=this.url?`${this.url}${methodToCall}`:`http${this.PROTOCOL?'s':''}://${this.DOMAIN}${this.PATH}/${methodToCall}`;
470
475
 
471
476
  if (!cb){
472
477
  if (this.AUTH_EXPIRES<(new Date()).valueOf()){
@@ -124,6 +124,8 @@ class BX24Server extends BX24_1.baseBX24 {
124
124
  .catch(err => {
125
125
  if (cb)
126
126
  cb(err);
127
+ else
128
+ throw new Error(String(err));
127
129
  });
128
130
  }
129
131
  refreshAuthAsync() {
@@ -60,9 +60,10 @@ class BatchHelper {
60
60
  const deps = dependencies[key] || [];
61
61
  for (const dep of deps) {
62
62
  alradyInBatch.push(dep);
63
- if (!obj[dep])
63
+ const depValue = obj[dep];
64
+ if (!depValue)
64
65
  continue;
65
- currentChunk[dep] = obj[dep];
66
+ currentChunk[dep] = depValue;
66
67
  currentSize++;
67
68
  }
68
69
  currentSize++;
package/dist/base/BX24.js CHANGED
@@ -198,9 +198,6 @@ class baseBX24 {
198
198
  }
199
199
  return;
200
200
  }
201
- callSuccess(xhr) {
202
- 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;
203
- }
204
201
  call(url, config) {
205
202
  if (this.logger) {
206
203
  this.logger.log(`Вызов: ${url}`, {
@@ -214,12 +211,23 @@ class baseBX24 {
214
211
  if (this.AUTH_CONNECTOR && !params.auth_connector) {
215
212
  params.auth_connector = this.AUTH_CONNECTOR;
216
213
  }
217
- const options = {
218
- method: 'POST',
219
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
220
- data: this.getHttpString(params),
221
- url: url
222
- };
214
+ let options;
215
+ if (config.method.startsWith('api/')) {
216
+ options = options = {
217
+ method: 'POST',
218
+ headers: { 'content-type': 'application/json' },
219
+ data: JSON.stringify(params),
220
+ url: url
221
+ };
222
+ }
223
+ else {
224
+ options = {
225
+ method: 'POST',
226
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
227
+ data: this.getHttpString(params),
228
+ url: url
229
+ };
230
+ }
223
231
  if (this.timeoutCall) {
224
232
  options.timeout = this.timeoutCall;
225
233
  }
@@ -254,6 +262,11 @@ class baseBX24 {
254
262
  ? { access_token: this.AUTH_ID, refresh_token: this.REFRESH_ID, expires_in: this.AUTH_EXPIRES, domain: this.DOMAIN, member_id: this.MEMBER_ID }
255
263
  : false;
256
264
  }
265
+ setProxyUrl(url) {
266
+ if (!url.startsWith('http'))
267
+ throw new Error('URL INCORRECT. HOT HAVE PROTOCOL');
268
+ this.url = `${url}/`;
269
+ }
257
270
  callBatch(cmd, cb, haltOnError = false) {
258
271
  const startCb = cb;
259
272
  const startHaltOnError = haltOnError;
@@ -391,7 +404,8 @@ class baseBX24 {
391
404
  return result;
392
405
  }
393
406
  callMethod(method, params, cb) {
394
- const url = this.url ? `${this.url}${method}.json` : `http${this.PROTOCOL ? 's' : ''}://${this.DOMAIN}${this.PATH}/${method}.json`;
407
+ const methodToCall = method.startsWith('api/') ? method : `${method}.json`;
408
+ const url = this.url ? `${this.url}${methodToCall}` : `http${this.PROTOCOL ? 's' : ''}://${this.DOMAIN}${this.PATH}/${methodToCall}`;
395
409
  if (!cb) {
396
410
  if (this.AUTH_EXPIRES < (new Date()).valueOf()) {
397
411
  return new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slavmak2486/bx24ts",
3
- "version": "1.2.19",
3
+ "version": "1.2.21",
4
4
  "description": "Library for bitrix24",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -82,21 +82,23 @@ test('real test', async ()=>{
82
82
 
83
83
  const batchRaw=helper.getArrBatches()[0];
84
84
 
85
+ if (!batchRaw) return;
86
+
85
87
  const result=await bx24.callBatch(batchRaw);
86
88
  for (const request in result){
87
89
  switch (request) {
88
90
  case 'getDeals':
89
- expect(result[request].data()).toEqual(batch.result.result.getDeals);
90
- expect(result[request].total()).toEqual(batch.result.result_total.getDeals);
91
- expect(result[request].error()).toBeUndefined();
91
+ expect(result[request]!.data()).toEqual(batch.result.result.getDeals);
92
+ expect(result[request]!.total()).toEqual(batch.result.result_total.getDeals);
93
+ expect(result[request]!.error()).toBeUndefined();
92
94
  break;
93
95
  case 'getLeads':
94
- expect(result[request].data()).toEqual(leadList.result);
95
- expect(result[request].total()).toEqual(leadList.total);
96
- expect(result[request].error()).toBeUndefined();
96
+ expect(result[request]!.data()).toEqual(leadList.result);
97
+ expect(result[request]!.total()).toEqual(leadList.total);
98
+ expect(result[request]!.error()).toBeUndefined();
97
99
  break;
98
100
  case 'getByID':{
99
- expect(result[request].error()).toEqual({
101
+ expect(result[request]!.error()).toEqual({
100
102
  status: 200,
101
103
  ex: { error: '', error_description: 'Not found' }
102
104
  });
@@ -1,4 +1,3 @@
1
- import {BX24Server} from '../index';
2
1
  import axios from 'axios';
3
2
  import MockAdapter from "axios-mock-adapter";
4
3
  import dealList from './mocks/dealList'
@@ -1,4 +1,3 @@
1
- import {BX24Server} from '../index';
2
1
  import axios from 'axios';
3
2
  import MockAdapter from "axios-mock-adapter";
4
3
  import dealList from './mocks/dealList';