@nextgis/ngw-connector 1.9.2 → 1.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 1.9.2; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 1.11.2; author: NextGIS */
2
2
  var NgwConnector = (function () {
3
3
  'use strict';
4
4
 
@@ -44,10 +44,52 @@ var NgwConnector = (function () {
44
44
  return __assign.apply(this, arguments);
45
45
  };
46
46
 
47
- function __spreadArray(to, from) {
48
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
49
- to[j] = from[i];
50
- return to;
47
+ function __awaiter(thisArg, _arguments, P, generator) {
48
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
49
+ return new (P || (P = Promise))(function (resolve, reject) {
50
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
51
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
52
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
53
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
54
+ });
55
+ }
56
+
57
+ function __generator(thisArg, body) {
58
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
59
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
60
+ function verb(n) { return function (v) { return step([n, v]); }; }
61
+ function step(op) {
62
+ if (f) throw new TypeError("Generator is already executing.");
63
+ while (_) try {
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
+ if (y = 0, t) op = [op[0] & 2, t.value];
66
+ switch (op[0]) {
67
+ case 0: case 1: t = op; break;
68
+ case 4: _.label++; return { value: op[1], done: false };
69
+ case 5: _.label++; y = op[1]; op = [0]; continue;
70
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
71
+ default:
72
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
73
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
74
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
75
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
76
+ if (t[2]) _.ops.pop();
77
+ _.trys.pop(); continue;
78
+ }
79
+ op = body.call(thisArg, _);
80
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
81
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
82
+ }
83
+ }
84
+
85
+ function __spreadArray(to, from, pack) {
86
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
87
+ if (ar || !(i in from)) {
88
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
89
+ ar[i] = from[i];
90
+ }
91
+ }
92
+ return to.concat(ar || Array.prototype.slice.call(from));
51
93
  }
52
94
 
53
95
  var events = {exports: {}};
@@ -909,6 +951,23 @@ var NgwConnector = (function () {
909
951
  */
910
952
  function defined(val) {
911
953
  return val !== undefined && val !== null;
954
+ }
955
+ /**
956
+ * from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
957
+ *
958
+ * @param val - The object.
959
+ * @returns Returns true if the object is defined and not empty string, returns false otherwise.
960
+ *
961
+ * @example
962
+ * ```javascript
963
+ * full('foo') // true
964
+ * full('') // false
965
+ * full(undefined) // false
966
+ * full(0) // true
967
+ * ```
968
+ */
969
+ function full(val) {
970
+ return typeof val === 'string' ? !!val : defined(val);
912
971
  }
913
972
 
914
973
  function isEqual(a, b, o, p) {
@@ -977,13 +1036,46 @@ var NgwConnector = (function () {
977
1036
  return url.replace(/([^:]\/)\/+/g, '$1');
978
1037
  }
979
1038
 
1039
+ /**
1040
+ * @example
1041
+ * ```javascript
1042
+ * const cache1 = new Cache();
1043
+ * cache1.add('foo', 'value');
1044
+ *
1045
+ * const cache2 = new Cache();
1046
+ * cache2.match('foo'); // value
1047
+ *
1048
+ * const cache3 = new Cache({ namespace: 'foo' });
1049
+ * cache3.match('foo'); // undefined
1050
+ * ```
1051
+ *
1052
+ * @example
1053
+ * ```javascript
1054
+ * let COUNTER = 0;
1055
+ * const createPromise = () => new Promise((res) => {
1056
+ * COUNTER++
1057
+ * setTimeout(() => res('Ok'), 300)
1058
+ * });
1059
+ *
1060
+ * const cache = new Cache();
1061
+ * for (let i = 0; i < 10; i++) {
1062
+ * cache.add('foo', createPromise).then((data) => {
1063
+ * console.log(data); // 'Ok'
1064
+ * });
1065
+ * }
1066
+ * console.log(COUNTER); // 1
1067
+ * ```
1068
+ */
980
1069
  var Cache$1 = /** @class */ (function () {
981
- function Cache() {
1070
+ function Cache(options) {
1071
+ if (options === void 0) { options = {}; }
1072
+ var _a;
982
1073
  this.cache = [];
983
- if (Cache.instance) {
984
- return Cache.instance;
1074
+ var namespace = (_a = options.namespace) !== null && _a !== void 0 ? _a : 'default';
1075
+ if (Cache.instance[namespace]) {
1076
+ return Cache.instance[namespace];
985
1077
  }
986
- Cache.instance = this;
1078
+ Cache.instance[namespace] = this;
987
1079
  return this;
988
1080
  }
989
1081
  Cache.prototype.clean = function () {
@@ -992,9 +1084,34 @@ var NgwConnector = (function () {
992
1084
  Cache.prototype.all = function () {
993
1085
  return this.cache;
994
1086
  };
995
- Cache.prototype.add = function (key, valueToSet, options) {
1087
+ /**
1088
+ * Caching only a non-empty value.
1089
+ *
1090
+ * Useful for get or create strategy
1091
+ * @example
1092
+ * ```javascript
1093
+ * const cache = new Cache();
1094
+ * const getItemFunc = () => fetch(url).then((data) => {
1095
+ * return data.json(); // undefined
1096
+ * });
1097
+ * const item = await cache.addFull('foo', getItemFunc);
1098
+ * if (!item) {
1099
+ * await createItem(); // 'New item'
1100
+ * }
1101
+ *
1102
+ * // somewhere else in the code
1103
+ * const item = await cache.addFull('foo', getItemFunc).then((resp) => {
1104
+ * console.log(resp); // 'New item'
1105
+ * })
1106
+ *
1107
+ * ```
1108
+ */
1109
+ Cache.prototype.addFull = function (key, valueToSet, props) {
1110
+ return this.add(key, valueToSet, props, true);
1111
+ };
1112
+ Cache.prototype.add = function (key, valueToSet, props, onlyFull) {
996
1113
  var _this = this;
997
- var exist = this._find(key, options);
1114
+ var exist = this._find(key, props);
998
1115
  if (!exist) {
999
1116
  var value = void 0;
1000
1117
  if (valueToSet instanceof Function) {
@@ -1003,18 +1120,31 @@ var NgwConnector = (function () {
1003
1120
  else {
1004
1121
  value = valueToSet;
1005
1122
  }
1006
- var options_ = options && JSON.parse(JSON.stringify(objectRemoveEmpty(options)));
1123
+ var props_ = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
1007
1124
  var cacheItem = {
1008
1125
  key: key,
1009
1126
  value: value,
1010
- options: options_,
1127
+ props: props_,
1128
+ // TODO: remove backward compatibility use only props
1129
+ options: props_,
1011
1130
  };
1131
+ if (onlyFull && !full(value)) {
1132
+ return value;
1133
+ }
1012
1134
  this.cache.push(cacheItem);
1013
1135
  if (value instanceof Promise) {
1014
1136
  value.catch(function (er) {
1015
- _this.delete(key, options);
1137
+ _this.delete(key, props);
1016
1138
  throw er;
1017
1139
  });
1140
+ if (onlyFull) {
1141
+ value.then(function (x) {
1142
+ if (!full(x)) {
1143
+ _this.delete(key, props);
1144
+ }
1145
+ return x;
1146
+ });
1147
+ }
1018
1148
  return value;
1019
1149
  }
1020
1150
  return value;
@@ -1023,43 +1153,52 @@ var NgwConnector = (function () {
1023
1153
  return exist.value;
1024
1154
  }
1025
1155
  };
1026
- Cache.prototype.match = function (key, options) {
1027
- var cacheRecord = this._find(key, options);
1156
+ Cache.prototype.match = function (key, props) {
1157
+ var cacheRecord = this._find(key, props);
1028
1158
  if (cacheRecord) {
1029
1159
  return cacheRecord.value;
1030
1160
  }
1031
1161
  };
1032
- Cache.prototype.matchAll = function (key, options) {
1162
+ Cache.prototype.matchAll = function (key, props) {
1033
1163
  var _this = this;
1034
1164
  if (key) {
1035
1165
  return this.cache
1036
- .filter(function (x) { return _this._filter(x, key, options); })
1166
+ .filter(function (x) { return _this._filter(x, key, props); })
1037
1167
  .map(function (x) { return x.value; });
1038
1168
  }
1039
1169
  return this.cache.map(function (x) { return x.value; });
1040
1170
  };
1041
- Cache.prototype.delete = function (key, options) {
1171
+ Cache.prototype.delete = function (keyOrItem, props) {
1042
1172
  var _this = this;
1043
- var exist = this.cache.filter(function (x) { return _this._filter(x, key, options); });
1173
+ var exist = [];
1174
+ if (typeof keyOrItem === 'string') {
1175
+ exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, props); });
1176
+ }
1177
+ else {
1178
+ exist.push(keyOrItem);
1179
+ }
1044
1180
  for (var _i = 0, exist_1 = exist; _i < exist_1.length; _i++) {
1045
1181
  var e = exist_1[_i];
1046
1182
  var index = this.cache.indexOf(e);
1047
1183
  this.cache.splice(index, 1);
1048
1184
  }
1049
1185
  };
1050
- Cache.prototype._find = function (key, options) {
1186
+ Cache.prototype._find = function (key, props) {
1051
1187
  var _this = this;
1052
- return this.cache.find(function (x) { return _this._filter(x, key, options); });
1188
+ return this.cache.find(function (x) { return _this._filter(x, key, props); });
1053
1189
  };
1054
- Cache.prototype._filter = function (item, key, options) {
1190
+ Cache.prototype._filter = function (item, key, props) {
1055
1191
  if (item.key === key) {
1056
- if (options) {
1057
- return objectDeepEqual(item.options || {}, objectRemoveEmpty(options));
1192
+ if (props) {
1193
+ // TODO: remove backward compatibility
1194
+ var itemProps = item.props || item.options;
1195
+ return objectDeepEqual(itemProps || {}, objectRemoveEmpty(props));
1058
1196
  }
1059
1197
  return true;
1060
1198
  }
1061
1199
  return false;
1062
1200
  };
1201
+ Cache.instance = {};
1063
1202
  return Cache;
1064
1203
  }());
1065
1204
 
@@ -1095,12 +1234,16 @@ var NgwConnector = (function () {
1095
1234
  var _this = _super.call(this) || this;
1096
1235
  _this.name = 'NetworkError';
1097
1236
  Object.setPrototypeOf(_this, NetworkError.prototype);
1098
- _this.message = "Unable to request " + url + ".\n Possibly invalid NGW URL entered or CORS not configured to get request from " + location.origin; // /control-panel/cors
1237
+ _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
1099
1238
  return _this;
1100
1239
  }
1101
1240
  return NetworkError;
1102
1241
  }(Error));
1103
1242
 
1243
+ function isObject(val) {
1244
+ return Object.prototype.toString.call(val) === '[object Object]';
1245
+ }
1246
+
1104
1247
  var loadData;
1105
1248
  // @ts-ignore
1106
1249
  {
@@ -1136,7 +1279,7 @@ var NgwConnector = (function () {
1136
1279
  }
1137
1280
  };
1138
1281
  xhr.onload = function () {
1139
- if ([401, 403, 404, 500].indexOf(xhr.status) !== -1) {
1282
+ if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
1140
1283
  error(new NgwError(getResponseText()));
1141
1284
  }
1142
1285
  processingResponse();
@@ -1191,7 +1334,7 @@ var NgwConnector = (function () {
1191
1334
  data = new FormData();
1192
1335
  data.append('file', options.file);
1193
1336
  if (options.data) {
1194
- for (var d in data) {
1337
+ for (var d in options.data) {
1195
1338
  data.append(d, data[d]);
1196
1339
  }
1197
1340
  }
@@ -1283,30 +1426,38 @@ var NgwConnector = (function () {
1283
1426
  }
1284
1427
  }
1285
1428
 
1286
- function isObject(val) {
1287
- return Object.prototype.toString.call(val) === '[object Object]';
1288
- }
1289
-
1429
+ var exclude = ['description'];
1290
1430
  /**
1431
+ * @remarks
1432
+ * https://docs.nextgis.ru/docs_ngweb_dev/doc/developer/resource.html#search-resources
1433
+ *
1291
1434
  * ```
1292
- * { keyname, parent: { id }} > { keyname, parent__id }
1435
+ * { keyname, owner_user: { id }} > { keyname, owner_user__id }
1293
1436
  * ```
1437
+ *
1294
1438
  * @param resource - Any property from NGW resource item
1295
1439
  */
1296
1440
  function resourceToQuery(resource, prefix) {
1297
1441
  if (prefix === void 0) { prefix = ''; }
1298
1442
  prefix = prefix ? prefix + '__' : '';
1299
1443
  var query = {};
1300
- Object.entries(resource).forEach(function (_a) {
1301
- var key = _a[0], value = _a[1];
1302
- if (isObject(value)) {
1303
- var children = resourceToQuery(value, key);
1304
- Object.assign(query, children);
1305
- }
1306
- else if (defined(value)) {
1307
- query[prefix + key] = value;
1444
+ for (var _i = 0, _a = Object.entries(resource); _i < _a.length; _i++) {
1445
+ var _b = _a[_i], key = _b[0], value = _b[1];
1446
+ if (exclude.indexOf(key) === -1) {
1447
+ if (isObject(value)) {
1448
+ if (key === 'owner_user') {
1449
+ var children = resourceToQuery(value, key);
1450
+ Object.assign(query, children);
1451
+ }
1452
+ else if (key === 'parent' && 'id' in value) {
1453
+ query.parent_id = value.id;
1454
+ }
1455
+ }
1456
+ else if (defined(value)) {
1457
+ query[prefix + key] = value;
1458
+ }
1308
1459
  }
1309
- });
1460
+ }
1310
1461
  return query;
1311
1462
  }
1312
1463
 
@@ -1332,19 +1483,30 @@ var NgwConnector = (function () {
1332
1483
  ResourcesControl.prototype.getOne = function (resource, requestOptions) {
1333
1484
  var _this = this;
1334
1485
  var cache = new Cache();
1486
+ var forCache = {};
1487
+ var opt = __assign(__assign({}, requestOptions), { cache: false });
1488
+ if (typeof resource === 'string') {
1489
+ forCache.keyname = resource;
1490
+ }
1491
+ else if (typeof resource === 'number') {
1492
+ forCache.id = resource;
1493
+ }
1494
+ else if (isObject(resource)) {
1495
+ forCache.id = resource.id;
1496
+ }
1335
1497
  var makeRequest = function () {
1336
1498
  if (typeof resource === 'string') {
1337
- return _this._fetchResourceBy({ keyname: resource }, requestOptions);
1499
+ return _this._fetchResourceBy({ keyname: resource }, opt);
1338
1500
  }
1339
1501
  else if (typeof resource === 'number') {
1340
- return _this._fetchResourceById(resource, requestOptions);
1502
+ return _this._fetchResourceById(resource, opt);
1341
1503
  }
1342
1504
  else if (isObject(resource)) {
1343
- return _this._fetchResourceBy(resource, requestOptions);
1505
+ return _this._fetchResourceBy(resource, opt);
1344
1506
  }
1345
1507
  return CancelablePromise.resolve(undefined);
1346
1508
  };
1347
- return cache.add('resource', makeRequest, { resource: resource });
1509
+ return cache.addFull('resource', makeRequest, forCache);
1348
1510
  };
1349
1511
  ResourcesControl.prototype.getOneOrFail = function (resource, requestOptions) {
1350
1512
  return this.getOne(resource, requestOptions).then(function (res) {
@@ -1408,11 +1570,12 @@ var NgwConnector = (function () {
1408
1570
  .get('resource.search', requestOptions, __assign({ serialization: 'full' }, query))
1409
1571
  .then(function (resources) {
1410
1572
  if (resources) {
1411
- resources.forEach(function (x) {
1573
+ for (var _i = 0, resources_1 = resources; _i < resources_1.length; _i++) {
1574
+ var x = resources_1[_i];
1412
1575
  _this.cache.add('resource.item', CancelablePromise.resolve(x), {
1413
1576
  id: x.resource.id,
1414
1577
  });
1415
- });
1578
+ }
1416
1579
  }
1417
1580
  return resources;
1418
1581
  });
@@ -1464,12 +1627,52 @@ var NgwConnector = (function () {
1464
1627
  return this.getId(resource).then(function (id) {
1465
1628
  if (id !== undefined) {
1466
1629
  return _this.connector.delete('resource.item', null, { id: id }).then(function () {
1467
- _this.cache.delete('resource.item', { id: id });
1630
+ _this._cleanResourceItemCache(id);
1468
1631
  return undefined;
1469
1632
  });
1470
1633
  }
1471
1634
  });
1472
1635
  };
1636
+ ResourcesControl.prototype._cleanResourceItemCache = function (id) {
1637
+ return __awaiter(this, void 0, void 0, function () {
1638
+ var all, toDelete, _i, all_1, c, cid, rid, _a, toDelete_1, d;
1639
+ return __generator(this, function (_b) {
1640
+ switch (_b.label) {
1641
+ case 0:
1642
+ all = this.cache.all();
1643
+ toDelete = [];
1644
+ _i = 0, all_1 = all;
1645
+ _b.label = 1;
1646
+ case 1:
1647
+ if (!(_i < all_1.length)) return [3 /*break*/, 5];
1648
+ c = all_1[_i];
1649
+ cid = c.options && c.options.id;
1650
+ if (!(['resource.item', 'resource'].includes(c.key) && cid !== undefined)) return [3 /*break*/, 4];
1651
+ if (!(typeof cid === 'number')) return [3 /*break*/, 2];
1652
+ if (cid === id) {
1653
+ toDelete.push(c);
1654
+ }
1655
+ return [3 /*break*/, 4];
1656
+ case 2: return [4 /*yield*/, this.getId(cid)];
1657
+ case 3:
1658
+ rid = _b.sent();
1659
+ if (rid === id) {
1660
+ toDelete.push(c);
1661
+ }
1662
+ _b.label = 4;
1663
+ case 4:
1664
+ _i++;
1665
+ return [3 /*break*/, 1];
1666
+ case 5:
1667
+ for (_a = 0, toDelete_1 = toDelete; _a < toDelete_1.length; _a++) {
1668
+ d = toDelete_1[_a];
1669
+ this.cache.delete(d);
1670
+ }
1671
+ return [2 /*return*/];
1672
+ }
1673
+ });
1674
+ });
1675
+ };
1473
1676
  ResourcesControl.prototype._fetchResourceById = function (id, requestOptions) {
1474
1677
  var _this = this;
1475
1678
  var promise = function () {
@@ -1520,7 +1723,7 @@ var NgwConnector = (function () {
1520
1723
  .then(function (apiItems) {
1521
1724
  var apiItem = apiItems && apiItems[opt.name];
1522
1725
  if (apiItem) {
1523
- apiItem = __spreadArray([], apiItem);
1726
+ apiItem = __spreadArray([], apiItem, true);
1524
1727
  var url = apiItem.shift();
1525
1728
  if (apiItem.length) {
1526
1729
  var replaceParams = {};
@@ -1542,12 +1745,12 @@ var NgwConnector = (function () {
1542
1745
  if (Array.isArray(paramList)) {
1543
1746
  delete params.paramList;
1544
1747
  paramList.forEach(function (x) {
1545
- paramArray_1.push(x[0] + "=" + x[1]);
1748
+ paramArray_1.push("".concat(x[0], "=").concat(x[1]));
1546
1749
  });
1547
1750
  }
1548
1751
  for (var p in params) {
1549
1752
  if (apiItem.indexOf(p) === -1) {
1550
- paramArray_1.push(p + "=" + params[p]);
1753
+ paramArray_1.push("".concat(p, "=").concat(params[p]));
1551
1754
  }
1552
1755
  }
1553
1756
  if (paramArray_1.length) {
@@ -1681,7 +1884,7 @@ var NgwConnector = (function () {
1681
1884
  headers: this.getAuthorizationHeaders(credentials),
1682
1885
  // withCredentials: true
1683
1886
  };
1684
- // Do not use request('auth.current_user') to avoid circular-references
1887
+ // Do not use apiRequest('auth.current_user') to avoid circular-references
1685
1888
  return this.makeQuery('/api/component/auth/current_user', {}, options)
1686
1889
  .then(function (data) {
1687
1890
  _this.user = data;
@@ -1708,7 +1911,7 @@ var NgwConnector = (function () {
1708
1911
  credentials = credentials || this.options.auth;
1709
1912
  if (credentials) {
1710
1913
  var login = credentials.login, password = credentials.password;
1711
- var str = unescape(encodeURIComponent(login + ":" + password));
1914
+ var str = unescape(encodeURIComponent("".concat(login, ":").concat(password)));
1712
1915
  // @ts-ignore
1713
1916
  {
1714
1917
  return window.btoa(str);
@@ -1842,7 +2045,7 @@ var NgwConnector = (function () {
1842
2045
  }
1843
2046
  // remove double slash
1844
2047
  url = fixUrlStr(url);
1845
- return this._loadData(url, options);
2048
+ return this._loadData(encodeURI(url), options);
1846
2049
  }
1847
2050
  else {
1848
2051
  throw new Error('Empty `url` not allowed');
@@ -1941,7 +2144,7 @@ var NgwConnector = (function () {
1941
2144
  else {
1942
2145
  // @ts-ignore
1943
2146
  {
1944
- console.warn(httpError);
2147
+ console.warn('DEV WARN', httpError);
1945
2148
  }
1946
2149
  var er = _this._handleHttpError(httpError);
1947
2150
  if (er) {