@nextgis/ngw-connector 2.5.0 → 3.0.0-alpha.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.
@@ -1,12 +1,11 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 2.5.0; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.1; author: NextGIS */
2
2
  import { EventEmitter } from 'events';
3
3
  import Cache from '@nextgis/cache';
4
- import CancelablePromise from '@nextgis/cancelable-promise';
5
4
  import { objectDeepEqual, defined, objectRemoveEmpty, fixUrlStr } from '@nextgis/utils';
6
5
 
7
- var __defProp$5 = Object.defineProperty;
8
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+ var __defProp$6 = Object.defineProperty;
7
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __publicField$5 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
10
9
  class NgwError extends Error {
11
10
  constructor(er) {
12
11
  super();
@@ -23,9 +22,9 @@ class NgwError extends Error {
23
22
  }
24
23
  }
25
24
 
26
- var __defProp$4 = Object.defineProperty;
27
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
28
- var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
25
+ var __defProp$5 = Object.defineProperty;
26
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
+ var __publicField$4 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
29
28
  class ResourceNotFoundError extends NgwError {
30
29
  constructor(obj) {
31
30
  super(obj);
@@ -64,27 +63,24 @@ function resourceToQuery(resource, prefix = "") {
64
63
  return query;
65
64
  }
66
65
 
67
- var __defProp$3 = Object.defineProperty;
68
- var __defProps = Object.defineProperties;
69
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
70
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
71
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
72
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
73
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
74
- var __spreadValues$1 = (a, b) => {
66
+ var __defProp$4 = Object.defineProperty;
67
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
68
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
69
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
70
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
71
+ var __spreadValues$2 = (a, b) => {
75
72
  for (var prop in b || (b = {}))
76
- if (__hasOwnProp$1.call(b, prop))
77
- __defNormalProp$3(a, prop, b[prop]);
78
- if (__getOwnPropSymbols$1)
79
- for (var prop of __getOwnPropSymbols$1(b)) {
80
- if (__propIsEnum$1.call(b, prop))
81
- __defNormalProp$3(a, prop, b[prop]);
73
+ if (__hasOwnProp$2.call(b, prop))
74
+ __defNormalProp$4(a, prop, b[prop]);
75
+ if (__getOwnPropSymbols$2)
76
+ for (var prop of __getOwnPropSymbols$2(b)) {
77
+ if (__propIsEnum$2.call(b, prop))
78
+ __defNormalProp$4(a, prop, b[prop]);
82
79
  }
83
80
  return a;
84
81
  };
85
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
86
- var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
87
- var __async = (__this, __arguments, generator) => {
82
+ var __publicField$3 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
83
+ var __async$2 = (__this, __arguments, generator) => {
88
84
  return new Promise((resolve, reject) => {
89
85
  var fulfilled = (value) => {
90
86
  try {
@@ -125,39 +121,27 @@ class ResourcesControl {
125
121
  * Fetching resource would be cached to speed up next call
126
122
  */
127
123
  getOne(resource, requestOptions) {
128
- const cache = this.cache;
129
- const forCache = {};
130
- const opt = __spreadProps(__spreadValues$1({}, requestOptions), { cache: false });
131
- if (typeof resource === "string") {
132
- forCache.keyname = resource;
133
- } else if (typeof resource === "number") {
134
- forCache.id = resource;
135
- } else if (isObject(resource)) {
124
+ const opt = __spreadValues$2({}, requestOptions);
125
+ if (typeof resource === "string") ; else if (typeof resource === "number") ; else if (isObject(resource)) {
136
126
  if (resource.id !== void 0) {
137
- forCache.id = resource.id;
127
+ resource.id;
138
128
  } else {
139
129
  if (resource.keyname) {
140
- forCache.keyname = resource.keyname;
130
+ resource.keyname;
141
131
  }
142
132
  if (resource.display_name) {
143
- forCache.display_name = resource.display_name;
133
+ resource.display_name;
144
134
  }
145
135
  }
146
136
  }
147
- const makeRequest = () => {
148
- if (typeof resource === "string") {
149
- return this._fetchResourceBy({ keyname: resource }, opt);
150
- } else if (typeof resource === "number") {
151
- return this._fetchResourceById(resource, opt);
152
- } else if (isObject(resource)) {
153
- return this._fetchResourceBy(resource, opt);
154
- }
155
- return CancelablePromise.resolve(void 0);
156
- };
157
- if (requestOptions == null ? void 0 : requestOptions.cache) {
158
- return cache.addFull("resource", makeRequest, forCache);
137
+ if (typeof resource === "string") {
138
+ return this._fetchResourceBy({ keyname: resource }, opt);
139
+ } else if (typeof resource === "number") {
140
+ return this._fetchResourceById(resource, opt);
141
+ } else if (isObject(resource)) {
142
+ return this._fetchResourceBy(resource, opt);
159
143
  }
160
- return makeRequest();
144
+ return Promise.resolve(void 0);
161
145
  }
162
146
  getOneOrFail(resource, requestOptions) {
163
147
  return this.getOne(resource, requestOptions).then((res) => {
@@ -179,7 +163,7 @@ class ResourcesControl {
179
163
  */
180
164
  getId(resource, requestOptions) {
181
165
  if (typeof resource === "number") {
182
- return CancelablePromise.resolve(resource);
166
+ return Promise.resolve(resource);
183
167
  } else if (typeof resource === "string" || isObject(resource)) {
184
168
  return this.getOne(resource, requestOptions).then((res) => {
185
169
  if (res) {
@@ -187,7 +171,7 @@ class ResourcesControl {
187
171
  }
188
172
  });
189
173
  }
190
- return CancelablePromise.resolve(void 0);
174
+ return Promise.resolve(void 0);
191
175
  }
192
176
  /**
193
177
  * A fast way to retrieve resource ID for any resource definition.
@@ -214,12 +198,12 @@ class ResourcesControl {
214
198
  } else {
215
199
  Object.assign(query, resourceToQuery(resource));
216
200
  }
217
- return this.connector.get("resource.search", requestOptions, __spreadValues$1({
201
+ return this.connector.get("resource.search", requestOptions, __spreadValues$2({
218
202
  serialization: "full"
219
203
  }, query)).then((resources) => {
220
- if (resources) {
204
+ if ((requestOptions == null ? void 0 : requestOptions.cache) && resources) {
221
205
  for (const x of resources) {
222
- this.cache.add("resource.item", CancelablePromise.resolve(x), {
206
+ this.cache.add("resource.item", Promise.resolve(x), {
223
207
  id: x.resource.id
224
208
  });
225
209
  }
@@ -235,7 +219,7 @@ class ResourcesControl {
235
219
  if (child) {
236
220
  return this.getOne(child.resource.parent.id, requestOptions);
237
221
  }
238
- return CancelablePromise.resolve(void 0);
222
+ return Promise.resolve(void 0);
239
223
  });
240
224
  }
241
225
  getChildrenOf(resource, requestOptions) {
@@ -265,18 +249,16 @@ class ResourcesControl {
265
249
  });
266
250
  }
267
251
  _getChildrenOf(parent, requestOptions, _items = []) {
268
- return this.connector.get(
269
- "resource.collection",
270
- __spreadValues$1({ cache: true }, requestOptions),
271
- {
272
- parent
273
- }
274
- ).then((items) => {
252
+ return this.connector.get("resource.collection", requestOptions, {
253
+ parent
254
+ }).then((items) => {
275
255
  const recursivePromises = [];
276
256
  for (const item of items) {
277
- this.cache.add("resource.item", CancelablePromise.resolve(item), {
278
- id: item.resource.id
279
- });
257
+ if (requestOptions == null ? void 0 : requestOptions.cache) {
258
+ this.cache.add("resource.item", Promise.resolve(item), {
259
+ id: item.resource.id
260
+ });
261
+ }
280
262
  _items.push(item);
281
263
  if ((requestOptions == null ? void 0 : requestOptions.recursive) && item.resource.children) {
282
264
  recursivePromises.push(
@@ -285,7 +267,7 @@ class ResourcesControl {
285
267
  }
286
268
  }
287
269
  if (recursivePromises.length) {
288
- return CancelablePromise.all(recursivePromises).then(() => {
270
+ return Promise.all(recursivePromises).then(() => {
289
271
  return _items;
290
272
  });
291
273
  }
@@ -293,11 +275,12 @@ class ResourcesControl {
293
275
  });
294
276
  }
295
277
  _cleanResourceItemCache(id) {
296
- return __async(this, null, function* () {
278
+ return __async$2(this, null, function* () {
279
+ var _a;
297
280
  const all = this.cache.all();
298
281
  const toDelete = [];
299
282
  for (const c of all) {
300
- const cid = c.options && c.options.id;
283
+ const cid = (_a = c.props) == null ? void 0 : _a.id;
301
284
  if (["resource.item", "resource"].includes(c.key) && cid !== void 0) {
302
285
  if (typeof cid === "number") {
303
286
  if (cid === id) {
@@ -317,18 +300,7 @@ class ResourcesControl {
317
300
  });
318
301
  }
319
302
  _fetchResourceById(id, requestOptions) {
320
- const promise = () => this.connector.get("resource.item", requestOptions, { id });
321
- if (requestOptions == null ? void 0 : requestOptions.cache) {
322
- return this.cache.add("resource.item", promise, {
323
- id
324
- }).catch((er) => {
325
- if (!(er instanceof ResourceNotFoundError)) {
326
- throw er;
327
- }
328
- return void 0;
329
- });
330
- }
331
- return promise();
303
+ return this.connector.get("resource.item", requestOptions, { id });
332
304
  }
333
305
  _fetchResourceBy(resource, requestOptions) {
334
306
  return this.getMany(resource, requestOptions).then((resources) => {
@@ -336,7 +308,7 @@ class ResourcesControl {
336
308
  });
337
309
  }
338
310
  _resourceCacheFilter(resource) {
339
- return CancelablePromise.all(this.cache.matchAll("resource.item")).then(
311
+ return Promise.all(this.cache.matchAll("resource.item")).then(
340
312
  (resources) => {
341
313
  const items = [];
342
314
  resources.filter((x) => {
@@ -380,9 +352,16 @@ function removeConnector(connector) {
380
352
  }
381
353
  }
382
354
 
383
- var __defProp$2 = Object.defineProperty;
384
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
385
- var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
355
+ class AbortError extends Error {
356
+ constructor(message = "AbortError") {
357
+ super(message);
358
+ this.name = "AbortError";
359
+ }
360
+ }
361
+
362
+ var __defProp$3 = Object.defineProperty;
363
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
364
+ var __publicField$2 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
386
365
  class InsufficientPermissionsError extends NgwError {
387
366
  constructor(obj) {
388
367
  super(obj);
@@ -405,58 +384,90 @@ function template(str, data) {
405
384
  });
406
385
  }
407
386
 
387
+ var __defProp$2 = Object.defineProperty;
388
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
389
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
390
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
391
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
392
+ var __spreadValues$1 = (a, b) => {
393
+ for (var prop in b || (b = {}))
394
+ if (__hasOwnProp$1.call(b, prop))
395
+ __defNormalProp$2(a, prop, b[prop]);
396
+ if (__getOwnPropSymbols$1)
397
+ for (var prop of __getOwnPropSymbols$1(b)) {
398
+ if (__propIsEnum$1.call(b, prop))
399
+ __defNormalProp$2(a, prop, b[prop]);
400
+ }
401
+ return a;
402
+ };
403
+ var __async$1 = (__this, __arguments, generator) => {
404
+ return new Promise((resolve, reject) => {
405
+ var fulfilled = (value) => {
406
+ try {
407
+ step(generator.next(value));
408
+ } catch (e) {
409
+ reject(e);
410
+ }
411
+ };
412
+ var rejected = (value) => {
413
+ try {
414
+ step(generator.throw(value));
415
+ } catch (e) {
416
+ reject(e);
417
+ }
418
+ };
419
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
420
+ step((generator = generator.apply(__this, __arguments)).next());
421
+ });
422
+ };
408
423
  function apiRequest(opt) {
409
- const params = opt.params;
410
- return new CancelablePromise((resolve, reject) => {
411
- return opt.connector.connect().then((apiItems) => {
412
- let apiItem = apiItems && apiItems[opt.name];
413
- if (apiItem) {
414
- apiItem = [...apiItem];
415
- let url = apiItem.shift();
416
- if (apiItem.length) {
417
- const replaceParams = {};
418
- for (let fry = 0; fry < apiItem.length; fry++) {
419
- const arg = apiItem[fry];
420
- replaceParams[fry] = "{" + arg + "}";
421
- if (params[arg] === void 0) {
422
- throw new Error(
423
- "`" + arg + "` url api argument is not specified"
424
- );
425
- }
426
- }
427
- if (url) {
428
- url = template(url, replaceParams);
424
+ return __async$1(this, null, function* () {
425
+ const { params, name, connector, requestOptions } = opt;
426
+ const apiItems = yield connector.connect();
427
+ let apiItem = apiItems && apiItems[name];
428
+ if (apiItem) {
429
+ apiItem = [...apiItem];
430
+ let url = apiItem.shift();
431
+ if (apiItem.length) {
432
+ const replaceParams = {};
433
+ for (let fry = 0; fry < apiItem.length; fry++) {
434
+ const arg = apiItem[fry];
435
+ replaceParams[fry] = `{${arg}}`;
436
+ if (params[arg] === void 0) {
437
+ throw new Error(`\`${arg}\` URL API argument is not specified`);
429
438
  }
430
439
  }
431
- if (params) {
432
- const paramArray = [];
433
- const paramList = params.paramList;
434
- if (Array.isArray(paramList)) {
435
- delete params.paramList;
436
- paramList.forEach((x) => {
437
- paramArray.push(`${x[0]}=${x[1]}`);
438
- });
439
- }
440
- for (const p in params) {
441
- if (apiItem.indexOf(p) === -1) {
442
- paramArray.push(`${p}=${params[p]}`);
443
- }
444
- }
445
- if (paramArray.length) {
446
- url = url + "?" + paramArray.join("&");
440
+ if (url) {
441
+ url = template(url, replaceParams);
442
+ }
443
+ }
444
+ if (params) {
445
+ const paramArray = [];
446
+ const paramList = params.paramList;
447
+ if (Array.isArray(paramList)) {
448
+ paramList.forEach(([key, value]) => {
449
+ paramArray.push(`${key}=${value}`);
450
+ });
451
+ }
452
+ for (const p in params) {
453
+ if (apiItem.indexOf(p) === -1) {
454
+ paramArray.push(`${p}=${params[p]}`);
447
455
  }
448
456
  }
449
- if (url) {
450
- resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
451
- } else {
452
- reject(new Error("request url is not set"));
457
+ if (paramArray.length) {
458
+ url = `${url}?${paramArray.join("&")}`;
453
459
  }
460
+ }
461
+ if (url) {
462
+ return connector.makeQuery(url, params, __spreadValues$1({
463
+ cacheName: name
464
+ }, requestOptions));
454
465
  } else {
455
- resolve(void 0);
466
+ throw new Error("Request URL is not set");
456
467
  }
457
- }).catch((er) => {
458
- reject(er);
459
- });
468
+ } else {
469
+ return void 0;
470
+ }
460
471
  });
461
472
  }
462
473
 
@@ -556,6 +567,8 @@ let loadData;
556
567
  }
557
568
 
558
569
  var __defProp = Object.defineProperty;
570
+ var __defProps = Object.defineProperties;
571
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
559
572
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
560
573
  var __hasOwnProp = Object.prototype.hasOwnProperty;
561
574
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -571,8 +584,42 @@ var __spreadValues = (a, b) => {
571
584
  }
572
585
  return a;
573
586
  };
587
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
588
+ var __objRest = (source, exclude) => {
589
+ var target = {};
590
+ for (var prop in source)
591
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
592
+ target[prop] = source[prop];
593
+ if (source != null && __getOwnPropSymbols)
594
+ for (var prop of __getOwnPropSymbols(source)) {
595
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
596
+ target[prop] = source[prop];
597
+ }
598
+ return target;
599
+ };
574
600
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
601
+ var __async = (__this, __arguments, generator) => {
602
+ return new Promise((resolve, reject) => {
603
+ var fulfilled = (value) => {
604
+ try {
605
+ step(generator.next(value));
606
+ } catch (e) {
607
+ reject(e);
608
+ }
609
+ };
610
+ var rejected = (value) => {
611
+ try {
612
+ step(generator.throw(value));
613
+ } catch (e) {
614
+ reject(e);
615
+ }
616
+ };
617
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
618
+ step((generator = generator.apply(__this, __arguments)).next());
619
+ });
620
+ };
575
621
  let ID = 0;
622
+ let REQUEST_ID = 0;
576
623
  class NgwConnector {
577
624
  constructor(options) {
578
625
  this.options = options;
@@ -583,7 +630,7 @@ class NgwConnector {
583
630
  __publicField(this, "cache");
584
631
  __publicField(this, "withCredentials");
585
632
  __publicField(this, "routeStr", "/api/component/pyramid/route");
586
- __publicField(this, "activeRequests", []);
633
+ __publicField(this, "activeRequests", {});
587
634
  __publicField(this, "requestTransform");
588
635
  const exist = findConnector(options);
589
636
  this.cache = new Cache({ namespace: options.cacheId });
@@ -644,29 +691,27 @@ class NgwConnector {
644
691
  * ```
645
692
  */
646
693
  connect() {
647
- const auth = this.options.auth;
648
- const makeConnect = () => new CancelablePromise((resolve, reject) => {
649
- const makeQuery = () => {
650
- return this.makeQuery(this.routeStr, {}, {}).then((route) => {
651
- resolve(route);
652
- }).catch((er) => {
653
- reject(er);
654
- });
655
- };
694
+ return __async(this, null, function* () {
695
+ const auth = this.options.auth;
656
696
  if (auth) {
657
697
  const { login, password } = auth;
658
698
  if (login && password) {
659
- return this._login({ login, password }).then(() => {
660
- return makeQuery();
661
- }).catch((er) => reject(er));
699
+ yield this._login({ login, password });
662
700
  }
663
701
  }
664
- return makeQuery();
665
- });
666
- return this.cache.add("route", makeConnect, {
667
- id: this.id,
668
- auth,
669
- baseUrl: this.options.baseUrl
702
+ return yield this.makeQuery(
703
+ this.routeStr,
704
+ {},
705
+ {
706
+ cacheName: "route",
707
+ cache: true,
708
+ cacheProps: {
709
+ id: this.id,
710
+ auth,
711
+ baseUrl: this.options.baseUrl
712
+ }
713
+ }
714
+ );
670
715
  });
671
716
  }
672
717
  /**
@@ -690,20 +735,22 @@ class NgwConnector {
690
735
  this.emitter.emit("logout");
691
736
  }
692
737
  getUserInfo(credentials, options) {
693
- if (this.user && this.user.id) {
694
- return CancelablePromise.resolve(this.user);
695
- }
696
- if (credentials) {
697
- this.options.auth = credentials;
698
- }
699
- const options_ = __spreadValues({
700
- headers: this.getAuthorizationHeaders(credentials)
701
- }, options);
702
- return this.makeQuery(
703
- "/api/component/auth/current_user",
704
- {},
705
- options_
706
- );
738
+ return __async(this, null, function* () {
739
+ if (this.user && this.user.id) {
740
+ return this.user;
741
+ }
742
+ if (credentials) {
743
+ this.options.auth = credentials;
744
+ }
745
+ const options_ = __spreadValues({
746
+ headers: this.getAuthorizationHeaders(credentials)
747
+ }, options);
748
+ return this.makeQuery(
749
+ "/api/component/auth/current_user",
750
+ {},
751
+ options_
752
+ );
753
+ });
707
754
  }
708
755
  /**
709
756
  * Obtaining the required Headers for authentication of requests in the NGW.
@@ -712,7 +759,7 @@ class NgwConnector {
712
759
  const client = this.makeClientId(credentials);
713
760
  if (client) {
714
761
  return {
715
- Authorization: "Basic " + client
762
+ Authorization: `Basic ${client}`
716
763
  };
717
764
  }
718
765
  }
@@ -720,21 +767,22 @@ class NgwConnector {
720
767
  credentials = credentials || this.options.auth;
721
768
  if (credentials) {
722
769
  const { login, password } = credentials;
723
- const str = unescape(encodeURIComponent(`${login}:${password}`));
770
+ const str = `${login}:${password}`;
771
+ const encodedStr = encodeURIComponent(str);
724
772
  {
725
- return window.btoa(str);
773
+ return window.btoa(encodedStr);
726
774
  }
727
775
  }
728
776
  }
729
777
  /** Stop all api requests */
730
778
  abort() {
731
- for (const req of this.activeRequests) {
732
- req.cancel();
779
+ for (const abortController of Object.values(this.activeRequests)) {
780
+ abortController.abort();
733
781
  }
734
- this.activeRequests = [];
782
+ this.activeRequests = {};
735
783
  }
736
784
  getActiveApiRequests() {
737
- return [...this.activeRequests];
785
+ return __spreadValues({}, this.activeRequests);
738
786
  }
739
787
  /**
740
788
  * Send request to NGW api router.
@@ -766,26 +814,10 @@ class NgwConnector {
766
814
  * ```
767
815
  */
768
816
  apiRequest(name, params_ = {}, requestOptions = {}) {
769
- var _a, _b;
770
- requestOptions = __spreadValues({
771
- withCredentials: this.withCredentials
772
- }, requestOptions);
773
- const { method, headers, withCredentials, responseType } = requestOptions;
817
+ var _a;
774
818
  params_ = (_a = requestOptions.params) != null ? _a : params_;
775
819
  const params = objectRemoveEmpty(params_);
776
- const makeApiRequest = () => apiRequest({ name, params, requestOptions, connector: this });
777
- if (requestOptions.cache && method === "GET") {
778
- return this.cache.add(name, makeApiRequest, __spreadValues({
779
- params
780
- }, objectRemoveEmpty({
781
- headers,
782
- withCredentials,
783
- responseType,
784
- baseUrl: this.options.baseUrl,
785
- userId: (_b = this.user) == null ? void 0 : _b.id
786
- })));
787
- }
788
- return makeApiRequest();
820
+ return apiRequest({ name, params, requestOptions, connector: this });
789
821
  }
790
822
  /**
791
823
  * Shortcut method for send POST request to NGW.
@@ -875,17 +907,68 @@ class NgwConnector {
875
907
  * @param params - Query params
876
908
  * @param options - Request options
877
909
  */
878
- makeQuery(url, params, options = {}) {
879
- url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
880
- if (url) {
910
+ makeQuery(_0, _1) {
911
+ return __async(this, arguments, function* (url, params, options = {}) {
912
+ var _b;
913
+ url = (this.options.baseUrl ? this.options.baseUrl : "") + url;
914
+ if (!url) {
915
+ throw new Error("Empty `url` not allowed");
916
+ }
881
917
  if (params) {
882
- url = template(url, params);
918
+ const _a = params, restParams = __objRest(_a, ["paramList"]);
919
+ url = template(url, restParams);
883
920
  }
884
921
  url = encodeURI(fixUrlStr(url));
885
- return this._loadData(url, options);
886
- } else {
887
- throw new Error("Empty `url` not allowed");
888
- }
922
+ options = __spreadValues({ withCredentials: this.withCredentials }, options);
923
+ const {
924
+ cache,
925
+ signal: externalSignal,
926
+ method = "GET",
927
+ headers,
928
+ cacheName,
929
+ cacheProps,
930
+ responseType,
931
+ withCredentials
932
+ } = options;
933
+ const internalAbortController = new AbortController();
934
+ const internalSignal = internalAbortController.signal;
935
+ if (externalSignal) {
936
+ if (externalSignal.aborted) {
937
+ throw new AbortError();
938
+ }
939
+ externalSignal.addEventListener("abort", () => {
940
+ internalAbortController.abort();
941
+ });
942
+ }
943
+ options.signal = internalSignal;
944
+ const createPromise = () => __async(this, null, function* () {
945
+ const id = REQUEST_ID++;
946
+ this.activeRequests[id] = internalAbortController;
947
+ try {
948
+ return this._loadData(url, options);
949
+ } finally {
950
+ this._cleanActiveRequest(id);
951
+ }
952
+ });
953
+ if (cache && method === "GET") {
954
+ const cacheOptions = cacheProps ? cacheProps : __spreadProps(__spreadValues({}, objectRemoveEmpty({
955
+ headers,
956
+ withCredentials,
957
+ responseType,
958
+ baseUrl: this.options.baseUrl,
959
+ userId: (_b = this.user) == null ? void 0 : _b.id
960
+ })), {
961
+ params
962
+ });
963
+ return this.cache.add(
964
+ cacheName || url,
965
+ createPromise,
966
+ cacheOptions,
967
+ false
968
+ );
969
+ }
970
+ return createPromise();
971
+ });
889
972
  }
890
973
  // -------------------------------------------------------------------------
891
974
  // Resource Methods
@@ -967,10 +1050,8 @@ class NgwConnector {
967
1050
  */
968
1051
  _loadData(url, options) {
969
1052
  options.responseType = options.responseType || "json";
970
- if (options.signal && options.signal.aborted) {
971
- return CancelablePromise.reject(new CancelablePromise.CancelError());
972
- }
973
- const request = new CancelablePromise((resolve, reject, onCancel) => {
1053
+ return new Promise((resolve, reject) => {
1054
+ var _a;
974
1055
  if (this.user) {
975
1056
  options = options || {};
976
1057
  options.headers = __spreadValues(__spreadValues({}, this.getAuthorizationHeaders()), options.headers);
@@ -980,13 +1061,18 @@ class NgwConnector {
980
1061
  url = transUrl;
981
1062
  options = transOptions;
982
1063
  }
983
- loadData(url, resolve, options, reject, onCancel);
984
- }).then((resp) => {
985
- this._cleanActiveRequests(request);
986
- return resp;
1064
+ let runOnAbort = void 0;
1065
+ loadData(url, resolve, options, reject, (handler) => {
1066
+ runOnAbort = handler;
1067
+ });
1068
+ (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
1069
+ if (runOnAbort !== void 0) {
1070
+ runOnAbort();
1071
+ }
1072
+ reject(new AbortError());
1073
+ });
987
1074
  }).catch((httpError) => {
988
- this._cleanActiveRequests(request);
989
- if (httpError instanceof CancelablePromise.CancelError) ; else {
1075
+ if (httpError.name !== "AbortError") {
990
1076
  if (!!(process.env.NODE_ENV !== "production")) {
991
1077
  console.warn("DEV WARN", httpError);
992
1078
  }
@@ -995,31 +1081,24 @@ class NgwConnector {
995
1081
  throw er;
996
1082
  }
997
1083
  }
1084
+ throw httpError;
998
1085
  });
999
- if (options.signal && typeof options.signal.addEventListener === "function") {
1000
- options.signal.addEventListener("abort", () => {
1001
- request.cancel();
1002
- this._cleanActiveRequests(request);
1003
- });
1004
- }
1005
- this.activeRequests.push(request);
1006
- return request;
1007
1086
  }
1008
1087
  _login(credentials, options) {
1009
- return this.getUserInfo(credentials, options).then((data) => {
1010
- this.user = data;
1011
- this.emitter.emit("login", data);
1012
- return data;
1013
- }).catch((er) => {
1014
- this.emitter.emit("login:error", er);
1015
- throw er;
1088
+ return __async(this, null, function* () {
1089
+ try {
1090
+ const data = yield this.getUserInfo(credentials, options);
1091
+ this.user = data;
1092
+ this.emitter.emit("login", data);
1093
+ return data;
1094
+ } catch (er) {
1095
+ this.emitter.emit("login:error", er);
1096
+ throw er;
1097
+ }
1016
1098
  });
1017
1099
  }
1018
- _cleanActiveRequests(request) {
1019
- const activeRequestIndex = this.activeRequests.indexOf(request);
1020
- if (activeRequestIndex !== -1) {
1021
- this.activeRequests.splice(activeRequestIndex, 1);
1022
- }
1100
+ _cleanActiveRequest(requestId) {
1101
+ delete this.activeRequests[requestId];
1023
1102
  }
1024
1103
  _handleHttpError(er) {
1025
1104
  if (er) {