@nextgis/geocoder 1.9.3 → 1.11.3

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.3; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 1.11.3; author: NextGIS */
2
2
  import CancelablePromise from '@nextgis/cancelable-promise';
3
3
  import { objectAssign, fixUrlStr } from '@nextgis/utils';
4
4
  import NgwConnector from '@nextgis/ngw-connector';
@@ -1,2 +1,2 @@
1
- import e from"@nextgis/cancelable-promise";import{objectAssign as t,fixUrlStr as s}from"@nextgis/utils";import r from"@nextgis/ngw-connector";class o{constructor(e){e&&t(this,e)}}let c=0;class n extends o{constructor(e){super(e),this.providers=[],e&&e.providers&&(this.providers=e.providers)}abort(){this.providers.forEach((e=>{e.abort&&e.abort()}))}async*search(e){for(const t of this.providers)try{const s=t.search(e);for await(const e of s)yield{...e,provider:t,_id:c++}}catch(e){}}result(t){return t.provider?t.provider.result(t):e.resolve(void 0)}}class i extends o{}class a extends i{constructor(e){super(e),this.caseInsensitive=!0,this.searchUrl="https://maxim.nextgis.com",this.searchResources=[],this.requests=[],this.connector=e.connector||new r({baseUrl:this.searchUrl}),this.searchResources=e.searchResources}abort(){this.requests.forEach((e=>{e.cancel()})),this.requests=[]}async*search(e){for(const t of this.searchResources){const s=this.connector.get("feature_layer.store",{headers:{RANGE:`items=0-${t.limit||300}`}},{id:t.id,like:e});this.requests.push(s),s.catch((e=>{console.warn(e)}));const r=await s,o=[];r&&r.forEach((s=>{o.push({query:e,resourceId:t.id,...s})}));for(const e of this._items(o))e.result=()=>this.result(e),yield e}}result(e){const t=this.connector.get("feature_layer.feature.item_extent",null,{id:e.resourceId,fid:e.featureId}).then((t=>{const s=t.extent;return{text:e.text,extent:[s.minLon,s.minLat,s.maxLon,s.maxLat]}}));return this.requests.push(t),t}_items(e){const t=[];return e.forEach((e=>{this.searchResources.find((t=>t.id===e.resourceId))&&t.push({query:e.query,text:e.label,featureId:e.id,resourceId:e.resourceId})})),t}}class h extends i{constructor(e){super(e),this.searchUrl="https://nominatim.openstreetmap.org",this._requests=[]}abort(){this._requests.forEach((e=>e.cancel())),this._requests=[]}async*search(t){const r=s(`${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${t}`),o=new e(((e,s,o)=>{const c=new XMLHttpRequest;c.open("GET",r,!0),c.onload=()=>{try{const s=JSON.parse(c.responseText).features.map((e=>({geom:e,extent:e.bbox,text:e.properties&&e.properties.display_name,query:t})));e(s)}catch(e){s(e)}},c.onerror=e=>{s(e)},o((()=>{c.abort()})),c.send()})).catch((e=>{throw e}));this._requests.push(o);const c=await o;if(c)for(const e of c)e.result=()=>this.result(e),yield e}result(t){return e.resolve({text:t.text,extent:t.extent||[],geom:t.geom})}}function u(e){return new n(e)}export{i as BaseProvider,n as Geocoder,a as NgwProvider,h as NominatimProvider,u as create};
1
+ import e from"@nextgis/cancelable-promise";import{objectAssign as t,fixUrlStr as s}from"@nextgis/utils";import r from"@nextgis/ngw-connector";class o{constructor(e){e&&t(this,e)}}let c=0;class n extends o{constructor(e){super(e),this.providers=[],e&&e.providers&&(this.providers=e.providers)}abort(){this.providers.forEach((e=>{e.abort&&e.abort()}))}async*search(e){for(const s of this.providers)try{const t=s.search(e);for await(const e of t)yield{...e,provider:s,_id:c++}}catch(t){}}result(t){return t.provider?t.provider.result(t):e.resolve(void 0)}}class i extends o{}class a extends i{constructor(e){super(e),this.caseInsensitive=!0,this.searchUrl="https://maxim.nextgis.com",this.searchResources=[],this.requests=[],this.connector=e.connector||new r({baseUrl:this.searchUrl}),this.searchResources=e.searchResources}abort(){this.requests.forEach((e=>{e.cancel()})),this.requests=[]}async*search(e){for(const t of this.searchResources){const s=this.connector.get("feature_layer.store",{headers:{RANGE:`items=0-${t.limit||300}`}},{id:t.id,like:e});this.requests.push(s),s.catch((e=>{console.warn(e)}));const r=await s,o=[];r&&r.forEach((s=>{o.push({query:e,resourceId:t.id,...s})}));for(const e of this._items(o))e.result=()=>this.result(e),yield e}}result(e){const t=this.connector.get("feature_layer.feature.item_extent",null,{id:e.resourceId,fid:e.featureId}).then((t=>{const s=t.extent;return{text:e.text,extent:[s.minLon,s.minLat,s.maxLon,s.maxLat]}}));return this.requests.push(t),t}_items(e){const t=[];return e.forEach((e=>{this.searchResources.find((t=>t.id===e.resourceId))&&t.push({query:e.query,text:e.label,featureId:e.id,resourceId:e.resourceId})})),t}}class h extends i{constructor(e){super(e),this.searchUrl="https://nominatim.openstreetmap.org",this._requests=[]}abort(){this._requests.forEach((e=>e.cancel())),this._requests=[]}async*search(t){const r=s(`${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${t}`),o=new e(((e,s,o)=>{const c=new XMLHttpRequest;c.open("GET",r,!0),c.onload=()=>{try{const s=JSON.parse(c.responseText).features.map((e=>({geom:e,extent:e.bbox,text:e.properties&&e.properties.display_name,query:t})));e(s)}catch(r){s(r)}},c.onerror=e=>{s(e)},o((()=>{c.abort()})),c.send()})).catch((e=>{throw e}));this._requests.push(o);const c=await o;if(c)for(const e of c)e.result=()=>this.result(e),yield e}result(t){return e.resolve({text:t.text,extent:t.extent||[],geom:t.geom})}}function u(e){return new n(e)}export{i as BaseProvider,n as Geocoder,a as NgwProvider,h as NominatimProvider,u as create};
2
2
  //# sourceMappingURL=geocoder.esm-bundler.prod.js.map
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/geocoder; version: 1.9.3; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 1.11.3; author: NextGIS */
2
2
  var Geocoder = (function (exports) {
3
3
  'use strict';
4
4
 
@@ -94,10 +94,14 @@ var Geocoder = (function (exports) {
94
94
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
95
95
  }
96
96
 
97
- function __spreadArray(to, from) {
98
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
99
- to[j] = from[i];
100
- 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));
101
105
  }
102
106
 
103
107
  function __await(v) {
@@ -1192,13 +1196,46 @@ var Geocoder = (function (exports) {
1192
1196
  }
1193
1197
  }
1194
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
+ */
1195
1229
  var Cache$1 = /** @class */ (function () {
1196
- function Cache() {
1230
+ function Cache(options) {
1231
+ if (options === void 0) { options = {}; }
1232
+ var _a;
1197
1233
  this.cache = [];
1198
- if (Cache.instance) {
1199
- 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];
1200
1237
  }
1201
- Cache.instance = this;
1238
+ Cache.instance[namespace] = this;
1202
1239
  return this;
1203
1240
  }
1204
1241
  Cache.prototype.clean = function () {
@@ -1207,12 +1244,34 @@ var Geocoder = (function (exports) {
1207
1244
  Cache.prototype.all = function () {
1208
1245
  return this.cache;
1209
1246
  };
1210
- Cache.prototype.addFull = function (key, valueToSet, options) {
1211
- return this.add(key, valueToSet, options, true);
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);
1212
1271
  };
1213
- Cache.prototype.add = function (key, valueToSet, options, onlyFull) {
1272
+ Cache.prototype.add = function (key, valueToSet, props, onlyFull) {
1214
1273
  var _this = this;
1215
- var exist = this._find(key, options);
1274
+ var exist = this._find(key, props);
1216
1275
  if (!exist) {
1217
1276
  var value = void 0;
1218
1277
  if (valueToSet instanceof Function) {
@@ -1221,11 +1280,13 @@ var Geocoder = (function (exports) {
1221
1280
  else {
1222
1281
  value = valueToSet;
1223
1282
  }
1224
- var options_ = options && JSON.parse(JSON.stringify(objectRemoveEmpty(options)));
1283
+ var props_ = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
1225
1284
  var cacheItem = {
1226
1285
  key: key,
1227
1286
  value: value,
1228
- options: options_,
1287
+ props: props_,
1288
+ // TODO: remove backward compatibility use only props
1289
+ options: props_,
1229
1290
  };
1230
1291
  if (onlyFull && !full(value)) {
1231
1292
  return value;
@@ -1233,13 +1294,13 @@ var Geocoder = (function (exports) {
1233
1294
  this.cache.push(cacheItem);
1234
1295
  if (value instanceof Promise) {
1235
1296
  value.catch(function (er) {
1236
- _this.delete(key, options);
1297
+ _this.delete(key, props);
1237
1298
  throw er;
1238
1299
  });
1239
1300
  if (onlyFull) {
1240
1301
  value.then(function (x) {
1241
1302
  if (!full(x)) {
1242
- _this.delete(key, options);
1303
+ _this.delete(key, props);
1243
1304
  }
1244
1305
  return x;
1245
1306
  });
@@ -1252,26 +1313,26 @@ var Geocoder = (function (exports) {
1252
1313
  return exist.value;
1253
1314
  }
1254
1315
  };
1255
- Cache.prototype.match = function (key, options) {
1256
- var cacheRecord = this._find(key, options);
1316
+ Cache.prototype.match = function (key, props) {
1317
+ var cacheRecord = this._find(key, props);
1257
1318
  if (cacheRecord) {
1258
1319
  return cacheRecord.value;
1259
1320
  }
1260
1321
  };
1261
- Cache.prototype.matchAll = function (key, options) {
1322
+ Cache.prototype.matchAll = function (key, props) {
1262
1323
  var _this = this;
1263
1324
  if (key) {
1264
1325
  return this.cache
1265
- .filter(function (x) { return _this._filter(x, key, options); })
1326
+ .filter(function (x) { return _this._filter(x, key, props); })
1266
1327
  .map(function (x) { return x.value; });
1267
1328
  }
1268
1329
  return this.cache.map(function (x) { return x.value; });
1269
1330
  };
1270
- Cache.prototype.delete = function (keyOrItem, options) {
1331
+ Cache.prototype.delete = function (keyOrItem, props) {
1271
1332
  var _this = this;
1272
1333
  var exist = [];
1273
1334
  if (typeof keyOrItem === 'string') {
1274
- exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, options); });
1335
+ exist = this.cache.filter(function (x) { return _this._filter(x, keyOrItem, props); });
1275
1336
  }
1276
1337
  else {
1277
1338
  exist.push(keyOrItem);
@@ -1282,19 +1343,22 @@ var Geocoder = (function (exports) {
1282
1343
  this.cache.splice(index, 1);
1283
1344
  }
1284
1345
  };
1285
- Cache.prototype._find = function (key, options) {
1346
+ Cache.prototype._find = function (key, props) {
1286
1347
  var _this = this;
1287
- 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); });
1288
1349
  };
1289
- Cache.prototype._filter = function (item, key, options) {
1350
+ Cache.prototype._filter = function (item, key, props) {
1290
1351
  if (item.key === key) {
1291
- if (options) {
1292
- 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));
1293
1356
  }
1294
1357
  return true;
1295
1358
  }
1296
1359
  return false;
1297
1360
  };
1361
+ Cache.instance = {};
1298
1362
  return Cache;
1299
1363
  }());
1300
1364
 
@@ -1330,12 +1394,16 @@ var Geocoder = (function (exports) {
1330
1394
  var _this = _super.call(this) || this;
1331
1395
  _this.name = 'NetworkError';
1332
1396
  Object.setPrototypeOf(_this, NetworkError.prototype);
1333
- _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
1334
1398
  return _this;
1335
1399
  }
1336
1400
  return NetworkError;
1337
1401
  }(Error));
1338
1402
 
1403
+ function isObject(val) {
1404
+ return Object.prototype.toString.call(val) === '[object Object]';
1405
+ }
1406
+
1339
1407
  var loadData;
1340
1408
  // @ts-ignore
1341
1409
  {
@@ -1371,7 +1439,7 @@ var Geocoder = (function (exports) {
1371
1439
  }
1372
1440
  };
1373
1441
  xhr.onload = function () {
1374
- if ([401, 403, 404, 500].indexOf(xhr.status) !== -1) {
1442
+ if ([401, 403, 404, 422, 500].indexOf(xhr.status) !== -1) {
1375
1443
  error(new NgwError(getResponseText()));
1376
1444
  }
1377
1445
  processingResponse();
@@ -1426,7 +1494,7 @@ var Geocoder = (function (exports) {
1426
1494
  data = new FormData();
1427
1495
  data.append('file', options.file);
1428
1496
  if (options.data) {
1429
- for (var d in data) {
1497
+ for (var d in options.data) {
1430
1498
  data.append(d, data[d]);
1431
1499
  }
1432
1500
  }
@@ -1518,10 +1586,6 @@ var Geocoder = (function (exports) {
1518
1586
  }
1519
1587
  }
1520
1588
 
1521
- function isObject(val) {
1522
- return Object.prototype.toString.call(val) === '[object Object]';
1523
- }
1524
-
1525
1589
  var exclude = ['description'];
1526
1590
  /**
1527
1591
  * @remarks
@@ -1772,7 +1836,7 @@ var Geocoder = (function (exports) {
1772
1836
  ResourcesControl.prototype._fetchResourceById = function (id, requestOptions) {
1773
1837
  var _this = this;
1774
1838
  var promise = function () {
1775
- return _this.connector.get('resource.item', requestOptions, { id: id }).catch();
1839
+ return _this.connector.get('resource.item', requestOptions, { id: id });
1776
1840
  };
1777
1841
  return this.cache
1778
1842
  .add('resource.item', promise, {
@@ -1819,7 +1883,7 @@ var Geocoder = (function (exports) {
1819
1883
  .then(function (apiItems) {
1820
1884
  var apiItem = apiItems && apiItems[opt.name];
1821
1885
  if (apiItem) {
1822
- apiItem = __spreadArray([], apiItem);
1886
+ apiItem = __spreadArray([], apiItem, true);
1823
1887
  var url = apiItem.shift();
1824
1888
  if (apiItem.length) {
1825
1889
  var replaceParams = {};
@@ -1841,12 +1905,12 @@ var Geocoder = (function (exports) {
1841
1905
  if (Array.isArray(paramList)) {
1842
1906
  delete params.paramList;
1843
1907
  paramList.forEach(function (x) {
1844
- paramArray_1.push(x[0] + "=" + x[1]);
1908
+ paramArray_1.push("".concat(x[0], "=").concat(x[1]));
1845
1909
  });
1846
1910
  }
1847
1911
  for (var p in params) {
1848
1912
  if (apiItem.indexOf(p) === -1) {
1849
- paramArray_1.push(p + "=" + params[p]);
1913
+ paramArray_1.push("".concat(p, "=").concat(params[p]));
1850
1914
  }
1851
1915
  }
1852
1916
  if (paramArray_1.length) {
@@ -1980,7 +2044,7 @@ var Geocoder = (function (exports) {
1980
2044
  headers: this.getAuthorizationHeaders(credentials),
1981
2045
  // withCredentials: true
1982
2046
  };
1983
- // Do not use request('auth.current_user') to avoid circular-references
2047
+ // Do not use apiRequest('auth.current_user') to avoid circular-references
1984
2048
  return this.makeQuery('/api/component/auth/current_user', {}, options)
1985
2049
  .then(function (data) {
1986
2050
  _this.user = data;
@@ -2007,7 +2071,7 @@ var Geocoder = (function (exports) {
2007
2071
  credentials = credentials || this.options.auth;
2008
2072
  if (credentials) {
2009
2073
  var login = credentials.login, password = credentials.password;
2010
- var str = unescape(encodeURIComponent(login + ":" + password));
2074
+ var str = unescape(encodeURIComponent("".concat(login, ":").concat(password)));
2011
2075
  // @ts-ignore
2012
2076
  {
2013
2077
  return window.btoa(str);
@@ -2317,7 +2381,7 @@ var Geocoder = (function (exports) {
2317
2381
  case 0:
2318
2382
  limit = resource.limit || 300;
2319
2383
  request = this_1.connector.get('feature_layer.store', {
2320
- headers: { RANGE: "items=0-" + limit },
2384
+ headers: { RANGE: "items=0-".concat(limit) },
2321
2385
  }, {
2322
2386
  id: resource.id,
2323
2387
  like: like,
@@ -2438,7 +2502,7 @@ var Geocoder = (function (exports) {
2438
2502
  case 0:
2439
2503
  url = fixUrlStr(
2440
2504
  // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
2441
- this.searchUrl + "/search?format=geojson&polygon_geojson=1&q=" + val);
2505
+ "".concat(this.searchUrl, "/search?format=geojson&polygon_geojson=1&q=").concat(val));
2442
2506
  request = new CancelablePromise(function (resolve, reject, onCancel) {
2443
2507
  var xhr = new XMLHttpRequest();
2444
2508
  xhr.open('GET', url, true);