@nextgis/ngw-connector 1.16.7 → 1.17.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/lib/index.d.ts +290 -79
- package/lib/ngw-connector.cjs.js +981 -1125
- package/lib/ngw-connector.cjs.js.map +1 -1
- package/lib/ngw-connector.cjs.prod.js +1 -1
- package/lib/ngw-connector.cjs.prod.js.map +1 -1
- package/lib/ngw-connector.esm-browser.js +1572 -1743
- package/lib/ngw-connector.esm-browser.js.map +1 -1
- package/lib/ngw-connector.esm-browser.prod.js +1 -1
- package/lib/ngw-connector.esm-browser.prod.js.map +1 -1
- package/lib/ngw-connector.esm-bundler.js +969 -1107
- package/lib/ngw-connector.esm-bundler.js.map +1 -1
- package/lib/ngw-connector.esm-bundler.prod.js +1 -1
- package/lib/ngw-connector.esm-bundler.prod.js.map +1 -1
- package/lib/ngw-connector.global.js +1641 -1636
- package/lib/ngw-connector.global.js.map +1 -1
- package/lib/ngw-connector.global.prod.js +1 -1
- package/lib/ngw-connector.global.prod.js.map +1 -1
- package/package.json +8 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Bundle of @nextgis/ngw-connector; version: 1.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 1.17.0; author: NextGIS */
|
|
2
2
|
var NgwConnector = (function () {
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
@@ -16,7 +16,7 @@ var NgwConnector = (function () {
|
|
|
16
16
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
17
|
PERFORMANCE OF THIS SOFTWARE.
|
|
18
18
|
***************************************************************************** */
|
|
19
|
-
/* global Reflect, Promise */
|
|
19
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
20
20
|
|
|
21
21
|
var extendStatics = function(d, b) {
|
|
22
22
|
extendStatics = Object.setPrototypeOf ||
|
|
@@ -60,7 +60,7 @@ var NgwConnector = (function () {
|
|
|
60
60
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
61
61
|
function step(op) {
|
|
62
62
|
if (f) throw new TypeError("Generator is already executing.");
|
|
63
|
-
while (_) try {
|
|
63
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
64
64
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
65
65
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
66
66
|
switch (op[0]) {
|
|
@@ -90,7 +90,12 @@ var NgwConnector = (function () {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
93
|
-
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
96
|
+
var e = new Error(message);
|
|
97
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
98
|
+
};
|
|
94
99
|
|
|
95
100
|
var events = {exports: {}};
|
|
96
101
|
|
|
@@ -568,1686 +573,1686 @@ var NgwConnector = (function () {
|
|
|
568
573
|
}
|
|
569
574
|
}
|
|
570
575
|
|
|
571
|
-
var CancelError = /** @class */ (function (_super) {
|
|
572
|
-
__extends(CancelError, _super);
|
|
573
|
-
function CancelError() {
|
|
574
|
-
var _this = _super.call(this) || this;
|
|
575
|
-
_this.name = 'CancelError';
|
|
576
|
-
Object.setPrototypeOf(_this, CancelError.prototype);
|
|
577
|
-
return _this;
|
|
578
|
-
}
|
|
579
|
-
return CancelError;
|
|
576
|
+
var CancelError = /** @class */ (function (_super) {
|
|
577
|
+
__extends(CancelError, _super);
|
|
578
|
+
function CancelError() {
|
|
579
|
+
var _this = _super.call(this) || this;
|
|
580
|
+
_this.name = 'CancelError';
|
|
581
|
+
Object.setPrototypeOf(_this, CancelError.prototype);
|
|
582
|
+
return _this;
|
|
583
|
+
}
|
|
584
|
+
return CancelError;
|
|
580
585
|
}(Error));
|
|
581
586
|
|
|
582
|
-
var PromiseControl = /** @class */ (function () {
|
|
583
|
-
function PromiseControl(options) {
|
|
584
|
-
if (options === void 0) { options = {}; }
|
|
585
|
-
this.options = options;
|
|
586
|
-
this._promises = new Map();
|
|
587
|
-
}
|
|
588
|
-
Object.defineProperty(PromiseControl.prototype, "isLoaded", {
|
|
589
|
-
get: function () {
|
|
590
|
-
return this._promises.size > 0;
|
|
591
|
-
},
|
|
592
|
-
enumerable: false,
|
|
593
|
-
configurable: true
|
|
594
|
-
});
|
|
595
|
-
PromiseControl.prototype.remove = function (promise) {
|
|
596
|
-
if (this._promises.has(promise)) {
|
|
597
|
-
this._promises.delete(promise);
|
|
598
|
-
this._onStop();
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
PromiseControl.prototype.get = function (promise) {
|
|
602
|
-
return this._promises.get(promise);
|
|
603
|
-
};
|
|
604
|
-
PromiseControl.prototype.add = function (promise, name) {
|
|
605
|
-
var _this = this;
|
|
606
|
-
var key = name ? name : promise;
|
|
607
|
-
var exist = this._promises.get(key);
|
|
608
|
-
if (this.options.onStart && !this.isLoaded) {
|
|
609
|
-
this.options.onStart();
|
|
610
|
-
}
|
|
611
|
-
if (exist) {
|
|
612
|
-
return exist;
|
|
613
|
-
}
|
|
614
|
-
this._promises.set(key, promise);
|
|
615
|
-
promise.finally(function () {
|
|
616
|
-
_this.remove(key);
|
|
617
|
-
});
|
|
618
|
-
return promise;
|
|
619
|
-
};
|
|
620
|
-
PromiseControl.prototype.abort = function () {
|
|
621
|
-
if (this.isLoaded) {
|
|
622
|
-
this._promises.forEach(function (x) {
|
|
623
|
-
if (x.cancel) {
|
|
624
|
-
x.cancel();
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
this._promises.clear();
|
|
628
|
-
this._onStop();
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
PromiseControl.prototype.waitFunc = function (func, name) {
|
|
632
|
-
if (name === void 0) { name = ''; }
|
|
633
|
-
name = name || func.name;
|
|
634
|
-
var exist = this.get(name);
|
|
635
|
-
if (exist) {
|
|
636
|
-
return exist;
|
|
637
|
-
}
|
|
638
|
-
return this.add(func(), name);
|
|
639
|
-
};
|
|
640
|
-
PromiseControl.prototype.WaitForMe = function (name) {
|
|
641
|
-
if (name === void 0) { name = ''; }
|
|
642
|
-
var get = this.get.bind(this);
|
|
643
|
-
var add = this.add.bind(this);
|
|
644
|
-
return function (target, key, descriptor) {
|
|
645
|
-
var originalMethod = descriptor.value;
|
|
646
|
-
name = name || key;
|
|
647
|
-
descriptor.value = function () {
|
|
648
|
-
var args = [];
|
|
649
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
650
|
-
args[_i] = arguments[_i];
|
|
651
|
-
}
|
|
652
|
-
var exist = get(name);
|
|
653
|
-
if (exist) {
|
|
654
|
-
return exist;
|
|
655
|
-
}
|
|
656
|
-
var result = add(originalMethod.apply(this, args), name);
|
|
657
|
-
return result;
|
|
658
|
-
};
|
|
659
|
-
return descriptor;
|
|
660
|
-
};
|
|
661
|
-
};
|
|
662
|
-
/** @deprecated use {@link PromiseControl.WaitForMe } instead */
|
|
663
|
-
PromiseControl.prototype.GetOrCreateDecorator = function (name) {
|
|
664
|
-
if (name === void 0) { name = ''; }
|
|
665
|
-
return this.WaitForMe(name);
|
|
666
|
-
};
|
|
667
|
-
PromiseControl.prototype._onStop = function () {
|
|
668
|
-
if (this.options.onStop && !this.isLoaded) {
|
|
669
|
-
this.options.onStop();
|
|
670
|
-
}
|
|
671
|
-
};
|
|
672
|
-
return PromiseControl;
|
|
587
|
+
var PromiseControl = /** @class */ (function () {
|
|
588
|
+
function PromiseControl(options) {
|
|
589
|
+
if (options === void 0) { options = {}; }
|
|
590
|
+
this.options = options;
|
|
591
|
+
this._promises = new Map();
|
|
592
|
+
}
|
|
593
|
+
Object.defineProperty(PromiseControl.prototype, "isLoaded", {
|
|
594
|
+
get: function () {
|
|
595
|
+
return this._promises.size > 0;
|
|
596
|
+
},
|
|
597
|
+
enumerable: false,
|
|
598
|
+
configurable: true
|
|
599
|
+
});
|
|
600
|
+
PromiseControl.prototype.remove = function (promise) {
|
|
601
|
+
if (this._promises.has(promise)) {
|
|
602
|
+
this._promises.delete(promise);
|
|
603
|
+
this._onStop();
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
PromiseControl.prototype.get = function (promise) {
|
|
607
|
+
return this._promises.get(promise);
|
|
608
|
+
};
|
|
609
|
+
PromiseControl.prototype.add = function (promise, name) {
|
|
610
|
+
var _this = this;
|
|
611
|
+
var key = name ? name : promise;
|
|
612
|
+
var exist = this._promises.get(key);
|
|
613
|
+
if (this.options.onStart && !this.isLoaded) {
|
|
614
|
+
this.options.onStart();
|
|
615
|
+
}
|
|
616
|
+
if (exist) {
|
|
617
|
+
return exist;
|
|
618
|
+
}
|
|
619
|
+
this._promises.set(key, promise);
|
|
620
|
+
promise.finally(function () {
|
|
621
|
+
_this.remove(key);
|
|
622
|
+
});
|
|
623
|
+
return promise;
|
|
624
|
+
};
|
|
625
|
+
PromiseControl.prototype.abort = function () {
|
|
626
|
+
if (this.isLoaded) {
|
|
627
|
+
this._promises.forEach(function (x) {
|
|
628
|
+
if (x.cancel) {
|
|
629
|
+
x.cancel();
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
this._promises.clear();
|
|
633
|
+
this._onStop();
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
PromiseControl.prototype.waitFunc = function (func, name) {
|
|
637
|
+
if (name === void 0) { name = ''; }
|
|
638
|
+
name = name || func.name;
|
|
639
|
+
var exist = this.get(name);
|
|
640
|
+
if (exist) {
|
|
641
|
+
return exist;
|
|
642
|
+
}
|
|
643
|
+
return this.add(func(), name);
|
|
644
|
+
};
|
|
645
|
+
PromiseControl.prototype.WaitForMe = function (name) {
|
|
646
|
+
if (name === void 0) { name = ''; }
|
|
647
|
+
var get = this.get.bind(this);
|
|
648
|
+
var add = this.add.bind(this);
|
|
649
|
+
return function (target, key, descriptor) {
|
|
650
|
+
var originalMethod = descriptor.value;
|
|
651
|
+
name = name || key;
|
|
652
|
+
descriptor.value = function () {
|
|
653
|
+
var args = [];
|
|
654
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
655
|
+
args[_i] = arguments[_i];
|
|
656
|
+
}
|
|
657
|
+
var exist = get(name);
|
|
658
|
+
if (exist) {
|
|
659
|
+
return exist;
|
|
660
|
+
}
|
|
661
|
+
var result = add(originalMethod.apply(this, args), name);
|
|
662
|
+
return result;
|
|
663
|
+
};
|
|
664
|
+
return descriptor;
|
|
665
|
+
};
|
|
666
|
+
};
|
|
667
|
+
/** @deprecated use {@link PromiseControl.WaitForMe } instead */
|
|
668
|
+
PromiseControl.prototype.GetOrCreateDecorator = function (name) {
|
|
669
|
+
if (name === void 0) { name = ''; }
|
|
670
|
+
return this.WaitForMe(name);
|
|
671
|
+
};
|
|
672
|
+
PromiseControl.prototype._onStop = function () {
|
|
673
|
+
if (this.options.onStop && !this.isLoaded) {
|
|
674
|
+
this.options.onStop();
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
return PromiseControl;
|
|
673
678
|
}());
|
|
674
679
|
|
|
675
|
-
var TimeoutError = /** @class */ (function (_super) {
|
|
676
|
-
__extends(TimeoutError, _super);
|
|
677
|
-
function TimeoutError() {
|
|
678
|
-
var _this = _super.call(this) || this;
|
|
679
|
-
_this.name = 'TimeoutError';
|
|
680
|
-
Object.setPrototypeOf(_this, TimeoutError.prototype);
|
|
681
|
-
return _this;
|
|
682
|
-
}
|
|
683
|
-
return TimeoutError;
|
|
680
|
+
var TimeoutError = /** @class */ (function (_super) {
|
|
681
|
+
__extends(TimeoutError, _super);
|
|
682
|
+
function TimeoutError() {
|
|
683
|
+
var _this = _super.call(this) || this;
|
|
684
|
+
_this.name = 'TimeoutError';
|
|
685
|
+
Object.setPrototypeOf(_this, TimeoutError.prototype);
|
|
686
|
+
return _this;
|
|
687
|
+
}
|
|
688
|
+
return TimeoutError;
|
|
684
689
|
}(Error));
|
|
685
690
|
|
|
686
|
-
var handleCallback = function (resolve, reject, callback, r) {
|
|
687
|
-
try {
|
|
688
|
-
resolve(callback(r));
|
|
689
|
-
}
|
|
690
|
-
catch (e) {
|
|
691
|
-
reject(e);
|
|
692
|
-
}
|
|
693
|
-
};
|
|
694
|
-
var ID$1 = 0;
|
|
695
|
-
/**
|
|
696
|
-
* Promise that can be canceled
|
|
697
|
-
*
|
|
698
|
-
* @example
|
|
699
|
-
* Catch `CancelError`
|
|
700
|
-
* ```javascript
|
|
701
|
-
* import CancelablePromise from "@nextgis/cancelable-promise";
|
|
702
|
-
*
|
|
703
|
-
* const promise = new CancelablePromise((resolve, reject) => {
|
|
704
|
-
* setTimeout(() => resolve(), 100);
|
|
705
|
-
* }).catch((er) => {
|
|
706
|
-
* if (er.name === "CancelError") {
|
|
707
|
-
* // handle cancel error
|
|
708
|
-
* }
|
|
709
|
-
* throw er;
|
|
710
|
-
* });
|
|
711
|
-
*
|
|
712
|
-
* promise.cancel();
|
|
713
|
-
* ```
|
|
714
|
-
* @example
|
|
715
|
-
* Handle `onCancel` callback
|
|
716
|
-
* ```javascript
|
|
717
|
-
* import CancelablePromise from "@nextgis/cancelable-promise";
|
|
718
|
-
*
|
|
719
|
-
* const promise = new CancelablePromise((resolve, reject, onCancel) => {
|
|
720
|
-
* const xhr = new XMLHttpRequest();
|
|
721
|
-
* xhr.open("GET", url, true);
|
|
722
|
-
* xhr.onload = () => {
|
|
723
|
-
* resolve(xhr.responseText);
|
|
724
|
-
* };
|
|
725
|
-
* xhr.onerror = (er) => {
|
|
726
|
-
* reject(er);
|
|
727
|
-
* };
|
|
728
|
-
*
|
|
729
|
-
* onCancel(() => {
|
|
730
|
-
* xhr.abort();
|
|
731
|
-
* });
|
|
732
|
-
*
|
|
733
|
-
* xhr.send();
|
|
734
|
-
* });
|
|
735
|
-
*
|
|
736
|
-
* promise.cancel();
|
|
737
|
-
* ```
|
|
738
|
-
*/
|
|
739
|
-
var CancelablePromise$1 = /** @class */ (function () {
|
|
740
|
-
function CancelablePromise(executor, timeout) {
|
|
741
|
-
var _this = this;
|
|
742
|
-
this.id = ID$1++;
|
|
743
|
-
this._isCanceled = false;
|
|
744
|
-
this._isPending = true;
|
|
745
|
-
this._cancelHandlers = [];
|
|
746
|
-
this._children = [];
|
|
747
|
-
this._cancelPromise = new Promise(function (resolve_, reject_) {
|
|
748
|
-
_this._setCanceledCallback = function (er) { return resolve_(er || new CancelError()); };
|
|
749
|
-
});
|
|
750
|
-
var promises = [
|
|
751
|
-
this._cancelPromise,
|
|
752
|
-
new Promise(function (resolve, reject) {
|
|
753
|
-
var onResolve = function (value) {
|
|
754
|
-
if (value instanceof CancelablePromise) {
|
|
755
|
-
_this.attach(value);
|
|
756
|
-
}
|
|
757
|
-
else {
|
|
758
|
-
_this._isPending = false;
|
|
759
|
-
}
|
|
760
|
-
// TODO: fix types, `undefined` not allowed since 19.12.2020
|
|
761
|
-
resolve(value);
|
|
762
|
-
};
|
|
763
|
-
var onReject = function (error) {
|
|
764
|
-
_this._isPending = false;
|
|
765
|
-
reject(error);
|
|
766
|
-
};
|
|
767
|
-
var onCancel = function (handler) {
|
|
768
|
-
if (!_this._isPending) {
|
|
769
|
-
throw new Error('The `onCancel` handler was attached after the promise settled.');
|
|
770
|
-
}
|
|
771
|
-
_this._cancelHandlers.push(handler);
|
|
772
|
-
};
|
|
773
|
-
return executor(onResolve, onReject, onCancel);
|
|
774
|
-
}),
|
|
775
|
-
];
|
|
776
|
-
if (timeout) {
|
|
777
|
-
promises.push(new Promise(function (resolve, reject) {
|
|
778
|
-
setTimeout(function () {
|
|
779
|
-
if (_this._isPending) {
|
|
780
|
-
try {
|
|
781
|
-
_this.cancel();
|
|
782
|
-
}
|
|
783
|
-
finally {
|
|
784
|
-
reject(new TimeoutError());
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
}, timeout);
|
|
788
|
-
}));
|
|
789
|
-
}
|
|
790
|
-
this._promise = Promise.race(promises);
|
|
791
|
-
}
|
|
792
|
-
CancelablePromise.createControl = function (opt) {
|
|
793
|
-
return new PromiseControl(opt);
|
|
794
|
-
};
|
|
795
|
-
CancelablePromise.resolve = function (value) {
|
|
796
|
-
return new CancelablePromise(function (resolve) { return resolve(value); });
|
|
797
|
-
};
|
|
798
|
-
CancelablePromise.reject = function (value) {
|
|
799
|
-
return new CancelablePromise(function (resolve, reject) { return reject(value); });
|
|
800
|
-
};
|
|
801
|
-
CancelablePromise.all = function (values) {
|
|
802
|
-
var _this = this;
|
|
803
|
-
return new CancelablePromise(function (resolve, reject) {
|
|
804
|
-
Promise.all(values).then(resolve).catch(reject);
|
|
805
|
-
}).catch(function (er) {
|
|
806
|
-
if (er instanceof _this.CancelError) {
|
|
807
|
-
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
|
|
808
|
-
var v = values_1[_i];
|
|
809
|
-
var v_ = v;
|
|
810
|
-
if ('cancel' in v_) {
|
|
811
|
-
v_.cancel();
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
throw er;
|
|
816
|
-
});
|
|
817
|
-
};
|
|
818
|
-
CancelablePromise.prototype.attach = function (p) {
|
|
819
|
-
if (this._isCanceled) {
|
|
820
|
-
p.cancel();
|
|
821
|
-
}
|
|
822
|
-
else {
|
|
823
|
-
this._children.push(p);
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
CancelablePromise.prototype.then = function (onfulfilled, onrejected) {
|
|
827
|
-
var _this = this;
|
|
828
|
-
var p = new CancelablePromise(function (resolve, reject) {
|
|
829
|
-
if (_this._promise) {
|
|
830
|
-
var reject_1 = function (r) {
|
|
831
|
-
if (onrejected) {
|
|
832
|
-
handleCallback(resolve, reject, onrejected, r);
|
|
833
|
-
}
|
|
834
|
-
else {
|
|
835
|
-
reject(r);
|
|
836
|
-
}
|
|
837
|
-
};
|
|
838
|
-
_this._promise.then(function (r) {
|
|
839
|
-
if (_this._isCanceled) {
|
|
840
|
-
reject_1(r);
|
|
841
|
-
}
|
|
842
|
-
else {
|
|
843
|
-
if (onfulfilled) {
|
|
844
|
-
handleCallback(resolve, reject, onfulfilled, r);
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
resolve(r);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
}, reject_1);
|
|
851
|
-
}
|
|
852
|
-
});
|
|
853
|
-
p._parentPromise = this;
|
|
854
|
-
this._children.push(p);
|
|
855
|
-
return p;
|
|
856
|
-
};
|
|
857
|
-
CancelablePromise.prototype.catch = function (onrejected) {
|
|
858
|
-
if (this._isCanceled && onrejected) {
|
|
859
|
-
onrejected(new CancelError());
|
|
860
|
-
}
|
|
861
|
-
return this.then(undefined, onrejected);
|
|
862
|
-
};
|
|
863
|
-
CancelablePromise.prototype.finally = function (onfinally) {
|
|
864
|
-
if (this._promise) {
|
|
865
|
-
return this._promise.finally(onfinally);
|
|
866
|
-
}
|
|
867
|
-
if (this._isCanceled) {
|
|
868
|
-
return Promise.reject(new CancelError());
|
|
869
|
-
}
|
|
870
|
-
return Promise.reject(onfinally);
|
|
871
|
-
};
|
|
872
|
-
CancelablePromise.prototype.cancel = function () {
|
|
873
|
-
// No reason to run cancel action if promise is already complete
|
|
874
|
-
if (this._isCanceled || !this._isPending) {
|
|
875
|
-
return this;
|
|
876
|
-
}
|
|
877
|
-
this._isCanceled = true;
|
|
878
|
-
var parent = this._getTopParent();
|
|
879
|
-
if (parent) {
|
|
880
|
-
parent.cancel();
|
|
881
|
-
}
|
|
882
|
-
if (this._children) {
|
|
883
|
-
this._children.forEach(function (x) { return x.cancel(); });
|
|
884
|
-
}
|
|
885
|
-
if (this._isPending) {
|
|
886
|
-
if (this._cancelHandlers.length) {
|
|
887
|
-
try {
|
|
888
|
-
for (var _i = 0, _a = this._cancelHandlers; _i < _a.length; _i++) {
|
|
889
|
-
var handler = _a[_i];
|
|
890
|
-
handler();
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
catch (error) {
|
|
894
|
-
// this._setCanceledCallback(error);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
if (this._setCanceledCallback) {
|
|
898
|
-
this._setCanceledCallback();
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
this._destroy();
|
|
902
|
-
return this;
|
|
903
|
-
};
|
|
904
|
-
CancelablePromise.prototype._getTopParent = function () {
|
|
905
|
-
var parent = this._parentPromise;
|
|
906
|
-
var hasParent = !!parent;
|
|
907
|
-
while (hasParent) {
|
|
908
|
-
if (parent && parent._parentPromise) {
|
|
909
|
-
parent = parent._parentPromise;
|
|
910
|
-
hasParent = !!parent;
|
|
911
|
-
}
|
|
912
|
-
else {
|
|
913
|
-
hasParent = false;
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
return parent;
|
|
917
|
-
};
|
|
918
|
-
CancelablePromise.prototype._destroy = function () {
|
|
919
|
-
this._setCanceledCallback = undefined;
|
|
920
|
-
this._cancelPromise = undefined;
|
|
921
|
-
this._promise = undefined;
|
|
922
|
-
};
|
|
923
|
-
CancelablePromise.CancelError = CancelError;
|
|
924
|
-
CancelablePromise.TimeoutError = TimeoutError;
|
|
925
|
-
CancelablePromise.PromiseControl = PromiseControl;
|
|
926
|
-
return CancelablePromise;
|
|
927
|
-
}());
|
|
691
|
+
var handleCallback = function (resolve, reject, callback, r) {
|
|
692
|
+
try {
|
|
693
|
+
resolve(callback(r));
|
|
694
|
+
}
|
|
695
|
+
catch (e) {
|
|
696
|
+
reject(e);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
var ID$1 = 0;
|
|
700
|
+
/**
|
|
701
|
+
* Promise that can be canceled
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* Catch `CancelError`
|
|
705
|
+
* ```javascript
|
|
706
|
+
* import CancelablePromise from "@nextgis/cancelable-promise";
|
|
707
|
+
*
|
|
708
|
+
* const promise = new CancelablePromise((resolve, reject) => {
|
|
709
|
+
* setTimeout(() => resolve(), 100);
|
|
710
|
+
* }).catch((er) => {
|
|
711
|
+
* if (er.name === "CancelError") {
|
|
712
|
+
* // handle cancel error
|
|
713
|
+
* }
|
|
714
|
+
* throw er;
|
|
715
|
+
* });
|
|
716
|
+
*
|
|
717
|
+
* promise.cancel();
|
|
718
|
+
* ```
|
|
719
|
+
* @example
|
|
720
|
+
* Handle `onCancel` callback
|
|
721
|
+
* ```javascript
|
|
722
|
+
* import CancelablePromise from "@nextgis/cancelable-promise";
|
|
723
|
+
*
|
|
724
|
+
* const promise = new CancelablePromise((resolve, reject, onCancel) => {
|
|
725
|
+
* const xhr = new XMLHttpRequest();
|
|
726
|
+
* xhr.open("GET", url, true);
|
|
727
|
+
* xhr.onload = () => {
|
|
728
|
+
* resolve(xhr.responseText);
|
|
729
|
+
* };
|
|
730
|
+
* xhr.onerror = (er) => {
|
|
731
|
+
* reject(er);
|
|
732
|
+
* };
|
|
733
|
+
*
|
|
734
|
+
* onCancel(() => {
|
|
735
|
+
* xhr.abort();
|
|
736
|
+
* });
|
|
737
|
+
*
|
|
738
|
+
* xhr.send();
|
|
739
|
+
* });
|
|
740
|
+
*
|
|
741
|
+
* promise.cancel();
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
var CancelablePromise$1 = /** @class */ (function () {
|
|
745
|
+
function CancelablePromise(executor, timeout) {
|
|
746
|
+
var _this = this;
|
|
747
|
+
this.id = ID$1++;
|
|
748
|
+
this._isCanceled = false;
|
|
749
|
+
this._isPending = true;
|
|
750
|
+
this._cancelHandlers = [];
|
|
751
|
+
this._children = [];
|
|
752
|
+
this._cancelPromise = new Promise(function (resolve_, reject_) {
|
|
753
|
+
_this._setCanceledCallback = function (er) { return resolve_(er || new CancelError()); };
|
|
754
|
+
});
|
|
755
|
+
var promises = [
|
|
756
|
+
this._cancelPromise,
|
|
757
|
+
new Promise(function (resolve, reject) {
|
|
758
|
+
var onResolve = function (value) {
|
|
759
|
+
if (value instanceof CancelablePromise) {
|
|
760
|
+
_this.attach(value);
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
_this._isPending = false;
|
|
764
|
+
}
|
|
765
|
+
// TODO: fix types, `undefined` not allowed since 19.12.2020
|
|
766
|
+
resolve(value);
|
|
767
|
+
};
|
|
768
|
+
var onReject = function (error) {
|
|
769
|
+
_this._isPending = false;
|
|
770
|
+
reject(error);
|
|
771
|
+
};
|
|
772
|
+
var onCancel = function (handler) {
|
|
773
|
+
if (!_this._isPending) {
|
|
774
|
+
throw new Error('The `onCancel` handler was attached after the promise settled.');
|
|
775
|
+
}
|
|
776
|
+
_this._cancelHandlers.push(handler);
|
|
777
|
+
};
|
|
778
|
+
return executor(onResolve, onReject, onCancel);
|
|
779
|
+
}),
|
|
780
|
+
];
|
|
781
|
+
if (timeout) {
|
|
782
|
+
promises.push(new Promise(function (resolve, reject) {
|
|
783
|
+
setTimeout(function () {
|
|
784
|
+
if (_this._isPending) {
|
|
785
|
+
try {
|
|
786
|
+
_this.cancel();
|
|
787
|
+
}
|
|
788
|
+
finally {
|
|
789
|
+
reject(new TimeoutError());
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}, timeout);
|
|
793
|
+
}));
|
|
794
|
+
}
|
|
795
|
+
this._promise = Promise.race(promises);
|
|
796
|
+
}
|
|
797
|
+
CancelablePromise.createControl = function (opt) {
|
|
798
|
+
return new PromiseControl(opt);
|
|
799
|
+
};
|
|
800
|
+
CancelablePromise.resolve = function (value) {
|
|
801
|
+
return new CancelablePromise(function (resolve) { return resolve(value); });
|
|
802
|
+
};
|
|
803
|
+
CancelablePromise.reject = function (value) {
|
|
804
|
+
return new CancelablePromise(function (resolve, reject) { return reject(value); });
|
|
805
|
+
};
|
|
806
|
+
CancelablePromise.all = function (values) {
|
|
807
|
+
var _this = this;
|
|
808
|
+
return new CancelablePromise(function (resolve, reject) {
|
|
809
|
+
Promise.all(values).then(resolve).catch(reject);
|
|
810
|
+
}).catch(function (er) {
|
|
811
|
+
if (er instanceof _this.CancelError) {
|
|
812
|
+
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
|
|
813
|
+
var v = values_1[_i];
|
|
814
|
+
var v_ = v;
|
|
815
|
+
if ('cancel' in v_) {
|
|
816
|
+
v_.cancel();
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
throw er;
|
|
821
|
+
});
|
|
822
|
+
};
|
|
823
|
+
CancelablePromise.prototype.attach = function (p) {
|
|
824
|
+
if (this._isCanceled) {
|
|
825
|
+
p.cancel();
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
this._children.push(p);
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
CancelablePromise.prototype.then = function (onfulfilled, onrejected) {
|
|
832
|
+
var _this = this;
|
|
833
|
+
var p = new CancelablePromise(function (resolve, reject) {
|
|
834
|
+
if (_this._promise) {
|
|
835
|
+
var reject_1 = function (r) {
|
|
836
|
+
if (onrejected) {
|
|
837
|
+
handleCallback(resolve, reject, onrejected, r);
|
|
838
|
+
}
|
|
839
|
+
else {
|
|
840
|
+
reject(r);
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
_this._promise.then(function (r) {
|
|
844
|
+
if (_this._isCanceled) {
|
|
845
|
+
reject_1(r);
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
if (onfulfilled) {
|
|
849
|
+
handleCallback(resolve, reject, onfulfilled, r);
|
|
850
|
+
}
|
|
851
|
+
else {
|
|
852
|
+
resolve(r);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}, reject_1);
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
p._parentPromise = this;
|
|
859
|
+
this._children.push(p);
|
|
860
|
+
return p;
|
|
861
|
+
};
|
|
862
|
+
CancelablePromise.prototype.catch = function (onrejected) {
|
|
863
|
+
if (this._isCanceled && onrejected) {
|
|
864
|
+
onrejected(new CancelError());
|
|
865
|
+
}
|
|
866
|
+
return this.then(undefined, onrejected);
|
|
867
|
+
};
|
|
868
|
+
CancelablePromise.prototype.finally = function (onfinally) {
|
|
869
|
+
if (this._promise) {
|
|
870
|
+
return this._promise.finally(onfinally);
|
|
871
|
+
}
|
|
872
|
+
if (this._isCanceled) {
|
|
873
|
+
return Promise.reject(new CancelError());
|
|
874
|
+
}
|
|
875
|
+
return Promise.reject(onfinally);
|
|
876
|
+
};
|
|
877
|
+
CancelablePromise.prototype.cancel = function () {
|
|
878
|
+
// No reason to run cancel action if promise is already complete
|
|
879
|
+
if (this._isCanceled || !this._isPending) {
|
|
880
|
+
return this;
|
|
881
|
+
}
|
|
882
|
+
this._isCanceled = true;
|
|
883
|
+
var parent = this._getTopParent();
|
|
884
|
+
if (parent) {
|
|
885
|
+
parent.cancel();
|
|
886
|
+
}
|
|
887
|
+
if (this._children) {
|
|
888
|
+
this._children.forEach(function (x) { return x.cancel(); });
|
|
889
|
+
}
|
|
890
|
+
if (this._isPending) {
|
|
891
|
+
if (this._cancelHandlers.length) {
|
|
892
|
+
try {
|
|
893
|
+
for (var _i = 0, _a = this._cancelHandlers; _i < _a.length; _i++) {
|
|
894
|
+
var handler = _a[_i];
|
|
895
|
+
handler();
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
catch (error) {
|
|
899
|
+
// this._setCanceledCallback(error);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
if (this._setCanceledCallback) {
|
|
903
|
+
this._setCanceledCallback();
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
this._destroy();
|
|
907
|
+
return this;
|
|
908
|
+
};
|
|
909
|
+
CancelablePromise.prototype._getTopParent = function () {
|
|
910
|
+
var parent = this._parentPromise;
|
|
911
|
+
var hasParent = !!parent;
|
|
912
|
+
while (hasParent) {
|
|
913
|
+
if (parent && parent._parentPromise) {
|
|
914
|
+
parent = parent._parentPromise;
|
|
915
|
+
hasParent = !!parent;
|
|
916
|
+
}
|
|
917
|
+
else {
|
|
918
|
+
hasParent = false;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
return parent;
|
|
922
|
+
};
|
|
923
|
+
CancelablePromise.prototype._destroy = function () {
|
|
924
|
+
this._setCanceledCallback = undefined;
|
|
925
|
+
this._cancelPromise = undefined;
|
|
926
|
+
this._promise = undefined;
|
|
927
|
+
};
|
|
928
|
+
CancelablePromise.CancelError = CancelError;
|
|
929
|
+
CancelablePromise.TimeoutError = TimeoutError;
|
|
930
|
+
CancelablePromise.PromiseControl = PromiseControl;
|
|
931
|
+
return CancelablePromise;
|
|
932
|
+
}());
|
|
928
933
|
Object.setPrototypeOf(CancelablePromise$1.prototype, Promise.prototype);
|
|
929
934
|
|
|
930
|
-
/**
|
|
931
|
-
* A promise you can stop
|
|
932
|
-
*
|
|
933
|
-
* @packageDocumentation
|
|
934
|
-
* @module cancelable-promise
|
|
935
|
-
*/
|
|
935
|
+
/**
|
|
936
|
+
* A promise you can stop
|
|
937
|
+
*
|
|
938
|
+
* @packageDocumentation
|
|
939
|
+
* @module cancelable-promise
|
|
940
|
+
*/
|
|
936
941
|
var CancelablePromise = CancelablePromise$1;
|
|
937
942
|
|
|
938
|
-
/**
|
|
939
|
-
* from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
|
|
940
|
-
*
|
|
941
|
-
* @param val - The object.
|
|
942
|
-
* @returns Returns true if the object is defined, returns false otherwise.
|
|
943
|
-
*
|
|
944
|
-
* @example
|
|
945
|
-
* ```javascript
|
|
946
|
-
* if (defined(positions)) {
|
|
947
|
-
* doSomething();
|
|
948
|
-
* } else {
|
|
949
|
-
* doSomethingElse();
|
|
950
|
-
* }
|
|
951
|
-
* ```
|
|
952
|
-
*/
|
|
953
|
-
function defined(val) {
|
|
954
|
-
return val !== undefined && val !== null;
|
|
955
|
-
}
|
|
956
|
-
/**
|
|
957
|
-
* from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
|
|
958
|
-
*
|
|
959
|
-
* @param val - The object.
|
|
960
|
-
* @returns Returns true if the object is defined and not empty string, returns false otherwise.
|
|
961
|
-
*
|
|
962
|
-
* @example
|
|
963
|
-
* ```javascript
|
|
964
|
-
* full('foo') // true
|
|
965
|
-
* full('') // false
|
|
966
|
-
* full(undefined) // false
|
|
967
|
-
* full(0) // true
|
|
968
|
-
* ```
|
|
969
|
-
*/
|
|
970
|
-
function full(val) {
|
|
971
|
-
return typeof val === 'string' ? !!val : defined(val);
|
|
943
|
+
/**
|
|
944
|
+
* from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
|
|
945
|
+
*
|
|
946
|
+
* @param val - The object.
|
|
947
|
+
* @returns Returns true if the object is defined, returns false otherwise.
|
|
948
|
+
*
|
|
949
|
+
* @example
|
|
950
|
+
* ```javascript
|
|
951
|
+
* if (defined(positions)) {
|
|
952
|
+
* doSomething();
|
|
953
|
+
* } else {
|
|
954
|
+
* doSomethingElse();
|
|
955
|
+
* }
|
|
956
|
+
* ```
|
|
957
|
+
*/
|
|
958
|
+
function defined(val) {
|
|
959
|
+
return val !== undefined && val !== null;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
|
|
963
|
+
*
|
|
964
|
+
* @param val - The object.
|
|
965
|
+
* @returns Returns true if the object is defined and not empty string, returns false otherwise.
|
|
966
|
+
*
|
|
967
|
+
* @example
|
|
968
|
+
* ```javascript
|
|
969
|
+
* full('foo') // true
|
|
970
|
+
* full('') // false
|
|
971
|
+
* full(undefined) // false
|
|
972
|
+
* full(0) // true
|
|
973
|
+
* ```
|
|
974
|
+
*/
|
|
975
|
+
function full(val) {
|
|
976
|
+
return typeof val === 'string' ? !!val : defined(val);
|
|
972
977
|
}
|
|
973
978
|
|
|
974
|
-
function isEqual(a, b, o, p) {
|
|
975
|
-
if (a instanceof Array) {
|
|
976
|
-
if (!(b instanceof Array))
|
|
977
|
-
return false;
|
|
978
|
-
//if (compareObjects(oVal, pVal === false) return false
|
|
979
|
-
//would work, too, and perhaps is a better fit, still, this is easy, too
|
|
980
|
-
return b.sort().join('') === a.sort().join('');
|
|
981
|
-
}
|
|
982
|
-
else if (a instanceof Date) {
|
|
983
|
-
if (!(b instanceof Date))
|
|
984
|
-
return false;
|
|
985
|
-
return '' + a === '' + b;
|
|
986
|
-
}
|
|
987
|
-
else if (a instanceof Function) {
|
|
988
|
-
if (!(b instanceof Function))
|
|
989
|
-
return false;
|
|
990
|
-
//ignore functions, or check them regardless?
|
|
991
|
-
}
|
|
992
|
-
else if (a instanceof Object) {
|
|
993
|
-
if (!(b instanceof Object))
|
|
994
|
-
return false;
|
|
995
|
-
if (a === o) {
|
|
996
|
-
return b === p;
|
|
997
|
-
}
|
|
998
|
-
else {
|
|
999
|
-
return objectDeepEqual(a, b);
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
return a === b;
|
|
1003
|
-
}
|
|
1004
|
-
// based on https://stackoverflow.com/a/13143059
|
|
1005
|
-
function objectDeepEqual(o, p) {
|
|
1006
|
-
var keysO = Object.keys(o).sort();
|
|
1007
|
-
var keysP = Object.keys(p).sort();
|
|
1008
|
-
if (keysO.length !== keysP.length)
|
|
1009
|
-
return false;
|
|
1010
|
-
if (keysO.join('') !== keysP.join(''))
|
|
1011
|
-
return false;
|
|
1012
|
-
for (var i = 0; i < keysO.length; i++) {
|
|
1013
|
-
var oVal = o[keysO[i]];
|
|
1014
|
-
var pVal = p[keysP[i]];
|
|
1015
|
-
if (!isEqual(oVal, pVal, o, p)) {
|
|
1016
|
-
return false;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
return true;
|
|
979
|
+
function isEqual(a, b, o, p) {
|
|
980
|
+
if (a instanceof Array) {
|
|
981
|
+
if (!(b instanceof Array))
|
|
982
|
+
return false;
|
|
983
|
+
//if (compareObjects(oVal, pVal === false) return false
|
|
984
|
+
//would work, too, and perhaps is a better fit, still, this is easy, too
|
|
985
|
+
return b.sort().join('') === a.sort().join('');
|
|
986
|
+
}
|
|
987
|
+
else if (a instanceof Date) {
|
|
988
|
+
if (!(b instanceof Date))
|
|
989
|
+
return false;
|
|
990
|
+
return '' + a === '' + b;
|
|
991
|
+
}
|
|
992
|
+
else if (a instanceof Function) {
|
|
993
|
+
if (!(b instanceof Function))
|
|
994
|
+
return false;
|
|
995
|
+
//ignore functions, or check them regardless?
|
|
996
|
+
}
|
|
997
|
+
else if (a instanceof Object) {
|
|
998
|
+
if (!(b instanceof Object))
|
|
999
|
+
return false;
|
|
1000
|
+
if (a === o) {
|
|
1001
|
+
return b === p;
|
|
1002
|
+
}
|
|
1003
|
+
else {
|
|
1004
|
+
return objectDeepEqual(a, b);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return a === b;
|
|
1008
|
+
}
|
|
1009
|
+
// based on https://stackoverflow.com/a/13143059
|
|
1010
|
+
function objectDeepEqual(o, p) {
|
|
1011
|
+
var keysO = Object.keys(o).sort();
|
|
1012
|
+
var keysP = Object.keys(p).sort();
|
|
1013
|
+
if (keysO.length !== keysP.length)
|
|
1014
|
+
return false;
|
|
1015
|
+
if (keysO.join('') !== keysP.join(''))
|
|
1016
|
+
return false;
|
|
1017
|
+
for (var i = 0; i < keysO.length; i++) {
|
|
1018
|
+
var oVal = o[keysO[i]];
|
|
1019
|
+
var pVal = p[keysP[i]];
|
|
1020
|
+
if (!isEqual(oVal, pVal, o, p)) {
|
|
1021
|
+
return false;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
return true;
|
|
1020
1025
|
}
|
|
1021
1026
|
|
|
1022
|
-
function objectRemoveEmpty(obj) {
|
|
1023
|
-
var newObj = {};
|
|
1024
|
-
Object.keys(obj).forEach(function (key) {
|
|
1025
|
-
if (!(obj[key] instanceof Array) && obj[key] === Object(obj[key])) {
|
|
1026
|
-
newObj[key] = objectRemoveEmpty(obj[key]);
|
|
1027
|
-
}
|
|
1028
|
-
else if (obj[key] !== undefined) {
|
|
1029
|
-
newObj[key] = obj[key];
|
|
1030
|
-
}
|
|
1031
|
-
});
|
|
1032
|
-
return newObj;
|
|
1027
|
+
function objectRemoveEmpty(obj) {
|
|
1028
|
+
var newObj = {};
|
|
1029
|
+
Object.keys(obj).forEach(function (key) {
|
|
1030
|
+
if (!(obj[key] instanceof Array) && obj[key] === Object(obj[key])) {
|
|
1031
|
+
newObj[key] = objectRemoveEmpty(obj[key]);
|
|
1032
|
+
}
|
|
1033
|
+
else if (obj[key] !== undefined) {
|
|
1034
|
+
newObj[key] = obj[key];
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
return newObj;
|
|
1033
1038
|
}
|
|
1034
1039
|
|
|
1035
|
-
function fixUrlStr(url) {
|
|
1036
|
-
// remove double slash
|
|
1037
|
-
return url.replace(/([^:]\/)\/+/g, '$1');
|
|
1040
|
+
function fixUrlStr(url) {
|
|
1041
|
+
// remove double slash
|
|
1042
|
+
return url.replace(/([^:]\/)\/+/g, '$1');
|
|
1038
1043
|
}
|
|
1039
1044
|
|
|
1040
|
-
/**
|
|
1041
|
-
* @example
|
|
1042
|
-
* ```javascript
|
|
1043
|
-
* const cache1 = new Cache();
|
|
1044
|
-
* cache1.add('foo', 'value');
|
|
1045
|
-
*
|
|
1046
|
-
* const cache2 = new Cache();
|
|
1047
|
-
* cache2.match('foo'); // value
|
|
1048
|
-
*
|
|
1049
|
-
* const cache3 = new Cache({ namespace: 'foo' });
|
|
1050
|
-
* cache3.match('foo'); // undefined
|
|
1051
|
-
* ```
|
|
1052
|
-
*
|
|
1053
|
-
* @example
|
|
1054
|
-
* ```javascript
|
|
1055
|
-
* let COUNTER = 0;
|
|
1056
|
-
* const createPromise = () => new Promise((res) => {
|
|
1057
|
-
* COUNTER++
|
|
1058
|
-
* setTimeout(() => res('Ok'), 300)
|
|
1059
|
-
* });
|
|
1060
|
-
*
|
|
1061
|
-
* const cache = new Cache();
|
|
1062
|
-
* for (let i = 0; i < 10; i++) {
|
|
1063
|
-
* cache.add('foo', createPromise).then((data) => {
|
|
1064
|
-
* console.log(data); // 'Ok'
|
|
1065
|
-
* });
|
|
1066
|
-
* }
|
|
1067
|
-
* console.log(COUNTER); // 1
|
|
1068
|
-
* ```
|
|
1069
|
-
*/
|
|
1070
|
-
var Cache$1 = /** @class */ (function () {
|
|
1071
|
-
function Cache(options) {
|
|
1072
|
-
if (options === void 0) { options = {}; }
|
|
1073
|
-
var _a;
|
|
1074
|
-
this.cache = [];
|
|
1075
|
-
var namespace = (_a = options.namespace) !== null && _a !== void 0 ? _a : 'default';
|
|
1076
|
-
if (Cache.instance[namespace]) {
|
|
1077
|
-
return Cache.instance[namespace];
|
|
1078
|
-
}
|
|
1079
|
-
Cache.instance[namespace] = this;
|
|
1080
|
-
return this;
|
|
1081
|
-
}
|
|
1082
|
-
Cache.prototype.clean = function () {
|
|
1083
|
-
this.cache.length = 0;
|
|
1084
|
-
};
|
|
1085
|
-
Cache.prototype.all = function () {
|
|
1086
|
-
return this.cache;
|
|
1087
|
-
};
|
|
1088
|
-
/**
|
|
1089
|
-
* Caching only a non-empty value.
|
|
1090
|
-
*
|
|
1091
|
-
* Useful for get or create strategy
|
|
1092
|
-
* @example
|
|
1093
|
-
* ```javascript
|
|
1094
|
-
* const cache = new Cache();
|
|
1095
|
-
* const getItemFunc = () => fetch(url).then((data) => {
|
|
1096
|
-
* return data.json(); // undefined
|
|
1097
|
-
* });
|
|
1098
|
-
* const item = await cache.addFull('foo', getItemFunc);
|
|
1099
|
-
* if (!item) {
|
|
1100
|
-
* await createItem(); // 'New item'
|
|
1101
|
-
* }
|
|
1102
|
-
*
|
|
1103
|
-
* // somewhere else in the code
|
|
1104
|
-
* const item = await cache.addFull('foo', getItemFunc).then((resp) => {
|
|
1105
|
-
* console.log(resp); // 'New item'
|
|
1106
|
-
* })
|
|
1107
|
-
*
|
|
1108
|
-
* ```
|
|
1109
|
-
*/
|
|
1110
|
-
Cache.prototype.addFull = function (key, valueToSet, props) {
|
|
1111
|
-
return this.add(key, valueToSet, props, true);
|
|
1112
|
-
};
|
|
1113
|
-
Cache.prototype.add = function (key, valueToSet, props, onlyFull) {
|
|
1114
|
-
var _this = this;
|
|
1115
|
-
var exist = this._find(key, props);
|
|
1116
|
-
if (!exist) {
|
|
1117
|
-
var value = void 0;
|
|
1118
|
-
if (valueToSet instanceof Function) {
|
|
1119
|
-
value = valueToSet();
|
|
1120
|
-
}
|
|
1121
|
-
else {
|
|
1122
|
-
value = valueToSet;
|
|
1123
|
-
}
|
|
1124
|
-
var props_ = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
|
|
1125
|
-
var cacheItem = {
|
|
1126
|
-
key: key,
|
|
1127
|
-
value: value,
|
|
1128
|
-
props: props_,
|
|
1129
|
-
// TODO: remove backward compatibility use only props
|
|
1130
|
-
options: props_,
|
|
1131
|
-
};
|
|
1132
|
-
if (onlyFull && !full(value)) {
|
|
1133
|
-
return value;
|
|
1134
|
-
}
|
|
1135
|
-
this.cache.push(cacheItem);
|
|
1136
|
-
if (value instanceof Promise) {
|
|
1137
|
-
value.catch(function (er) {
|
|
1138
|
-
_this.delete(key, props);
|
|
1139
|
-
throw er;
|
|
1140
|
-
});
|
|
1141
|
-
if (onlyFull) {
|
|
1142
|
-
value.then(function (x) {
|
|
1143
|
-
if (!full(x)) {
|
|
1144
|
-
_this.delete(key, props);
|
|
1145
|
-
}
|
|
1146
|
-
return x;
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
return value;
|
|
1150
|
-
}
|
|
1151
|
-
return value;
|
|
1152
|
-
}
|
|
1153
|
-
else {
|
|
1154
|
-
return exist.value;
|
|
1155
|
-
}
|
|
1156
|
-
};
|
|
1157
|
-
Cache.prototype.match = function (key, props) {
|
|
1158
|
-
var cacheRecord = this._find(key, props);
|
|
1159
|
-
if (cacheRecord) {
|
|
1160
|
-
return cacheRecord.value;
|
|
1161
|
-
}
|
|
1162
|
-
};
|
|
1163
|
-
Cache.prototype.matchAll = function (key, props) {
|
|
1164
|
-
var _this = this;
|
|
1165
|
-
if (key) {
|
|
1166
|
-
return this.cache
|
|
1167
|
-
.filter(function (x) { return _this._filter(x, key, props); })
|
|
1168
|
-
.map(function (x) { return x.value; });
|
|
1169
|
-
}
|
|
1170
|
-
return this.cache.map(function (x) { return x.value; });
|
|
1171
|
-
};
|
|
1172
|
-
Cache.prototype.delete = function (keyOrItem, props) {
|
|
1173
|
-
var _this = this;
|
|
1174
|
-
var exist = [];
|
|
1175
|
-
if (typeof keyOrItem === 'string') {
|
|
1176
|
-
exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, props); });
|
|
1177
|
-
}
|
|
1178
|
-
else {
|
|
1179
|
-
exist.push(keyOrItem);
|
|
1180
|
-
}
|
|
1181
|
-
for (var _i = 0, exist_1 = exist; _i < exist_1.length; _i++) {
|
|
1182
|
-
var e = exist_1[_i];
|
|
1183
|
-
var index = this.cache.indexOf(e);
|
|
1184
|
-
this.cache.splice(index, 1);
|
|
1185
|
-
}
|
|
1186
|
-
};
|
|
1187
|
-
Cache.prototype._find = function (key, props) {
|
|
1188
|
-
var _this = this;
|
|
1189
|
-
return this.cache.find(function (x) { return _this._filter(x, key, props); });
|
|
1190
|
-
};
|
|
1191
|
-
Cache.prototype._filter = function (item, key, props) {
|
|
1192
|
-
if (item.key === key) {
|
|
1193
|
-
if (props) {
|
|
1194
|
-
// TODO: remove backward compatibility
|
|
1195
|
-
var itemProps = item.props || item.options;
|
|
1196
|
-
return objectDeepEqual(itemProps || {}, objectRemoveEmpty(props));
|
|
1197
|
-
}
|
|
1198
|
-
return true;
|
|
1199
|
-
}
|
|
1200
|
-
return false;
|
|
1201
|
-
};
|
|
1202
|
-
Cache.instance = {};
|
|
1203
|
-
return Cache;
|
|
1045
|
+
/**
|
|
1046
|
+
* @example
|
|
1047
|
+
* ```javascript
|
|
1048
|
+
* const cache1 = new Cache();
|
|
1049
|
+
* cache1.add('foo', 'value');
|
|
1050
|
+
*
|
|
1051
|
+
* const cache2 = new Cache();
|
|
1052
|
+
* cache2.match('foo'); // value
|
|
1053
|
+
*
|
|
1054
|
+
* const cache3 = new Cache({ namespace: 'foo' });
|
|
1055
|
+
* cache3.match('foo'); // undefined
|
|
1056
|
+
* ```
|
|
1057
|
+
*
|
|
1058
|
+
* @example
|
|
1059
|
+
* ```javascript
|
|
1060
|
+
* let COUNTER = 0;
|
|
1061
|
+
* const createPromise = () => new Promise((res) => {
|
|
1062
|
+
* COUNTER++
|
|
1063
|
+
* setTimeout(() => res('Ok'), 300)
|
|
1064
|
+
* });
|
|
1065
|
+
*
|
|
1066
|
+
* const cache = new Cache();
|
|
1067
|
+
* for (let i = 0; i < 10; i++) {
|
|
1068
|
+
* cache.add('foo', createPromise).then((data) => {
|
|
1069
|
+
* console.log(data); // 'Ok'
|
|
1070
|
+
* });
|
|
1071
|
+
* }
|
|
1072
|
+
* console.log(COUNTER); // 1
|
|
1073
|
+
* ```
|
|
1074
|
+
*/
|
|
1075
|
+
var Cache$1 = /** @class */ (function () {
|
|
1076
|
+
function Cache(options) {
|
|
1077
|
+
if (options === void 0) { options = {}; }
|
|
1078
|
+
var _a;
|
|
1079
|
+
this.cache = [];
|
|
1080
|
+
var namespace = (_a = options.namespace) !== null && _a !== void 0 ? _a : 'default';
|
|
1081
|
+
if (Cache.instance[namespace]) {
|
|
1082
|
+
return Cache.instance[namespace];
|
|
1083
|
+
}
|
|
1084
|
+
Cache.instance[namespace] = this;
|
|
1085
|
+
return this;
|
|
1086
|
+
}
|
|
1087
|
+
Cache.prototype.clean = function () {
|
|
1088
|
+
this.cache.length = 0;
|
|
1089
|
+
};
|
|
1090
|
+
Cache.prototype.all = function () {
|
|
1091
|
+
return this.cache;
|
|
1092
|
+
};
|
|
1093
|
+
/**
|
|
1094
|
+
* Caching only a non-empty value.
|
|
1095
|
+
*
|
|
1096
|
+
* Useful for get or create strategy
|
|
1097
|
+
* @example
|
|
1098
|
+
* ```javascript
|
|
1099
|
+
* const cache = new Cache();
|
|
1100
|
+
* const getItemFunc = () => fetch(url).then((data) => {
|
|
1101
|
+
* return data.json(); // undefined
|
|
1102
|
+
* });
|
|
1103
|
+
* const item = await cache.addFull('foo', getItemFunc);
|
|
1104
|
+
* if (!item) {
|
|
1105
|
+
* await createItem(); // 'New item'
|
|
1106
|
+
* }
|
|
1107
|
+
*
|
|
1108
|
+
* // somewhere else in the code
|
|
1109
|
+
* const item = await cache.addFull('foo', getItemFunc).then((resp) => {
|
|
1110
|
+
* console.log(resp); // 'New item'
|
|
1111
|
+
* })
|
|
1112
|
+
*
|
|
1113
|
+
* ```
|
|
1114
|
+
*/
|
|
1115
|
+
Cache.prototype.addFull = function (key, valueToSet, props) {
|
|
1116
|
+
return this.add(key, valueToSet, props, true);
|
|
1117
|
+
};
|
|
1118
|
+
Cache.prototype.add = function (key, valueToSet, props, onlyFull) {
|
|
1119
|
+
var _this = this;
|
|
1120
|
+
var exist = this._find(key, props);
|
|
1121
|
+
if (!exist) {
|
|
1122
|
+
var value = void 0;
|
|
1123
|
+
if (valueToSet instanceof Function) {
|
|
1124
|
+
value = valueToSet();
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
value = valueToSet;
|
|
1128
|
+
}
|
|
1129
|
+
var props_ = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
|
|
1130
|
+
var cacheItem = {
|
|
1131
|
+
key: key,
|
|
1132
|
+
value: value,
|
|
1133
|
+
props: props_,
|
|
1134
|
+
// TODO: remove backward compatibility use only props
|
|
1135
|
+
options: props_,
|
|
1136
|
+
};
|
|
1137
|
+
if (onlyFull && !full(value)) {
|
|
1138
|
+
return value;
|
|
1139
|
+
}
|
|
1140
|
+
this.cache.push(cacheItem);
|
|
1141
|
+
if (value instanceof Promise) {
|
|
1142
|
+
value.catch(function (er) {
|
|
1143
|
+
_this.delete(key, props);
|
|
1144
|
+
throw er;
|
|
1145
|
+
});
|
|
1146
|
+
if (onlyFull) {
|
|
1147
|
+
value.then(function (x) {
|
|
1148
|
+
if (!full(x)) {
|
|
1149
|
+
_this.delete(key, props);
|
|
1150
|
+
}
|
|
1151
|
+
return x;
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
return value;
|
|
1155
|
+
}
|
|
1156
|
+
return value;
|
|
1157
|
+
}
|
|
1158
|
+
else {
|
|
1159
|
+
return exist.value;
|
|
1160
|
+
}
|
|
1161
|
+
};
|
|
1162
|
+
Cache.prototype.match = function (key, props) {
|
|
1163
|
+
var cacheRecord = this._find(key, props);
|
|
1164
|
+
if (cacheRecord) {
|
|
1165
|
+
return cacheRecord.value;
|
|
1166
|
+
}
|
|
1167
|
+
};
|
|
1168
|
+
Cache.prototype.matchAll = function (key, props) {
|
|
1169
|
+
var _this = this;
|
|
1170
|
+
if (key) {
|
|
1171
|
+
return this.cache
|
|
1172
|
+
.filter(function (x) { return _this._filter(x, key, props); })
|
|
1173
|
+
.map(function (x) { return x.value; });
|
|
1174
|
+
}
|
|
1175
|
+
return this.cache.map(function (x) { return x.value; });
|
|
1176
|
+
};
|
|
1177
|
+
Cache.prototype.delete = function (keyOrItem, props) {
|
|
1178
|
+
var _this = this;
|
|
1179
|
+
var exist = [];
|
|
1180
|
+
if (typeof keyOrItem === 'string') {
|
|
1181
|
+
exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, props); });
|
|
1182
|
+
}
|
|
1183
|
+
else {
|
|
1184
|
+
exist.push(keyOrItem);
|
|
1185
|
+
}
|
|
1186
|
+
for (var _i = 0, exist_1 = exist; _i < exist_1.length; _i++) {
|
|
1187
|
+
var e = exist_1[_i];
|
|
1188
|
+
var index = this.cache.indexOf(e);
|
|
1189
|
+
this.cache.splice(index, 1);
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
Cache.prototype._find = function (key, props) {
|
|
1193
|
+
var _this = this;
|
|
1194
|
+
return this.cache.find(function (x) { return _this._filter(x, key, props); });
|
|
1195
|
+
};
|
|
1196
|
+
Cache.prototype._filter = function (item, key, props) {
|
|
1197
|
+
if (item.key === key) {
|
|
1198
|
+
if (props) {
|
|
1199
|
+
// TODO: remove backward compatibility
|
|
1200
|
+
var itemProps = item.props || item.options;
|
|
1201
|
+
return objectDeepEqual(itemProps || {}, objectRemoveEmpty(props));
|
|
1202
|
+
}
|
|
1203
|
+
return true;
|
|
1204
|
+
}
|
|
1205
|
+
return false;
|
|
1206
|
+
};
|
|
1207
|
+
Cache.instance = {};
|
|
1208
|
+
return Cache;
|
|
1204
1209
|
}());
|
|
1205
1210
|
|
|
1206
|
-
/**
|
|
1207
|
-
* Caching for asynchronous functions
|
|
1208
|
-
*
|
|
1209
|
-
* @packageDocumentation
|
|
1210
|
-
* @module cache
|
|
1211
|
-
*/
|
|
1211
|
+
/**
|
|
1212
|
+
* Caching for asynchronous functions
|
|
1213
|
+
*
|
|
1214
|
+
* @packageDocumentation
|
|
1215
|
+
* @module cache
|
|
1216
|
+
*/
|
|
1212
1217
|
var Cache = Cache$1;
|
|
1213
1218
|
|
|
1214
|
-
/**
|
|
1215
|
-
* Thrown when...
|
|
1216
|
-
*/
|
|
1217
|
-
var NetworkError = /** @class */ (function (_super) {
|
|
1218
|
-
__extends(NetworkError, _super);
|
|
1219
|
-
function NetworkError(url) {
|
|
1220
|
-
var _this = _super.call(this) || this;
|
|
1221
|
-
_this.name = 'NetworkError';
|
|
1222
|
-
Object.setPrototypeOf(_this, NetworkError.prototype);
|
|
1223
|
-
_this.message = "Unable to request ".concat(url, ".\n Possibly invalid NGW URL entered or CORS not configured to get request from ").concat(location.origin); // /control-panel/cors
|
|
1224
|
-
return _this;
|
|
1225
|
-
}
|
|
1226
|
-
return NetworkError;
|
|
1219
|
+
/**
|
|
1220
|
+
* Thrown when...
|
|
1221
|
+
*/
|
|
1222
|
+
var NetworkError = /** @class */ (function (_super) {
|
|
1223
|
+
__extends(NetworkError, _super);
|
|
1224
|
+
function NetworkError(url) {
|
|
1225
|
+
var _this = _super.call(this) || this;
|
|
1226
|
+
_this.name = 'NetworkError';
|
|
1227
|
+
Object.setPrototypeOf(_this, NetworkError.prototype);
|
|
1228
|
+
_this.message = "Unable to request ".concat(url, ".\n Possibly invalid NGW URL entered or CORS not configured to get request from ").concat(location.origin); // /control-panel/cors
|
|
1229
|
+
return _this;
|
|
1230
|
+
}
|
|
1231
|
+
return NetworkError;
|
|
1227
1232
|
}(Error));
|
|
1228
1233
|
|
|
1229
|
-
/**
|
|
1230
|
-
* Thrown when...
|
|
1231
|
-
*/
|
|
1232
|
-
var NgwError = /** @class */ (function (_super) {
|
|
1233
|
-
__extends(NgwError, _super);
|
|
1234
|
-
function NgwError(er) {
|
|
1235
|
-
var _this = _super.call(this) || this;
|
|
1236
|
-
_this.name = 'NgwError';
|
|
1237
|
-
Object.assign(_this, er);
|
|
1238
|
-
Object.setPrototypeOf(_this, NgwError.prototype);
|
|
1239
|
-
return _this;
|
|
1240
|
-
}
|
|
1241
|
-
return NgwError;
|
|
1234
|
+
/**
|
|
1235
|
+
* Thrown when...
|
|
1236
|
+
*/
|
|
1237
|
+
var NgwError = /** @class */ (function (_super) {
|
|
1238
|
+
__extends(NgwError, _super);
|
|
1239
|
+
function NgwError(er) {
|
|
1240
|
+
var _this = _super.call(this) || this;
|
|
1241
|
+
_this.name = 'NgwError';
|
|
1242
|
+
Object.assign(_this, er);
|
|
1243
|
+
Object.setPrototypeOf(_this, NgwError.prototype);
|
|
1244
|
+
return _this;
|
|
1245
|
+
}
|
|
1246
|
+
return NgwError;
|
|
1242
1247
|
}(Error));
|
|
1243
1248
|
|
|
1244
|
-
function isObject(val) {
|
|
1245
|
-
return Object.prototype.toString.call(val) === '[object Object]';
|
|
1249
|
+
function isObject(val) {
|
|
1250
|
+
return Object.prototype.toString.call(val) === '[object Object]';
|
|
1246
1251
|
}
|
|
1247
1252
|
|
|
1248
|
-
var loadData;
|
|
1249
|
-
// @ts-ignore
|
|
1250
|
-
{
|
|
1251
|
-
loadData = function (url, callback, options, error, onCancel) {
|
|
1252
|
-
if (options === void 0) { options = {}; }
|
|
1253
|
-
options.method = options.method || 'GET';
|
|
1254
|
-
var xhr = new XMLHttpRequest();
|
|
1255
|
-
xhr.open(options.method || 'GET', url, true); // true for asynchronous
|
|
1256
|
-
if (options.responseType === 'blob') {
|
|
1257
|
-
xhr.responseType = options.responseType;
|
|
1258
|
-
}
|
|
1259
|
-
var getResponseText = function () {
|
|
1260
|
-
try {
|
|
1261
|
-
return JSON.parse(xhr.responseText);
|
|
1262
|
-
}
|
|
1263
|
-
catch (er) {
|
|
1264
|
-
return xhr.responseText;
|
|
1265
|
-
}
|
|
1266
|
-
};
|
|
1267
|
-
var processingResponse = function (forError) {
|
|
1268
|
-
if (forError === void 0) { forError = false; }
|
|
1269
|
-
var cb = forError ? error : callback;
|
|
1270
|
-
if (options.responseType === 'blob') {
|
|
1271
|
-
cb(xhr.response);
|
|
1272
|
-
}
|
|
1273
|
-
else {
|
|
1274
|
-
if (xhr.responseText) {
|
|
1275
|
-
cb(getResponseText());
|
|
1276
|
-
}
|
|
1277
|
-
else {
|
|
1278
|
-
error({ message: '' });
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
};
|
|
1282
|
-
xhr.onload = function () {
|
|
1283
|
-
if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
|
|
1284
|
-
error(new NgwError(getResponseText()));
|
|
1285
|
-
}
|
|
1286
|
-
processingResponse();
|
|
1287
|
-
};
|
|
1288
|
-
// xhr.onreadystatechange = () => {
|
|
1289
|
-
// if (
|
|
1290
|
-
// (xhr.readyState === 4 && xhr.status === 200) ||
|
|
1291
|
-
// (xhr.readyState === 3 && xhr.status === 201)
|
|
1292
|
-
// ) {
|
|
1293
|
-
// processingResponse();
|
|
1294
|
-
// } else if (xhr.readyState === 3 && xhr.status === 400) {
|
|
1295
|
-
// processingResponse();
|
|
1296
|
-
// } else if (xhr.readyState === 4 && xhr.status === 500) {
|
|
1297
|
-
// processingResponse();
|
|
1298
|
-
// } else if (xhr.readyState === 4 && xhr.status === 401) {
|
|
1299
|
-
// error(xhr.statusText);
|
|
1300
|
-
// } else if (xhr.readyState === 4) {
|
|
1301
|
-
// error('request error');
|
|
1302
|
-
// }
|
|
1303
|
-
// };
|
|
1304
|
-
xhr.onerror = function (er) {
|
|
1305
|
-
if (xhr.status === 0) {
|
|
1306
|
-
error(new NetworkError(url));
|
|
1307
|
-
}
|
|
1308
|
-
else {
|
|
1309
|
-
error(er);
|
|
1310
|
-
}
|
|
1311
|
-
};
|
|
1312
|
-
xhr.upload.onprogress = function (e) {
|
|
1313
|
-
if (e.lengthComputable) {
|
|
1314
|
-
var percentComplete = (e.loaded / e.total) * 100;
|
|
1315
|
-
if (options.onProgress) {
|
|
1316
|
-
options.onProgress(percentComplete, e);
|
|
1317
|
-
}
|
|
1318
|
-
// console.log(percentComplete + '% uploaded');
|
|
1319
|
-
}
|
|
1320
|
-
};
|
|
1321
|
-
var headers = options.headers;
|
|
1322
|
-
if (headers) {
|
|
1323
|
-
for (var h in headers) {
|
|
1324
|
-
var header = headers[h];
|
|
1325
|
-
if (typeof header === 'string') {
|
|
1326
|
-
xhr.setRequestHeader(h, header);
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
if (options.withCredentials !== undefined) {
|
|
1331
|
-
xhr.withCredentials = options.withCredentials;
|
|
1332
|
-
}
|
|
1333
|
-
var data;
|
|
1334
|
-
if (options.file) {
|
|
1335
|
-
data = new FormData();
|
|
1336
|
-
data.append('file', options.file);
|
|
1337
|
-
if (options.data) {
|
|
1338
|
-
for (var d in options.data) {
|
|
1339
|
-
data.append(d, data[d]);
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
else {
|
|
1344
|
-
data = options.data
|
|
1345
|
-
? typeof options.data === 'string'
|
|
1346
|
-
? options.data
|
|
1347
|
-
: JSON.stringify(options.data)
|
|
1348
|
-
: null;
|
|
1349
|
-
}
|
|
1350
|
-
if (onCancel) {
|
|
1351
|
-
onCancel(function () {
|
|
1352
|
-
xhr.abort();
|
|
1353
|
-
});
|
|
1354
|
-
}
|
|
1355
|
-
xhr.send(data);
|
|
1356
|
-
};
|
|
1253
|
+
var loadData;
|
|
1254
|
+
// @ts-ignore
|
|
1255
|
+
{
|
|
1256
|
+
loadData = function (url, callback, options, error, onCancel) {
|
|
1257
|
+
if (options === void 0) { options = {}; }
|
|
1258
|
+
options.method = options.method || 'GET';
|
|
1259
|
+
var xhr = new XMLHttpRequest();
|
|
1260
|
+
xhr.open(options.method || 'GET', url, true); // true for asynchronous
|
|
1261
|
+
if (options.responseType === 'blob') {
|
|
1262
|
+
xhr.responseType = options.responseType;
|
|
1263
|
+
}
|
|
1264
|
+
var getResponseText = function () {
|
|
1265
|
+
try {
|
|
1266
|
+
return JSON.parse(xhr.responseText);
|
|
1267
|
+
}
|
|
1268
|
+
catch (er) {
|
|
1269
|
+
return xhr.responseText;
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
var processingResponse = function (forError) {
|
|
1273
|
+
if (forError === void 0) { forError = false; }
|
|
1274
|
+
var cb = forError ? error : callback;
|
|
1275
|
+
if (options.responseType === 'blob') {
|
|
1276
|
+
cb(xhr.response);
|
|
1277
|
+
}
|
|
1278
|
+
else {
|
|
1279
|
+
if (xhr.responseText) {
|
|
1280
|
+
cb(getResponseText());
|
|
1281
|
+
}
|
|
1282
|
+
else {
|
|
1283
|
+
error({ message: '' });
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
xhr.onload = function () {
|
|
1288
|
+
if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
|
|
1289
|
+
error(new NgwError(getResponseText()));
|
|
1290
|
+
}
|
|
1291
|
+
processingResponse();
|
|
1292
|
+
};
|
|
1293
|
+
// xhr.onreadystatechange = () => {
|
|
1294
|
+
// if (
|
|
1295
|
+
// (xhr.readyState === 4 && xhr.status === 200) ||
|
|
1296
|
+
// (xhr.readyState === 3 && xhr.status === 201)
|
|
1297
|
+
// ) {
|
|
1298
|
+
// processingResponse();
|
|
1299
|
+
// } else if (xhr.readyState === 3 && xhr.status === 400) {
|
|
1300
|
+
// processingResponse();
|
|
1301
|
+
// } else if (xhr.readyState === 4 && xhr.status === 500) {
|
|
1302
|
+
// processingResponse();
|
|
1303
|
+
// } else if (xhr.readyState === 4 && xhr.status === 401) {
|
|
1304
|
+
// error(xhr.statusText);
|
|
1305
|
+
// } else if (xhr.readyState === 4) {
|
|
1306
|
+
// error('request error');
|
|
1307
|
+
// }
|
|
1308
|
+
// };
|
|
1309
|
+
xhr.onerror = function (er) {
|
|
1310
|
+
if (xhr.status === 0) {
|
|
1311
|
+
error(new NetworkError(url));
|
|
1312
|
+
}
|
|
1313
|
+
else {
|
|
1314
|
+
error(er);
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
xhr.upload.onprogress = function (e) {
|
|
1318
|
+
if (e.lengthComputable) {
|
|
1319
|
+
var percentComplete = (e.loaded / e.total) * 100;
|
|
1320
|
+
if (options.onProgress) {
|
|
1321
|
+
options.onProgress(percentComplete, e);
|
|
1322
|
+
}
|
|
1323
|
+
// console.log(percentComplete + '% uploaded');
|
|
1324
|
+
}
|
|
1325
|
+
};
|
|
1326
|
+
var headers = options.headers;
|
|
1327
|
+
if (headers) {
|
|
1328
|
+
for (var h in headers) {
|
|
1329
|
+
var header = headers[h];
|
|
1330
|
+
if (typeof header === 'string') {
|
|
1331
|
+
xhr.setRequestHeader(h, header);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
if (options.withCredentials !== undefined) {
|
|
1336
|
+
xhr.withCredentials = options.withCredentials;
|
|
1337
|
+
}
|
|
1338
|
+
var data;
|
|
1339
|
+
if (options.file) {
|
|
1340
|
+
data = new FormData();
|
|
1341
|
+
data.append('file', options.file);
|
|
1342
|
+
if (options.data) {
|
|
1343
|
+
for (var d in options.data) {
|
|
1344
|
+
data.append(d, data[d]);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
else {
|
|
1349
|
+
data = options.data
|
|
1350
|
+
? typeof options.data === 'string'
|
|
1351
|
+
? options.data
|
|
1352
|
+
: JSON.stringify(options.data)
|
|
1353
|
+
: null;
|
|
1354
|
+
}
|
|
1355
|
+
if (onCancel) {
|
|
1356
|
+
onCancel(function () {
|
|
1357
|
+
xhr.abort();
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
xhr.send(data);
|
|
1361
|
+
};
|
|
1357
1362
|
}
|
|
1358
1363
|
|
|
1359
|
-
// https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
|
|
1360
|
-
var templateRe = /\{ *([\w_-]+) *\}/g;
|
|
1361
|
-
function template(str, data) {
|
|
1362
|
-
return str.replace(templateRe, function (s, key) {
|
|
1363
|
-
var value = data[key];
|
|
1364
|
-
if (value === undefined) {
|
|
1365
|
-
throw new Error('No value provided for letiable ' + s);
|
|
1366
|
-
}
|
|
1367
|
-
else if (typeof value === 'function') {
|
|
1368
|
-
value = value(data);
|
|
1369
|
-
}
|
|
1370
|
-
return value;
|
|
1371
|
-
});
|
|
1364
|
+
// https://github.com/Leaflet/Leaflet/blob/b507e21c510b53cd704fb8d3f89bb46ea925c8eb/src/core/Util.js#L165
|
|
1365
|
+
var templateRe = /\{ *([\w_-]+) *\}/g;
|
|
1366
|
+
function template(str, data) {
|
|
1367
|
+
return str.replace(templateRe, function (s, key) {
|
|
1368
|
+
var value = data[key];
|
|
1369
|
+
if (value === undefined) {
|
|
1370
|
+
throw new Error('No value provided for letiable ' + s);
|
|
1371
|
+
}
|
|
1372
|
+
else if (typeof value === 'function') {
|
|
1373
|
+
value = value(data);
|
|
1374
|
+
}
|
|
1375
|
+
return value;
|
|
1376
|
+
});
|
|
1372
1377
|
}
|
|
1373
1378
|
|
|
1374
|
-
function apiRequest(opt) {
|
|
1375
|
-
var params = opt.params;
|
|
1376
|
-
return new CancelablePromise(function (resolve, reject) {
|
|
1377
|
-
opt.connector
|
|
1378
|
-
.connect()
|
|
1379
|
-
.then(function (apiItems) {
|
|
1380
|
-
var apiItem = apiItems && apiItems[opt.name];
|
|
1381
|
-
if (apiItem) {
|
|
1382
|
-
apiItem = __spreadArray([], apiItem, true);
|
|
1383
|
-
var url = apiItem.shift();
|
|
1384
|
-
if (apiItem.length) {
|
|
1385
|
-
var replaceParams = {};
|
|
1386
|
-
for (var fry = 0; fry < apiItem.length; fry++) {
|
|
1387
|
-
var arg = apiItem[fry];
|
|
1388
|
-
replaceParams[fry] = '{' + arg + '}';
|
|
1389
|
-
if (params[arg] === undefined) {
|
|
1390
|
-
throw new Error('`' + arg + '`' + ' url api argument is not specified');
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
if (url) {
|
|
1394
|
-
url = template(url, replaceParams);
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
// Transfer part of the parameters from `params` to the URL string
|
|
1398
|
-
if (params) {
|
|
1399
|
-
var paramArray_1 = [];
|
|
1400
|
-
var paramList = params.paramList;
|
|
1401
|
-
if (Array.isArray(paramList)) {
|
|
1402
|
-
delete params.paramList;
|
|
1403
|
-
paramList.forEach(function (x) {
|
|
1404
|
-
paramArray_1.push("".concat(x[0], "=").concat(x[1]));
|
|
1405
|
-
});
|
|
1406
|
-
}
|
|
1407
|
-
for (var p in params) {
|
|
1408
|
-
if (apiItem.indexOf(p) === -1) {
|
|
1409
|
-
paramArray_1.push("".concat(p, "=").concat(params[p]));
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
if (paramArray_1.length) {
|
|
1413
|
-
url = url + '?' + paramArray_1.join('&');
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
if (url) {
|
|
1417
|
-
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
1418
|
-
}
|
|
1419
|
-
else {
|
|
1420
|
-
reject(new Error('request url is not set'));
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
|
-
else {
|
|
1424
|
-
resolve(undefined);
|
|
1425
|
-
}
|
|
1426
|
-
})
|
|
1427
|
-
.catch(function (er) {
|
|
1428
|
-
reject(er);
|
|
1429
|
-
});
|
|
1430
|
-
});
|
|
1379
|
+
function apiRequest(opt) {
|
|
1380
|
+
var params = opt.params;
|
|
1381
|
+
return new CancelablePromise(function (resolve, reject) {
|
|
1382
|
+
opt.connector
|
|
1383
|
+
.connect()
|
|
1384
|
+
.then(function (apiItems) {
|
|
1385
|
+
var apiItem = apiItems && apiItems[opt.name];
|
|
1386
|
+
if (apiItem) {
|
|
1387
|
+
apiItem = __spreadArray([], apiItem, true);
|
|
1388
|
+
var url = apiItem.shift();
|
|
1389
|
+
if (apiItem.length) {
|
|
1390
|
+
var replaceParams = {};
|
|
1391
|
+
for (var fry = 0; fry < apiItem.length; fry++) {
|
|
1392
|
+
var arg = apiItem[fry];
|
|
1393
|
+
replaceParams[fry] = '{' + arg + '}';
|
|
1394
|
+
if (params[arg] === undefined) {
|
|
1395
|
+
throw new Error('`' + arg + '`' + ' url api argument is not specified');
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
if (url) {
|
|
1399
|
+
url = template(url, replaceParams);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
// Transfer part of the parameters from `params` to the URL string
|
|
1403
|
+
if (params) {
|
|
1404
|
+
var paramArray_1 = [];
|
|
1405
|
+
var paramList = params.paramList;
|
|
1406
|
+
if (Array.isArray(paramList)) {
|
|
1407
|
+
delete params.paramList;
|
|
1408
|
+
paramList.forEach(function (x) {
|
|
1409
|
+
paramArray_1.push("".concat(x[0], "=").concat(x[1]));
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
1412
|
+
for (var p in params) {
|
|
1413
|
+
if (apiItem.indexOf(p) === -1) {
|
|
1414
|
+
paramArray_1.push("".concat(p, "=").concat(params[p]));
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
if (paramArray_1.length) {
|
|
1418
|
+
url = url + '?' + paramArray_1.join('&');
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
if (url) {
|
|
1422
|
+
resolve(opt.connector.makeQuery(url, params, opt.requestOptions));
|
|
1423
|
+
}
|
|
1424
|
+
else {
|
|
1425
|
+
reject(new Error('request url is not set'));
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
else {
|
|
1429
|
+
resolve(undefined);
|
|
1430
|
+
}
|
|
1431
|
+
})
|
|
1432
|
+
.catch(function (er) {
|
|
1433
|
+
reject(er);
|
|
1434
|
+
});
|
|
1435
|
+
});
|
|
1431
1436
|
}
|
|
1432
1437
|
|
|
1433
|
-
/**
|
|
1434
|
-
* Thrown when ...
|
|
1435
|
-
*/
|
|
1436
|
-
var ResourceNotFoundError = /** @class */ (function (_super) {
|
|
1437
|
-
__extends(ResourceNotFoundError, _super);
|
|
1438
|
-
function ResourceNotFoundError(obj) {
|
|
1439
|
-
var _this = _super.call(this, obj) || this;
|
|
1440
|
-
_this.name = 'ResourceNotFoundError';
|
|
1441
|
-
_this.exception = 'nextgisweb.resource.exception.ResourceNotFound';
|
|
1442
|
-
Object.setPrototypeOf(_this, ResourceNotFoundError.prototype);
|
|
1443
|
-
return _this;
|
|
1444
|
-
}
|
|
1445
|
-
return ResourceNotFoundError;
|
|
1438
|
+
/**
|
|
1439
|
+
* Thrown when ...
|
|
1440
|
+
*/
|
|
1441
|
+
var ResourceNotFoundError = /** @class */ (function (_super) {
|
|
1442
|
+
__extends(ResourceNotFoundError, _super);
|
|
1443
|
+
function ResourceNotFoundError(obj) {
|
|
1444
|
+
var _this = _super.call(this, obj) || this;
|
|
1445
|
+
_this.name = 'ResourceNotFoundError';
|
|
1446
|
+
_this.exception = 'nextgisweb.resource.exception.ResourceNotFound';
|
|
1447
|
+
Object.setPrototypeOf(_this, ResourceNotFoundError.prototype);
|
|
1448
|
+
return _this;
|
|
1449
|
+
}
|
|
1450
|
+
return ResourceNotFoundError;
|
|
1446
1451
|
}(NgwError));
|
|
1447
1452
|
|
|
1448
|
-
/**
|
|
1449
|
-
* Thrown when ...
|
|
1450
|
-
*/
|
|
1451
|
-
var InsufficientPermissionsError = /** @class */ (function (_super) {
|
|
1452
|
-
__extends(InsufficientPermissionsError, _super);
|
|
1453
|
-
function InsufficientPermissionsError(obj) {
|
|
1454
|
-
var _this = _super.call(this, obj) || this;
|
|
1455
|
-
_this.name = 'InsufficientPermissionsError';
|
|
1456
|
-
_this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
|
|
1457
|
-
Object.setPrototypeOf(_this, InsufficientPermissionsError.prototype);
|
|
1458
|
-
return _this;
|
|
1459
|
-
}
|
|
1460
|
-
return InsufficientPermissionsError;
|
|
1453
|
+
/**
|
|
1454
|
+
* Thrown when ...
|
|
1455
|
+
*/
|
|
1456
|
+
var InsufficientPermissionsError = /** @class */ (function (_super) {
|
|
1457
|
+
__extends(InsufficientPermissionsError, _super);
|
|
1458
|
+
function InsufficientPermissionsError(obj) {
|
|
1459
|
+
var _this = _super.call(this, obj) || this;
|
|
1460
|
+
_this.name = 'InsufficientPermissionsError';
|
|
1461
|
+
_this.exception = 'nextgisweb.core.exception.InsufficientPermissions';
|
|
1462
|
+
Object.setPrototypeOf(_this, InsufficientPermissionsError.prototype);
|
|
1463
|
+
return _this;
|
|
1464
|
+
}
|
|
1465
|
+
return InsufficientPermissionsError;
|
|
1461
1466
|
}(NgwError));
|
|
1462
1467
|
|
|
1463
|
-
var CONNECTORS = [];
|
|
1464
|
-
function addConnector(connector) {
|
|
1465
|
-
CONNECTORS.push(connector);
|
|
1466
|
-
}
|
|
1467
|
-
function findConnector(options) {
|
|
1468
|
-
return CONNECTORS.find(function (x) {
|
|
1469
|
-
if (x.options.baseUrl === options.baseUrl) {
|
|
1470
|
-
if (options.auth) {
|
|
1471
|
-
if (x.options.auth) {
|
|
1472
|
-
return objectDeepEqual(x.options.auth, options.auth);
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
else {
|
|
1476
|
-
// if auth not in find options; use first connector with same url
|
|
1477
|
-
return true;
|
|
1478
|
-
}
|
|
1479
|
-
}
|
|
1480
|
-
});
|
|
1481
|
-
}
|
|
1482
|
-
function removeConnector(connector) {
|
|
1483
|
-
var index = CONNECTORS.indexOf(connector);
|
|
1484
|
-
if (index !== -1) {
|
|
1485
|
-
CONNECTORS.splice(index, 1);
|
|
1486
|
-
}
|
|
1468
|
+
var CONNECTORS = [];
|
|
1469
|
+
function addConnector(connector) {
|
|
1470
|
+
CONNECTORS.push(connector);
|
|
1471
|
+
}
|
|
1472
|
+
function findConnector(options) {
|
|
1473
|
+
return CONNECTORS.find(function (x) {
|
|
1474
|
+
if (x.options.baseUrl === options.baseUrl) {
|
|
1475
|
+
if (options.auth) {
|
|
1476
|
+
if (x.options.auth) {
|
|
1477
|
+
return objectDeepEqual(x.options.auth, options.auth);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
else {
|
|
1481
|
+
// if auth not in find options; use first connector with same url
|
|
1482
|
+
return true;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
function removeConnector(connector) {
|
|
1488
|
+
var index = CONNECTORS.indexOf(connector);
|
|
1489
|
+
if (index !== -1) {
|
|
1490
|
+
CONNECTORS.splice(index, 1);
|
|
1491
|
+
}
|
|
1487
1492
|
}
|
|
1488
1493
|
|
|
1489
|
-
var exclude = ['description'];
|
|
1490
|
-
/**
|
|
1491
|
-
* @remarks
|
|
1492
|
-
* https://docs.nextgis.ru/docs_ngweb_dev/doc/developer/resource.html#search-resources
|
|
1493
|
-
*
|
|
1494
|
-
* ```
|
|
1495
|
-
* { keyname, owner_user: { id }} > { keyname, owner_user__id }
|
|
1496
|
-
* ```
|
|
1497
|
-
*
|
|
1498
|
-
* @param resource - Any property from NGW resource item
|
|
1499
|
-
*/
|
|
1500
|
-
function resourceToQuery(resource, prefix) {
|
|
1501
|
-
if (prefix === void 0) { prefix = ''; }
|
|
1502
|
-
prefix = prefix ? prefix + '__' : '';
|
|
1503
|
-
var query = {};
|
|
1504
|
-
for (var _i = 0, _a = Object.entries(resource); _i < _a.length; _i++) {
|
|
1505
|
-
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
1506
|
-
if (exclude.indexOf(key) === -1) {
|
|
1507
|
-
if (isObject(value)) {
|
|
1508
|
-
if (key === 'owner_user') {
|
|
1509
|
-
var children = resourceToQuery(value, key);
|
|
1510
|
-
Object.assign(query, children);
|
|
1511
|
-
}
|
|
1512
|
-
else if (key === 'parent' && 'id' in value) {
|
|
1513
|
-
query.parent_id = value.id;
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1516
|
-
else if (defined(value)) {
|
|
1517
|
-
query[prefix + key] = value;
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
return query;
|
|
1494
|
+
var exclude = ['description'];
|
|
1495
|
+
/**
|
|
1496
|
+
* @remarks
|
|
1497
|
+
* https://docs.nextgis.ru/docs_ngweb_dev/doc/developer/resource.html#search-resources
|
|
1498
|
+
*
|
|
1499
|
+
* ```
|
|
1500
|
+
* { keyname, owner_user: { id }} > { keyname, owner_user__id }
|
|
1501
|
+
* ```
|
|
1502
|
+
*
|
|
1503
|
+
* @param resource - Any property from NGW resource item
|
|
1504
|
+
*/
|
|
1505
|
+
function resourceToQuery(resource, prefix) {
|
|
1506
|
+
if (prefix === void 0) { prefix = ''; }
|
|
1507
|
+
prefix = prefix ? prefix + '__' : '';
|
|
1508
|
+
var query = {};
|
|
1509
|
+
for (var _i = 0, _a = Object.entries(resource); _i < _a.length; _i++) {
|
|
1510
|
+
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
1511
|
+
if (exclude.indexOf(key) === -1) {
|
|
1512
|
+
if (isObject(value)) {
|
|
1513
|
+
if (key === 'owner_user') {
|
|
1514
|
+
var children = resourceToQuery(value, key);
|
|
1515
|
+
Object.assign(query, children);
|
|
1516
|
+
}
|
|
1517
|
+
else if (key === 'parent' && 'id' in value) {
|
|
1518
|
+
query.parent_id = value.id;
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
else if (defined(value)) {
|
|
1522
|
+
query[prefix + key] = value;
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
return query;
|
|
1522
1527
|
}
|
|
1523
1528
|
|
|
1524
|
-
function resourceCompare(res1, res2) {
|
|
1525
|
-
return objectDeepEqual(res1, res2);
|
|
1529
|
+
function resourceCompare(res1, res2) {
|
|
1530
|
+
return objectDeepEqual(res1, res2);
|
|
1526
1531
|
}
|
|
1527
1532
|
|
|
1528
|
-
var ResourcesControl = /** @class */ (function () {
|
|
1529
|
-
function ResourcesControl(connector) {
|
|
1530
|
-
this.connector = connector;
|
|
1531
|
-
this.cache = new Cache();
|
|
1532
|
-
}
|
|
1533
|
-
// -------------------------------------------------------------------------
|
|
1534
|
-
// Resource Methods
|
|
1535
|
-
// -------------------------------------------------------------------------
|
|
1536
|
-
/**
|
|
1537
|
-
* Receive resource from NGW by id, keyname or search-object parameter.
|
|
1538
|
-
* @param resource - Resource id, keyname or search-object
|
|
1539
|
-
*
|
|
1540
|
-
* @remarks
|
|
1541
|
-
* Fetching resource would be cached to speed up next call
|
|
1542
|
-
*/
|
|
1543
|
-
ResourcesControl.prototype.getOne = function (resource, requestOptions) {
|
|
1544
|
-
var _this = this;
|
|
1545
|
-
var cache = new Cache();
|
|
1546
|
-
var forCache = {};
|
|
1547
|
-
var opt = __assign(__assign({}, requestOptions), { cache: false });
|
|
1548
|
-
if (typeof resource === 'string') {
|
|
1549
|
-
forCache.keyname = resource;
|
|
1550
|
-
}
|
|
1551
|
-
else if (typeof resource === 'number') {
|
|
1552
|
-
forCache.id = resource;
|
|
1553
|
-
}
|
|
1554
|
-
else if (isObject(resource)) {
|
|
1555
|
-
if (resource.id !== undefined) {
|
|
1556
|
-
forCache.id = resource.id;
|
|
1557
|
-
}
|
|
1558
|
-
else if (resource.keyname) {
|
|
1559
|
-
forCache.keyname = resource.keyname;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
var makeRequest = function () {
|
|
1563
|
-
if (typeof resource === 'string') {
|
|
1564
|
-
return _this._fetchResourceBy({ keyname: resource }, opt);
|
|
1565
|
-
}
|
|
1566
|
-
else if (typeof resource === 'number') {
|
|
1567
|
-
return _this._fetchResourceById(resource, opt);
|
|
1568
|
-
}
|
|
1569
|
-
else if (isObject(resource)) {
|
|
1570
|
-
return _this._fetchResourceBy(resource, opt);
|
|
1571
|
-
}
|
|
1572
|
-
return CancelablePromise.resolve(undefined);
|
|
1573
|
-
};
|
|
1574
|
-
return cache.addFull('resource', makeRequest, forCache);
|
|
1575
|
-
};
|
|
1576
|
-
ResourcesControl.prototype.getOneOrFail = function (resource, requestOptions) {
|
|
1577
|
-
return this.getOne(resource, requestOptions).then(function (res) {
|
|
1578
|
-
if (res) {
|
|
1579
|
-
return res;
|
|
1580
|
-
}
|
|
1581
|
-
throw new ResourceNotFoundError();
|
|
1582
|
-
});
|
|
1583
|
-
};
|
|
1584
|
-
/**
|
|
1585
|
-
* A fast way to retrieve resource ID for any resource definition.
|
|
1586
|
-
* @param resource - Any available resource definition
|
|
1587
|
-
*
|
|
1588
|
-
* @remarks
|
|
1589
|
-
* There are situations when exactly the resource id is needed
|
|
1590
|
-
* (for example, to compose the correct request to the api)
|
|
1591
|
-
* then this method will come in handy to facilitate the extraction of the identifier
|
|
1592
|
-
* if the resource is specified through a keyname or other parameters.
|
|
1593
|
-
*/
|
|
1594
|
-
ResourcesControl.prototype.getId = function (resource, requestOptions) {
|
|
1595
|
-
if (typeof resource === 'number') {
|
|
1596
|
-
return CancelablePromise.resolve(resource);
|
|
1597
|
-
}
|
|
1598
|
-
else if (typeof resource === 'string' || isObject(resource)) {
|
|
1599
|
-
return this.getOne(resource, requestOptions).then(function (res) {
|
|
1600
|
-
if (res) {
|
|
1601
|
-
return res.resource.id;
|
|
1602
|
-
}
|
|
1603
|
-
});
|
|
1604
|
-
}
|
|
1605
|
-
return CancelablePromise.resolve(undefined);
|
|
1606
|
-
};
|
|
1607
|
-
/**
|
|
1608
|
-
* A fast way to retrieve resource ID for any resource definition.
|
|
1609
|
-
* @param resource - Any available resource definition
|
|
1610
|
-
*
|
|
1611
|
-
* @remarks
|
|
1612
|
-
* Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
|
|
1613
|
-
* To not make one more checks if the resource is definitely exists
|
|
1614
|
-
*/
|
|
1615
|
-
ResourcesControl.prototype.getIdOrFail = function (resource, requestOptions) {
|
|
1616
|
-
return this.getId(resource, requestOptions).then(function (resp) {
|
|
1617
|
-
if (resp === undefined) {
|
|
1618
|
-
throw new Error();
|
|
1619
|
-
}
|
|
1620
|
-
return resp;
|
|
1621
|
-
});
|
|
1622
|
-
};
|
|
1623
|
-
ResourcesControl.prototype.getMany = function (resource, requestOptions) {
|
|
1624
|
-
var _this = this;
|
|
1625
|
-
return this._resourceCacheFilter(resource).then(function (items) {
|
|
1626
|
-
if (!items.length) {
|
|
1627
|
-
var query = {};
|
|
1628
|
-
if (resource.keyname) {
|
|
1629
|
-
query.keyname = resource.keyname;
|
|
1630
|
-
}
|
|
1631
|
-
else {
|
|
1632
|
-
Object.assign(query, resourceToQuery(resource));
|
|
1633
|
-
}
|
|
1634
|
-
return _this.connector
|
|
1635
|
-
.get('resource.search', requestOptions, __assign({ serialization: 'full' }, query))
|
|
1636
|
-
.then(function (resources) {
|
|
1637
|
-
if (resources) {
|
|
1638
|
-
for (var _i = 0, resources_1 = resources; _i < resources_1.length; _i++) {
|
|
1639
|
-
var x = resources_1[_i];
|
|
1640
|
-
_this.cache.add('resource.item', CancelablePromise.resolve(x), {
|
|
1641
|
-
id: x.resource.id,
|
|
1642
|
-
});
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
|
-
return resources;
|
|
1646
|
-
});
|
|
1647
|
-
}
|
|
1648
|
-
return items;
|
|
1649
|
-
});
|
|
1650
|
-
};
|
|
1651
|
-
ResourcesControl.prototype.getParent = function (resource, requestOptions) {
|
|
1652
|
-
var _this = this;
|
|
1653
|
-
return this.getOne(resource, requestOptions).then(function (child) {
|
|
1654
|
-
if (child) {
|
|
1655
|
-
return _this.getOne(child.resource.parent.id, requestOptions);
|
|
1656
|
-
}
|
|
1657
|
-
return CancelablePromise.resolve(undefined);
|
|
1658
|
-
});
|
|
1659
|
-
};
|
|
1660
|
-
ResourcesControl.prototype.getChildrenOf = function (resource, requestOptions) {
|
|
1661
|
-
var _this = this;
|
|
1662
|
-
return this.getIdOrFail(resource).then(function (parent) {
|
|
1663
|
-
return _this._getChildrenOf(parent, requestOptions);
|
|
1664
|
-
});
|
|
1665
|
-
};
|
|
1666
|
-
ResourcesControl.prototype.update = function (resource, data) {
|
|
1667
|
-
var _this = this;
|
|
1668
|
-
return this.getId(resource).then(function (id) {
|
|
1669
|
-
if (id !== undefined) {
|
|
1670
|
-
return _this.connector.put('resource.item', { data: data }, { id: id });
|
|
1671
|
-
}
|
|
1672
|
-
});
|
|
1673
|
-
};
|
|
1674
|
-
/**
|
|
1675
|
-
* Fast way to delete resource from NGW and clean cache.
|
|
1676
|
-
* @param resource - Resource definition
|
|
1677
|
-
*/
|
|
1678
|
-
ResourcesControl.prototype.delete = function (resource) {
|
|
1679
|
-
var _this = this;
|
|
1680
|
-
return this.getId(resource).then(function (id) {
|
|
1681
|
-
if (id !== undefined) {
|
|
1682
|
-
return _this.connector.delete('resource.item', null, { id: id }).then(function () {
|
|
1683
|
-
_this._cleanResourceItemCache(id);
|
|
1684
|
-
return undefined;
|
|
1685
|
-
});
|
|
1686
|
-
}
|
|
1687
|
-
});
|
|
1688
|
-
};
|
|
1689
|
-
ResourcesControl.prototype._getChildrenOf = function (parent, requestOptions, _items) {
|
|
1690
|
-
var _this = this;
|
|
1691
|
-
if (_items === void 0) { _items = []; }
|
|
1692
|
-
return this.connector
|
|
1693
|
-
.get('resource.collection', __assign({ cache: true }, requestOptions), {
|
|
1694
|
-
parent: parent,
|
|
1695
|
-
})
|
|
1696
|
-
.then(function (items) {
|
|
1697
|
-
var recursivePromises = [];
|
|
1698
|
-
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
|
|
1699
|
-
var item = items_1[_i];
|
|
1700
|
-
_this.cache.add('resource.item', CancelablePromise.resolve(item), {
|
|
1701
|
-
id: item.resource.id,
|
|
1702
|
-
});
|
|
1703
|
-
_items.push(item);
|
|
1704
|
-
if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.recursive) && item.resource.children) {
|
|
1705
|
-
recursivePromises.push(_this._getChildrenOf(item.resource.id, requestOptions, _items));
|
|
1706
|
-
}
|
|
1707
|
-
}
|
|
1708
|
-
if (recursivePromises.length) {
|
|
1709
|
-
return CancelablePromise.all(recursivePromises).then(function () {
|
|
1710
|
-
return _items;
|
|
1711
|
-
});
|
|
1712
|
-
}
|
|
1713
|
-
return _items;
|
|
1714
|
-
});
|
|
1715
|
-
};
|
|
1716
|
-
ResourcesControl.prototype._cleanResourceItemCache = function (id) {
|
|
1717
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1718
|
-
var all, toDelete, _i, all_1, c, cid, rid, _a, toDelete_1, d;
|
|
1719
|
-
return __generator(this, function (_b) {
|
|
1720
|
-
switch (_b.label) {
|
|
1721
|
-
case 0:
|
|
1722
|
-
all = this.cache.all();
|
|
1723
|
-
toDelete = [];
|
|
1724
|
-
_i = 0, all_1 = all;
|
|
1725
|
-
_b.label = 1;
|
|
1726
|
-
case 1:
|
|
1727
|
-
if (!(_i < all_1.length)) return [3 /*break*/, 5];
|
|
1728
|
-
c = all_1[_i];
|
|
1729
|
-
cid = c.options && c.options.id;
|
|
1730
|
-
if (!(['resource.item', 'resource'].includes(c.key) && cid !== undefined)) return [3 /*break*/, 4];
|
|
1731
|
-
if (!(typeof cid === 'number')) return [3 /*break*/, 2];
|
|
1732
|
-
if (cid === id) {
|
|
1733
|
-
toDelete.push(c);
|
|
1734
|
-
}
|
|
1735
|
-
return [3 /*break*/, 4];
|
|
1736
|
-
case 2: return [4 /*yield*/, this.getId(cid)];
|
|
1737
|
-
case 3:
|
|
1738
|
-
rid = _b.sent();
|
|
1739
|
-
if (rid === id) {
|
|
1740
|
-
toDelete.push(c);
|
|
1741
|
-
}
|
|
1742
|
-
_b.label = 4;
|
|
1743
|
-
case 4:
|
|
1744
|
-
_i++;
|
|
1745
|
-
return [3 /*break*/, 1];
|
|
1746
|
-
case 5:
|
|
1747
|
-
for (_a = 0, toDelete_1 = toDelete; _a < toDelete_1.length; _a++) {
|
|
1748
|
-
d = toDelete_1[_a];
|
|
1749
|
-
this.cache.delete(d);
|
|
1750
|
-
}
|
|
1751
|
-
return [2 /*return*/];
|
|
1752
|
-
}
|
|
1753
|
-
});
|
|
1754
|
-
});
|
|
1755
|
-
};
|
|
1756
|
-
ResourcesControl.prototype._fetchResourceById = function (id, requestOptions) {
|
|
1757
|
-
var _this = this;
|
|
1758
|
-
var promise = function () {
|
|
1759
|
-
return _this.connector.get('resource.item', requestOptions, { id: id });
|
|
1760
|
-
};
|
|
1761
|
-
return this.cache
|
|
1762
|
-
.add('resource.item', promise, {
|
|
1763
|
-
id: id,
|
|
1764
|
-
})
|
|
1765
|
-
.catch(function (er) {
|
|
1766
|
-
if (!(er instanceof ResourceNotFoundError)) {
|
|
1767
|
-
throw er;
|
|
1768
|
-
}
|
|
1769
|
-
return undefined;
|
|
1770
|
-
});
|
|
1771
|
-
};
|
|
1772
|
-
ResourcesControl.prototype._fetchResourceBy = function (resource, requestOptions) {
|
|
1773
|
-
return this.getMany(resource, requestOptions).then(function (resources) {
|
|
1774
|
-
return resources[0];
|
|
1775
|
-
});
|
|
1776
|
-
};
|
|
1777
|
-
ResourcesControl.prototype._resourceCacheFilter = function (resource) {
|
|
1778
|
-
return CancelablePromise.all(this.cache.matchAll('resource.item')).then(function (resources) {
|
|
1779
|
-
var items = [];
|
|
1780
|
-
resources.filter(function (x) {
|
|
1781
|
-
if (x) {
|
|
1782
|
-
// identical by uniq props
|
|
1783
|
-
if (resource.keyname && x.resource.keyname) {
|
|
1784
|
-
return resource.keyname === x.resource.keyname;
|
|
1785
|
-
}
|
|
1786
|
-
if (defined(resource.id) && defined(x.resource.id)) {
|
|
1787
|
-
return resource.id === x.resource.id;
|
|
1788
|
-
}
|
|
1789
|
-
return resourceCompare(resource, x.resource);
|
|
1790
|
-
}
|
|
1791
|
-
});
|
|
1792
|
-
return items;
|
|
1793
|
-
});
|
|
1794
|
-
};
|
|
1795
|
-
return ResourcesControl;
|
|
1533
|
+
var ResourcesControl = /** @class */ (function () {
|
|
1534
|
+
function ResourcesControl(connector) {
|
|
1535
|
+
this.connector = connector;
|
|
1536
|
+
this.cache = new Cache();
|
|
1537
|
+
}
|
|
1538
|
+
// -------------------------------------------------------------------------
|
|
1539
|
+
// Resource Methods
|
|
1540
|
+
// -------------------------------------------------------------------------
|
|
1541
|
+
/**
|
|
1542
|
+
* Receive resource from NGW by id, keyname or search-object parameter.
|
|
1543
|
+
* @param resource - Resource id, keyname or search-object
|
|
1544
|
+
*
|
|
1545
|
+
* @remarks
|
|
1546
|
+
* Fetching resource would be cached to speed up next call
|
|
1547
|
+
*/
|
|
1548
|
+
ResourcesControl.prototype.getOne = function (resource, requestOptions) {
|
|
1549
|
+
var _this = this;
|
|
1550
|
+
var cache = new Cache();
|
|
1551
|
+
var forCache = {};
|
|
1552
|
+
var opt = __assign(__assign({}, requestOptions), { cache: false });
|
|
1553
|
+
if (typeof resource === 'string') {
|
|
1554
|
+
forCache.keyname = resource;
|
|
1555
|
+
}
|
|
1556
|
+
else if (typeof resource === 'number') {
|
|
1557
|
+
forCache.id = resource;
|
|
1558
|
+
}
|
|
1559
|
+
else if (isObject(resource)) {
|
|
1560
|
+
if (resource.id !== undefined) {
|
|
1561
|
+
forCache.id = resource.id;
|
|
1562
|
+
}
|
|
1563
|
+
else if (resource.keyname) {
|
|
1564
|
+
forCache.keyname = resource.keyname;
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
var makeRequest = function () {
|
|
1568
|
+
if (typeof resource === 'string') {
|
|
1569
|
+
return _this._fetchResourceBy({ keyname: resource }, opt);
|
|
1570
|
+
}
|
|
1571
|
+
else if (typeof resource === 'number') {
|
|
1572
|
+
return _this._fetchResourceById(resource, opt);
|
|
1573
|
+
}
|
|
1574
|
+
else if (isObject(resource)) {
|
|
1575
|
+
return _this._fetchResourceBy(resource, opt);
|
|
1576
|
+
}
|
|
1577
|
+
return CancelablePromise.resolve(undefined);
|
|
1578
|
+
};
|
|
1579
|
+
return cache.addFull('resource', makeRequest, forCache);
|
|
1580
|
+
};
|
|
1581
|
+
ResourcesControl.prototype.getOneOrFail = function (resource, requestOptions) {
|
|
1582
|
+
return this.getOne(resource, requestOptions).then(function (res) {
|
|
1583
|
+
if (res) {
|
|
1584
|
+
return res;
|
|
1585
|
+
}
|
|
1586
|
+
throw new ResourceNotFoundError();
|
|
1587
|
+
});
|
|
1588
|
+
};
|
|
1589
|
+
/**
|
|
1590
|
+
* A fast way to retrieve resource ID for any resource definition.
|
|
1591
|
+
* @param resource - Any available resource definition
|
|
1592
|
+
*
|
|
1593
|
+
* @remarks
|
|
1594
|
+
* There are situations when exactly the resource id is needed
|
|
1595
|
+
* (for example, to compose the correct request to the api)
|
|
1596
|
+
* then this method will come in handy to facilitate the extraction of the identifier
|
|
1597
|
+
* if the resource is specified through a keyname or other parameters.
|
|
1598
|
+
*/
|
|
1599
|
+
ResourcesControl.prototype.getId = function (resource, requestOptions) {
|
|
1600
|
+
if (typeof resource === 'number') {
|
|
1601
|
+
return CancelablePromise.resolve(resource);
|
|
1602
|
+
}
|
|
1603
|
+
else if (typeof resource === 'string' || isObject(resource)) {
|
|
1604
|
+
return this.getOne(resource, requestOptions).then(function (res) {
|
|
1605
|
+
if (res) {
|
|
1606
|
+
return res.resource.id;
|
|
1607
|
+
}
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
return CancelablePromise.resolve(undefined);
|
|
1611
|
+
};
|
|
1612
|
+
/**
|
|
1613
|
+
* A fast way to retrieve resource ID for any resource definition.
|
|
1614
|
+
* @param resource - Any available resource definition
|
|
1615
|
+
*
|
|
1616
|
+
* @remarks
|
|
1617
|
+
* Similar with {@link NgwConnector.getResourceId | getResourceId} but rise error if resource is not exist.
|
|
1618
|
+
* To not make one more checks if the resource is definitely exists
|
|
1619
|
+
*/
|
|
1620
|
+
ResourcesControl.prototype.getIdOrFail = function (resource, requestOptions) {
|
|
1621
|
+
return this.getId(resource, requestOptions).then(function (resp) {
|
|
1622
|
+
if (resp === undefined) {
|
|
1623
|
+
throw new Error();
|
|
1624
|
+
}
|
|
1625
|
+
return resp;
|
|
1626
|
+
});
|
|
1627
|
+
};
|
|
1628
|
+
ResourcesControl.prototype.getMany = function (resource, requestOptions) {
|
|
1629
|
+
var _this = this;
|
|
1630
|
+
return this._resourceCacheFilter(resource).then(function (items) {
|
|
1631
|
+
if (!items.length) {
|
|
1632
|
+
var query = {};
|
|
1633
|
+
if (resource.keyname) {
|
|
1634
|
+
query.keyname = resource.keyname;
|
|
1635
|
+
}
|
|
1636
|
+
else {
|
|
1637
|
+
Object.assign(query, resourceToQuery(resource));
|
|
1638
|
+
}
|
|
1639
|
+
return _this.connector
|
|
1640
|
+
.get('resource.search', requestOptions, __assign({ serialization: 'full' }, query))
|
|
1641
|
+
.then(function (resources) {
|
|
1642
|
+
if (resources) {
|
|
1643
|
+
for (var _i = 0, resources_1 = resources; _i < resources_1.length; _i++) {
|
|
1644
|
+
var x = resources_1[_i];
|
|
1645
|
+
_this.cache.add('resource.item', CancelablePromise.resolve(x), {
|
|
1646
|
+
id: x.resource.id,
|
|
1647
|
+
});
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
return resources;
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
return items;
|
|
1654
|
+
});
|
|
1655
|
+
};
|
|
1656
|
+
ResourcesControl.prototype.getParent = function (resource, requestOptions) {
|
|
1657
|
+
var _this = this;
|
|
1658
|
+
return this.getOne(resource, requestOptions).then(function (child) {
|
|
1659
|
+
if (child) {
|
|
1660
|
+
return _this.getOne(child.resource.parent.id, requestOptions);
|
|
1661
|
+
}
|
|
1662
|
+
return CancelablePromise.resolve(undefined);
|
|
1663
|
+
});
|
|
1664
|
+
};
|
|
1665
|
+
ResourcesControl.prototype.getChildrenOf = function (resource, requestOptions) {
|
|
1666
|
+
var _this = this;
|
|
1667
|
+
return this.getIdOrFail(resource).then(function (parent) {
|
|
1668
|
+
return _this._getChildrenOf(parent, requestOptions);
|
|
1669
|
+
});
|
|
1670
|
+
};
|
|
1671
|
+
ResourcesControl.prototype.update = function (resource, data) {
|
|
1672
|
+
var _this = this;
|
|
1673
|
+
return this.getId(resource).then(function (id) {
|
|
1674
|
+
if (id !== undefined) {
|
|
1675
|
+
return _this.connector.put('resource.item', { data: data }, { id: id });
|
|
1676
|
+
}
|
|
1677
|
+
});
|
|
1678
|
+
};
|
|
1679
|
+
/**
|
|
1680
|
+
* Fast way to delete resource from NGW and clean cache.
|
|
1681
|
+
* @param resource - Resource definition
|
|
1682
|
+
*/
|
|
1683
|
+
ResourcesControl.prototype.delete = function (resource) {
|
|
1684
|
+
var _this = this;
|
|
1685
|
+
return this.getId(resource).then(function (id) {
|
|
1686
|
+
if (id !== undefined) {
|
|
1687
|
+
return _this.connector.delete('resource.item', null, { id: id }).then(function () {
|
|
1688
|
+
_this._cleanResourceItemCache(id);
|
|
1689
|
+
return undefined;
|
|
1690
|
+
});
|
|
1691
|
+
}
|
|
1692
|
+
});
|
|
1693
|
+
};
|
|
1694
|
+
ResourcesControl.prototype._getChildrenOf = function (parent, requestOptions, _items) {
|
|
1695
|
+
var _this = this;
|
|
1696
|
+
if (_items === void 0) { _items = []; }
|
|
1697
|
+
return this.connector
|
|
1698
|
+
.get('resource.collection', __assign({ cache: true }, requestOptions), {
|
|
1699
|
+
parent: parent,
|
|
1700
|
+
})
|
|
1701
|
+
.then(function (items) {
|
|
1702
|
+
var recursivePromises = [];
|
|
1703
|
+
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
|
|
1704
|
+
var item = items_1[_i];
|
|
1705
|
+
_this.cache.add('resource.item', CancelablePromise.resolve(item), {
|
|
1706
|
+
id: item.resource.id,
|
|
1707
|
+
});
|
|
1708
|
+
_items.push(item);
|
|
1709
|
+
if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.recursive) && item.resource.children) {
|
|
1710
|
+
recursivePromises.push(_this._getChildrenOf(item.resource.id, requestOptions, _items));
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
if (recursivePromises.length) {
|
|
1714
|
+
return CancelablePromise.all(recursivePromises).then(function () {
|
|
1715
|
+
return _items;
|
|
1716
|
+
});
|
|
1717
|
+
}
|
|
1718
|
+
return _items;
|
|
1719
|
+
});
|
|
1720
|
+
};
|
|
1721
|
+
ResourcesControl.prototype._cleanResourceItemCache = function (id) {
|
|
1722
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1723
|
+
var all, toDelete, _i, all_1, c, cid, rid, _a, toDelete_1, d;
|
|
1724
|
+
return __generator(this, function (_b) {
|
|
1725
|
+
switch (_b.label) {
|
|
1726
|
+
case 0:
|
|
1727
|
+
all = this.cache.all();
|
|
1728
|
+
toDelete = [];
|
|
1729
|
+
_i = 0, all_1 = all;
|
|
1730
|
+
_b.label = 1;
|
|
1731
|
+
case 1:
|
|
1732
|
+
if (!(_i < all_1.length)) return [3 /*break*/, 5];
|
|
1733
|
+
c = all_1[_i];
|
|
1734
|
+
cid = c.options && c.options.id;
|
|
1735
|
+
if (!(['resource.item', 'resource'].includes(c.key) && cid !== undefined)) return [3 /*break*/, 4];
|
|
1736
|
+
if (!(typeof cid === 'number')) return [3 /*break*/, 2];
|
|
1737
|
+
if (cid === id) {
|
|
1738
|
+
toDelete.push(c);
|
|
1739
|
+
}
|
|
1740
|
+
return [3 /*break*/, 4];
|
|
1741
|
+
case 2: return [4 /*yield*/, this.getId(cid)];
|
|
1742
|
+
case 3:
|
|
1743
|
+
rid = _b.sent();
|
|
1744
|
+
if (rid === id) {
|
|
1745
|
+
toDelete.push(c);
|
|
1746
|
+
}
|
|
1747
|
+
_b.label = 4;
|
|
1748
|
+
case 4:
|
|
1749
|
+
_i++;
|
|
1750
|
+
return [3 /*break*/, 1];
|
|
1751
|
+
case 5:
|
|
1752
|
+
for (_a = 0, toDelete_1 = toDelete; _a < toDelete_1.length; _a++) {
|
|
1753
|
+
d = toDelete_1[_a];
|
|
1754
|
+
this.cache.delete(d);
|
|
1755
|
+
}
|
|
1756
|
+
return [2 /*return*/];
|
|
1757
|
+
}
|
|
1758
|
+
});
|
|
1759
|
+
});
|
|
1760
|
+
};
|
|
1761
|
+
ResourcesControl.prototype._fetchResourceById = function (id, requestOptions) {
|
|
1762
|
+
var _this = this;
|
|
1763
|
+
var promise = function () {
|
|
1764
|
+
return _this.connector.get('resource.item', requestOptions, { id: id });
|
|
1765
|
+
};
|
|
1766
|
+
return this.cache
|
|
1767
|
+
.add('resource.item', promise, {
|
|
1768
|
+
id: id,
|
|
1769
|
+
})
|
|
1770
|
+
.catch(function (er) {
|
|
1771
|
+
if (!(er instanceof ResourceNotFoundError)) {
|
|
1772
|
+
throw er;
|
|
1773
|
+
}
|
|
1774
|
+
return undefined;
|
|
1775
|
+
});
|
|
1776
|
+
};
|
|
1777
|
+
ResourcesControl.prototype._fetchResourceBy = function (resource, requestOptions) {
|
|
1778
|
+
return this.getMany(resource, requestOptions).then(function (resources) {
|
|
1779
|
+
return resources[0];
|
|
1780
|
+
});
|
|
1781
|
+
};
|
|
1782
|
+
ResourcesControl.prototype._resourceCacheFilter = function (resource) {
|
|
1783
|
+
return CancelablePromise.all(this.cache.matchAll('resource.item')).then(function (resources) {
|
|
1784
|
+
var items = [];
|
|
1785
|
+
resources.filter(function (x) {
|
|
1786
|
+
if (x) {
|
|
1787
|
+
// identical by uniq props
|
|
1788
|
+
if (resource.keyname && x.resource.keyname) {
|
|
1789
|
+
return resource.keyname === x.resource.keyname;
|
|
1790
|
+
}
|
|
1791
|
+
if (defined(resource.id) && defined(x.resource.id)) {
|
|
1792
|
+
return resource.id === x.resource.id;
|
|
1793
|
+
}
|
|
1794
|
+
return resourceCompare(resource, x.resource);
|
|
1795
|
+
}
|
|
1796
|
+
});
|
|
1797
|
+
return items;
|
|
1798
|
+
});
|
|
1799
|
+
};
|
|
1800
|
+
return ResourcesControl;
|
|
1796
1801
|
}());
|
|
1797
1802
|
|
|
1798
|
-
var ID = 0;
|
|
1799
|
-
var NgwConnector$1 = /** @class */ (function () {
|
|
1800
|
-
function NgwConnector(options) {
|
|
1801
|
-
this.options = options;
|
|
1802
|
-
this.id = ID++;
|
|
1803
|
-
this.emitter = new events.exports.EventEmitter();
|
|
1804
|
-
this.routeStr = '/api/component/pyramid/route';
|
|
1805
|
-
this.activeRequests = [];
|
|
1806
|
-
var exist = findConnector(options);
|
|
1807
|
-
if (exist) {
|
|
1808
|
-
return exist;
|
|
1809
|
-
}
|
|
1810
|
-
else {
|
|
1811
|
-
if (this.options.route) {
|
|
1812
|
-
this.routeStr = this.options.route;
|
|
1813
|
-
}
|
|
1814
|
-
if (this.options.requestTransform) {
|
|
1815
|
-
this.requestTransform = this.options.requestTransform;
|
|
1816
|
-
}
|
|
1817
|
-
this.resources = new ResourcesControl(this);
|
|
1818
|
-
addConnector(this);
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
NgwConnector.create = function (options) {
|
|
1822
|
-
return new this(options);
|
|
1823
|
-
};
|
|
1824
|
-
NgwConnector.prototype.setRequestTransform = function (requestTransform) {
|
|
1825
|
-
this.requestTransform = requestTransform;
|
|
1826
|
-
};
|
|
1827
|
-
/**
|
|
1828
|
-
* Fast way to specify the connection address to NextGIS Web.
|
|
1829
|
-
* The current connection will be severed.
|
|
1830
|
-
* @param baseUrl - NGW url
|
|
1831
|
-
*/
|
|
1832
|
-
NgwConnector.prototype.setNgw = function (baseUrl) {
|
|
1833
|
-
this.logout();
|
|
1834
|
-
this.options.baseUrl = baseUrl;
|
|
1835
|
-
addConnector(this);
|
|
1836
|
-
};
|
|
1837
|
-
/**
|
|
1838
|
-
* Establishing a connection with NextGIS Web to fulfill all other requests.
|
|
1839
|
-
* @remarks
|
|
1840
|
-
* This method need not be called manually as it is used when forming a request in {@link apiRequest | apiRequest}.
|
|
1841
|
-
* Can be used to check connection.
|
|
1842
|
-
* @example
|
|
1843
|
-
* ```javascript
|
|
1844
|
-
* const connector = new NgwConnector({baseUrl: 'https://demo.nextgis.com'});
|
|
1845
|
-
* connector.connect()
|
|
1846
|
-
* .then(() => console.log('Ok'))
|
|
1847
|
-
* .catch((er) => console.log('Connection problem', er));
|
|
1848
|
-
* ```
|
|
1849
|
-
*/
|
|
1850
|
-
NgwConnector.prototype.connect = function () {
|
|
1851
|
-
var _this = this;
|
|
1852
|
-
var cache = new Cache();
|
|
1853
|
-
var auth = this.options.auth;
|
|
1854
|
-
var makeConnect = function () {
|
|
1855
|
-
return new CancelablePromise(function (resolve, reject) {
|
|
1856
|
-
var makeQuery = function () {
|
|
1857
|
-
return _this.makeQuery(_this.routeStr, {}, {})
|
|
1858
|
-
.then(function (route) {
|
|
1859
|
-
resolve(route);
|
|
1860
|
-
})
|
|
1861
|
-
.catch(function (er) {
|
|
1862
|
-
reject(er);
|
|
1863
|
-
});
|
|
1864
|
-
};
|
|
1865
|
-
if (auth) {
|
|
1866
|
-
var login = auth.login, password = auth.password;
|
|
1867
|
-
if (login && password) {
|
|
1868
|
-
return _this._login({ login: login, password: password })
|
|
1869
|
-
.then(function () {
|
|
1870
|
-
return makeQuery();
|
|
1871
|
-
})
|
|
1872
|
-
.catch(function (er) { return reject(er); });
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
return makeQuery();
|
|
1876
|
-
});
|
|
1877
|
-
};
|
|
1878
|
-
return cache.add('route', makeConnect, {
|
|
1879
|
-
id: this.id,
|
|
1880
|
-
auth: auth,
|
|
1881
|
-
baseUrl: this.options.baseUrl,
|
|
1882
|
-
});
|
|
1883
|
-
};
|
|
1884
|
-
/**
|
|
1885
|
-
* Quick way to change NextGIS Web user.
|
|
1886
|
-
* @param credentials - New user credentials
|
|
1887
|
-
*/
|
|
1888
|
-
NgwConnector.prototype.login = function (credentials, options) {
|
|
1889
|
-
this.logout();
|
|
1890
|
-
addConnector(this);
|
|
1891
|
-
return this._login(credentials, options);
|
|
1892
|
-
};
|
|
1893
|
-
/**
|
|
1894
|
-
* Disconnecting a user. Aborting all current requests
|
|
1895
|
-
*/
|
|
1896
|
-
NgwConnector.prototype.logout = function () {
|
|
1897
|
-
this.abort();
|
|
1898
|
-
removeConnector(this);
|
|
1899
|
-
this.options.auth = undefined;
|
|
1900
|
-
this.user = undefined;
|
|
1901
|
-
this.emitter.emit('logout');
|
|
1902
|
-
this.resources.cache.clean();
|
|
1903
|
-
};
|
|
1904
|
-
NgwConnector.prototype.getUserInfo = function (credentials, options) {
|
|
1905
|
-
if (this.user && this.user.id) {
|
|
1906
|
-
return CancelablePromise.resolve(this.user);
|
|
1907
|
-
}
|
|
1908
|
-
if (credentials) {
|
|
1909
|
-
this.options.auth = credentials;
|
|
1910
|
-
}
|
|
1911
|
-
var options_ = __assign({ headers: this.getAuthorizationHeaders(credentials) }, options);
|
|
1912
|
-
// Do not use apiRequest('auth.current_user') to avoid circular-references
|
|
1913
|
-
return this.makeQuery('/api/component/auth/current_user', {}, options_);
|
|
1914
|
-
};
|
|
1915
|
-
/**
|
|
1916
|
-
* Obtaining the required Headers for authentication of requests in the NGW.
|
|
1917
|
-
*/
|
|
1918
|
-
NgwConnector.prototype.getAuthorizationHeaders = function (credentials) {
|
|
1919
|
-
var client = this.makeClientId(credentials);
|
|
1920
|
-
if (client) {
|
|
1921
|
-
return {
|
|
1922
|
-
Authorization: 'Basic ' + client,
|
|
1923
|
-
};
|
|
1924
|
-
}
|
|
1925
|
-
};
|
|
1926
|
-
NgwConnector.prototype.makeClientId = function (credentials) {
|
|
1927
|
-
credentials = credentials || this.options.auth;
|
|
1928
|
-
if (credentials) {
|
|
1929
|
-
var login = credentials.login, password = credentials.password;
|
|
1930
|
-
var str = unescape(encodeURIComponent("".concat(login, ":").concat(password)));
|
|
1931
|
-
// @ts-ignore
|
|
1932
|
-
{
|
|
1933
|
-
return window.btoa(str);
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
};
|
|
1937
|
-
/** Stop all api requests */
|
|
1938
|
-
NgwConnector.prototype.abort = function () {
|
|
1939
|
-
for (var _i = 0, _a = this.activeRequests; _i < _a.length; _i++) {
|
|
1940
|
-
var req = _a[_i];
|
|
1941
|
-
req.cancel();
|
|
1942
|
-
}
|
|
1943
|
-
this.activeRequests = [];
|
|
1944
|
-
};
|
|
1945
|
-
NgwConnector.prototype.getActiveApiRequests = function () {
|
|
1946
|
-
return __spreadArray([], this.activeRequests, true);
|
|
1947
|
-
};
|
|
1948
|
-
/**
|
|
1949
|
-
* Send request to NGW api router.
|
|
1950
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
1951
|
-
* @param params - Request item params or query params
|
|
1952
|
-
* @param requestOptions - Request options
|
|
1953
|
-
*
|
|
1954
|
-
* @example
|
|
1955
|
-
* ```javascript
|
|
1956
|
-
*
|
|
1957
|
-
* // there is such an NGW route item
|
|
1958
|
-
* // "feature_layer.feature.item": [
|
|
1959
|
-
* // "/api/resource/{0}/feature/{1}",
|
|
1960
|
-
* // "id",
|
|
1961
|
-
* // "fid"
|
|
1962
|
-
* // ],
|
|
1963
|
-
*
|
|
1964
|
-
* const connector = new NgwConnector({ baseUrl: 'https://example.nextgis.com' });
|
|
1965
|
-
* connector.apiRequest('feature_layer.feature.item', {
|
|
1966
|
-
* // request params for {0} and {1}
|
|
1967
|
-
* 'id': 2011,
|
|
1968
|
-
* 'fid': 101,
|
|
1969
|
-
* // query params
|
|
1970
|
-
* 'srs': 4326,
|
|
1971
|
-
* 'geom_format': 'geojson',
|
|
1972
|
-
* }, { method: 'GET' });
|
|
1973
|
-
* // send get-request to 'https://example.nextgis.com/api/resource/2011/feature/101?srs=4326&geom_format=geojson'
|
|
1974
|
-
*
|
|
1975
|
-
* ```
|
|
1976
|
-
*/
|
|
1977
|
-
NgwConnector.prototype.apiRequest = function (name, params_, requestOptions) {
|
|
1978
|
-
var _this = this;
|
|
1979
|
-
var _a;
|
|
1980
|
-
if (params_ === void 0) { params_ = {}; }
|
|
1981
|
-
if (requestOptions === void 0) { requestOptions = {}; }
|
|
1982
|
-
var method = requestOptions.method, headers = requestOptions.headers, withCredentials = requestOptions.withCredentials, responseType = requestOptions.responseType;
|
|
1983
|
-
var params = objectRemoveEmpty(params_);
|
|
1984
|
-
var makeApiRequest = function () {
|
|
1985
|
-
return apiRequest({ name: name, params: params, requestOptions: requestOptions, connector: _this });
|
|
1986
|
-
};
|
|
1987
|
-
if (requestOptions.cache && method === 'GET') {
|
|
1988
|
-
var cache = new Cache();
|
|
1989
|
-
return cache.add(name, makeApiRequest, __assign({ params: params }, objectRemoveEmpty({
|
|
1990
|
-
headers: headers,
|
|
1991
|
-
withCredentials: withCredentials,
|
|
1992
|
-
responseType: responseType,
|
|
1993
|
-
baseUrl: this.options.baseUrl,
|
|
1994
|
-
userId: (_a = this.user) === null || _a === void 0 ? void 0 : _a.id,
|
|
1995
|
-
})));
|
|
1996
|
-
}
|
|
1997
|
-
return makeApiRequest();
|
|
1998
|
-
};
|
|
1999
|
-
/**
|
|
2000
|
-
* Shortcut method for send POST request to NGW.
|
|
2001
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2002
|
-
* @param options - Request options
|
|
2003
|
-
* @param params - Request item params or query params
|
|
2004
|
-
*
|
|
2005
|
-
* @example
|
|
2006
|
-
* ```javascript
|
|
2007
|
-
* connector.post('resource.collection', { data: POST_PAYLOAD })
|
|
2008
|
-
* .then((newResource) => console.log(newResource))
|
|
2009
|
-
* .catch((error) => console.warn(error));
|
|
2010
|
-
* ```
|
|
2011
|
-
*/
|
|
2012
|
-
NgwConnector.prototype.post = function (name, options, params) {
|
|
2013
|
-
options = options || {};
|
|
2014
|
-
options.method = 'POST';
|
|
2015
|
-
return this.apiRequest(name, params, options);
|
|
2016
|
-
};
|
|
2017
|
-
/**
|
|
2018
|
-
* Shortcut method for send GET request to NGW.
|
|
2019
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2020
|
-
* @param options - Request options
|
|
2021
|
-
* @param params - Request item params or query params
|
|
2022
|
-
*/
|
|
2023
|
-
NgwConnector.prototype.get = function (name, options, params) {
|
|
2024
|
-
options = options || {};
|
|
2025
|
-
options.method = 'GET';
|
|
2026
|
-
return this.apiRequest(name, params, options);
|
|
2027
|
-
};
|
|
2028
|
-
/**
|
|
2029
|
-
* Shortcut method for send PATCH request to NGW.
|
|
2030
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2031
|
-
* @param options - Request options
|
|
2032
|
-
* @param params - Request item params or query params
|
|
2033
|
-
*/
|
|
2034
|
-
NgwConnector.prototype.patch = function (name, options, params) {
|
|
2035
|
-
options = options || {};
|
|
2036
|
-
options.method = 'PATCH';
|
|
2037
|
-
return this.apiRequest(name, params, options);
|
|
2038
|
-
};
|
|
2039
|
-
/**
|
|
2040
|
-
* Shortcut method for send PUT request to NGW.
|
|
2041
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2042
|
-
* @param options - Request options
|
|
2043
|
-
* @param params - Request item params or query params
|
|
2044
|
-
*/
|
|
2045
|
-
NgwConnector.prototype.put = function (name, options, params) {
|
|
2046
|
-
options = options || {};
|
|
2047
|
-
options.method = 'PUT';
|
|
2048
|
-
return this.apiRequest(name, params, options);
|
|
2049
|
-
};
|
|
2050
|
-
/**
|
|
2051
|
-
* Shortcut method for send DELETE request to NGW.
|
|
2052
|
-
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2053
|
-
* @param options - Request options
|
|
2054
|
-
* @param params - Request item params or query params
|
|
2055
|
-
*/
|
|
2056
|
-
NgwConnector.prototype.delete = function (name, options, params) {
|
|
2057
|
-
options = options || {};
|
|
2058
|
-
options.method = 'DELETE';
|
|
2059
|
-
return this.apiRequest(name, params, options);
|
|
2060
|
-
};
|
|
2061
|
-
/**
|
|
2062
|
-
* Send request to NGW.
|
|
2063
|
-
* @param url - URL address to NGW
|
|
2064
|
-
* @param params - Query params
|
|
2065
|
-
* @param options - Request options
|
|
2066
|
-
*/
|
|
2067
|
-
NgwConnector.prototype.makeQuery = function (url, params, options) {
|
|
2068
|
-
if (options === void 0) { options = {}; }
|
|
2069
|
-
url = (this.options.baseUrl ? this.options.baseUrl : '') + url;
|
|
2070
|
-
if (url) {
|
|
2071
|
-
if (params) {
|
|
2072
|
-
url = template(url, params);
|
|
2073
|
-
}
|
|
2074
|
-
url = encodeURI(fixUrlStr(url));
|
|
2075
|
-
return this._loadData(url, options);
|
|
2076
|
-
}
|
|
2077
|
-
else {
|
|
2078
|
-
throw new Error('Empty `url` not allowed');
|
|
2079
|
-
}
|
|
2080
|
-
};
|
|
2081
|
-
// -------------------------------------------------------------------------
|
|
2082
|
-
// Resource Methods
|
|
2083
|
-
// -------------------------------------------------------------------------
|
|
2084
|
-
/**
|
|
2085
|
-
* {@link ResourcesControl.getOne}
|
|
2086
|
-
*/
|
|
2087
|
-
NgwConnector.prototype.getResource = function (resource, requestOptions) {
|
|
2088
|
-
return this.resources.getOne(resource, requestOptions);
|
|
2089
|
-
};
|
|
2090
|
-
/**
|
|
2091
|
-
* {@link ResourcesControl.getOneOrFail}
|
|
2092
|
-
*/
|
|
2093
|
-
NgwConnector.prototype.getResourceOrFail = function (resource, requestOptions) {
|
|
2094
|
-
return this.resources.getOneOrFail(resource, requestOptions);
|
|
2095
|
-
};
|
|
2096
|
-
/**
|
|
2097
|
-
* @deprecated - use {@link getResource}
|
|
2098
|
-
*/
|
|
2099
|
-
NgwConnector.prototype.getResourceBy = function (resource) {
|
|
2100
|
-
return this.resources.getOne(resource);
|
|
2101
|
-
};
|
|
2102
|
-
/**
|
|
2103
|
-
* @deprecated - use {@link getResource}
|
|
2104
|
-
*/
|
|
2105
|
-
NgwConnector.prototype.getResourceByKeyname = function (keyname) {
|
|
2106
|
-
return this.resources.getOne(keyname);
|
|
2107
|
-
};
|
|
2108
|
-
/**
|
|
2109
|
-
* @deprecated - use {@link getResource}
|
|
2110
|
-
*/
|
|
2111
|
-
NgwConnector.prototype.getResourceById = function (id) {
|
|
2112
|
-
return this.resources.getOne(id);
|
|
2113
|
-
};
|
|
2114
|
-
/**
|
|
2115
|
-
* {@link ResourcesControl.getId}
|
|
2116
|
-
*/
|
|
2117
|
-
NgwConnector.prototype.getResourceId = function (resource, requestOptions) {
|
|
2118
|
-
return this.resources.getId(resource, requestOptions);
|
|
2119
|
-
};
|
|
2120
|
-
/**
|
|
2121
|
-
* {@link ResourcesControl.getIdOrFail}
|
|
2122
|
-
*/
|
|
2123
|
-
NgwConnector.prototype.getResourceIdOrFail = function (resource, requestOptions) {
|
|
2124
|
-
return this.resources.getIdOrFail(resource, requestOptions);
|
|
2125
|
-
};
|
|
2126
|
-
/**
|
|
2127
|
-
* {@link ResourcesControl.getMany}
|
|
2128
|
-
*/
|
|
2129
|
-
NgwConnector.prototype.getResourcesBy = function (resource, requestOptions) {
|
|
2130
|
-
return this.resources.getMany(resource, requestOptions);
|
|
2131
|
-
};
|
|
2132
|
-
/**
|
|
2133
|
-
* {@link ResourcesControl.getParent}
|
|
2134
|
-
*/
|
|
2135
|
-
NgwConnector.prototype.getResourceParent = function (resource, requestOptions) {
|
|
2136
|
-
return this.resources.getParent(resource, requestOptions);
|
|
2137
|
-
};
|
|
2138
|
-
/**
|
|
2139
|
-
* {@link ResourcesControl.getChildrenOf}
|
|
2140
|
-
*/
|
|
2141
|
-
NgwConnector.prototype.getResourceChildren = function (resource, requestOptions) {
|
|
2142
|
-
return this.resources.getChildrenOf(resource, requestOptions);
|
|
2143
|
-
};
|
|
2144
|
-
/**
|
|
2145
|
-
* {@link ResourcesControl.update}
|
|
2146
|
-
*/
|
|
2147
|
-
NgwConnector.prototype.updateResource = function (resource, data) {
|
|
2148
|
-
return this.resources.update(resource, data);
|
|
2149
|
-
};
|
|
2150
|
-
/**
|
|
2151
|
-
* {@link ResourcesControl.delete}
|
|
2152
|
-
*/
|
|
2153
|
-
NgwConnector.prototype.deleteResource = function (resource) {
|
|
2154
|
-
return this.resources.delete(resource);
|
|
2155
|
-
};
|
|
2156
|
-
/**
|
|
2157
|
-
* @internal
|
|
2158
|
-
*/
|
|
2159
|
-
NgwConnector.prototype._loadData = function (url, options) {
|
|
2160
|
-
var _this = this;
|
|
2161
|
-
options.responseType = options.responseType || 'json';
|
|
2162
|
-
var request = new CancelablePromise(function (resolve, reject, onCancel) {
|
|
2163
|
-
if (_this.user) {
|
|
2164
|
-
options = options || {};
|
|
2165
|
-
// options.withCredentials = true;
|
|
2166
|
-
options.headers = __assign(__assign({}, _this.getAuthorizationHeaders()), options.headers);
|
|
2167
|
-
}
|
|
2168
|
-
if (_this.requestTransform) {
|
|
2169
|
-
var _a = _this.requestTransform(url, options), transUrl = _a[0], transOptions = _a[1];
|
|
2170
|
-
url = transUrl;
|
|
2171
|
-
options = transOptions;
|
|
2172
|
-
}
|
|
2173
|
-
loadData(url, resolve, options, reject, onCancel);
|
|
2174
|
-
})
|
|
2175
|
-
.then(function (resp) {
|
|
2176
|
-
_this._cleanActiveRequests(request);
|
|
2177
|
-
return resp;
|
|
2178
|
-
})
|
|
2179
|
-
.catch(function (httpError) {
|
|
2180
|
-
_this._cleanActiveRequests(request);
|
|
2181
|
-
if (httpError instanceof CancelablePromise.CancelError) ;
|
|
2182
|
-
else {
|
|
2183
|
-
// @ts-ignore
|
|
2184
|
-
{
|
|
2185
|
-
console.warn('DEV WARN', httpError);
|
|
2186
|
-
}
|
|
2187
|
-
var er = _this._handleHttpError(httpError);
|
|
2188
|
-
if (er) {
|
|
2189
|
-
throw er;
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
});
|
|
2193
|
-
if (options.signal &&
|
|
2194
|
-
typeof options.signal.addEventListener === 'function') {
|
|
2195
|
-
options.signal.addEventListener('abort', function () {
|
|
2196
|
-
request.cancel();
|
|
2197
|
-
_this._cleanActiveRequests(request);
|
|
2198
|
-
});
|
|
2199
|
-
}
|
|
2200
|
-
this.activeRequests.push(request);
|
|
2201
|
-
return request;
|
|
2202
|
-
};
|
|
2203
|
-
NgwConnector.prototype._login = function (credentials, options) {
|
|
2204
|
-
var _this = this;
|
|
2205
|
-
return this.getUserInfo(credentials, options)
|
|
2206
|
-
.then(function (data) {
|
|
2207
|
-
_this.user = data;
|
|
2208
|
-
_this.emitter.emit('login', data);
|
|
2209
|
-
return data;
|
|
2210
|
-
})
|
|
2211
|
-
.catch(function (er) {
|
|
2212
|
-
_this.emitter.emit('login:error', er);
|
|
2213
|
-
throw er;
|
|
2214
|
-
});
|
|
2215
|
-
};
|
|
2216
|
-
NgwConnector.prototype._cleanActiveRequests = function (request) {
|
|
2217
|
-
var activeRequestIndex = this.activeRequests.indexOf(request);
|
|
2218
|
-
if (activeRequestIndex !== -1) {
|
|
2219
|
-
this.activeRequests.splice(activeRequestIndex, 1);
|
|
2220
|
-
}
|
|
2221
|
-
};
|
|
2222
|
-
NgwConnector.prototype._handleHttpError = function (er) {
|
|
2223
|
-
if (er) {
|
|
2224
|
-
if (er instanceof NgwError) {
|
|
2225
|
-
if (er.exception === 'nextgisweb.resource.exception.ResourceNotFound') {
|
|
2226
|
-
throw new ResourceNotFoundError(er);
|
|
2227
|
-
}
|
|
2228
|
-
else if (er.exception === 'nextgisweb.core.exception.InsufficientPermissions') {
|
|
2229
|
-
throw new InsufficientPermissionsError(er);
|
|
2230
|
-
}
|
|
2231
|
-
}
|
|
2232
|
-
}
|
|
2233
|
-
return er;
|
|
2234
|
-
};
|
|
2235
|
-
NgwConnector.errors = {
|
|
2236
|
-
NgwError: NgwError,
|
|
2237
|
-
ResourceNotFoundError: ResourceNotFoundError,
|
|
2238
|
-
};
|
|
2239
|
-
return NgwConnector;
|
|
1803
|
+
var ID = 0;
|
|
1804
|
+
var NgwConnector$1 = /** @class */ (function () {
|
|
1805
|
+
function NgwConnector(options) {
|
|
1806
|
+
this.options = options;
|
|
1807
|
+
this.id = ID++;
|
|
1808
|
+
this.emitter = new events.exports.EventEmitter();
|
|
1809
|
+
this.routeStr = '/api/component/pyramid/route';
|
|
1810
|
+
this.activeRequests = [];
|
|
1811
|
+
var exist = findConnector(options);
|
|
1812
|
+
if (exist) {
|
|
1813
|
+
return exist;
|
|
1814
|
+
}
|
|
1815
|
+
else {
|
|
1816
|
+
if (this.options.route) {
|
|
1817
|
+
this.routeStr = this.options.route;
|
|
1818
|
+
}
|
|
1819
|
+
if (this.options.requestTransform) {
|
|
1820
|
+
this.requestTransform = this.options.requestTransform;
|
|
1821
|
+
}
|
|
1822
|
+
this.resources = new ResourcesControl(this);
|
|
1823
|
+
addConnector(this);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
NgwConnector.create = function (options) {
|
|
1827
|
+
return new this(options);
|
|
1828
|
+
};
|
|
1829
|
+
NgwConnector.prototype.setRequestTransform = function (requestTransform) {
|
|
1830
|
+
this.requestTransform = requestTransform;
|
|
1831
|
+
};
|
|
1832
|
+
/**
|
|
1833
|
+
* Fast way to specify the connection address to NextGIS Web.
|
|
1834
|
+
* The current connection will be severed.
|
|
1835
|
+
* @param baseUrl - NGW url
|
|
1836
|
+
*/
|
|
1837
|
+
NgwConnector.prototype.setNgw = function (baseUrl) {
|
|
1838
|
+
this.logout();
|
|
1839
|
+
this.options.baseUrl = baseUrl;
|
|
1840
|
+
addConnector(this);
|
|
1841
|
+
};
|
|
1842
|
+
/**
|
|
1843
|
+
* Establishing a connection with NextGIS Web to fulfill all other requests.
|
|
1844
|
+
* @remarks
|
|
1845
|
+
* This method need not be called manually as it is used when forming a request in {@link apiRequest | apiRequest}.
|
|
1846
|
+
* Can be used to check connection.
|
|
1847
|
+
* @example
|
|
1848
|
+
* ```javascript
|
|
1849
|
+
* const connector = new NgwConnector({baseUrl: 'https://demo.nextgis.com'});
|
|
1850
|
+
* connector.connect()
|
|
1851
|
+
* .then(() => console.log('Ok'))
|
|
1852
|
+
* .catch((er) => console.log('Connection problem', er));
|
|
1853
|
+
* ```
|
|
1854
|
+
*/
|
|
1855
|
+
NgwConnector.prototype.connect = function () {
|
|
1856
|
+
var _this = this;
|
|
1857
|
+
var cache = new Cache();
|
|
1858
|
+
var auth = this.options.auth;
|
|
1859
|
+
var makeConnect = function () {
|
|
1860
|
+
return new CancelablePromise(function (resolve, reject) {
|
|
1861
|
+
var makeQuery = function () {
|
|
1862
|
+
return _this.makeQuery(_this.routeStr, {}, {})
|
|
1863
|
+
.then(function (route) {
|
|
1864
|
+
resolve(route);
|
|
1865
|
+
})
|
|
1866
|
+
.catch(function (er) {
|
|
1867
|
+
reject(er);
|
|
1868
|
+
});
|
|
1869
|
+
};
|
|
1870
|
+
if (auth) {
|
|
1871
|
+
var login = auth.login, password = auth.password;
|
|
1872
|
+
if (login && password) {
|
|
1873
|
+
return _this._login({ login: login, password: password })
|
|
1874
|
+
.then(function () {
|
|
1875
|
+
return makeQuery();
|
|
1876
|
+
})
|
|
1877
|
+
.catch(function (er) { return reject(er); });
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
return makeQuery();
|
|
1881
|
+
});
|
|
1882
|
+
};
|
|
1883
|
+
return cache.add('route', makeConnect, {
|
|
1884
|
+
id: this.id,
|
|
1885
|
+
auth: auth,
|
|
1886
|
+
baseUrl: this.options.baseUrl,
|
|
1887
|
+
});
|
|
1888
|
+
};
|
|
1889
|
+
/**
|
|
1890
|
+
* Quick way to change NextGIS Web user.
|
|
1891
|
+
* @param credentials - New user credentials
|
|
1892
|
+
*/
|
|
1893
|
+
NgwConnector.prototype.login = function (credentials, options) {
|
|
1894
|
+
this.logout();
|
|
1895
|
+
addConnector(this);
|
|
1896
|
+
return this._login(credentials, options);
|
|
1897
|
+
};
|
|
1898
|
+
/**
|
|
1899
|
+
* Disconnecting a user. Aborting all current requests
|
|
1900
|
+
*/
|
|
1901
|
+
NgwConnector.prototype.logout = function () {
|
|
1902
|
+
this.abort();
|
|
1903
|
+
removeConnector(this);
|
|
1904
|
+
this.options.auth = undefined;
|
|
1905
|
+
this.user = undefined;
|
|
1906
|
+
this.emitter.emit('logout');
|
|
1907
|
+
this.resources.cache.clean();
|
|
1908
|
+
};
|
|
1909
|
+
NgwConnector.prototype.getUserInfo = function (credentials, options) {
|
|
1910
|
+
if (this.user && this.user.id) {
|
|
1911
|
+
return CancelablePromise.resolve(this.user);
|
|
1912
|
+
}
|
|
1913
|
+
if (credentials) {
|
|
1914
|
+
this.options.auth = credentials;
|
|
1915
|
+
}
|
|
1916
|
+
var options_ = __assign({ headers: this.getAuthorizationHeaders(credentials) }, options);
|
|
1917
|
+
// Do not use apiRequest('auth.current_user') to avoid circular-references
|
|
1918
|
+
return this.makeQuery('/api/component/auth/current_user', {}, options_);
|
|
1919
|
+
};
|
|
1920
|
+
/**
|
|
1921
|
+
* Obtaining the required Headers for authentication of requests in the NGW.
|
|
1922
|
+
*/
|
|
1923
|
+
NgwConnector.prototype.getAuthorizationHeaders = function (credentials) {
|
|
1924
|
+
var client = this.makeClientId(credentials);
|
|
1925
|
+
if (client) {
|
|
1926
|
+
return {
|
|
1927
|
+
Authorization: 'Basic ' + client,
|
|
1928
|
+
};
|
|
1929
|
+
}
|
|
1930
|
+
};
|
|
1931
|
+
NgwConnector.prototype.makeClientId = function (credentials) {
|
|
1932
|
+
credentials = credentials || this.options.auth;
|
|
1933
|
+
if (credentials) {
|
|
1934
|
+
var login = credentials.login, password = credentials.password;
|
|
1935
|
+
var str = unescape(encodeURIComponent("".concat(login, ":").concat(password)));
|
|
1936
|
+
// @ts-ignore
|
|
1937
|
+
{
|
|
1938
|
+
return window.btoa(str);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
/** Stop all api requests */
|
|
1943
|
+
NgwConnector.prototype.abort = function () {
|
|
1944
|
+
for (var _i = 0, _a = this.activeRequests; _i < _a.length; _i++) {
|
|
1945
|
+
var req = _a[_i];
|
|
1946
|
+
req.cancel();
|
|
1947
|
+
}
|
|
1948
|
+
this.activeRequests = [];
|
|
1949
|
+
};
|
|
1950
|
+
NgwConnector.prototype.getActiveApiRequests = function () {
|
|
1951
|
+
return __spreadArray([], this.activeRequests, true);
|
|
1952
|
+
};
|
|
1953
|
+
/**
|
|
1954
|
+
* Send request to NGW api router.
|
|
1955
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
1956
|
+
* @param params - Request item params or query params
|
|
1957
|
+
* @param requestOptions - Request options
|
|
1958
|
+
*
|
|
1959
|
+
* @example
|
|
1960
|
+
* ```javascript
|
|
1961
|
+
*
|
|
1962
|
+
* // there is such an NGW route item
|
|
1963
|
+
* // "feature_layer.feature.item": [
|
|
1964
|
+
* // "/api/resource/{0}/feature/{1}",
|
|
1965
|
+
* // "id",
|
|
1966
|
+
* // "fid"
|
|
1967
|
+
* // ],
|
|
1968
|
+
*
|
|
1969
|
+
* const connector = new NgwConnector({ baseUrl: 'https://example.nextgis.com' });
|
|
1970
|
+
* connector.apiRequest('feature_layer.feature.item', {
|
|
1971
|
+
* // request params for {0} and {1}
|
|
1972
|
+
* 'id': 2011,
|
|
1973
|
+
* 'fid': 101,
|
|
1974
|
+
* // query params
|
|
1975
|
+
* 'srs': 4326,
|
|
1976
|
+
* 'geom_format': 'geojson',
|
|
1977
|
+
* }, { method: 'GET' });
|
|
1978
|
+
* // send get-request to 'https://example.nextgis.com/api/resource/2011/feature/101?srs=4326&geom_format=geojson'
|
|
1979
|
+
*
|
|
1980
|
+
* ```
|
|
1981
|
+
*/
|
|
1982
|
+
NgwConnector.prototype.apiRequest = function (name, params_, requestOptions) {
|
|
1983
|
+
var _this = this;
|
|
1984
|
+
var _a;
|
|
1985
|
+
if (params_ === void 0) { params_ = {}; }
|
|
1986
|
+
if (requestOptions === void 0) { requestOptions = {}; }
|
|
1987
|
+
var method = requestOptions.method, headers = requestOptions.headers, withCredentials = requestOptions.withCredentials, responseType = requestOptions.responseType;
|
|
1988
|
+
var params = objectRemoveEmpty(params_);
|
|
1989
|
+
var makeApiRequest = function () {
|
|
1990
|
+
return apiRequest({ name: name, params: params, requestOptions: requestOptions, connector: _this });
|
|
1991
|
+
};
|
|
1992
|
+
if (requestOptions.cache && method === 'GET') {
|
|
1993
|
+
var cache = new Cache();
|
|
1994
|
+
return cache.add(name, makeApiRequest, __assign({ params: params }, objectRemoveEmpty({
|
|
1995
|
+
headers: headers,
|
|
1996
|
+
withCredentials: withCredentials,
|
|
1997
|
+
responseType: responseType,
|
|
1998
|
+
baseUrl: this.options.baseUrl,
|
|
1999
|
+
userId: (_a = this.user) === null || _a === void 0 ? void 0 : _a.id,
|
|
2000
|
+
})));
|
|
2001
|
+
}
|
|
2002
|
+
return makeApiRequest();
|
|
2003
|
+
};
|
|
2004
|
+
/**
|
|
2005
|
+
* Shortcut method for send POST request to NGW.
|
|
2006
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2007
|
+
* @param options - Request options
|
|
2008
|
+
* @param params - Request item params or query params
|
|
2009
|
+
*
|
|
2010
|
+
* @example
|
|
2011
|
+
* ```javascript
|
|
2012
|
+
* connector.post('resource.collection', { data: POST_PAYLOAD })
|
|
2013
|
+
* .then((newResource) => console.log(newResource))
|
|
2014
|
+
* .catch((error) => console.warn(error));
|
|
2015
|
+
* ```
|
|
2016
|
+
*/
|
|
2017
|
+
NgwConnector.prototype.post = function (name, options, params) {
|
|
2018
|
+
options = options || {};
|
|
2019
|
+
options.method = 'POST';
|
|
2020
|
+
return this.apiRequest(name, params, options);
|
|
2021
|
+
};
|
|
2022
|
+
/**
|
|
2023
|
+
* Shortcut method for send GET request to NGW.
|
|
2024
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2025
|
+
* @param options - Request options
|
|
2026
|
+
* @param params - Request item params or query params
|
|
2027
|
+
*/
|
|
2028
|
+
NgwConnector.prototype.get = function (name, options, params) {
|
|
2029
|
+
options = options || {};
|
|
2030
|
+
options.method = 'GET';
|
|
2031
|
+
return this.apiRequest(name, params, options);
|
|
2032
|
+
};
|
|
2033
|
+
/**
|
|
2034
|
+
* Shortcut method for send PATCH request to NGW.
|
|
2035
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2036
|
+
* @param options - Request options
|
|
2037
|
+
* @param params - Request item params or query params
|
|
2038
|
+
*/
|
|
2039
|
+
NgwConnector.prototype.patch = function (name, options, params) {
|
|
2040
|
+
options = options || {};
|
|
2041
|
+
options.method = 'PATCH';
|
|
2042
|
+
return this.apiRequest(name, params, options);
|
|
2043
|
+
};
|
|
2044
|
+
/**
|
|
2045
|
+
* Shortcut method for send PUT request to NGW.
|
|
2046
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2047
|
+
* @param options - Request options
|
|
2048
|
+
* @param params - Request item params or query params
|
|
2049
|
+
*/
|
|
2050
|
+
NgwConnector.prototype.put = function (name, options, params) {
|
|
2051
|
+
options = options || {};
|
|
2052
|
+
options.method = 'PUT';
|
|
2053
|
+
return this.apiRequest(name, params, options);
|
|
2054
|
+
};
|
|
2055
|
+
/**
|
|
2056
|
+
* Shortcut method for send DELETE request to NGW.
|
|
2057
|
+
* @param name - NGW route name from {@link https://docs.nextgis.com/docs_ngweb_dev/doc/developer/resource.html#routes | routes}
|
|
2058
|
+
* @param options - Request options
|
|
2059
|
+
* @param params - Request item params or query params
|
|
2060
|
+
*/
|
|
2061
|
+
NgwConnector.prototype.delete = function (name, options, params) {
|
|
2062
|
+
options = options || {};
|
|
2063
|
+
options.method = 'DELETE';
|
|
2064
|
+
return this.apiRequest(name, params, options);
|
|
2065
|
+
};
|
|
2066
|
+
/**
|
|
2067
|
+
* Send request to NGW.
|
|
2068
|
+
* @param url - URL address to NGW
|
|
2069
|
+
* @param params - Query params
|
|
2070
|
+
* @param options - Request options
|
|
2071
|
+
*/
|
|
2072
|
+
NgwConnector.prototype.makeQuery = function (url, params, options) {
|
|
2073
|
+
if (options === void 0) { options = {}; }
|
|
2074
|
+
url = (this.options.baseUrl ? this.options.baseUrl : '') + url;
|
|
2075
|
+
if (url) {
|
|
2076
|
+
if (params) {
|
|
2077
|
+
url = template(url, params);
|
|
2078
|
+
}
|
|
2079
|
+
url = encodeURI(fixUrlStr(url));
|
|
2080
|
+
return this._loadData(url, options);
|
|
2081
|
+
}
|
|
2082
|
+
else {
|
|
2083
|
+
throw new Error('Empty `url` not allowed');
|
|
2084
|
+
}
|
|
2085
|
+
};
|
|
2086
|
+
// -------------------------------------------------------------------------
|
|
2087
|
+
// Resource Methods
|
|
2088
|
+
// -------------------------------------------------------------------------
|
|
2089
|
+
/**
|
|
2090
|
+
* {@link ResourcesControl.getOne}
|
|
2091
|
+
*/
|
|
2092
|
+
NgwConnector.prototype.getResource = function (resource, requestOptions) {
|
|
2093
|
+
return this.resources.getOne(resource, requestOptions);
|
|
2094
|
+
};
|
|
2095
|
+
/**
|
|
2096
|
+
* {@link ResourcesControl.getOneOrFail}
|
|
2097
|
+
*/
|
|
2098
|
+
NgwConnector.prototype.getResourceOrFail = function (resource, requestOptions) {
|
|
2099
|
+
return this.resources.getOneOrFail(resource, requestOptions);
|
|
2100
|
+
};
|
|
2101
|
+
/**
|
|
2102
|
+
* @deprecated - use {@link getResource}
|
|
2103
|
+
*/
|
|
2104
|
+
NgwConnector.prototype.getResourceBy = function (resource) {
|
|
2105
|
+
return this.resources.getOne(resource);
|
|
2106
|
+
};
|
|
2107
|
+
/**
|
|
2108
|
+
* @deprecated - use {@link getResource}
|
|
2109
|
+
*/
|
|
2110
|
+
NgwConnector.prototype.getResourceByKeyname = function (keyname) {
|
|
2111
|
+
return this.resources.getOne(keyname);
|
|
2112
|
+
};
|
|
2113
|
+
/**
|
|
2114
|
+
* @deprecated - use {@link getResource}
|
|
2115
|
+
*/
|
|
2116
|
+
NgwConnector.prototype.getResourceById = function (id) {
|
|
2117
|
+
return this.resources.getOne(id);
|
|
2118
|
+
};
|
|
2119
|
+
/**
|
|
2120
|
+
* {@link ResourcesControl.getId}
|
|
2121
|
+
*/
|
|
2122
|
+
NgwConnector.prototype.getResourceId = function (resource, requestOptions) {
|
|
2123
|
+
return this.resources.getId(resource, requestOptions);
|
|
2124
|
+
};
|
|
2125
|
+
/**
|
|
2126
|
+
* {@link ResourcesControl.getIdOrFail}
|
|
2127
|
+
*/
|
|
2128
|
+
NgwConnector.prototype.getResourceIdOrFail = function (resource, requestOptions) {
|
|
2129
|
+
return this.resources.getIdOrFail(resource, requestOptions);
|
|
2130
|
+
};
|
|
2131
|
+
/**
|
|
2132
|
+
* {@link ResourcesControl.getMany}
|
|
2133
|
+
*/
|
|
2134
|
+
NgwConnector.prototype.getResourcesBy = function (resource, requestOptions) {
|
|
2135
|
+
return this.resources.getMany(resource, requestOptions);
|
|
2136
|
+
};
|
|
2137
|
+
/**
|
|
2138
|
+
* {@link ResourcesControl.getParent}
|
|
2139
|
+
*/
|
|
2140
|
+
NgwConnector.prototype.getResourceParent = function (resource, requestOptions) {
|
|
2141
|
+
return this.resources.getParent(resource, requestOptions);
|
|
2142
|
+
};
|
|
2143
|
+
/**
|
|
2144
|
+
* {@link ResourcesControl.getChildrenOf}
|
|
2145
|
+
*/
|
|
2146
|
+
NgwConnector.prototype.getResourceChildren = function (resource, requestOptions) {
|
|
2147
|
+
return this.resources.getChildrenOf(resource, requestOptions);
|
|
2148
|
+
};
|
|
2149
|
+
/**
|
|
2150
|
+
* {@link ResourcesControl.update}
|
|
2151
|
+
*/
|
|
2152
|
+
NgwConnector.prototype.updateResource = function (resource, data) {
|
|
2153
|
+
return this.resources.update(resource, data);
|
|
2154
|
+
};
|
|
2155
|
+
/**
|
|
2156
|
+
* {@link ResourcesControl.delete}
|
|
2157
|
+
*/
|
|
2158
|
+
NgwConnector.prototype.deleteResource = function (resource) {
|
|
2159
|
+
return this.resources.delete(resource);
|
|
2160
|
+
};
|
|
2161
|
+
/**
|
|
2162
|
+
* @internal
|
|
2163
|
+
*/
|
|
2164
|
+
NgwConnector.prototype._loadData = function (url, options) {
|
|
2165
|
+
var _this = this;
|
|
2166
|
+
options.responseType = options.responseType || 'json';
|
|
2167
|
+
var request = new CancelablePromise(function (resolve, reject, onCancel) {
|
|
2168
|
+
if (_this.user) {
|
|
2169
|
+
options = options || {};
|
|
2170
|
+
// options.withCredentials = true;
|
|
2171
|
+
options.headers = __assign(__assign({}, _this.getAuthorizationHeaders()), options.headers);
|
|
2172
|
+
}
|
|
2173
|
+
if (_this.requestTransform) {
|
|
2174
|
+
var _a = _this.requestTransform(url, options), transUrl = _a[0], transOptions = _a[1];
|
|
2175
|
+
url = transUrl;
|
|
2176
|
+
options = transOptions;
|
|
2177
|
+
}
|
|
2178
|
+
loadData(url, resolve, options, reject, onCancel);
|
|
2179
|
+
})
|
|
2180
|
+
.then(function (resp) {
|
|
2181
|
+
_this._cleanActiveRequests(request);
|
|
2182
|
+
return resp;
|
|
2183
|
+
})
|
|
2184
|
+
.catch(function (httpError) {
|
|
2185
|
+
_this._cleanActiveRequests(request);
|
|
2186
|
+
if (httpError instanceof CancelablePromise.CancelError) ;
|
|
2187
|
+
else {
|
|
2188
|
+
// @ts-ignore
|
|
2189
|
+
{
|
|
2190
|
+
console.warn('DEV WARN', httpError);
|
|
2191
|
+
}
|
|
2192
|
+
var er = _this._handleHttpError(httpError);
|
|
2193
|
+
if (er) {
|
|
2194
|
+
throw er;
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
});
|
|
2198
|
+
if (options.signal &&
|
|
2199
|
+
typeof options.signal.addEventListener === 'function') {
|
|
2200
|
+
options.signal.addEventListener('abort', function () {
|
|
2201
|
+
request.cancel();
|
|
2202
|
+
_this._cleanActiveRequests(request);
|
|
2203
|
+
});
|
|
2204
|
+
}
|
|
2205
|
+
this.activeRequests.push(request);
|
|
2206
|
+
return request;
|
|
2207
|
+
};
|
|
2208
|
+
NgwConnector.prototype._login = function (credentials, options) {
|
|
2209
|
+
var _this = this;
|
|
2210
|
+
return this.getUserInfo(credentials, options)
|
|
2211
|
+
.then(function (data) {
|
|
2212
|
+
_this.user = data;
|
|
2213
|
+
_this.emitter.emit('login', data);
|
|
2214
|
+
return data;
|
|
2215
|
+
})
|
|
2216
|
+
.catch(function (er) {
|
|
2217
|
+
_this.emitter.emit('login:error', er);
|
|
2218
|
+
throw er;
|
|
2219
|
+
});
|
|
2220
|
+
};
|
|
2221
|
+
NgwConnector.prototype._cleanActiveRequests = function (request) {
|
|
2222
|
+
var activeRequestIndex = this.activeRequests.indexOf(request);
|
|
2223
|
+
if (activeRequestIndex !== -1) {
|
|
2224
|
+
this.activeRequests.splice(activeRequestIndex, 1);
|
|
2225
|
+
}
|
|
2226
|
+
};
|
|
2227
|
+
NgwConnector.prototype._handleHttpError = function (er) {
|
|
2228
|
+
if (er) {
|
|
2229
|
+
if (er instanceof NgwError) {
|
|
2230
|
+
if (er.exception === 'nextgisweb.resource.exception.ResourceNotFound') {
|
|
2231
|
+
throw new ResourceNotFoundError(er);
|
|
2232
|
+
}
|
|
2233
|
+
else if (er.exception === 'nextgisweb.core.exception.InsufficientPermissions') {
|
|
2234
|
+
throw new InsufficientPermissionsError(er);
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
return er;
|
|
2239
|
+
};
|
|
2240
|
+
NgwConnector.errors = {
|
|
2241
|
+
NgwError: NgwError,
|
|
2242
|
+
ResourceNotFoundError: ResourceNotFoundError,
|
|
2243
|
+
};
|
|
2244
|
+
return NgwConnector;
|
|
2240
2245
|
}());
|
|
2241
2246
|
|
|
2242
|
-
/**
|
|
2243
|
-
* A lightweight HTTP client optimized for use with {@link http://docs.nextgis.ru/docs_ngweb_dev/doc/developer/toc.html | NextGIS Web} API
|
|
2244
|
-
*
|
|
2245
|
-
* @remarks
|
|
2246
|
-
* Make sure CORS is registered in the {@link https://docs.nextgis.com/docs_ngcom/source/CORS.html | NextGIS Web settings } to be able to send requests.
|
|
2247
|
-
*
|
|
2248
|
-
* @packageDocumentation
|
|
2249
|
-
* @module ngw-connector
|
|
2250
|
-
*/
|
|
2247
|
+
/**
|
|
2248
|
+
* A lightweight HTTP client optimized for use with {@link http://docs.nextgis.ru/docs_ngweb_dev/doc/developer/toc.html | NextGIS Web} API
|
|
2249
|
+
*
|
|
2250
|
+
* @remarks
|
|
2251
|
+
* Make sure CORS is registered in the {@link https://docs.nextgis.com/docs_ngcom/source/CORS.html | NextGIS Web settings } to be able to send requests.
|
|
2252
|
+
*
|
|
2253
|
+
* @packageDocumentation
|
|
2254
|
+
* @module ngw-connector
|
|
2255
|
+
*/
|
|
2251
2256
|
var NgwConnector = NgwConnector$1;
|
|
2252
2257
|
|
|
2253
2258
|
return NgwConnector;
|