@nextgis/geocoder 1.16.8 → 1.18.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,454 +1,268 @@
1
- /** Bundle of @nextgis/geocoder; version: 1.16.8; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 1.18.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';
5
+ import { fetchNgwLayerItems } from '@nextgis/ngw-kit';
5
6
 
6
- /******************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
- /* global Reflect, Promise */
21
-
22
- var extendStatics = function(d, b) {
23
- extendStatics = Object.setPrototypeOf ||
24
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
25
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
26
- return extendStatics(d, b);
27
- };
28
-
29
- function __extends(d, b) {
30
- if (typeof b !== "function" && b !== null)
31
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
32
- extendStatics(d, b);
33
- function __() { this.constructor = d; }
34
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
35
- }
36
-
37
- var __assign = function() {
38
- __assign = Object.assign || function __assign(t) {
39
- for (var s, i = 1, n = arguments.length; i < n; i++) {
40
- s = arguments[i];
41
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
42
- }
43
- return t;
44
- };
45
- return __assign.apply(this, arguments);
46
- };
47
-
48
- function __generator(thisArg, body) {
49
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
50
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
51
- function verb(n) { return function (v) { return step([n, v]); }; }
52
- function step(op) {
53
- if (f) throw new TypeError("Generator is already executing.");
54
- while (_) try {
55
- 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;
56
- if (y = 0, t) op = [op[0] & 2, t.value];
57
- switch (op[0]) {
58
- case 0: case 1: t = op; break;
59
- case 4: _.label++; return { value: op[1], done: false };
60
- case 5: _.label++; y = op[1]; op = [0]; continue;
61
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
62
- default:
63
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
64
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
65
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
66
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
67
- if (t[2]) _.ops.pop();
68
- _.trys.pop(); continue;
69
- }
70
- op = body.call(thisArg, _);
71
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
72
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
73
- }
74
- }
75
-
76
- function __values(o) {
77
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
78
- if (m) return m.call(o);
79
- if (o && typeof o.length === "number") return {
80
- next: function () {
81
- if (o && i >= o.length) o = void 0;
82
- return { value: o && o[i++], done: !o };
83
- }
84
- };
85
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
86
- }
87
-
88
- function __await(v) {
89
- return this instanceof __await ? (this.v = v, this) : new __await(v);
90
- }
91
-
92
- function __asyncGenerator(thisArg, _arguments, generator) {
93
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
94
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
95
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
96
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
97
- function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
98
- function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
99
- function fulfill(value) { resume("next", value); }
100
- function reject(value) { resume("throw", value); }
101
- function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
102
- }
103
-
104
- function __asyncValues(o) {
105
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
106
- var m = o[Symbol.asyncIterator], i;
107
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
108
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
109
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
7
+ /**
8
+ * Represents the base class for a geocoder.
9
+ * This abstract class provides the basic methods that every geocoder should implement.
10
+ *
11
+ * @template O - The type of options that can be passed to the geocoder.
12
+ */
13
+ class BaseGeocoder {
14
+ constructor(options) {
15
+ if (options) {
16
+ objectAssign(this, options);
17
+ }
18
+ }
110
19
  }
111
20
 
112
- var BaseGeocoder = /** @class */ (function () {
113
- function BaseGeocoder(options) {
114
- if (options) {
115
- objectAssign(this, options);
116
- }
117
- }
118
- return BaseGeocoder;
119
- }());
120
-
121
- var ID = 0;
122
- var Geocoder = /** @class */ (function (_super) {
123
- __extends(Geocoder, _super);
124
- function Geocoder(options) {
125
- var _this = _super.call(this, options) || this;
126
- _this.providers = [];
127
- if (options && options.providers) {
128
- _this.providers = options.providers;
129
- }
130
- return _this;
131
- }
132
- Geocoder.prototype.abort = function () {
133
- this.providers.forEach(function (x) {
134
- if (x.abort) {
135
- x.abort();
136
- }
137
- });
138
- };
139
- Geocoder.prototype.search = function (query) {
140
- return __asyncGenerator(this, arguments, function search_1() {
141
- var _i, _a, provider, results, results_1, results_1_1, r, e_1_1;
142
- var e_1, _b;
143
- return __generator(this, function (_c) {
144
- switch (_c.label) {
145
- case 0:
146
- _i = 0, _a = this.providers;
147
- _c.label = 1;
148
- case 1:
149
- if (!(_i < _a.length)) return [3 /*break*/, 19];
150
- provider = _a[_i];
151
- _c.label = 2;
152
- case 2:
153
- _c.trys.push([2, 17, , 18]);
154
- results = provider.search(query);
155
- _c.label = 3;
156
- case 3:
157
- _c.trys.push([3, 10, 11, 16]);
158
- results_1 = (e_1 = void 0, __asyncValues(results));
159
- _c.label = 4;
160
- case 4: return [4 /*yield*/, __await(results_1.next())];
161
- case 5:
162
- if (!(results_1_1 = _c.sent(), !results_1_1.done)) return [3 /*break*/, 9];
163
- r = results_1_1.value;
164
- return [4 /*yield*/, __await(__assign(__assign({}, r), { provider: provider, _id: ID++ }))];
165
- case 6: return [4 /*yield*/, _c.sent()];
166
- case 7:
167
- _c.sent();
168
- _c.label = 8;
169
- case 8: return [3 /*break*/, 4];
170
- case 9: return [3 /*break*/, 16];
171
- case 10:
172
- e_1_1 = _c.sent();
173
- e_1 = { error: e_1_1 };
174
- return [3 /*break*/, 16];
175
- case 11:
176
- _c.trys.push([11, , 14, 15]);
177
- if (!(results_1_1 && !results_1_1.done && (_b = results_1.return))) return [3 /*break*/, 13];
178
- return [4 /*yield*/, __await(_b.call(results_1))];
179
- case 12:
180
- _c.sent();
181
- _c.label = 13;
182
- case 13: return [3 /*break*/, 15];
183
- case 14:
184
- if (e_1) throw e_1.error;
185
- return [7 /*endfinally*/];
186
- case 15: return [7 /*endfinally*/];
187
- case 16: return [3 /*break*/, 18];
188
- case 17:
189
- _c.sent();
190
- return [3 /*break*/, 18];
191
- case 18:
192
- _i++;
193
- return [3 /*break*/, 1];
194
- case 19: return [2 /*return*/];
195
- }
196
- });
197
- });
198
- };
199
- Geocoder.prototype.result = function (item) {
200
- if (item.provider) {
201
- return item.provider.result(item);
202
- }
203
- return CancelablePromise.resolve(undefined);
204
- };
205
- return Geocoder;
206
- }(BaseGeocoder));
21
+ let ID = 0;
22
+ class Geocoder extends BaseGeocoder {
23
+ constructor(options) {
24
+ super(options);
25
+ this.providers = [];
26
+ if (options && options.providers) {
27
+ this.providers = options.providers;
28
+ }
29
+ }
30
+ abort() {
31
+ this.providers.forEach((x) => {
32
+ if (x.abort) {
33
+ x.abort();
34
+ }
35
+ });
36
+ }
37
+ async *search(query) {
38
+ for (const provider of this.providers) {
39
+ try {
40
+ const results = provider.search(query);
41
+ for await (const r of results) {
42
+ yield { ...r, provider, _id: ID++ };
43
+ }
44
+ }
45
+ catch (er) {
46
+ //
47
+ }
48
+ }
49
+ }
50
+ result(item) {
51
+ if (item.provider) {
52
+ return item.provider.result(item);
53
+ }
54
+ return CancelablePromise.resolve(undefined);
55
+ }
56
+ }
207
57
 
208
- var BaseProvider = /** @class */ (function (_super) {
209
- __extends(BaseProvider, _super);
210
- function BaseProvider() {
211
- return _super !== null && _super.apply(this, arguments) || this;
212
- }
213
- return BaseProvider;
214
- }(BaseGeocoder));
58
+ /**
59
+ * Abstract base class representing a search provider.
60
+ * It builds upon the BaseGeocoder functionalities with provider-specific properties.
61
+ * @template O - Options type which extends BaseProviderOptions.
62
+ */
63
+ class BaseProvider extends BaseGeocoder {
64
+ }
215
65
 
216
- var NgwProvider = /** @class */ (function (_super) {
217
- __extends(NgwProvider, _super);
218
- function NgwProvider(options) {
219
- var _this = _super.call(this, options) || this;
220
- _this.caseInsensitive = true;
221
- _this.searchUrl = 'https://maxim.nextgis.com';
222
- _this.searchResources = [];
223
- _this.requests = [];
224
- _this.connector =
225
- options.connector ||
226
- new NgwConnector({
227
- baseUrl: _this.searchUrl,
228
- });
229
- _this.searchResources = options.searchResources;
230
- return _this;
231
- }
232
- NgwProvider.prototype.abort = function () {
233
- this.requests.forEach(function (x) {
234
- x.cancel();
235
- });
236
- this.requests = [];
237
- };
238
- NgwProvider.prototype.search = function (like) {
239
- return __asyncGenerator(this, arguments, function search_1() {
240
- var _loop_1, this_1, _i, _a, resource;
241
- var _this = this;
242
- return __generator(this, function (_b) {
243
- switch (_b.label) {
244
- case 0:
245
- _loop_1 = function (resource) {
246
- var limit, request, resp, entries, _loop_2, _c, _d, item;
247
- return __generator(this, function (_e) {
248
- switch (_e.label) {
249
- case 0:
250
- limit = resource.limit || 300;
251
- request = this_1.connector.get('feature_layer.store', {
252
- headers: { RANGE: "items=0-".concat(limit) },
253
- }, {
254
- id: resource.id,
255
- like: like,
256
- });
257
- this_1.requests.push(request);
258
- request.catch(function (er) {
259
- console.warn(er);
260
- });
261
- return [4 /*yield*/, __await(request)];
262
- case 1:
263
- resp = _e.sent();
264
- entries = [];
265
- if (resp) {
266
- resp.forEach(function (x) {
267
- entries.push(__assign({ query: like, resourceId: resource.id }, x));
268
- });
269
- }
270
- _loop_2 = function (item) {
271
- return __generator(this, function (_f) {
272
- switch (_f.label) {
273
- case 0:
274
- item.result = function () { return _this.result(item); };
275
- return [4 /*yield*/, __await(item)];
276
- case 1: return [4 /*yield*/, _f.sent()];
277
- case 2:
278
- _f.sent();
279
- return [2 /*return*/];
280
- }
281
- });
282
- };
283
- _c = 0, _d = this_1._items(entries);
284
- _e.label = 2;
285
- case 2:
286
- if (!(_c < _d.length)) return [3 /*break*/, 5];
287
- item = _d[_c];
288
- return [5 /*yield**/, _loop_2(item)];
289
- case 3:
290
- _e.sent();
291
- _e.label = 4;
292
- case 4:
293
- _c++;
294
- return [3 /*break*/, 2];
295
- case 5: return [2 /*return*/];
296
- }
297
- });
298
- };
299
- this_1 = this;
300
- _i = 0, _a = this.searchResources;
301
- _b.label = 1;
302
- case 1:
303
- if (!(_i < _a.length)) return [3 /*break*/, 4];
304
- resource = _a[_i];
305
- return [5 /*yield**/, _loop_1(resource)];
306
- case 2:
307
- _b.sent();
308
- _b.label = 3;
309
- case 3:
310
- _i++;
311
- return [3 /*break*/, 1];
312
- case 4: return [2 /*return*/];
313
- }
314
- });
315
- });
316
- };
317
- NgwProvider.prototype.result = function (item) {
318
- var req = this.connector
319
- .get('feature_layer.feature.item_extent', null, {
320
- id: item.resourceId,
321
- fid: item.featureId,
322
- })
323
- .then(function (x) {
324
- var e = x.extent;
325
- return {
326
- text: item.text,
327
- extent: [e.minLon, e.minLat, e.maxLon, e.maxLat],
328
- };
329
- });
330
- this.requests.push(req);
331
- return req;
332
- };
333
- NgwProvider.prototype._items = function (entries) {
334
- var _this = this;
335
- var items = [];
336
- entries.forEach(function (entry) {
337
- var meta = _this.searchResources.find(function (x) { return x.id === entry.resourceId; });
338
- if (meta) {
339
- items.push({
340
- query: entry.query,
341
- text: entry.label,
342
- featureId: entry.id,
343
- resourceId: entry.resourceId,
344
- });
345
- }
346
- });
347
- return items;
348
- };
349
- return NgwProvider;
350
- }(BaseProvider));
66
+ class NgwProvider extends BaseProvider {
67
+ constructor(options) {
68
+ var _a;
69
+ super(options);
70
+ this.caseInsensitive = true;
71
+ this.searchResources = [];
72
+ this._abortControllers = [];
73
+ if (!options.searchUrl && !((_a = options.connectorOptions) === null || _a === void 0 ? void 0 : _a.baseUrl)) {
74
+ throw new Error('The `searchUrl` of `connectorOptions.baseUrl` are required');
75
+ }
76
+ this.connector =
77
+ options.connector ||
78
+ new NgwConnector({
79
+ baseUrl: this.searchUrl,
80
+ ...options.connectorOptions,
81
+ });
82
+ this.searchResources = options.searchResources;
83
+ }
84
+ abort() {
85
+ this._abortControllers.forEach((a) => {
86
+ a.abort();
87
+ });
88
+ this._abortControllers.length = 0;
89
+ }
90
+ async *search(query) {
91
+ var _a, _b;
92
+ const signal = this._makeSignal();
93
+ for (const config of this.searchResources) {
94
+ if (signal.aborted) {
95
+ break;
96
+ }
97
+ const { searchMethod } = config;
98
+ const limit = (_a = config.limit) !== null && _a !== void 0 ? _a : NgwProvider.limit;
99
+ //
100
+ const resourceId = (_b = config.id) !== null && _b !== void 0 ? _b : config.resourceId;
101
+ try {
102
+ const resource = await this.connector.getResourceOrFail(resourceId, {
103
+ cache: true,
104
+ });
105
+ const options = {
106
+ connector: this.connector,
107
+ resourceId,
108
+ signal,
109
+ limit,
110
+ };
111
+ if (searchMethod === 'like') {
112
+ options.like = query;
113
+ }
114
+ else {
115
+ options.ilike = query;
116
+ }
117
+ const request = fetchNgwLayerItems(options);
118
+ const entries = await request;
119
+ for (const item of this._items({
120
+ entries,
121
+ query,
122
+ config,
123
+ resourceItem: resource,
124
+ })) {
125
+ item.result = () => this.result(item);
126
+ yield item;
127
+ }
128
+ }
129
+ catch (er) {
130
+ if (er &&
131
+ 'name' in er &&
132
+ er.name !== 'CancelError') {
133
+ console.warn(er);
134
+ }
135
+ }
136
+ }
137
+ }
138
+ result(item) {
139
+ const signal = this._makeSignal();
140
+ const req = this.connector
141
+ .get('feature_layer.feature.item_extent', { signal }, {
142
+ id: item.resourceId,
143
+ fid: item.featureId,
144
+ })
145
+ .then((x) => {
146
+ const e = x.extent;
147
+ return {
148
+ text: item.text,
149
+ extent: [e.minLon, e.minLat, e.maxLon, e.maxLat],
150
+ };
151
+ });
152
+ return req;
153
+ }
154
+ _makeSignal() {
155
+ const abortController = new AbortController();
156
+ this._abortControllers.push(abortController);
157
+ return abortController.signal;
158
+ }
159
+ _items({ entries, query, config, resourceItem, }) {
160
+ var _a;
161
+ const items = [];
162
+ let labelField = undefined;
163
+ if (resourceItem.feature_layer) {
164
+ labelField = (_a = resourceItem.feature_layer.fields.find((f) => f.label_field)) === null || _a === void 0 ? void 0 : _a.keyname;
165
+ }
166
+ const meta = this.searchResources.find((x) => { var _a; return (_a = x.id) !== null && _a !== void 0 ? _a : x.resourceId === resourceItem.resource.id; });
167
+ if (meta) {
168
+ entries.forEach((entry) => {
169
+ let text = labelField ? entry.fields[labelField] : entry.id;
170
+ if (config.renderSearch) {
171
+ text = config.renderSearch({
172
+ item: entry,
173
+ resourceItem,
174
+ });
175
+ }
176
+ else if (labelField) {
177
+ text = entry.fields[labelField];
178
+ }
179
+ items.push({
180
+ query: query,
181
+ text,
182
+ featureId: entry.id,
183
+ resourceId: resourceItem.resource.id,
184
+ item: entry,
185
+ resourceItem,
186
+ });
187
+ });
188
+ }
189
+ return items;
190
+ }
191
+ }
192
+ NgwProvider.limit = 300;
351
193
 
352
- var NominatimProvider = /** @class */ (function (_super) {
353
- __extends(NominatimProvider, _super);
354
- function NominatimProvider(options) {
355
- var _this = _super.call(this, options) || this;
356
- _this.searchUrl = 'https://nominatim.openstreetmap.org';
357
- _this._requests = [];
358
- return _this;
359
- }
360
- NominatimProvider.prototype.abort = function () {
361
- this._requests.forEach(function (x) { return x.cancel(); });
362
- this._requests = [];
363
- };
364
- NominatimProvider.prototype.search = function (val) {
365
- return __asyncGenerator(this, arguments, function search_1() {
366
- var url, request, items, _loop_1, _i, items_1, i;
367
- var _this = this;
368
- return __generator(this, function (_a) {
369
- switch (_a.label) {
370
- case 0:
371
- url = fixUrlStr(
372
- // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
373
- "".concat(this.searchUrl, "/search?format=geojson&polygon_geojson=1&q=").concat(val));
374
- request = new CancelablePromise(function (resolve, reject, onCancel) {
375
- var xhr = new XMLHttpRequest();
376
- xhr.open('GET', url, true);
377
- xhr.onload = function () {
378
- try {
379
- var geojson = JSON.parse(xhr.responseText);
380
- var features = geojson.features.map(function (x) {
381
- return {
382
- geom: x,
383
- extent: x.bbox,
384
- text: x.properties && x.properties.display_name,
385
- query: val,
386
- };
387
- });
388
- resolve(features);
389
- }
390
- catch (er) {
391
- reject(er);
392
- }
393
- };
394
- xhr.onerror = function (er) {
395
- reject(er);
396
- };
397
- onCancel(function () {
398
- xhr.abort();
399
- });
400
- xhr.send();
401
- }).catch(function (er) {
402
- if (er.name === 'CancelError') ;
403
- throw er;
404
- });
405
- this._requests.push(request);
406
- return [4 /*yield*/, __await(request)];
407
- case 1:
408
- items = _a.sent();
409
- if (!items) return [3 /*break*/, 5];
410
- _loop_1 = function (i) {
411
- return __generator(this, function (_b) {
412
- switch (_b.label) {
413
- case 0:
414
- i.result = function () { return _this.result(i); };
415
- return [4 /*yield*/, __await(i)];
416
- case 1: return [4 /*yield*/, _b.sent()];
417
- case 2:
418
- _b.sent();
419
- return [2 /*return*/];
420
- }
421
- });
422
- };
423
- _i = 0, items_1 = items;
424
- _a.label = 2;
425
- case 2:
426
- if (!(_i < items_1.length)) return [3 /*break*/, 5];
427
- i = items_1[_i];
428
- return [5 /*yield**/, _loop_1(i)];
429
- case 3:
430
- _a.sent();
431
- _a.label = 4;
432
- case 4:
433
- _i++;
434
- return [3 /*break*/, 2];
435
- case 5: return [2 /*return*/];
436
- }
437
- });
438
- });
439
- };
440
- NominatimProvider.prototype.result = function (model) {
441
- return CancelablePromise.resolve({
442
- text: model.text,
443
- extent: model.extent || [],
444
- geom: model.geom,
445
- });
446
- };
447
- return NominatimProvider;
448
- }(BaseProvider));
194
+ class NominatimProvider extends BaseProvider {
195
+ constructor(options) {
196
+ super(options);
197
+ this.searchUrl = 'https://nominatim.openstreetmap.org';
198
+ this._requests = [];
199
+ }
200
+ abort() {
201
+ this._requests.forEach((x) => x.cancel());
202
+ this._requests = [];
203
+ }
204
+ async *search(val) {
205
+ // RFC2616 accept - language string
206
+ const url = fixUrlStr(
207
+ // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
208
+ `${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${val}`);
209
+ const request = new CancelablePromise((resolve, reject, onCancel) => {
210
+ const xhr = new XMLHttpRequest();
211
+ xhr.open('GET', url, true);
212
+ xhr.onload = () => {
213
+ try {
214
+ const geojson = JSON.parse(xhr.responseText);
215
+ const features = geojson.features.map((x) => {
216
+ return {
217
+ geom: x,
218
+ extent: x.bbox,
219
+ text: x.properties && x.properties.display_name,
220
+ query: val,
221
+ };
222
+ });
223
+ resolve(features);
224
+ }
225
+ catch (er) {
226
+ reject(er);
227
+ }
228
+ };
229
+ xhr.onerror = (er) => {
230
+ reject(er);
231
+ };
232
+ onCancel(() => {
233
+ xhr.abort();
234
+ });
235
+ xhr.send();
236
+ }).catch((er) => {
237
+ if (er.name === 'CancelError') ;
238
+ throw er;
239
+ });
240
+ this._requests.push(request);
241
+ const items = await request;
242
+ if (items) {
243
+ for (const i of items) {
244
+ i.result = () => this.result(i);
245
+ yield i;
246
+ }
247
+ }
248
+ }
249
+ result(model) {
250
+ return CancelablePromise.resolve({
251
+ text: model.text,
252
+ extent: model.extent || [],
253
+ geom: model.geom,
254
+ });
255
+ }
256
+ }
449
257
 
450
- function create(options) {
451
- return new Geocoder(options);
258
+ /**
259
+ * Modern geocoder on async generators. May use different data providers.
260
+ *
261
+ * @packageDocumentation
262
+ * @module geocoder
263
+ */
264
+ function create(options) {
265
+ return new Geocoder(options);
452
266
  }
453
267
 
454
268
  export { BaseProvider, Geocoder, NgwProvider, NominatimProvider, create };