bx-rest 0.6.8 → 0.8.1

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.
@@ -269,9 +269,9 @@ async function getBase64FileLikeStr(file) {
269
269
  return undefined;
270
270
  }
271
271
  function prepareBaseAddress(url, add = '') {
272
- if (url.search('https://') !== 0 && url.search('http://')) {
273
- url = 'https://' + url;
274
- }
272
+ // if (url.search('https://') !== 0 && url.search('http://')) {
273
+ // url = 'https://' + url
274
+ // }
275
275
  if (add && add.length > 0) {
276
276
  if (url.lastIndexOf('/') !== (url.length - 1)) {
277
277
  url = url + '/';
@@ -424,7 +424,11 @@ class SessionKeyServices {
424
424
  return '';
425
425
  }
426
426
  getAuthParams() {
427
- switch (this.BX_REST_SETTINGS.auth.source) {
427
+ const source = this.BX_REST_SETTINGS.auth.source;
428
+ if (typeof source === 'function') {
429
+ return source();
430
+ }
431
+ switch (source) {
428
432
  case 'cookies':
429
433
  return this.getAuth();
430
434
  case 'localStorage':
@@ -506,7 +510,7 @@ class HttpData {
506
510
  case 'function':
507
511
  case 'undefined':
508
512
  default:
509
- console.error('Ты отправил что-то не то', obj[key], typeof obj[key], typeof obj);
513
+ console.error('You sent something wrong', obj[key], typeof obj[key], typeof obj);
510
514
  }
511
515
  }
512
516
  }
@@ -544,6 +548,30 @@ class HttpServices extends BaseHttp {
544
548
  return throwError(() => new Error('get base url error'));
545
549
  }
546
550
  const fullUrl = prepareBaseAddress(baseUrl) + url;
551
+ if (this.session.getKeyAuth() === 'sessid') {
552
+ const body = this.getHttpParamsPost(paramsClone, new FormData(), [], settings);
553
+ // Преобразуем параметры в формат x-www-form-urlencoded
554
+ const urlEncodedBody = new URLSearchParams();
555
+ Object.keys(body).forEach((key) => {
556
+ const value = paramsClone[key];
557
+ // если value — объект или массив, Bitrix ожидает fields[key] или array[] формат
558
+ if (typeof value === 'object' && value !== null) {
559
+ for (const subKey in value) {
560
+ if (Object.prototype.hasOwnProperty.call(value, subKey)) {
561
+ urlEncodedBody.append(`${key}[${subKey}]`, value[subKey]);
562
+ }
563
+ }
564
+ }
565
+ else {
566
+ urlEncodedBody.append(key, value);
567
+ }
568
+ });
569
+ return from(this.axiosInstance.post(fullUrl, urlEncodedBody.toString(), {
570
+ headers: {
571
+ 'Content-Type': 'application/x-www-form-urlencoded',
572
+ },
573
+ }).then((response) => response.data));
574
+ }
547
575
  const body = this.getHttpParamsPost(paramsClone, new FormData(), [], settings);
548
576
  return from(this.axiosInstance.post(fullUrl, body).then((response) => response.data));
549
577
  }));
@@ -636,27 +664,61 @@ class HttpBXServices extends HttpServices {
636
664
  }
637
665
  httpPost(url, params = {}) {
638
666
  const paramsClone = clone(params);
667
+ const key = this.session.getKeyAuth();
639
668
  let auth = this.session.getAuthParams();
640
669
  const checkIsOn = this.session.getCheckAuthParamsIsOn();
641
- if (checkIsOn) {
670
+ if (checkIsOn && key === 'OAuth2') {
642
671
  if (!auth && BXRestSettings.date.auth.source !== 'off') {
643
672
  throw new Error('Authorization required (post)');
644
673
  }
645
- paramsClone[this.session.getKeyAuth()] = auth;
674
+ paramsClone[key] = auth;
646
675
  }
647
676
  return this.session.getBaseUrl().pipe(switchMap((baseUrl) => {
648
677
  if (!baseUrl) {
649
678
  return throwError(() => new Error('get base url error'));
650
679
  }
651
680
  const fullUrl = prepareBaseAddress(baseUrl) + url;
652
- // const body = this.getHttpParamsPost(paramsClone, new FormData(), false, [])
681
+ if (auth === 'sessid') {
682
+ // Преобразуем параметры в формат x-www-form-urlencoded
683
+ const bodyString = this.serializeBitrixParams(paramsClone);
684
+ return from(this.axiosInstance.post(fullUrl, bodyString, {
685
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
686
+ }).then((response) => response.data));
687
+ }
653
688
  return from(this.axiosInstance.post(fullUrl, paramsClone, {
689
+ withCredentials: key === 'OAuth2',
654
690
  headers: {
655
691
  'Content-Type': 'application/json',
656
692
  }
657
693
  }).then((response) => response.data));
658
694
  }));
659
695
  }
696
+ serializeBitrixParams(obj, prefix = '') {
697
+ const pairs = [];
698
+ for (const key in obj) {
699
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
700
+ continue;
701
+ const value = obj[key];
702
+ const prefixedKey = prefix ? `${prefix}[${key}]` : key;
703
+ if (Array.isArray(value)) {
704
+ value.forEach((item) => {
705
+ if (typeof item === 'object') {
706
+ pairs.push(this.serializeBitrixParams(item, `${prefixedKey}[]`));
707
+ }
708
+ else {
709
+ pairs.push(`${encodeURIComponent(prefixedKey)}[]=${encodeURIComponent(item)}`);
710
+ }
711
+ });
712
+ }
713
+ else if (typeof value === 'object' && value !== null) {
714
+ pairs.push(this.serializeBitrixParams(value, prefixedKey));
715
+ }
716
+ else {
717
+ pairs.push(`${encodeURIComponent(prefixedKey)}=${encodeURIComponent(value)}`);
718
+ }
719
+ }
720
+ return pairs.join('&');
721
+ }
660
722
  // Мб не работает
661
723
  httpGet(url, params = {}) {
662
724
  if (params === null) {
@@ -6035,15 +6097,17 @@ class BXRestMapPullApplicationConfig {
6035
6097
 
6036
6098
  class BXRestMapPull {
6037
6099
  static get(value) {
6038
- console.log('Object.entries(value.publicChannels)', value);
6039
- if (value?.publicChannels) {
6040
- console.log('Object.entries(value.publicChannels)', value.publicChannels, Object.entries(value.publicChannels), Object.fromEntries(Object.entries(value.publicChannels).map(([key, val]) => ([key,
6041
- {
6042
- ...val,
6043
- start: toDate(val.start), end: toDate(val.end)
6044
- }
6045
- ]))));
6046
- }
6100
+ // console.log('Object.entries(value.publicChannels)', value)
6101
+ //if(value?.publicChannels) {
6102
+ // console.log('Object.entries(value.publicChannels)',
6103
+ // value.publicChannels, Object.entries(value.publicChannels), Object.fromEntries(Object.entries(value.publicChannels).map(([key, val]) => (
6104
+ // [key,
6105
+ // {
6106
+ // ...val,
6107
+ // start: toDate(val.start), end: toDate(val.end)
6108
+ // } as iBXRestPullConfigPublicChannelsGet
6109
+ // ]))) as Record<string, iBXRestPullConfigPublicChannelsGet>,)
6110
+ //}
6047
6111
  return value ? { ...BXRestMapPullApplicationConfig.get(value), ...{ serverTime: toDate(value.serverTime) } } : undefined;
6048
6112
  }
6049
6113
  }
@@ -6070,6 +6134,10 @@ class BXRestNavvyPullConfig {
6070
6134
  this.Navvy = new Navvy();
6071
6135
  }
6072
6136
  static { this.public = new BXRestMapPullChannelPublic(); }
6137
+ /**
6138
+ * @experimental
6139
+ * Этот метод находится в процессе тестирования и может измениться.
6140
+ */
6073
6141
  get() {
6074
6142
  return this.Navvy.simple(this.url.get, undefined, BXRestMapPull.get);
6075
6143
  }