@cdek-it/widget 3.8.2 → 3.9.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.
@@ -17934,35 +17934,39 @@ class CdekApi extends AbstractApi {
17934
17934
  const coreStorage = core();
17935
17935
  if (coreStorage.params.tariff) {
17936
17936
  if (coreStorage.debug) {
17937
- console.debug("[CDEK] We have fixed price", coreStorage.params.tariff);
17937
+ console.debug(
17938
+ "[CDEK] We have fixed price",
17939
+ coreStorage.params.tariff
17940
+ );
17938
17941
  }
17939
17942
  return this.mapTariffsWithTypes([coreStorage.params.tariff]);
17940
17943
  } else if (targetLocation === void 0) {
17941
- throw new Error("No location and no fixed tariff was passed to price calc");
17944
+ throw new Error(
17945
+ "No location and no fixed tariff was passed to price calc"
17946
+ );
17942
17947
  }
17943
17948
  if (coreStorage.debug) {
17944
17949
  console.debug("[CDEK] Loading prices from service");
17945
17950
  }
17946
17951
  this.cancelPriceRequest();
17947
17952
  this.getPriceAbort = new AbortController();
17948
- return await axios$1.post(
17949
- this.servicePath,
17950
- {
17951
- currency: Currency[coreStorage.params.currency],
17952
- lang: coreStorage.params.lang,
17953
- from_location: typeof coreStorage.params.from === "string" ? {
17954
- address: coreStorage.params.from
17955
- } : coreStorage.params.from,
17956
- to_location: targetLocation,
17957
- action: "calculate",
17958
- packages: coreStorage.params.goods
17959
- },
17960
- {
17961
- signal: this.getPriceAbort.signal
17962
- }
17963
- ).then((result) => {
17953
+ return await axios$1.post(this.servicePath, {
17954
+ currency: Currency[coreStorage.params.currency],
17955
+ lang: coreStorage.params.lang,
17956
+ from_location: typeof coreStorage.params.from === "string" ? {
17957
+ address: coreStorage.params.from
17958
+ } : coreStorage.params.from,
17959
+ to_location: targetLocation,
17960
+ action: "calculate",
17961
+ packages: coreStorage.params.goods
17962
+ }, {
17963
+ signal: this.getPriceAbort.signal
17964
+ }).then((result) => {
17964
17965
  if (coreStorage.debug) {
17965
- console.debug("[CDEK] Got prices from service", result.data.tariff_codes || []);
17966
+ console.debug(
17967
+ "[CDEK] Got prices from service",
17968
+ result.data.tariff_codes || []
17969
+ );
17966
17970
  }
17967
17971
  return this.mapTariffsWithTypes(result.data.tariff_codes || []);
17968
17972
  }).catch((e) => {
@@ -17978,7 +17982,6 @@ class CdekApi extends AbstractApi {
17978
17982
  }
17979
17983
  async getOffices() {
17980
17984
  const searchStorage = search();
17981
- const mapStorage = map();
17982
17985
  const coreStorage = core();
17983
17986
  searchStorage.loading = true;
17984
17987
  if (coreStorage.params.offices !== null || coreStorage.params.officesRaw !== null) {
@@ -17999,30 +18002,64 @@ class CdekApi extends AbstractApi {
17999
18002
  }
18000
18003
  this.cancelOfficeRequest();
18001
18004
  this.getOfficesAbort = new AbortController();
18002
- return await axios$1.get(this.servicePath, {
18005
+ const firstPageResult = await this.fetchOfficePage(
18006
+ 1,
18007
+ additionalParams
18008
+ );
18009
+ let offices = this.formatResult(firstPageResult);
18010
+ const firstPageResultIsArray = Array.isArray(firstPageResult);
18011
+ const totalPages = firstPageResultIsArray ? false : firstPageResult.headers["x-total-pages"];
18012
+ const currentPage = firstPageResultIsArray ? false : firstPageResult.headers["x-current-page"];
18013
+ if (totalPages && currentPage) {
18014
+ const remainingPages = parseInt(totalPages) - parseInt(currentPage);
18015
+ await Promise.all(Array.from(
18016
+ { length: remainingPages },
18017
+ (_e, i) => this.fetchOfficePage(
18018
+ i,
18019
+ additionalParams
18020
+ ).then((r) => {
18021
+ const response = typeof r.data === "string" ? JSON.parse(r.data) : r.data;
18022
+ offices = [
18023
+ ...offices,
18024
+ ...this.formatOffices(response)
18025
+ ];
18026
+ })
18027
+ ));
18028
+ } else {
18029
+ const allOffices = await this.fetchOfficePage(0, additionalParams, null);
18030
+ offices = this.formatResult(allOffices);
18031
+ }
18032
+ if (coreStorage.debug) {
18033
+ console.debug("[CDEK] Got points", offices);
18034
+ }
18035
+ searchStorage.loading = false;
18036
+ return offices;
18037
+ }
18038
+ async fetchOfficePage(pageNumber, additionalParams, sizeNumber = 50) {
18039
+ return axios$1.get(this.servicePath, {
18003
18040
  params: {
18004
- ...searchStorage.filters,
18041
+ ...search().filters,
18005
18042
  ...additionalParams,
18006
- action: "offices"
18043
+ action: "offices",
18044
+ page: pageNumber,
18045
+ size: sizeNumber
18007
18046
  },
18008
- signal: this.getOfficesAbort.signal
18009
- }).then((result) => {
18010
- const response = typeof result.data === "string" ? JSON.parse(result.data) : result.data;
18011
- if (coreStorage.debug) {
18012
- console.debug("[CDEK] Got points", response);
18013
- }
18014
- return this.formatOffices(response);
18047
+ signal: this.getOfficesAbort ? this.getOfficesAbort.signal : void 0
18015
18048
  }).catch((e) => {
18016
18049
  if (axios$1.isCancel(e)) {
18017
- if (coreStorage.debug) {
18050
+ if (core().debug) {
18018
18051
  console.debug("[CDEK] Offices request cancelled");
18019
18052
  }
18020
18053
  throw e;
18021
18054
  }
18022
18055
  console.error("[CDEK] Service error", e);
18023
- mapStorage.mapLoadError = YandexMapErrorCode.SERVICE_ERROR;
18056
+ map().mapLoadError = YandexMapErrorCode.SERVICE_ERROR;
18024
18057
  return [];
18025
- }).finally(() => searchStorage.loading = false);
18058
+ });
18059
+ }
18060
+ formatResult(result) {
18061
+ const response = typeof result.data === "string" ? JSON.parse(result.data) : result.data;
18062
+ return this.formatOffices(response);
18026
18063
  }
18027
18064
  formatOffices(offices) {
18028
18065
  return offices.map((e) => ({
@@ -18086,12 +18123,8 @@ class CdekApi extends AbstractApi {
18086
18123
  loadLocalOffices() {
18087
18124
  const searchStorage = search();
18088
18125
  const coreStorage = core();
18089
- const offices = coreStorage.params.officesRaw ? this.formatOffices(
18090
- Array.isArray(coreStorage.params.officesRaw) ? coreStorage.params.officesRaw : JSON.parse(coreStorage.params.officesRaw) || []
18091
- ) : coreStorage.params.offices || [];
18092
- return offices.filter(
18093
- (office) => (searchStorage.filters.allowed_cod === null || office.allowed_cod === searchStorage.filters.allowed_cod) && (searchStorage.filters.type === null || searchStorage.filters.type === OfficeType.ALL || office.type === searchStorage.filters.type) && (searchStorage.filters.have_cash === null || office.have_cash === searchStorage.filters.have_cash) && (searchStorage.filters.have_cashless === null || office.have_cashless === searchStorage.filters.have_cashless) && (searchStorage.filters.is_dressing_room === null || office.is_dressing_room === searchStorage.filters.is_dressing_room)
18094
- );
18126
+ const offices = coreStorage.params.officesRaw ? this.formatOffices(Array.isArray(coreStorage.params.officesRaw) ? coreStorage.params.officesRaw : JSON.parse(coreStorage.params.officesRaw) || []) : coreStorage.params.offices || [];
18127
+ return offices.filter((office) => (searchStorage.filters.allowed_cod === null || office.allowed_cod === searchStorage.filters.allowed_cod) && (searchStorage.filters.type === null || searchStorage.filters.type === OfficeType.ALL || office.type === searchStorage.filters.type) && (searchStorage.filters.have_cash === null || office.have_cash === searchStorage.filters.have_cash) && (searchStorage.filters.have_cashless === null || office.have_cashless === searchStorage.filters.have_cashless) && (searchStorage.filters.is_dressing_room === null || office.is_dressing_room === searchStorage.filters.is_dressing_room));
18095
18128
  }
18096
18129
  }
18097
18130
  class Widget {