@nextgis/geocoder 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/geocoder; version: 1.9.2; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 1.11.2; author: NextGIS */
2
2
  var Geocoder = (function (exports) {
3
3
  'use strict';
4
4
 
@@ -44,6 +44,16 @@ var Geocoder = (function (exports) {
44
44
  return __assign.apply(this, arguments);
45
45
  };
46
46
 
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
+
47
57
  function __generator(thisArg, body) {
48
58
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
49
59
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -84,10 +94,14 @@ var Geocoder = (function (exports) {
84
94
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
85
95
  }
86
96
 
87
- function __spreadArray(to, from) {
88
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
89
- to[j] = from[i];
90
- return to;
97
+ function __spreadArray(to, from, pack) {
98
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
99
+ if (ar || !(i in from)) {
100
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
101
+ ar[i] = from[i];
102
+ }
103
+ }
104
+ return to.concat(ar || Array.prototype.slice.call(from));
91
105
  }
92
106
 
93
107
  function __await(v) {
@@ -497,6 +511,23 @@ var Geocoder = (function (exports) {
497
511
  */
498
512
  function defined(val) {
499
513
  return val !== undefined && val !== null;
514
+ }
515
+ /**
516
+ * from https://github.com/CesiumGS/cesium/blob/master/Source/Core/defined.js
517
+ *
518
+ * @param val - The object.
519
+ * @returns Returns true if the object is defined and not empty string, returns false otherwise.
520
+ *
521
+ * @example
522
+ * ```javascript
523
+ * full('foo') // true
524
+ * full('') // false
525
+ * full(undefined) // false
526
+ * full(0) // true
527
+ * ```
528
+ */
529
+ function full(val) {
530
+ return typeof val === 'string' ? !!val : defined(val);
500
531
  }
501
532
 
502
533
  /**
@@ -1165,13 +1196,46 @@ var Geocoder = (function (exports) {
1165
1196
  }
1166
1197
  }
1167
1198
 
1199
+ /**
1200
+ * @example
1201
+ * ```javascript
1202
+ * const cache1 = new Cache();
1203
+ * cache1.add('foo', 'value');
1204
+ *
1205
+ * const cache2 = new Cache();
1206
+ * cache2.match('foo'); // value
1207
+ *
1208
+ * const cache3 = new Cache({ namespace: 'foo' });
1209
+ * cache3.match('foo'); // undefined
1210
+ * ```
1211
+ *
1212
+ * @example
1213
+ * ```javascript
1214
+ * let COUNTER = 0;
1215
+ * const createPromise = () => new Promise((res) => {
1216
+ * COUNTER++
1217
+ * setTimeout(() => res('Ok'), 300)
1218
+ * });
1219
+ *
1220
+ * const cache = new Cache();
1221
+ * for (let i = 0; i < 10; i++) {
1222
+ * cache.add('foo', createPromise).then((data) => {
1223
+ * console.log(data); // 'Ok'
1224
+ * });
1225
+ * }
1226
+ * console.log(COUNTER); // 1
1227
+ * ```
1228
+ */
1168
1229
  var Cache$1 = /** @class */ (function () {
1169
- function Cache() {
1230
+ function Cache(options) {
1231
+ if (options === void 0) { options = {}; }
1232
+ var _a;
1170
1233
  this.cache = [];
1171
- if (Cache.instance) {
1172
- return Cache.instance;
1234
+ var namespace = (_a = options.namespace) !== null && _a !== void 0 ? _a : 'default';
1235
+ if (Cache.instance[namespace]) {
1236
+ return Cache.instance[namespace];
1173
1237
  }
1174
- Cache.instance = this;
1238
+ Cache.instance[namespace] = this;
1175
1239
  return this;
1176
1240
  }
1177
1241
  Cache.prototype.clean = function () {
@@ -1180,9 +1244,34 @@ var Geocoder = (function (exports) {
1180
1244
  Cache.prototype.all = function () {
1181
1245
  return this.cache;
1182
1246
  };
1183
- Cache.prototype.add = function (key, valueToSet, options) {
1247
+ /**
1248
+ * Caching only a non-empty value.
1249
+ *
1250
+ * Useful for get or create strategy
1251
+ * @example
1252
+ * ```javascript
1253
+ * const cache = new Cache();
1254
+ * const getItemFunc = () => fetch(url).then((data) => {
1255
+ * return data.json(); // undefined
1256
+ * });
1257
+ * const item = await cache.addFull('foo', getItemFunc);
1258
+ * if (!item) {
1259
+ * await createItem(); // 'New item'
1260
+ * }
1261
+ *
1262
+ * // somewhere else in the code
1263
+ * const item = await cache.addFull('foo', getItemFunc).then((resp) => {
1264
+ * console.log(resp); // 'New item'
1265
+ * })
1266
+ *
1267
+ * ```
1268
+ */
1269
+ Cache.prototype.addFull = function (key, valueToSet, props) {
1270
+ return this.add(key, valueToSet, props, true);
1271
+ };
1272
+ Cache.prototype.add = function (key, valueToSet, props, onlyFull) {
1184
1273
  var _this = this;
1185
- var exist = this._find(key, options);
1274
+ var exist = this._find(key, props);
1186
1275
  if (!exist) {
1187
1276
  var value = void 0;
1188
1277
  if (valueToSet instanceof Function) {
@@ -1191,18 +1280,31 @@ var Geocoder = (function (exports) {
1191
1280
  else {
1192
1281
  value = valueToSet;
1193
1282
  }
1194
- var options_ = options && JSON.parse(JSON.stringify(objectRemoveEmpty(options)));
1283
+ var props_ = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
1195
1284
  var cacheItem = {
1196
1285
  key: key,
1197
1286
  value: value,
1198
- options: options_,
1287
+ props: props_,
1288
+ // TODO: remove backward compatibility use only props
1289
+ options: props_,
1199
1290
  };
1291
+ if (onlyFull && !full(value)) {
1292
+ return value;
1293
+ }
1200
1294
  this.cache.push(cacheItem);
1201
1295
  if (value instanceof Promise) {
1202
1296
  value.catch(function (er) {
1203
- _this.delete(key, options);
1297
+ _this.delete(key, props);
1204
1298
  throw er;
1205
1299
  });
1300
+ if (onlyFull) {
1301
+ value.then(function (x) {
1302
+ if (!full(x)) {
1303
+ _this.delete(key, props);
1304
+ }
1305
+ return x;
1306
+ });
1307
+ }
1206
1308
  return value;
1207
1309
  }
1208
1310
  return value;
@@ -1211,43 +1313,52 @@ var Geocoder = (function (exports) {
1211
1313
  return exist.value;
1212
1314
  }
1213
1315
  };
1214
- Cache.prototype.match = function (key, options) {
1215
- var cacheRecord = this._find(key, options);
1316
+ Cache.prototype.match = function (key, props) {
1317
+ var cacheRecord = this._find(key, props);
1216
1318
  if (cacheRecord) {
1217
1319
  return cacheRecord.value;
1218
1320
  }
1219
1321
  };
1220
- Cache.prototype.matchAll = function (key, options) {
1322
+ Cache.prototype.matchAll = function (key, props) {
1221
1323
  var _this = this;
1222
1324
  if (key) {
1223
1325
  return this.cache
1224
- .filter(function (x) { return _this._filter(x, key, options); })
1326
+ .filter(function (x) { return _this._filter(x, key, props); })
1225
1327
  .map(function (x) { return x.value; });
1226
1328
  }
1227
1329
  return this.cache.map(function (x) { return x.value; });
1228
1330
  };
1229
- Cache.prototype.delete = function (key, options) {
1331
+ Cache.prototype.delete = function (keyOrItem, props) {
1230
1332
  var _this = this;
1231
- var exist = this.cache.filter(function (x) { return _this._filter(x, key, options); });
1333
+ var exist = [];
1334
+ if (typeof keyOrItem === 'string') {
1335
+ exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, props); });
1336
+ }
1337
+ else {
1338
+ exist.push(keyOrItem);
1339
+ }
1232
1340
  for (var _i = 0, exist_1 = exist; _i < exist_1.length; _i++) {
1233
1341
  var e = exist_1[_i];
1234
1342
  var index = this.cache.indexOf(e);
1235
1343
  this.cache.splice(index, 1);
1236
1344
  }
1237
1345
  };
1238
- Cache.prototype._find = function (key, options) {
1346
+ Cache.prototype._find = function (key, props) {
1239
1347
  var _this = this;
1240
- return this.cache.find(function (x) { return _this._filter(x, key, options); });
1348
+ return this.cache.find(function (x) { return _this._filter(x, key, props); });
1241
1349
  };
1242
- Cache.prototype._filter = function (item, key, options) {
1350
+ Cache.prototype._filter = function (item, key, props) {
1243
1351
  if (item.key === key) {
1244
- if (options) {
1245
- return objectDeepEqual(item.options || {}, objectRemoveEmpty(options));
1352
+ if (props) {
1353
+ // TODO: remove backward compatibility
1354
+ var itemProps = item.props || item.options;
1355
+ return objectDeepEqual(itemProps || {}, objectRemoveEmpty(props));
1246
1356
  }
1247
1357
  return true;
1248
1358
  }
1249
1359
  return false;
1250
1360
  };
1361
+ Cache.instance = {};
1251
1362
  return Cache;
1252
1363
  }());
1253
1364
 
@@ -1283,12 +1394,16 @@ var Geocoder = (function (exports) {
1283
1394
  var _this = _super.call(this) || this;
1284
1395
  _this.name = 'NetworkError';
1285
1396
  Object.setPrototypeOf(_this, NetworkError.prototype);
1286
- _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
1397
+ _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
1287
1398
  return _this;
1288
1399
  }
1289
1400
  return NetworkError;
1290
1401
  }(Error));
1291
1402
 
1403
+ function isObject(val) {
1404
+ return Object.prototype.toString.call(val) === '[object Object]';
1405
+ }
1406
+
1292
1407
  var loadData;
1293
1408
  // @ts-ignore
1294
1409
  {
@@ -1324,7 +1439,7 @@ var Geocoder = (function (exports) {
1324
1439
  }
1325
1440
  };
1326
1441
  xhr.onload = function () {
1327
- if ([401, 403, 404, 500].indexOf(xhr.status) !== -1) {
1442
+ if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
1328
1443
  error(new NgwError(getResponseText()));
1329
1444
  }
1330
1445
  processingResponse();
@@ -1379,7 +1494,7 @@ var Geocoder = (function (exports) {
1379
1494
  data = new FormData();
1380
1495
  data.append('file', options.file);
1381
1496
  if (options.data) {
1382
- for (var d in data) {
1497
+ for (var d in options.data) {
1383
1498
  data.append(d, data[d]);
1384
1499
  }
1385
1500
  }
@@ -1471,30 +1586,38 @@ var Geocoder = (function (exports) {
1471
1586
  }
1472
1587
  }
1473
1588
 
1474
- function isObject(val) {
1475
- return Object.prototype.toString.call(val) === '[object Object]';
1476
- }
1477
-
1589
+ var exclude = ['description'];
1478
1590
  /**
1591
+ * @remarks
1592
+ * https://docs.nextgis.ru/docs_ngweb_dev/doc/developer/resource.html#search-resources
1593
+ *
1479
1594
  * ```
1480
- * { keyname, parent: { id }} > { keyname, parent__id }
1595
+ * { keyname, owner_user: { id }} > { keyname, owner_user__id }
1481
1596
  * ```
1597
+ *
1482
1598
  * @param resource - Any property from NGW resource item
1483
1599
  */
1484
1600
  function resourceToQuery(resource, prefix) {
1485
1601
  if (prefix === void 0) { prefix = ''; }
1486
1602
  prefix = prefix ? prefix + '__' : '';
1487
1603
  var query = {};
1488
- Object.entries(resource).forEach(function (_a) {
1489
- var key = _a[0], value = _a[1];
1490
- if (isObject(value)) {
1491
- var children = resourceToQuery(value, key);
1492
- Object.assign(query, children);
1493
- }
1494
- else if (defined(value)) {
1495
- query[prefix + key] = value;
1604
+ for (var _i = 0, _a = Object.entries(resource); _i < _a.length; _i++) {
1605
+ var _b = _a[_i], key = _b[0], value = _b[1];
1606
+ if (exclude.indexOf(key) === -1) {
1607
+ if (isObject(value)) {
1608
+ if (key === 'owner_user') {
1609
+ var children = resourceToQuery(value, key);
1610
+ Object.assign(query, children);
1611
+ }
1612
+ else if (key === 'parent' && 'id' in value) {
1613
+ query.parent_id = value.id;
1614
+ }
1615
+ }
1616
+ else if (defined(value)) {
1617
+ query[prefix + key] = value;
1618
+ }
1496
1619
  }
1497
- });
1620
+ }
1498
1621
  return query;
1499
1622
  }
1500
1623
 
@@ -1520,19 +1643,30 @@ var Geocoder = (function (exports) {
1520
1643
  ResourcesControl.prototype.getOne = function (resource, requestOptions) {
1521
1644
  var _this = this;
1522
1645
  var cache = new Cache();
1646
+ var forCache = {};
1647
+ var opt = __assign(__assign({}, requestOptions), { cache: false });
1648
+ if (typeof resource === 'string') {
1649
+ forCache.keyname = resource;
1650
+ }
1651
+ else if (typeof resource === 'number') {
1652
+ forCache.id = resource;
1653
+ }
1654
+ else if (isObject(resource)) {
1655
+ forCache.id = resource.id;
1656
+ }
1523
1657
  var makeRequest = function () {
1524
1658
  if (typeof resource === 'string') {
1525
- return _this._fetchResourceBy({ keyname: resource }, requestOptions);
1659
+ return _this._fetchResourceBy({ keyname: resource }, opt);
1526
1660
  }
1527
1661
  else if (typeof resource === 'number') {
1528
- return _this._fetchResourceById(resource, requestOptions);
1662
+ return _this._fetchResourceById(resource, opt);
1529
1663
  }
1530
1664
  else if (isObject(resource)) {
1531
- return _this._fetchResourceBy(resource, requestOptions);
1665
+ return _this._fetchResourceBy(resource, opt);
1532
1666
  }
1533
1667
  return CancelablePromise.resolve(undefined);
1534
1668
  };
1535
- return cache.add('resource', makeRequest, { resource: resource });
1669
+ return cache.addFull('resource', makeRequest, forCache);
1536
1670
  };
1537
1671
  ResourcesControl.prototype.getOneOrFail = function (resource, requestOptions) {
1538
1672
  return this.getOne(resource, requestOptions).then(function (res) {
@@ -1596,11 +1730,12 @@ var Geocoder = (function (exports) {
1596
1730
  .get('resource.search', requestOptions, __assign({ serialization: 'full' }, query))
1597
1731
  .then(function (resources) {
1598
1732
  if (resources) {
1599
- resources.forEach(function (x) {
1733
+ for (var _i = 0, resources_1 = resources; _i < resources_1.length; _i++) {
1734
+ var x = resources_1[_i];
1600
1735
  _this.cache.add('resource.item', CancelablePromise.resolve(x), {
1601
1736
  id: x.resource.id,
1602
1737
  });
1603
- });
1738
+ }
1604
1739
  }
1605
1740
  return resources;
1606
1741
  });
@@ -1652,12 +1787,52 @@ var Geocoder = (function (exports) {
1652
1787
  return this.getId(resource).then(function (id) {
1653
1788
  if (id !== undefined) {
1654
1789
  return _this.connector.delete('resource.item', null, { id: id }).then(function () {
1655
- _this.cache.delete('resource.item', { id: id });
1790
+ _this._cleanResourceItemCache(id);
1656
1791
  return undefined;
1657
1792
  });
1658
1793
  }
1659
1794
  });
1660
1795
  };
1796
+ ResourcesControl.prototype._cleanResourceItemCache = function (id) {
1797
+ return __awaiter(this, void 0, void 0, function () {
1798
+ var all, toDelete, _i, all_1, c, cid, rid, _a, toDelete_1, d;
1799
+ return __generator(this, function (_b) {
1800
+ switch (_b.label) {
1801
+ case 0:
1802
+ all = this.cache.all();
1803
+ toDelete = [];
1804
+ _i = 0, all_1 = all;
1805
+ _b.label = 1;
1806
+ case 1:
1807
+ if (!(_i < all_1.length)) return [3 /*break*/, 5];
1808
+ c = all_1[_i];
1809
+ cid = c.options && c.options.id;
1810
+ if (!(['resource.item', 'resource'].includes(c.key) && cid !== undefined)) return [3 /*break*/, 4];
1811
+ if (!(typeof cid === 'number')) return [3 /*break*/, 2];
1812
+ if (cid === id) {
1813
+ toDelete.push(c);
1814
+ }
1815
+ return [3 /*break*/, 4];
1816
+ case 2: return [4 /*yield*/, this.getId(cid)];
1817
+ case 3:
1818
+ rid = _b.sent();
1819
+ if (rid === id) {
1820
+ toDelete.push(c);
1821
+ }
1822
+ _b.label = 4;
1823
+ case 4:
1824
+ _i++;
1825
+ return [3 /*break*/, 1];
1826
+ case 5:
1827
+ for (_a = 0, toDelete_1 = toDelete; _a < toDelete_1.length; _a++) {
1828
+ d = toDelete_1[_a];
1829
+ this.cache.delete(d);
1830
+ }
1831
+ return [2 /*return*/];
1832
+ }
1833
+ });
1834
+ });
1835
+ };
1661
1836
  ResourcesControl.prototype._fetchResourceById = function (id, requestOptions) {
1662
1837
  var _this = this;
1663
1838
  var promise = function () {
@@ -1708,7 +1883,7 @@ var Geocoder = (function (exports) {
1708
1883
  .then(function (apiItems) {
1709
1884
  var apiItem = apiItems && apiItems[opt.name];
1710
1885
  if (apiItem) {
1711
- apiItem = __spreadArray([], apiItem);
1886
+ apiItem = __spreadArray([], apiItem, true);
1712
1887
  var url = apiItem.shift();
1713
1888
  if (apiItem.length) {
1714
1889
  var replaceParams = {};
@@ -1730,12 +1905,12 @@ var Geocoder = (function (exports) {
1730
1905
  if (Array.isArray(paramList)) {
1731
1906
  delete params.paramList;
1732
1907
  paramList.forEach(function (x) {
1733
- paramArray_1.push(x[0] + "=" + x[1]);
1908
+ paramArray_1.push("".concat(x[0], "=").concat(x[1]));
1734
1909
  });
1735
1910
  }
1736
1911
  for (var p in params) {
1737
1912
  if (apiItem.indexOf(p) === -1) {
1738
- paramArray_1.push(p + "=" + params[p]);
1913
+ paramArray_1.push("".concat(p, "=").concat(params[p]));
1739
1914
  }
1740
1915
  }
1741
1916
  if (paramArray_1.length) {
@@ -1869,7 +2044,7 @@ var Geocoder = (function (exports) {
1869
2044
  headers: this.getAuthorizationHeaders(credentials),
1870
2045
  // withCredentials: true
1871
2046
  };
1872
- // Do not use request('auth.current_user') to avoid circular-references
2047
+ // Do not use apiRequest('auth.current_user') to avoid circular-references
1873
2048
  return this.makeQuery('/api/component/auth/current_user', {}, options)
1874
2049
  .then(function (data) {
1875
2050
  _this.user = data;
@@ -1896,7 +2071,7 @@ var Geocoder = (function (exports) {
1896
2071
  credentials = credentials || this.options.auth;
1897
2072
  if (credentials) {
1898
2073
  var login = credentials.login, password = credentials.password;
1899
- var str = unescape(encodeURIComponent(login + ":" + password));
2074
+ var str = unescape(encodeURIComponent("".concat(login, ":").concat(password)));
1900
2075
  // @ts-ignore
1901
2076
  {
1902
2077
  return window.btoa(str);
@@ -2030,7 +2205,7 @@ var Geocoder = (function (exports) {
2030
2205
  }
2031
2206
  // remove double slash
2032
2207
  url = fixUrlStr(url);
2033
- return this._loadData(url, options);
2208
+ return this._loadData(encodeURI(url), options);
2034
2209
  }
2035
2210
  else {
2036
2211
  throw new Error('Empty `url` not allowed');
@@ -2129,7 +2304,7 @@ var Geocoder = (function (exports) {
2129
2304
  else {
2130
2305
  // @ts-ignore
2131
2306
  {
2132
- console.warn(httpError);
2307
+ console.warn('DEV WARN', httpError);
2133
2308
  }
2134
2309
  var er = _this._handleHttpError(httpError);
2135
2310
  if (er) {
@@ -2206,14 +2381,14 @@ var Geocoder = (function (exports) {
2206
2381
  case 0:
2207
2382
  limit = resource.limit || 300;
2208
2383
  request = this_1.connector.get('feature_layer.store', {
2209
- headers: { RANGE: "items=0-" + limit },
2384
+ headers: { RANGE: "items=0-".concat(limit) },
2210
2385
  }, {
2211
2386
  id: resource.id,
2212
2387
  like: like,
2213
2388
  });
2214
2389
  this_1.requests.push(request);
2215
2390
  request.catch(function (er) {
2216
- console.log(er);
2391
+ console.warn(er);
2217
2392
  });
2218
2393
  return [4 /*yield*/, __await(request)];
2219
2394
  case 1:
@@ -2327,7 +2502,7 @@ var Geocoder = (function (exports) {
2327
2502
  case 0:
2328
2503
  url = fixUrlStr(
2329
2504
  // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
2330
- this.searchUrl + "/search?format=geojson&polygon_geojson=1&q=" + val);
2505
+ "".concat(this.searchUrl, "/search?format=geojson&polygon_geojson=1&q=").concat(val));
2331
2506
  request = new CancelablePromise(function (resolve, reject, onCancel) {
2332
2507
  var xhr = new XMLHttpRequest();
2333
2508
  xhr.open('GET', url, true);