@nextgis/geocoder 1.19.2 → 2.0.0-alpha.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.
@@ -1,360 +1,553 @@
1
- /** Bundle of @nextgis/geocoder; version: 1.19.2; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 2.0.0-alpha.0; 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
5
  import { fetchNgwLayerItems } from '@nextgis/ngw-kit';
6
6
 
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
7
  class BaseGeocoder {
14
- constructor(options) {
15
- if (options) {
16
- objectAssign(this, options);
17
- }
8
+ constructor(options) {
9
+ if (options) {
10
+ objectAssign(this, options);
18
11
  }
12
+ }
19
13
  }
20
14
 
15
+ var __defProp$3 = Object.defineProperty;
16
+ var __defProps = Object.defineProperties;
17
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
18
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
19
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
20
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
21
+ var __knownSymbol$2 = (name, symbol) => {
22
+ return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
23
+ };
24
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
25
+ var __spreadValues$1 = (a, b) => {
26
+ for (var prop in b || (b = {}))
27
+ if (__hasOwnProp$1.call(b, prop))
28
+ __defNormalProp$3(a, prop, b[prop]);
29
+ if (__getOwnPropSymbols$1)
30
+ for (var prop of __getOwnPropSymbols$1(b)) {
31
+ if (__propIsEnum$1.call(b, prop))
32
+ __defNormalProp$3(a, prop, b[prop]);
33
+ }
34
+ return a;
35
+ };
36
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
37
+ var __publicField$3 = (obj, key, value) => {
38
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
39
+ return value;
40
+ };
41
+ var __async = (__this, __arguments, generator) => {
42
+ return new Promise((resolve, reject) => {
43
+ var fulfilled = (value) => {
44
+ try {
45
+ step(generator.next(value));
46
+ } catch (e) {
47
+ reject(e);
48
+ }
49
+ };
50
+ var rejected = (value) => {
51
+ try {
52
+ step(generator.throw(value));
53
+ } catch (e) {
54
+ reject(e);
55
+ }
56
+ };
57
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
58
+ step((generator = generator.apply(__this, __arguments)).next());
59
+ });
60
+ };
61
+ var __await$2 = function(promise, isYieldStar) {
62
+ this[0] = promise;
63
+ this[1] = isYieldStar;
64
+ };
65
+ var __asyncGenerator$2 = (__this, __arguments, generator) => {
66
+ var resume = (k, v, yes, no) => {
67
+ try {
68
+ var x = generator[k](v), isAwait = (v = x.value) instanceof __await$2, done = x.done;
69
+ Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
70
+ } catch (e) {
71
+ no(e);
72
+ }
73
+ };
74
+ var method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no));
75
+ var it = {};
76
+ return generator = generator.apply(__this, __arguments), it[__knownSymbol$2("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
77
+ };
78
+ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol$2("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol$2("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
21
79
  let ID = 0;
22
80
  class Geocoder extends BaseGeocoder {
23
- constructor(options) {
24
- super(options);
25
- this.providers = [];
26
- if (options && options.providers) {
27
- this.providers = options.providers;
28
- }
81
+ constructor(options) {
82
+ super(options);
83
+ __publicField$3(this, "providers", []);
84
+ if (options && options.providers) {
85
+ this.providers = options.providers;
29
86
  }
30
- abort() {
31
- this.providers.forEach((x) => {
32
- if (x.abort) {
33
- x.abort();
87
+ }
88
+ abort() {
89
+ this.providers.forEach((x) => {
90
+ if (x.abort) {
91
+ x.abort();
92
+ }
93
+ });
94
+ }
95
+ search(query) {
96
+ return __asyncGenerator$2(this, null, function* () {
97
+ this.abort();
98
+ let allProvidersFailed = true;
99
+ const errors = [];
100
+ for (const provider of this.providers) {
101
+ try {
102
+ const results = provider.search(query);
103
+ try {
104
+ for (var iter = __forAwait(results), more, temp, error; more = !(temp = yield new __await$2(iter.next())).done; more = false) {
105
+ const r = temp.value;
106
+ allProvidersFailed = false;
107
+ yield __spreadProps(__spreadValues$1({}, r), { provider, _id: ID++ });
34
108
  }
35
- });
36
- }
37
- async *search(query) {
38
- this.abort();
39
- let allProvidersFailed = true;
40
- const errors = [];
41
- for (const provider of this.providers) {
109
+ } catch (temp) {
110
+ error = [temp];
111
+ } finally {
42
112
  try {
43
- const results = provider.search(query);
44
- for await (const r of results) {
45
- allProvidersFailed = false;
46
- yield { ...r, provider, _id: ID++ };
47
- }
48
- }
49
- catch (err) {
50
- const er = err;
51
- if (er.name !== 'CancelError') {
52
- errors.push(er);
53
- }
113
+ more && (temp = iter.return) && (yield new __await$2(temp.call(iter)));
114
+ } finally {
115
+ if (error)
116
+ throw error[0];
54
117
  }
118
+ }
119
+ } catch (err) {
120
+ const er = err;
121
+ if (er.name !== "CancelError") {
122
+ errors.push(er);
123
+ }
55
124
  }
56
- if (allProvidersFailed && errors.length) {
57
- throw new Error(`All providers failed: ${errors.map((e) => e.message).join('; ')}`);
58
- }
125
+ }
126
+ if (allProvidersFailed && errors.length) {
127
+ throw new Error(
128
+ `All providers failed: ${errors.map((e) => e.message).join("; ")}`
129
+ );
130
+ }
131
+ });
132
+ }
133
+ result(item) {
134
+ if (item.provider) {
135
+ return item.provider.result(item);
59
136
  }
60
- result(item) {
61
- if (item.provider) {
62
- return item.provider.result(item);
137
+ return CancelablePromise.resolve(void 0);
138
+ }
139
+ reverse(coordinates) {
140
+ return __asyncGenerator$2(this, null, function* () {
141
+ for (const provider of this.providers) {
142
+ if (!provider.reverse) {
143
+ continue;
63
144
  }
64
- return CancelablePromise.resolve(undefined);
65
- }
66
- async *reverse(coordinates) {
67
- for (const provider of this.providers) {
68
- if (!provider.reverse) {
69
- continue;
145
+ try {
146
+ const results = provider.reverse(coordinates);
147
+ try {
148
+ for (var iter = __forAwait(results), more, temp, error; more = !(temp = yield new __await$2(iter.next())).done; more = false) {
149
+ const r = temp.value;
150
+ yield __spreadProps(__spreadValues$1({}, r), { provider, _id: ID++ });
70
151
  }
152
+ } catch (temp) {
153
+ error = [temp];
154
+ } finally {
71
155
  try {
72
- const results = provider.reverse(coordinates);
73
- for await (const r of results) {
74
- yield { ...r, provider, _id: ID++ };
75
- }
76
- }
77
- catch (er) {
78
- // Handle or log errors as needed
156
+ more && (temp = iter.return) && (yield new __await$2(temp.call(iter)));
157
+ } finally {
158
+ if (error)
159
+ throw error[0];
79
160
  }
161
+ }
162
+ } catch (er) {
80
163
  }
81
- }
82
- /**
83
- * Returns the first reverse geocoding result for given coordinates from any provider.
84
- *
85
- * @param coordinates - The longitude and latitude values.
86
- * @returns A promise that resolves to the first reverse geocoding result, or undefined if no results are found.
87
- */
88
- async reverseGeocodeFirstResult(coordinates) {
89
- for (const provider of this.providers) {
90
- if (typeof provider.reverse === 'function') {
91
- try {
92
- for await (const result of provider.reverse(coordinates)) {
93
- return result;
94
- }
95
- }
96
- catch (error) {
97
- console.error('Error in reverse geocoding:', error);
98
- }
164
+ }
165
+ });
166
+ }
167
+ /**
168
+ * Returns the first reverse geocoding result for given coordinates from any provider.
169
+ *
170
+ * @param coordinates - The longitude and latitude values.
171
+ * @returns A promise that resolves to the first reverse geocoding result, or undefined if no results are found.
172
+ */
173
+ reverseGeocodeFirstResult(coordinates) {
174
+ return __async(this, null, function* () {
175
+ for (const provider of this.providers) {
176
+ if (typeof provider.reverse === "function") {
177
+ try {
178
+ try {
179
+ for (var iter = __forAwait(provider.reverse(coordinates)), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
180
+ const result = temp.value;
181
+ return result;
182
+ }
183
+ } catch (temp) {
184
+ error = [temp];
185
+ } finally {
186
+ try {
187
+ more && (temp = iter.return) && (yield temp.call(iter));
188
+ } finally {
189
+ if (error)
190
+ throw error[0];
191
+ }
99
192
  }
193
+ } catch (error2) {
194
+ console.error("Error in reverse geocoding:", error2);
195
+ }
100
196
  }
101
- return undefined;
102
- }
197
+ }
198
+ return void 0;
199
+ });
200
+ }
103
201
  }
104
202
 
105
- /**
106
- * Abstract base class representing a search provider.
107
- * It builds upon the BaseGeocoder functionalities with provider-specific properties.
108
- * @template O - Options type which extends BaseProviderOptions.
109
- */
203
+ var __defProp$2 = Object.defineProperty;
204
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
205
+ var __publicField$2 = (obj, key, value) => {
206
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
207
+ return value;
208
+ };
110
209
  class BaseProvider extends BaseGeocoder {
210
+ constructor() {
211
+ super(...arguments);
212
+ /**
213
+ * URL endpoint for the search service.
214
+ * This is where queries will be sent to obtain search results.
215
+ */
216
+ __publicField$2(this, "searchUrl");
217
+ /**
218
+ * Label to represent the provider.
219
+ * Useful for user interfaces to display the source of search results.
220
+ */
221
+ __publicField$2(this, "label");
222
+ }
111
223
  }
112
224
 
113
- class NgwProvider extends BaseProvider {
114
- constructor(options) {
115
- var _a;
116
- super(options);
117
- this.searchResources = [];
118
- this._abortControllers = [];
119
- if (!options.searchUrl && !((_a = options.connectorOptions) === null || _a === void 0 ? void 0 : _a.baseUrl)) {
120
- throw new Error('The `searchUrl` of `connectorOptions.baseUrl` are required');
121
- }
122
- this.connector =
123
- options.connector ||
124
- new NgwConnector({
125
- baseUrl: this.searchUrl,
126
- ...options.connectorOptions,
127
- });
128
- this.searchResources = options.searchResources;
225
+ var __defProp$1 = Object.defineProperty;
226
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
227
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
228
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
229
+ var __knownSymbol$1 = (name, symbol) => {
230
+ return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
231
+ };
232
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
233
+ var __spreadValues = (a, b) => {
234
+ for (var prop in b || (b = {}))
235
+ if (__hasOwnProp.call(b, prop))
236
+ __defNormalProp$1(a, prop, b[prop]);
237
+ if (__getOwnPropSymbols)
238
+ for (var prop of __getOwnPropSymbols(b)) {
239
+ if (__propIsEnum.call(b, prop))
240
+ __defNormalProp$1(a, prop, b[prop]);
129
241
  }
130
- abort() {
131
- this._abortControllers.forEach((a) => {
132
- a.abort();
133
- });
134
- this._abortControllers.length = 0;
242
+ return a;
243
+ };
244
+ var __publicField$1 = (obj, key, value) => {
245
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
246
+ return value;
247
+ };
248
+ var __await$1 = function(promise, isYieldStar) {
249
+ this[0] = promise;
250
+ this[1] = isYieldStar;
251
+ };
252
+ var __asyncGenerator$1 = (__this, __arguments, generator) => {
253
+ var resume = (k, v, yes, no) => {
254
+ try {
255
+ var x = generator[k](v), isAwait = (v = x.value) instanceof __await$1, done = x.done;
256
+ Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
257
+ } catch (e) {
258
+ no(e);
135
259
  }
136
- async *search(query) {
137
- var _a, _b;
138
- const signal = this._makeSignal();
139
- for (const config of this.searchResources) {
140
- if (signal.aborted) {
141
- break;
142
- }
143
- const { searchMethod } = config;
144
- const limit = (_a = config.limit) !== null && _a !== void 0 ? _a : NgwProvider.limit;
145
- //
146
- const resourceId = (_b = config.id) !== null && _b !== void 0 ? _b : config.resourceId;
147
- try {
148
- const resource = await this.connector.getResourceOrFail(resourceId, {
149
- cache: true,
150
- });
151
- const options = {
152
- connector: this.connector,
153
- resourceId,
154
- signal,
155
- limit,
156
- };
157
- if (searchMethod === 'like') {
158
- options.like = query;
159
- }
160
- else {
161
- options.ilike = query;
162
- }
163
- const request = fetchNgwLayerItems(options);
164
- const entries = await request;
165
- for (const item of this._items({
166
- entries,
167
- query,
168
- config,
169
- resourceItem: resource,
170
- })) {
171
- item.result = () => this.result(item);
172
- yield item;
173
- }
174
- }
175
- catch (er) {
176
- if (er &&
177
- 'name' in er &&
178
- er.name !== 'CancelError') {
179
- console.warn(er);
180
- }
181
- }
182
- }
260
+ };
261
+ var method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no));
262
+ var it = {};
263
+ return generator = generator.apply(__this, __arguments), it[__knownSymbol$1("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
264
+ };
265
+ const _NgwProvider = class _NgwProvider extends BaseProvider {
266
+ constructor(options) {
267
+ var _a;
268
+ super(options);
269
+ __publicField$1(this, "searchResources", []);
270
+ __publicField$1(this, "connector");
271
+ __publicField$1(this, "_abortControllers", []);
272
+ if (!options.searchUrl && !((_a = options.connectorOptions) == null ? void 0 : _a.baseUrl)) {
273
+ throw new Error(
274
+ "The `searchUrl` of `connectorOptions.baseUrl` are required"
275
+ );
183
276
  }
184
- result(item) {
185
- const signal = this._makeSignal();
186
- const req = this.connector
187
- .get('feature_layer.feature.item_extent', { signal }, {
188
- id: item.resourceId,
189
- fid: item.featureId,
190
- })
191
- .then((x) => {
192
- const e = x.extent;
193
- return {
194
- text: item.text,
195
- extent: [e.minLon, e.minLat, e.maxLon, e.maxLat],
196
- };
197
- });
198
- return req;
199
- }
200
- async *reverse(coordinates) {
201
- throw new Error('Not implemented');
202
- }
203
- _makeSignal() {
204
- const abortController = new AbortController();
205
- this._abortControllers.push(abortController);
206
- return abortController.signal;
207
- }
208
- _items({ entries, query, config, resourceItem, }) {
209
- var _a;
210
- const items = [];
211
- let labelField = undefined;
212
- if (resourceItem.feature_layer) {
213
- labelField = (_a = resourceItem.feature_layer.fields.find((f) => f.label_field)) === null || _a === void 0 ? void 0 : _a.keyname;
277
+ this.connector = options.connector || new NgwConnector(__spreadValues({
278
+ baseUrl: this.searchUrl
279
+ }, options.connectorOptions));
280
+ this.searchResources = options.searchResources;
281
+ }
282
+ abort() {
283
+ this._abortControllers.forEach((a) => {
284
+ a.abort();
285
+ });
286
+ this._abortControllers.length = 0;
287
+ }
288
+ search(query) {
289
+ return __asyncGenerator$1(this, null, function* () {
290
+ var _a, _b;
291
+ const signal = this._makeSignal();
292
+ for (const config of this.searchResources) {
293
+ if (signal.aborted) {
294
+ break;
214
295
  }
215
- const meta = this.searchResources.find((x) => { var _a; return (_a = x.id) !== null && _a !== void 0 ? _a : x.resourceId === resourceItem.resource.id; });
216
- if (meta) {
217
- entries.forEach((entry) => {
218
- let text = labelField ? entry.fields[labelField] : entry.id;
219
- if (config.renderSearch) {
220
- text = config.renderSearch({
221
- item: entry,
222
- resourceItem,
223
- });
224
- }
225
- else if (labelField) {
226
- text = entry.fields[labelField];
227
- }
228
- items.push({
229
- query: query,
230
- text,
231
- featureId: entry.id,
232
- resourceId: resourceItem.resource.id,
233
- item: entry,
234
- resourceItem,
235
- });
236
- });
296
+ const { searchMethod } = config;
297
+ const limit = (_a = config.limit) != null ? _a : _NgwProvider.limit;
298
+ const resourceId = (_b = config.id) != null ? _b : config.resourceId;
299
+ try {
300
+ const resource = yield new __await$1(this.connector.getResourceOrFail(resourceId, {
301
+ cache: true
302
+ }));
303
+ const options = {
304
+ connector: this.connector,
305
+ resourceId,
306
+ signal,
307
+ limit
308
+ };
309
+ if (searchMethod === "like") {
310
+ options.like = query;
311
+ } else {
312
+ options.ilike = query;
313
+ }
314
+ const request = fetchNgwLayerItems(options);
315
+ const entries = yield new __await$1(request);
316
+ for (const item of this._items({
317
+ entries,
318
+ query,
319
+ config,
320
+ resourceItem: resource
321
+ })) {
322
+ item.result = () => this.result(item);
323
+ yield item;
324
+ }
325
+ } catch (er) {
326
+ if (er && "name" in er && er.name !== "CancelError") {
327
+ console.warn(er);
328
+ }
237
329
  }
238
- return items;
330
+ }
331
+ });
332
+ }
333
+ result(item) {
334
+ const signal = this._makeSignal();
335
+ const req = this.connector.get(
336
+ "feature_layer.feature.item_extent",
337
+ { signal },
338
+ {
339
+ id: item.resourceId,
340
+ fid: item.featureId
341
+ }
342
+ ).then((x) => {
343
+ const e = x.extent;
344
+ return {
345
+ text: item.text,
346
+ extent: [e.minLon, e.minLat, e.maxLon, e.maxLat]
347
+ };
348
+ });
349
+ return req;
350
+ }
351
+ reverse(coordinates) {
352
+ return __asyncGenerator$1(this, null, function* () {
353
+ throw new Error("Not implemented");
354
+ });
355
+ }
356
+ _makeSignal() {
357
+ const abortController = new AbortController();
358
+ this._abortControllers.push(abortController);
359
+ return abortController.signal;
360
+ }
361
+ _items({
362
+ entries,
363
+ query,
364
+ config,
365
+ resourceItem
366
+ }) {
367
+ var _a;
368
+ const items = [];
369
+ let labelField = void 0;
370
+ if (resourceItem.feature_layer) {
371
+ labelField = (_a = resourceItem.feature_layer.fields.find(
372
+ (f) => f.label_field
373
+ )) == null ? void 0 : _a.keyname;
239
374
  }
240
- }
241
- NgwProvider.limit = 300;
242
-
243
- class NominatimProvider extends BaseProvider {
244
- constructor(options) {
245
- super(options);
246
- this.searchUrl = 'https://nominatim.openstreetmap.org';
247
- this._requests = [];
248
- if (options && options.searchUrl) {
249
- this.searchUrl = options.searchUrl;
375
+ const meta = this.searchResources.find(
376
+ (x) => {
377
+ var _a2;
378
+ return (_a2 = x.id) != null ? _a2 : x.resourceId === resourceItem.resource.id;
379
+ }
380
+ );
381
+ if (meta) {
382
+ entries.forEach((entry) => {
383
+ let text = labelField ? entry.fields[labelField] : entry.id;
384
+ if (config.renderSearch) {
385
+ text = config.renderSearch({
386
+ item: entry,
387
+ resourceItem
388
+ });
389
+ } else if (labelField) {
390
+ text = entry.fields[labelField];
250
391
  }
392
+ items.push({
393
+ query,
394
+ text,
395
+ featureId: entry.id,
396
+ resourceId: resourceItem.resource.id,
397
+ item: entry,
398
+ resourceItem
399
+ });
400
+ });
251
401
  }
252
- abort() {
253
- this._requests.forEach((x) => x.cancel());
254
- this._requests = [];
402
+ return items;
403
+ }
404
+ };
405
+ __publicField$1(_NgwProvider, "limit", 300);
406
+ let NgwProvider = _NgwProvider;
407
+
408
+ var __defProp = Object.defineProperty;
409
+ var __knownSymbol = (name, symbol) => {
410
+ return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
411
+ };
412
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
413
+ var __publicField = (obj, key, value) => {
414
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
415
+ return value;
416
+ };
417
+ var __await = function(promise, isYieldStar) {
418
+ this[0] = promise;
419
+ this[1] = isYieldStar;
420
+ };
421
+ var __asyncGenerator = (__this, __arguments, generator) => {
422
+ var resume = (k, v, yes, no) => {
423
+ try {
424
+ var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
425
+ Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
426
+ } catch (e) {
427
+ no(e);
428
+ }
429
+ };
430
+ var method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no));
431
+ var it = {};
432
+ return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
433
+ };
434
+ class NominatimProvider extends BaseProvider {
435
+ constructor(options) {
436
+ super(options);
437
+ __publicField(this, "searchUrl", "https://nominatim.openstreetmap.org");
438
+ __publicField(this, "_requests", []);
439
+ if (options && options.searchUrl) {
440
+ this.searchUrl = options.searchUrl;
255
441
  }
256
- async *search(val) {
257
- // RFC2616 accept - language string
258
- const url = fixUrlStr(
442
+ }
443
+ abort() {
444
+ this._requests.forEach((x) => x.cancel());
445
+ this._requests = [];
446
+ }
447
+ search(val) {
448
+ return __asyncGenerator(this, null, function* () {
449
+ const url = fixUrlStr(
259
450
  // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
260
- `${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${val}`);
261
- const request = new CancelablePromise((resolve, reject, onCancel) => {
262
- const xhr = new XMLHttpRequest();
263
- xhr.open('GET', url, true);
264
- xhr.onload = () => {
265
- try {
266
- const geojson = JSON.parse(xhr.responseText);
267
- const features = geojson.features.map((x) => {
268
- return {
269
- geom: x,
270
- extent: x.bbox,
271
- text: x.properties && x.properties.display_name,
272
- query: val,
273
- };
274
- });
275
- resolve(features);
276
- }
277
- catch (er) {
278
- reject(er);
279
- }
280
- };
281
- xhr.onerror = (er) => {
282
- reject(er);
283
- };
284
- onCancel(() => {
285
- xhr.abort();
286
- });
287
- xhr.send();
288
- }).catch((er) => {
289
- if (er.name === 'CancelError') ;
290
- throw er;
291
- });
292
- this._requests.push(request);
293
- const items = await request;
294
- if (items) {
295
- for (const i of items) {
296
- i.result = () => this.result(i);
297
- yield i;
451
+ `${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${val}`
452
+ );
453
+ const request = new CancelablePromise(
454
+ (resolve, reject, onCancel) => {
455
+ const xhr = new XMLHttpRequest();
456
+ xhr.open("GET", url, true);
457
+ xhr.onload = () => {
458
+ try {
459
+ const geojson = JSON.parse(xhr.responseText);
460
+ const features = geojson.features.map((x) => {
461
+ return {
462
+ geom: x,
463
+ extent: x.bbox,
464
+ text: x.properties && x.properties.display_name,
465
+ query: val
466
+ };
467
+ });
468
+ resolve(features);
469
+ } catch (er) {
470
+ reject(er);
298
471
  }
472
+ };
473
+ xhr.onerror = (er) => {
474
+ reject(er);
475
+ };
476
+ onCancel(() => {
477
+ xhr.abort();
478
+ });
479
+ xhr.send();
299
480
  }
300
- }
301
- result(model) {
302
- return CancelablePromise.resolve({
303
- text: model.text,
304
- extent: model.extent || [],
305
- geom: model.geom,
306
- });
307
- }
308
- async *reverse(coordinates) {
309
- const [lng, lat] = coordinates;
310
- const url = fixUrlStr(`${this.searchUrl}/reverse?format=geojson&lat=${lat}&lon=${lng}`);
311
- const request = new CancelablePromise((resolve, reject, onCancel) => {
312
- const xhr = new XMLHttpRequest();
313
- xhr.open('GET', url, true);
314
- xhr.onload = () => {
315
- try {
316
- const geojson = JSON.parse(xhr.responseText);
317
- const feature = geojson.features[0];
318
- const result = {
319
- geom: feature,
320
- extent: feature.bbox,
321
- text: feature.properties && feature.properties.display_name,
322
- query: `${lat}, ${lng}`,
323
- };
324
- resolve(result);
325
- }
326
- catch (er) {
327
- reject(er);
328
- }
329
- };
330
- xhr.onerror = (er) => {
331
- reject(er);
332
- };
333
- onCancel(() => {
334
- xhr.abort();
335
- });
336
- xhr.send();
337
- }).catch((er) => {
338
- if (er.name === 'CancelError') ;
339
- throw er;
340
- });
341
- this._requests.push(request);
342
- const item = await request;
343
- if (item) {
344
- item.result = () => this.result(item);
345
- yield item;
481
+ ).catch((er) => {
482
+ if (er.name === "CancelError") ;
483
+ throw er;
484
+ });
485
+ this._requests.push(request);
486
+ const items = yield new __await(request);
487
+ if (items) {
488
+ for (const i of items) {
489
+ i.result = () => this.result(i);
490
+ yield i;
346
491
  }
347
- }
492
+ }
493
+ });
494
+ }
495
+ result(model) {
496
+ return CancelablePromise.resolve({
497
+ text: model.text,
498
+ extent: model.extent || [],
499
+ geom: model.geom
500
+ });
501
+ }
502
+ reverse(coordinates) {
503
+ return __asyncGenerator(this, null, function* () {
504
+ const [lng, lat] = coordinates;
505
+ const url = fixUrlStr(
506
+ `${this.searchUrl}/reverse?format=geojson&lat=${lat}&lon=${lng}`
507
+ );
508
+ const request = new CancelablePromise(
509
+ (resolve, reject, onCancel) => {
510
+ const xhr = new XMLHttpRequest();
511
+ xhr.open("GET", url, true);
512
+ xhr.onload = () => {
513
+ try {
514
+ const geojson = JSON.parse(xhr.responseText);
515
+ const feature = geojson.features[0];
516
+ const result = {
517
+ geom: feature,
518
+ extent: feature.bbox,
519
+ text: feature.properties && feature.properties.display_name,
520
+ query: `${lat}, ${lng}`
521
+ };
522
+ resolve(result);
523
+ } catch (er) {
524
+ reject(er);
525
+ }
526
+ };
527
+ xhr.onerror = (er) => {
528
+ reject(er);
529
+ };
530
+ onCancel(() => {
531
+ xhr.abort();
532
+ });
533
+ xhr.send();
534
+ }
535
+ ).catch((er) => {
536
+ if (er.name === "CancelError") ;
537
+ throw er;
538
+ });
539
+ this._requests.push(request);
540
+ const item = yield new __await(request);
541
+ if (item) {
542
+ item.result = () => this.result(item);
543
+ yield item;
544
+ }
545
+ });
546
+ }
348
547
  }
349
548
 
350
- /**
351
- * Modern geocoder on async generators. May use different data providers.
352
- *
353
- * @packageDocumentation
354
- * @module geocoder
355
- */
356
549
  function create(options) {
357
- return new Geocoder(options);
550
+ return new Geocoder(options);
358
551
  }
359
552
 
360
553
  export { BaseProvider, Geocoder, NgwProvider, NominatimProvider, create };