@aws-amplify/geo 2.3.6-api-v6-models.b3abc9b.0 → 3.0.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.
Files changed (56) hide show
  1. package/lib/Geo.d.ts +3 -9
  2. package/lib/Geo.js +137 -243
  3. package/lib/index.js +4 -2
  4. package/{lib-esm/Providers → lib/providers/location-service}/AmazonLocationServiceProvider.d.ts +3 -7
  5. package/lib/providers/location-service/AmazonLocationServiceProvider.js +618 -0
  6. package/lib/tsconfig.tsbuildinfo +1 -0
  7. package/lib/types/AmazonLocationServiceProvider.d.ts +7 -7
  8. package/lib/types/AmazonLocationServiceProvider.js +0 -1
  9. package/lib/types/Geo.d.ts +28 -45
  10. package/lib/types/Geo.js +0 -1
  11. package/lib/types/Provider.d.ts +0 -1
  12. package/lib/types/Provider.js +0 -1
  13. package/lib/types/index.js +6 -1
  14. package/lib/util.d.ts +4 -0
  15. package/lib/util.js +51 -36
  16. package/lib-esm/Geo.d.ts +3 -9
  17. package/lib-esm/Geo.js +136 -243
  18. package/lib-esm/index.js +1 -1
  19. package/{lib/Providers → lib-esm/providers/location-service}/AmazonLocationServiceProvider.d.ts +3 -7
  20. package/lib-esm/providers/location-service/AmazonLocationServiceProvider.js +614 -0
  21. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  22. package/lib-esm/types/AmazonLocationServiceProvider.d.ts +7 -7
  23. package/lib-esm/types/AmazonLocationServiceProvider.js +1 -1
  24. package/lib-esm/types/Geo.d.ts +28 -45
  25. package/lib-esm/types/Geo.js +1 -1
  26. package/lib-esm/types/Provider.d.ts +0 -1
  27. package/lib-esm/types/Provider.js +1 -1
  28. package/lib-esm/types/index.js +5 -1
  29. package/lib-esm/util.d.ts +4 -0
  30. package/lib-esm/util.js +45 -34
  31. package/package.json +26 -12
  32. package/src/Geo.ts +12 -36
  33. package/src/{Providers → providers/location-service}/AmazonLocationServiceProvider.ts +65 -68
  34. package/src/types/Geo.ts +1 -17
  35. package/src/types/Provider.ts +0 -3
  36. package/src/util.ts +24 -3
  37. package/lib/.tsbuildinfo +0 -3
  38. package/lib/Geo.js.map +0 -1
  39. package/lib/Providers/AmazonLocationServiceProvider.js +0 -737
  40. package/lib/Providers/AmazonLocationServiceProvider.js.map +0 -1
  41. package/lib/index.js.map +0 -1
  42. package/lib/types/AmazonLocationServiceProvider.js.map +0 -1
  43. package/lib/types/Geo.js.map +0 -1
  44. package/lib/types/Provider.js.map +0 -1
  45. package/lib/types/index.js.map +0 -1
  46. package/lib/util.js.map +0 -1
  47. package/lib-esm/.tsbuildinfo +0 -3
  48. package/lib-esm/Geo.js.map +0 -1
  49. package/lib-esm/Providers/AmazonLocationServiceProvider.js +0 -735
  50. package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +0 -1
  51. package/lib-esm/index.js.map +0 -1
  52. package/lib-esm/types/AmazonLocationServiceProvider.js.map +0 -1
  53. package/lib-esm/types/Geo.js.map +0 -1
  54. package/lib-esm/types/Provider.js.map +0 -1
  55. package/lib-esm/types/index.js.map +0 -1
  56. package/lib-esm/util.js.map +0 -1
@@ -1,737 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var tslib_1 = require("tslib");
4
- // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
- // SPDX-License-Identifier: Apache-2.0
6
- var camelcase_keys_1 = tslib_1.__importDefault(require("camelcase-keys"));
7
- var core_1 = require("@aws-amplify/core");
8
- var client_location_1 = require("@aws-sdk/client-location");
9
- var util_1 = require("../util");
10
- var logger = new core_1.ConsoleLogger('AmazonLocationServiceProvider');
11
- var AmazonLocationServiceProvider = /** @class */ (function () {
12
- /**
13
- * Initialize Geo with AWS configurations
14
- * @param {Object} config - Configuration object for Geo
15
- */
16
- function AmazonLocationServiceProvider(config) {
17
- this._config = config ? config : {};
18
- logger.debug('Geo Options', this._config);
19
- }
20
- /**
21
- * get the category of the plugin
22
- * @returns {string} name of the category
23
- */
24
- AmazonLocationServiceProvider.prototype.getCategory = function () {
25
- return AmazonLocationServiceProvider.CATEGORY;
26
- };
27
- /**
28
- * get provider name of the plugin
29
- * @returns {string} name of the provider
30
- */
31
- AmazonLocationServiceProvider.prototype.getProviderName = function () {
32
- return AmazonLocationServiceProvider.PROVIDER_NAME;
33
- };
34
- /**
35
- * Configure Geo part with aws configuration
36
- * @param {Object} config - Configuration of the Geo
37
- * @return {Object} - Current configuration
38
- */
39
- AmazonLocationServiceProvider.prototype.configure = function (config) {
40
- logger.debug('configure Amazon Location Service Provider', config);
41
- if (!config)
42
- return this._config;
43
- this._config = Object.assign({}, this._config, config);
44
- return this._config;
45
- };
46
- /**
47
- * Get the map resources that are currently available through the provider
48
- * @returns {AmazonLocationServiceMapStyle[]}- Array of available map resources
49
- */
50
- AmazonLocationServiceProvider.prototype.getAvailableMaps = function () {
51
- this._verifyMapResources();
52
- var mapStyles = [];
53
- var availableMaps = this._config.maps.items;
54
- var region = this._config.region;
55
- for (var mapName in availableMaps) {
56
- var style = availableMaps[mapName].style;
57
- mapStyles.push({ mapName: mapName, style: style, region: region });
58
- }
59
- return mapStyles;
60
- };
61
- /**
62
- * Get the map resource set as default in amplify config
63
- * @returns {AmazonLocationServiceMapStyle} - Map resource set as the default in amplify config
64
- */
65
- AmazonLocationServiceProvider.prototype.getDefaultMap = function () {
66
- this._verifyMapResources();
67
- var mapName = this._config.maps.default;
68
- var style = this._config.maps.items[mapName].style;
69
- var region = this._config.region;
70
- return { mapName: mapName, style: style, region: region };
71
- };
72
- /**
73
- * Search by text input with optional parameters
74
- * @param {string} text - The text string that is to be searched for
75
- * @param {SearchByTextOptions} options? - Optional parameters to the search
76
- * @returns {Promise<Place[]>} - Promise resolves to a list of Places that match search parameters
77
- */
78
- AmazonLocationServiceProvider.prototype.searchByText = function (text, options) {
79
- return tslib_1.__awaiter(this, void 0, void 0, function () {
80
- var credentialsOK, locationServiceInput, client, command, response, error_1, PascalResults, results;
81
- return tslib_1.__generator(this, function (_a) {
82
- switch (_a.label) {
83
- case 0: return [4 /*yield*/, this._ensureCredentials()];
84
- case 1:
85
- credentialsOK = _a.sent();
86
- if (!credentialsOK) {
87
- throw new Error('No credentials');
88
- }
89
- this._verifySearchIndex(options === null || options === void 0 ? void 0 : options.searchIndexName);
90
- locationServiceInput = {
91
- Text: text,
92
- IndexName: this._config.search_indices.default,
93
- };
94
- /**
95
- * Map search options to Amazon Location Service input object
96
- */
97
- if (options) {
98
- locationServiceInput = tslib_1.__assign(tslib_1.__assign({}, locationServiceInput), util_1.mapSearchOptions(options, locationServiceInput));
99
- }
100
- client = new client_location_1.LocationClient({
101
- credentials: this._config.credentials,
102
- region: this._config.region,
103
- customUserAgent: core_1.getAmplifyUserAgentObject(),
104
- });
105
- command = new client_location_1.SearchPlaceIndexForTextCommand(locationServiceInput);
106
- _a.label = 2;
107
- case 2:
108
- _a.trys.push([2, 4, , 5]);
109
- return [4 /*yield*/, client.send(command)];
110
- case 3:
111
- response = _a.sent();
112
- return [3 /*break*/, 5];
113
- case 4:
114
- error_1 = _a.sent();
115
- logger.debug(error_1);
116
- throw error_1;
117
- case 5:
118
- PascalResults = response.Results.map(function (result) { return result.Place; });
119
- results = camelcase_keys_1.default(PascalResults, {
120
- deep: true,
121
- });
122
- return [2 /*return*/, results];
123
- }
124
- });
125
- });
126
- };
127
- /**
128
- * Search for suggestions based on the input text
129
- * @param {string} text - The text string that is to be searched for
130
- * @param {SearchByTextOptions} options? - Optional parameters to the search
131
- * @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
132
- */
133
- AmazonLocationServiceProvider.prototype.searchForSuggestions = function (text, options) {
134
- return tslib_1.__awaiter(this, void 0, void 0, function () {
135
- var credentialsOK, locationServiceInput, client, command, response, error_2, results;
136
- return tslib_1.__generator(this, function (_a) {
137
- switch (_a.label) {
138
- case 0: return [4 /*yield*/, this._ensureCredentials()];
139
- case 1:
140
- credentialsOK = _a.sent();
141
- if (!credentialsOK) {
142
- throw new Error('No credentials');
143
- }
144
- this._verifySearchIndex(options === null || options === void 0 ? void 0 : options.searchIndexName);
145
- locationServiceInput = {
146
- Text: text,
147
- IndexName: this._config.search_indices.default,
148
- };
149
- /**
150
- * Map search options to Amazon Location Service input object
151
- */
152
- if (options) {
153
- locationServiceInput = tslib_1.__assign(tslib_1.__assign({}, locationServiceInput), util_1.mapSearchOptions(options, locationServiceInput));
154
- }
155
- client = new client_location_1.LocationClient({
156
- credentials: this._config.credentials,
157
- region: this._config.region,
158
- customUserAgent: core_1.getAmplifyUserAgentObject(),
159
- });
160
- command = new client_location_1.SearchPlaceIndexForSuggestionsCommand(locationServiceInput);
161
- _a.label = 2;
162
- case 2:
163
- _a.trys.push([2, 4, , 5]);
164
- return [4 /*yield*/, client.send(command)];
165
- case 3:
166
- response = _a.sent();
167
- return [3 /*break*/, 5];
168
- case 4:
169
- error_2 = _a.sent();
170
- logger.debug(error_2);
171
- throw error_2;
172
- case 5:
173
- results = response.Results.map(function (result) { return ({
174
- text: result.Text,
175
- placeId: result.PlaceId,
176
- }); });
177
- return [2 /*return*/, results];
178
- }
179
- });
180
- });
181
- };
182
- AmazonLocationServiceProvider.prototype._verifyPlaceId = function (placeId) {
183
- if (placeId.length === 0) {
184
- var errorString = 'PlaceId cannot be an empty string.';
185
- logger.debug(errorString);
186
- throw new Error(errorString);
187
- }
188
- };
189
- AmazonLocationServiceProvider.prototype.searchByPlaceId = function (placeId, options) {
190
- return tslib_1.__awaiter(this, void 0, void 0, function () {
191
- var credentialsOK, client, searchByPlaceIdInput, command, response, error_3, place;
192
- return tslib_1.__generator(this, function (_a) {
193
- switch (_a.label) {
194
- case 0: return [4 /*yield*/, this._ensureCredentials()];
195
- case 1:
196
- credentialsOK = _a.sent();
197
- if (!credentialsOK) {
198
- throw new Error('No credentials');
199
- }
200
- this._verifySearchIndex(options === null || options === void 0 ? void 0 : options.searchIndexName);
201
- this._verifyPlaceId(placeId);
202
- client = new client_location_1.LocationClient({
203
- credentials: this._config.credentials,
204
- region: this._config.region,
205
- customUserAgent: core_1.getAmplifyUserAgentObject(),
206
- });
207
- searchByPlaceIdInput = {
208
- PlaceId: placeId,
209
- IndexName: (options === null || options === void 0 ? void 0 : options.searchIndexName) || this._config.search_indices.default,
210
- };
211
- command = new client_location_1.GetPlaceCommand(searchByPlaceIdInput);
212
- _a.label = 2;
213
- case 2:
214
- _a.trys.push([2, 4, , 5]);
215
- return [4 /*yield*/, client.send(command)];
216
- case 3:
217
- response = _a.sent();
218
- return [3 /*break*/, 5];
219
- case 4:
220
- error_3 = _a.sent();
221
- logger.debug(error_3);
222
- throw error_3;
223
- case 5:
224
- place = response.Place;
225
- if (place) {
226
- return [2 /*return*/, camelcase_keys_1.default(place, { deep: true })];
227
- }
228
- return [2 /*return*/];
229
- }
230
- });
231
- });
232
- };
233
- /**
234
- * Reverse geocoding search via a coordinate point on the map
235
- * @param coordinates - Coordinates array for the search input
236
- * @param options - Options parameters for the search
237
- * @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
238
- */
239
- AmazonLocationServiceProvider.prototype.searchByCoordinates = function (coordinates, options) {
240
- return tslib_1.__awaiter(this, void 0, void 0, function () {
241
- var credentialsOK, locationServiceInput, client, command, response, error_4, PascalResults, results;
242
- return tslib_1.__generator(this, function (_a) {
243
- switch (_a.label) {
244
- case 0: return [4 /*yield*/, this._ensureCredentials()];
245
- case 1:
246
- credentialsOK = _a.sent();
247
- if (!credentialsOK) {
248
- throw new Error('No credentials');
249
- }
250
- this._verifySearchIndex(options === null || options === void 0 ? void 0 : options.searchIndexName);
251
- locationServiceInput = {
252
- Position: coordinates,
253
- IndexName: this._config.search_indices.default,
254
- };
255
- if (options) {
256
- if (options.searchIndexName) {
257
- locationServiceInput.IndexName = options.searchIndexName;
258
- }
259
- locationServiceInput.MaxResults = options.maxResults;
260
- }
261
- client = new client_location_1.LocationClient({
262
- credentials: this._config.credentials,
263
- region: this._config.region,
264
- customUserAgent: core_1.getAmplifyUserAgentObject(),
265
- });
266
- command = new client_location_1.SearchPlaceIndexForPositionCommand(locationServiceInput);
267
- _a.label = 2;
268
- case 2:
269
- _a.trys.push([2, 4, , 5]);
270
- return [4 /*yield*/, client.send(command)];
271
- case 3:
272
- response = _a.sent();
273
- return [3 /*break*/, 5];
274
- case 4:
275
- error_4 = _a.sent();
276
- logger.debug(error_4);
277
- throw error_4;
278
- case 5:
279
- PascalResults = response.Results.map(function (result) { return result.Place; });
280
- results = camelcase_keys_1.default(PascalResults[0], {
281
- deep: true,
282
- });
283
- return [2 /*return*/, results];
284
- }
285
- });
286
- });
287
- };
288
- /**
289
- * Create geofences inside of a geofence collection
290
- * @param geofences - Array of geofence objects to create
291
- * @param options? - Optional parameters for creating geofences
292
- * @returns {Promise<AmazonLocationServiceSaveGeofencesResults>} - Promise that resolves to an object with:
293
- * successes: list of geofences successfully created
294
- * errors: list of geofences that failed to create
295
- */
296
- AmazonLocationServiceProvider.prototype.saveGeofences = function (geofences, options) {
297
- return tslib_1.__awaiter(this, void 0, void 0, function () {
298
- var credentialsOK, PascalGeofences, results, geofenceBatches, apiLimit;
299
- var _this = this;
300
- return tslib_1.__generator(this, function (_a) {
301
- switch (_a.label) {
302
- case 0:
303
- if (geofences.length < 1) {
304
- throw new Error('Geofence input array is empty');
305
- }
306
- return [4 /*yield*/, this._ensureCredentials()];
307
- case 1:
308
- credentialsOK = _a.sent();
309
- if (!credentialsOK) {
310
- throw new Error('No credentials');
311
- }
312
- // Verify geofence collection exists in aws-config.js
313
- try {
314
- this._verifyGeofenceCollections(options === null || options === void 0 ? void 0 : options.collectionName);
315
- }
316
- catch (error) {
317
- logger.debug(error);
318
- throw error;
319
- }
320
- util_1.validateGeofencesInput(geofences);
321
- PascalGeofences = geofences.map(function (_a) {
322
- var geofenceId = _a.geofenceId, polygon = _a.geometry.polygon;
323
- return {
324
- GeofenceId: geofenceId,
325
- Geometry: {
326
- Polygon: polygon,
327
- },
328
- };
329
- });
330
- results = {
331
- successes: [],
332
- errors: [],
333
- };
334
- geofenceBatches = [];
335
- while (PascalGeofences.length > 0) {
336
- apiLimit = 10;
337
- geofenceBatches.push(PascalGeofences.splice(0, apiLimit));
338
- }
339
- return [4 /*yield*/, Promise.all(geofenceBatches.map(function (batch) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
340
- var response, error_5;
341
- return tslib_1.__generator(this, function (_a) {
342
- switch (_a.label) {
343
- case 0:
344
- _a.trys.push([0, 2, , 3]);
345
- return [4 /*yield*/, this._AmazonLocationServiceBatchPutGeofenceCall(batch, (options === null || options === void 0 ? void 0 : options.collectionName) || this._config.geofenceCollections.default)];
346
- case 1:
347
- response = _a.sent();
348
- return [3 /*break*/, 3];
349
- case 2:
350
- error_5 = _a.sent();
351
- // If the API call fails, add the geofences to the errors array and move to next batch
352
- batch.forEach(function (geofence) {
353
- results.errors.push({
354
- geofenceId: geofence.GeofenceId,
355
- error: {
356
- code: 'APIConnectionError',
357
- message: error_5.message,
358
- },
359
- });
360
- });
361
- return [2 /*return*/];
362
- case 3:
363
- // Push all successes to results
364
- response.Successes.forEach(function (success) {
365
- var GeofenceId = success.GeofenceId, CreateTime = success.CreateTime, UpdateTime = success.UpdateTime;
366
- results.successes.push({
367
- geofenceId: GeofenceId,
368
- createTime: CreateTime,
369
- updateTime: UpdateTime,
370
- });
371
- });
372
- // Push all errors to results
373
- response.Errors.forEach(function (error) {
374
- var _a = error.Error, Code = _a.Code, Message = _a.Message, GeofenceId = error.GeofenceId;
375
- results.errors.push({
376
- error: {
377
- code: Code,
378
- message: Message,
379
- },
380
- geofenceId: GeofenceId,
381
- });
382
- });
383
- return [2 /*return*/];
384
- }
385
- });
386
- }); }))];
387
- case 2:
388
- _a.sent();
389
- return [2 /*return*/, results];
390
- }
391
- });
392
- });
393
- };
394
- /**
395
- * Get geofence from a geofence collection
396
- * @param geofenceId:string
397
- * @param options?: Optional parameters for getGeofence
398
- * @returns {Promise<AmazonLocationServiceGeofence>} - Promise that resolves to a geofence object
399
- */
400
- AmazonLocationServiceProvider.prototype.getGeofence = function (geofenceId, options) {
401
- return tslib_1.__awaiter(this, void 0, void 0, function () {
402
- var credentialsOK, client, commandInput, command, response, error_6, GeofenceId, CreateTime, UpdateTime, Status, Geometry, geofence;
403
- return tslib_1.__generator(this, function (_a) {
404
- switch (_a.label) {
405
- case 0: return [4 /*yield*/, this._ensureCredentials()];
406
- case 1:
407
- credentialsOK = _a.sent();
408
- if (!credentialsOK) {
409
- throw new Error('No credentials');
410
- }
411
- // Verify geofence collection exists in aws-config.js
412
- try {
413
- this._verifyGeofenceCollections(options === null || options === void 0 ? void 0 : options.collectionName);
414
- }
415
- catch (error) {
416
- logger.debug(error);
417
- throw error;
418
- }
419
- util_1.validateGeofenceId(geofenceId);
420
- client = new client_location_1.LocationClient({
421
- credentials: this._config.credentials,
422
- region: this._config.region,
423
- customUserAgent: core_1.getAmplifyUserAgentObject(),
424
- });
425
- commandInput = {
426
- GeofenceId: geofenceId,
427
- CollectionName: (options === null || options === void 0 ? void 0 : options.collectionName) || this._config.geofenceCollections.default,
428
- };
429
- command = new client_location_1.GetGeofenceCommand(commandInput);
430
- _a.label = 2;
431
- case 2:
432
- _a.trys.push([2, 4, , 5]);
433
- return [4 /*yield*/, client.send(command)];
434
- case 3:
435
- response = _a.sent();
436
- return [3 /*break*/, 5];
437
- case 4:
438
- error_6 = _a.sent();
439
- logger.debug(error_6);
440
- throw error_6;
441
- case 5:
442
- GeofenceId = response.GeofenceId, CreateTime = response.CreateTime, UpdateTime = response.UpdateTime, Status = response.Status, Geometry = response.Geometry;
443
- geofence = {
444
- createTime: CreateTime,
445
- geofenceId: GeofenceId,
446
- geometry: {
447
- polygon: Geometry.Polygon,
448
- },
449
- status: Status,
450
- updateTime: UpdateTime,
451
- };
452
- return [2 /*return*/, geofence];
453
- }
454
- });
455
- });
456
- };
457
- /**
458
- * List geofences from a geofence collection
459
- * @param options?: ListGeofenceOptions
460
- * @returns {Promise<ListGeofencesResults>} - Promise that resolves to an object with:
461
- * entries: list of geofences - 100 geofences are listed per page
462
- * nextToken: token for next page of geofences
463
- */
464
- AmazonLocationServiceProvider.prototype.listGeofences = function (options) {
465
- return tslib_1.__awaiter(this, void 0, void 0, function () {
466
- var credentialsOK, client, listGeofencesInput, command, response, error_7, NextToken, Entries, results;
467
- return tslib_1.__generator(this, function (_a) {
468
- switch (_a.label) {
469
- case 0: return [4 /*yield*/, this._ensureCredentials()];
470
- case 1:
471
- credentialsOK = _a.sent();
472
- if (!credentialsOK) {
473
- throw new Error('No credentials');
474
- }
475
- // Verify geofence collection exists in aws-config.js
476
- try {
477
- this._verifyGeofenceCollections(options === null || options === void 0 ? void 0 : options.collectionName);
478
- }
479
- catch (error) {
480
- logger.debug(error);
481
- throw error;
482
- }
483
- client = new client_location_1.LocationClient({
484
- credentials: this._config.credentials,
485
- region: this._config.region,
486
- customUserAgent: core_1.getAmplifyUserAgentObject(),
487
- });
488
- listGeofencesInput = {
489
- NextToken: options === null || options === void 0 ? void 0 : options.nextToken,
490
- CollectionName: (options === null || options === void 0 ? void 0 : options.collectionName) || this._config.geofenceCollections.default,
491
- };
492
- command = new client_location_1.ListGeofencesCommand(listGeofencesInput);
493
- _a.label = 2;
494
- case 2:
495
- _a.trys.push([2, 4, , 5]);
496
- return [4 /*yield*/, client.send(command)];
497
- case 3:
498
- response = _a.sent();
499
- return [3 /*break*/, 5];
500
- case 4:
501
- error_7 = _a.sent();
502
- logger.debug(error_7);
503
- throw error_7;
504
- case 5:
505
- NextToken = response.NextToken, Entries = response.Entries;
506
- results = {
507
- entries: Entries.map(function (_a) {
508
- var GeofenceId = _a.GeofenceId, CreateTime = _a.CreateTime, UpdateTime = _a.UpdateTime, Status = _a.Status, Polygon = _a.Geometry.Polygon;
509
- return {
510
- geofenceId: GeofenceId,
511
- createTime: CreateTime,
512
- updateTime: UpdateTime,
513
- status: Status,
514
- geometry: {
515
- polygon: Polygon,
516
- },
517
- };
518
- }),
519
- nextToken: NextToken,
520
- };
521
- return [2 /*return*/, results];
522
- }
523
- });
524
- });
525
- };
526
- /**
527
- * Delete geofences from a geofence collection
528
- * @param geofenceIds: string|string[]
529
- * @param options?: GeofenceOptions
530
- * @returns {Promise<DeleteGeofencesResults>} - Promise that resolves to an object with:
531
- * successes: list of geofences successfully deleted
532
- * errors: list of geofences that failed to delete
533
- */
534
- AmazonLocationServiceProvider.prototype.deleteGeofences = function (geofenceIds, options) {
535
- return tslib_1.__awaiter(this, void 0, void 0, function () {
536
- var credentialsOK, badGeofenceIds, results, geofenceIdBatches, count;
537
- var _this = this;
538
- return tslib_1.__generator(this, function (_a) {
539
- switch (_a.label) {
540
- case 0:
541
- if (geofenceIds.length < 1) {
542
- throw new Error('GeofenceId input array is empty');
543
- }
544
- return [4 /*yield*/, this._ensureCredentials()];
545
- case 1:
546
- credentialsOK = _a.sent();
547
- if (!credentialsOK) {
548
- throw new Error('No credentials');
549
- }
550
- this._verifyGeofenceCollections(options === null || options === void 0 ? void 0 : options.collectionName);
551
- badGeofenceIds = geofenceIds.filter(function (geofenceId) {
552
- try {
553
- util_1.validateGeofenceId(geofenceId);
554
- }
555
- catch (error) {
556
- return true;
557
- }
558
- });
559
- if (badGeofenceIds.length > 0) {
560
- throw new Error("Invalid geofence ids: " + badGeofenceIds.join(', '));
561
- }
562
- results = {
563
- successes: [],
564
- errors: [],
565
- };
566
- geofenceIdBatches = [];
567
- count = 0;
568
- while (count < geofenceIds.length) {
569
- geofenceIdBatches.push(geofenceIds.slice(count, (count += 10)));
570
- }
571
- return [4 /*yield*/, Promise.all(geofenceIdBatches.map(function (batch) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
572
- var response, error_8, badGeofenceIds;
573
- var _a;
574
- return tslib_1.__generator(this, function (_b) {
575
- switch (_b.label) {
576
- case 0:
577
- _b.trys.push([0, 2, , 3]);
578
- return [4 /*yield*/, this._AmazonLocationServiceBatchDeleteGeofenceCall(batch, (options === null || options === void 0 ? void 0 : options.collectionName) || this._config.geofenceCollections.default)];
579
- case 1:
580
- response = _b.sent();
581
- return [3 /*break*/, 3];
582
- case 2:
583
- error_8 = _b.sent();
584
- // If the API call fails, add the geofences to the errors array and move to next batch
585
- batch.forEach(function (geofenceId) {
586
- var errorObject = {
587
- geofenceId: geofenceId,
588
- error: {
589
- code: error_8.message,
590
- message: error_8.message,
591
- },
592
- };
593
- results.errors.push(errorObject);
594
- });
595
- return [2 /*return*/];
596
- case 3:
597
- badGeofenceIds = response.Errors.map(function (_a) {
598
- var geofenceId = _a.geofenceId;
599
- return geofenceId;
600
- });
601
- (_a = results.successes).push.apply(_a, tslib_1.__spread(batch.filter(function (Id) { return !badGeofenceIds.includes(Id); })));
602
- return [2 /*return*/];
603
- }
604
- });
605
- }); }))];
606
- case 2:
607
- _a.sent();
608
- return [2 /*return*/, results];
609
- }
610
- });
611
- });
612
- };
613
- /**
614
- * @private
615
- */
616
- AmazonLocationServiceProvider.prototype._ensureCredentials = function () {
617
- return tslib_1.__awaiter(this, void 0, void 0, function () {
618
- var credentials, cred, error_9;
619
- return tslib_1.__generator(this, function (_a) {
620
- switch (_a.label) {
621
- case 0:
622
- _a.trys.push([0, 2, , 3]);
623
- return [4 /*yield*/, core_1.Credentials.get()];
624
- case 1:
625
- credentials = _a.sent();
626
- if (!credentials)
627
- return [2 /*return*/, false];
628
- cred = core_1.Credentials.shear(credentials);
629
- logger.debug('Set credentials for storage. Credentials are:', cred);
630
- this._config.credentials = cred;
631
- return [2 /*return*/, true];
632
- case 2:
633
- error_9 = _a.sent();
634
- logger.debug('Ensure credentials error. Credentials are:', error_9);
635
- return [2 /*return*/, false];
636
- case 3: return [2 /*return*/];
637
- }
638
- });
639
- });
640
- };
641
- AmazonLocationServiceProvider.prototype._verifyMapResources = function () {
642
- if (!this._config.maps) {
643
- var errorString = "No map resources found in amplify config, run 'amplify add geo' to create one and run `amplify push` after";
644
- logger.debug(errorString);
645
- throw new Error(errorString);
646
- }
647
- if (!this._config.maps.default) {
648
- var errorString = "No default map resource found in amplify config, run 'amplify add geo' to create one and run `amplify push` after";
649
- logger.debug(errorString);
650
- throw new Error(errorString);
651
- }
652
- };
653
- AmazonLocationServiceProvider.prototype._verifySearchIndex = function (optionalSearchIndex) {
654
- if ((!this._config.search_indices || !this._config.search_indices.default) &&
655
- !optionalSearchIndex) {
656
- var errorString = 'No Search Index found in amplify config, please run `amplify add geo` to create one and run `amplify push` after.';
657
- logger.debug(errorString);
658
- throw new Error(errorString);
659
- }
660
- };
661
- AmazonLocationServiceProvider.prototype._verifyGeofenceCollections = function (optionalGeofenceCollectionName) {
662
- if ((!this._config.geofenceCollections ||
663
- !this._config.geofenceCollections.default) &&
664
- !optionalGeofenceCollectionName) {
665
- var errorString = 'No Geofence Collections found, please run `amplify add geo` to create one and run `amplify push` after.';
666
- logger.debug(errorString);
667
- throw new Error(errorString);
668
- }
669
- };
670
- AmazonLocationServiceProvider.prototype._AmazonLocationServiceBatchPutGeofenceCall = function (PascalGeofences, collectionName) {
671
- return tslib_1.__awaiter(this, void 0, void 0, function () {
672
- var geofenceInput, client, command, response, error_10;
673
- return tslib_1.__generator(this, function (_a) {
674
- switch (_a.label) {
675
- case 0:
676
- geofenceInput = {
677
- Entries: PascalGeofences,
678
- CollectionName: collectionName || this._config.geofenceCollections.default,
679
- };
680
- client = new client_location_1.LocationClient({
681
- credentials: this._config.credentials,
682
- region: this._config.region,
683
- customUserAgent: core_1.getAmplifyUserAgentObject(),
684
- });
685
- command = new client_location_1.BatchPutGeofenceCommand(geofenceInput);
686
- _a.label = 1;
687
- case 1:
688
- _a.trys.push([1, 3, , 4]);
689
- return [4 /*yield*/, client.send(command)];
690
- case 2:
691
- response = _a.sent();
692
- return [3 /*break*/, 4];
693
- case 3:
694
- error_10 = _a.sent();
695
- throw error_10;
696
- case 4: return [2 /*return*/, response];
697
- }
698
- });
699
- });
700
- };
701
- AmazonLocationServiceProvider.prototype._AmazonLocationServiceBatchDeleteGeofenceCall = function (geofenceIds, collectionName) {
702
- return tslib_1.__awaiter(this, void 0, void 0, function () {
703
- var deleteGeofencesInput, client, command, response, error_11;
704
- return tslib_1.__generator(this, function (_a) {
705
- switch (_a.label) {
706
- case 0:
707
- deleteGeofencesInput = {
708
- GeofenceIds: geofenceIds,
709
- CollectionName: collectionName || this._config.geofenceCollections.default,
710
- };
711
- client = new client_location_1.LocationClient({
712
- credentials: this._config.credentials,
713
- region: this._config.region,
714
- customUserAgent: core_1.getAmplifyUserAgentObject(),
715
- });
716
- command = new client_location_1.BatchDeleteGeofenceCommand(deleteGeofencesInput);
717
- _a.label = 1;
718
- case 1:
719
- _a.trys.push([1, 3, , 4]);
720
- return [4 /*yield*/, client.send(command)];
721
- case 2:
722
- response = _a.sent();
723
- return [3 /*break*/, 4];
724
- case 3:
725
- error_11 = _a.sent();
726
- throw error_11;
727
- case 4: return [2 /*return*/, response];
728
- }
729
- });
730
- });
731
- };
732
- AmazonLocationServiceProvider.CATEGORY = 'Geo';
733
- AmazonLocationServiceProvider.PROVIDER_NAME = 'AmazonLocationService';
734
- return AmazonLocationServiceProvider;
735
- }());
736
- exports.AmazonLocationServiceProvider = AmazonLocationServiceProvider;
737
- //# sourceMappingURL=AmazonLocationServiceProvider.js.map