@aws-amplify/geo 1.2.4 → 1.2.5-geo.11
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.
- package/dist/aws-amplify-geo.js +2020 -137
- package/dist/aws-amplify-geo.js.map +1 -1
- package/dist/aws-amplify-geo.min.js +6 -6
- package/dist/aws-amplify-geo.min.js.map +1 -1
- package/lib/Geo.d.ts +36 -10
- package/lib/Geo.js +140 -20
- package/lib/Geo.js.map +1 -1
- package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -8
- package/lib/Providers/AmazonLocationServiceProvider.js +405 -53
- package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib/types/Geo.d.ts +48 -2
- package/lib/types/Provider.d.ts +5 -2
- package/lib/types/Provider.js +0 -12
- package/lib/types/Provider.js.map +1 -1
- package/lib/util.d.ts +6 -0
- package/lib/util.js +144 -0
- package/lib/util.js.map +1 -0
- package/lib-esm/Geo.d.ts +36 -10
- package/lib-esm/Geo.js +140 -20
- package/lib-esm/Geo.js.map +1 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -8
- package/lib-esm/Providers/AmazonLocationServiceProvider.js +406 -54
- package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib-esm/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib-esm/types/Geo.d.ts +48 -2
- package/lib-esm/types/Provider.d.ts +5 -2
- package/lib-esm/types/Provider.js +0 -12
- package/lib-esm/types/Provider.js.map +1 -1
- package/lib-esm/util.d.ts +6 -0
- package/lib-esm/util.js +134 -0
- package/lib-esm/util.js.map +1 -0
- package/package.json +6 -4
- package/src/Geo.ts +118 -20
- package/src/Providers/AmazonLocationServiceProvider.ts +422 -75
- package/src/types/AmazonLocationServiceProvider.ts +56 -1
- package/src/types/Geo.ts +71 -4
- package/src/types/Provider.ts +29 -6
- package/src/util.ts +173 -0
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* and limitations under the License.
|
|
12
12
|
*/
|
|
13
13
|
import camelcaseKeys from 'camelcase-keys';
|
|
14
|
+
|
|
14
15
|
import {
|
|
15
16
|
ConsoleLogger as Logger,
|
|
16
17
|
Credentials,
|
|
@@ -18,15 +19,28 @@ import {
|
|
|
18
19
|
} from '@aws-amplify/core';
|
|
19
20
|
import {
|
|
20
21
|
Place as PlaceResult,
|
|
22
|
+
SearchPlaceIndexForTextCommandInput,
|
|
21
23
|
LocationClient,
|
|
22
24
|
SearchPlaceIndexForTextCommand,
|
|
23
|
-
SearchPlaceIndexForTextCommandInput,
|
|
24
|
-
SearchPlaceIndexForSuggestionsCommand,
|
|
25
|
-
SearchPlaceIndexForSuggestionsCommandInput,
|
|
26
25
|
SearchPlaceIndexForPositionCommand,
|
|
27
26
|
SearchPlaceIndexForPositionCommandInput,
|
|
27
|
+
BatchPutGeofenceCommand,
|
|
28
|
+
BatchPutGeofenceCommandInput,
|
|
29
|
+
BatchPutGeofenceRequestEntry,
|
|
30
|
+
BatchPutGeofenceCommandOutput,
|
|
31
|
+
GetGeofenceCommand,
|
|
32
|
+
GetGeofenceCommandInput,
|
|
33
|
+
GetGeofenceCommandOutput,
|
|
34
|
+
ListGeofencesCommand,
|
|
35
|
+
ListGeofencesCommandInput,
|
|
36
|
+
ListGeofencesCommandOutput,
|
|
37
|
+
BatchDeleteGeofenceCommand,
|
|
38
|
+
BatchDeleteGeofenceCommandInput,
|
|
39
|
+
BatchDeleteGeofenceCommandOutput,
|
|
28
40
|
} from '@aws-sdk/client-location';
|
|
29
41
|
|
|
42
|
+
import { validateGeofenceId, validateGeofencesInput } from '../util';
|
|
43
|
+
|
|
30
44
|
import {
|
|
31
45
|
GeoConfig,
|
|
32
46
|
SearchByTextOptions,
|
|
@@ -35,7 +49,15 @@ import {
|
|
|
35
49
|
Place,
|
|
36
50
|
AmazonLocationServiceMapStyle,
|
|
37
51
|
Coordinates,
|
|
38
|
-
|
|
52
|
+
GeofenceInput,
|
|
53
|
+
AmazonLocationServiceGeofenceOptions,
|
|
54
|
+
AmazonLocationServiceListGeofenceOptions,
|
|
55
|
+
ListGeofenceResults,
|
|
56
|
+
AmazonLocationServiceGeofenceStatus,
|
|
57
|
+
SaveGeofencesResults,
|
|
58
|
+
AmazonLocationServiceGeofence,
|
|
59
|
+
GeofencePolygon,
|
|
60
|
+
AmazonLocationServiceDeleteGeofencesResults,
|
|
39
61
|
} from '../types';
|
|
40
62
|
|
|
41
63
|
const logger = new Logger('AmazonLocationServiceProvider');
|
|
@@ -199,16 +221,15 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
199
221
|
}
|
|
200
222
|
|
|
201
223
|
/**
|
|
202
|
-
*
|
|
203
|
-
* @param
|
|
204
|
-
* @param
|
|
205
|
-
* @returns {Promise<
|
|
224
|
+
* Reverse geocoding search via a coordinate point on the map
|
|
225
|
+
* @param coordinates - Coordinates array for the search input
|
|
226
|
+
* @param options - Options parameters for the search
|
|
227
|
+
* @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
|
|
206
228
|
*/
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
): Promise<SearchForSuggestionsResults> {
|
|
229
|
+
public async searchByCoordinates(
|
|
230
|
+
coordinates: Coordinates,
|
|
231
|
+
options?: SearchByCoordinatesOptions
|
|
232
|
+
): Promise<Place> {
|
|
212
233
|
const credentialsOK = await this._ensureCredentials();
|
|
213
234
|
if (!credentialsOK) {
|
|
214
235
|
throw new Error('No credentials');
|
|
@@ -216,36 +237,16 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
216
237
|
|
|
217
238
|
this._verifySearchIndex(options?.searchIndexName);
|
|
218
239
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
*/
|
|
222
|
-
const locationServiceInput: SearchPlaceIndexForSuggestionsCommandInput = {
|
|
223
|
-
Text: text,
|
|
240
|
+
const locationServiceInput: SearchPlaceIndexForPositionCommandInput = {
|
|
241
|
+
Position: coordinates,
|
|
224
242
|
IndexName: this._config.search_indices.default,
|
|
225
243
|
};
|
|
226
244
|
|
|
227
|
-
/**
|
|
228
|
-
* Map search options to Amazon Location Service input object
|
|
229
|
-
*/
|
|
230
245
|
if (options) {
|
|
231
|
-
locationServiceInput.FilterCountries = options.countries;
|
|
232
|
-
locationServiceInput.MaxResults = options.maxResults;
|
|
233
|
-
|
|
234
246
|
if (options.searchIndexName) {
|
|
235
247
|
locationServiceInput.IndexName = options.searchIndexName;
|
|
236
248
|
}
|
|
237
|
-
|
|
238
|
-
if (options['biasPosition'] && options['searchAreaConstraints']) {
|
|
239
|
-
throw new Error(
|
|
240
|
-
'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
if (options['biasPosition']) {
|
|
244
|
-
locationServiceInput.BiasPosition = options['biasPosition'];
|
|
245
|
-
}
|
|
246
|
-
if (options['searchAreaConstraints']) {
|
|
247
|
-
locationServiceInput.FilterBBox = options['searchAreaConstraints'];
|
|
248
|
-
}
|
|
249
|
+
locationServiceInput.MaxResults = options.maxResults;
|
|
249
250
|
}
|
|
250
251
|
|
|
251
252
|
const client = new LocationClient({
|
|
@@ -253,7 +254,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
253
254
|
region: this._config.region,
|
|
254
255
|
customUserAgent: getAmplifyUserAgent(),
|
|
255
256
|
});
|
|
256
|
-
const command = new
|
|
257
|
+
const command = new SearchPlaceIndexForPositionCommand(
|
|
257
258
|
locationServiceInput
|
|
258
259
|
);
|
|
259
260
|
|
|
@@ -266,53 +267,235 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
266
267
|
}
|
|
267
268
|
|
|
268
269
|
/**
|
|
269
|
-
* The response from Amazon Location Service is a "Results" array
|
|
270
|
-
*
|
|
270
|
+
* The response from Amazon Location Service is a "Results" array with a single `Place` object
|
|
271
|
+
* which are Place objects in PascalCase.
|
|
272
|
+
* Here we want to flatten that to an array of results and change them to camelCase
|
|
271
273
|
*/
|
|
272
|
-
const
|
|
274
|
+
const PascalResults = response.Results.map(result => result.Place);
|
|
275
|
+
const results: Place = camelcaseKeys(PascalResults[0], {
|
|
276
|
+
deep: true,
|
|
277
|
+
}) as any as Place;
|
|
273
278
|
|
|
274
279
|
return results;
|
|
275
280
|
}
|
|
276
281
|
|
|
277
282
|
/**
|
|
278
|
-
*
|
|
279
|
-
* @param
|
|
280
|
-
* @param options -
|
|
281
|
-
* @returns {Promise<
|
|
283
|
+
* Create geofences inside of a geofence collection
|
|
284
|
+
* @param geofences - Array of geofence objects to create
|
|
285
|
+
* @param options? - Optional parameters for creating geofences
|
|
286
|
+
* @returns {Promise<AmazonLocationServiceSaveGeofencesResults>} - Promise that resolves to an object with:
|
|
287
|
+
* successes: list of geofences successfully created
|
|
288
|
+
* errors: list of geofences that failed to create
|
|
282
289
|
*/
|
|
283
|
-
public async
|
|
284
|
-
|
|
285
|
-
options?:
|
|
286
|
-
): Promise<
|
|
290
|
+
public async saveGeofences(
|
|
291
|
+
geofences: GeofenceInput[],
|
|
292
|
+
options?: AmazonLocationServiceGeofenceOptions
|
|
293
|
+
): Promise<SaveGeofencesResults> {
|
|
294
|
+
if (geofences.length < 1) {
|
|
295
|
+
throw new Error('Geofence input array is empty');
|
|
296
|
+
}
|
|
297
|
+
|
|
287
298
|
const credentialsOK = await this._ensureCredentials();
|
|
288
299
|
if (!credentialsOK) {
|
|
289
300
|
throw new Error('No credentials');
|
|
290
301
|
}
|
|
291
302
|
|
|
292
|
-
|
|
303
|
+
// Verify geofence collection exists in aws-config.js
|
|
304
|
+
try {
|
|
305
|
+
this._verifyGeofenceCollections(options?.collectionName);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
logger.debug(error);
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
293
310
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
311
|
+
validateGeofencesInput(geofences);
|
|
312
|
+
|
|
313
|
+
// Convert geofences to PascalCase for Amazon Location Service format
|
|
314
|
+
const PascalGeofences: BatchPutGeofenceRequestEntry[] = geofences.map(
|
|
315
|
+
({ geofenceId, geometry: { polygon } }) => {
|
|
316
|
+
return {
|
|
317
|
+
GeofenceId: geofenceId,
|
|
318
|
+
Geometry: {
|
|
319
|
+
Polygon: polygon,
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
const results: SaveGeofencesResults = {
|
|
325
|
+
successes: [],
|
|
326
|
+
errors: [],
|
|
297
327
|
};
|
|
298
328
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
329
|
+
const batches = [];
|
|
330
|
+
|
|
331
|
+
while (PascalGeofences.length > 0) {
|
|
332
|
+
// Splice off 10 geofences from input clone due to Amazon Location Service API limit
|
|
333
|
+
const apiLimit = 10;
|
|
334
|
+
batches.push(PascalGeofences.splice(0, apiLimit));
|
|
304
335
|
}
|
|
305
336
|
|
|
337
|
+
await Promise.all(
|
|
338
|
+
batches.map(async batch => {
|
|
339
|
+
// for (const batch of batches) {
|
|
340
|
+
// Make API call for the 10 geofences
|
|
341
|
+
let response: BatchPutGeofenceCommandOutput;
|
|
342
|
+
try {
|
|
343
|
+
response = await this._AmazonLocationServiceBatchPutGeofenceCall(
|
|
344
|
+
batch,
|
|
345
|
+
options?.collectionName || this._config.geofenceCollections.default
|
|
346
|
+
);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
// If the API call fails, add the geofences to the errors array and move to next batch
|
|
349
|
+
batch.forEach(geofence => {
|
|
350
|
+
results.errors.push({
|
|
351
|
+
geofenceId: geofence.GeofenceId,
|
|
352
|
+
error: {
|
|
353
|
+
code: 'APIConnectionError',
|
|
354
|
+
message: error.message,
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Push all successes to results
|
|
362
|
+
response.Successes.forEach(success => {
|
|
363
|
+
const { GeofenceId, CreateTime, UpdateTime } = success;
|
|
364
|
+
results.successes.push({
|
|
365
|
+
geofenceId: GeofenceId,
|
|
366
|
+
createTime: CreateTime,
|
|
367
|
+
updateTime: UpdateTime,
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// Push all errors to results
|
|
372
|
+
response.Errors.forEach(error => {
|
|
373
|
+
const {
|
|
374
|
+
Error: { Code, Message },
|
|
375
|
+
GeofenceId,
|
|
376
|
+
} = error;
|
|
377
|
+
results.errors.push({
|
|
378
|
+
error: {
|
|
379
|
+
code: Code,
|
|
380
|
+
message: Message,
|
|
381
|
+
},
|
|
382
|
+
geofenceId: GeofenceId,
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
})
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
return results;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Get geofence from a geofence collection
|
|
393
|
+
* @param geofenceId:string
|
|
394
|
+
* @param options?: Optional parameters for getGeofence
|
|
395
|
+
* @returns {Promise<AmazonLocationServiceGeofence>} - Promise that resolves to a geofence object
|
|
396
|
+
*/
|
|
397
|
+
public async getGeofence(
|
|
398
|
+
geofenceId: string,
|
|
399
|
+
options?: AmazonLocationServiceGeofenceOptions
|
|
400
|
+
): Promise<AmazonLocationServiceGeofence> {
|
|
401
|
+
const credentialsOK = await this._ensureCredentials();
|
|
402
|
+
if (!credentialsOK) {
|
|
403
|
+
throw new Error('No credentials');
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Verify geofence collection exists in aws-config.js
|
|
407
|
+
try {
|
|
408
|
+
this._verifyGeofenceCollections(options?.collectionName);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
logger.debug(error);
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
validateGeofenceId(geofenceId);
|
|
415
|
+
|
|
416
|
+
// Create Amazon Location Service Client
|
|
306
417
|
const client = new LocationClient({
|
|
307
418
|
credentials: this._config.credentials,
|
|
308
419
|
region: this._config.region,
|
|
309
420
|
customUserAgent: getAmplifyUserAgent(),
|
|
310
421
|
});
|
|
311
|
-
|
|
312
|
-
|
|
422
|
+
|
|
423
|
+
// Create Amazon Location Service command
|
|
424
|
+
const commandInput: GetGeofenceCommandInput = {
|
|
425
|
+
GeofenceId: geofenceId,
|
|
426
|
+
CollectionName:
|
|
427
|
+
options?.collectionName || this._config.geofenceCollections.default,
|
|
428
|
+
};
|
|
429
|
+
const command = new GetGeofenceCommand(commandInput);
|
|
430
|
+
|
|
431
|
+
// Make API call
|
|
432
|
+
let response: GetGeofenceCommandOutput;
|
|
433
|
+
try {
|
|
434
|
+
response = await client.send(command);
|
|
435
|
+
} catch (error) {
|
|
436
|
+
logger.debug(error);
|
|
437
|
+
throw error;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Convert response to camelCase for return
|
|
441
|
+
const { GeofenceId, CreateTime, UpdateTime, Status, Geometry } = response;
|
|
442
|
+
const geofence: AmazonLocationServiceGeofence = {
|
|
443
|
+
createTime: CreateTime,
|
|
444
|
+
geofenceId: GeofenceId,
|
|
445
|
+
geometry: {
|
|
446
|
+
polygon: Geometry.Polygon as GeofencePolygon,
|
|
447
|
+
},
|
|
448
|
+
status: Status as AmazonLocationServiceGeofenceStatus,
|
|
449
|
+
updateTime: UpdateTime,
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
return geofence;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* List geofences from a geofence collection
|
|
457
|
+
* @param options?: ListGeofenceOptions
|
|
458
|
+
* @returns {Promise<ListGeofencesResults>} - Promise that resolves to an object with:
|
|
459
|
+
* entries: list of geofences - 100 geofences are listed per page
|
|
460
|
+
* nextToken: token for next page of geofences
|
|
461
|
+
*/
|
|
462
|
+
public async listGeofences(
|
|
463
|
+
options?: AmazonLocationServiceListGeofenceOptions
|
|
464
|
+
): Promise<ListGeofenceResults> {
|
|
465
|
+
const credentialsOK = await this._ensureCredentials();
|
|
466
|
+
if (!credentialsOK) {
|
|
467
|
+
throw new Error('No credentials');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Verify geofence collection exists in aws-config.js
|
|
471
|
+
try {
|
|
472
|
+
this._verifyGeofenceCollections(options?.collectionName);
|
|
473
|
+
} catch (error) {
|
|
474
|
+
logger.debug(error);
|
|
475
|
+
throw error;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Create Amazon Location Service Client
|
|
479
|
+
const client = new LocationClient({
|
|
480
|
+
credentials: this._config.credentials,
|
|
481
|
+
region: this._config.region,
|
|
482
|
+
customUserAgent: getAmplifyUserAgent(),
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
// Create Amazon Location Service input
|
|
486
|
+
const listGeofencesInput: ListGeofencesCommandInput = {
|
|
487
|
+
NextToken: options?.nextToken,
|
|
488
|
+
CollectionName:
|
|
489
|
+
options?.collectionName || this._config.geofenceCollections.default,
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
// Create Amazon Location Service command
|
|
493
|
+
const command: ListGeofencesCommand = new ListGeofencesCommand(
|
|
494
|
+
listGeofencesInput
|
|
313
495
|
);
|
|
314
496
|
|
|
315
|
-
|
|
497
|
+
// Make API call
|
|
498
|
+
let response: ListGeofencesCommandOutput;
|
|
316
499
|
try {
|
|
317
500
|
response = await client.send(command);
|
|
318
501
|
} catch (error) {
|
|
@@ -320,19 +503,116 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
320
503
|
throw error;
|
|
321
504
|
}
|
|
322
505
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
506
|
+
// Convert response to camelCase for return
|
|
507
|
+
const { NextToken, Entries } = response;
|
|
508
|
+
|
|
509
|
+
const results: ListGeofenceResults = {
|
|
510
|
+
entries: Entries.map(
|
|
511
|
+
({
|
|
512
|
+
GeofenceId,
|
|
513
|
+
CreateTime,
|
|
514
|
+
UpdateTime,
|
|
515
|
+
Status,
|
|
516
|
+
Geometry: { Polygon },
|
|
517
|
+
}) => {
|
|
518
|
+
return {
|
|
519
|
+
geofenceId: GeofenceId,
|
|
520
|
+
createTime: CreateTime,
|
|
521
|
+
updateTime: UpdateTime,
|
|
522
|
+
status: Status,
|
|
523
|
+
geometry: {
|
|
524
|
+
polygon: Polygon as GeofencePolygon,
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
),
|
|
529
|
+
nextToken: NextToken,
|
|
530
|
+
};
|
|
332
531
|
|
|
333
532
|
return results;
|
|
334
533
|
}
|
|
335
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Delete geofences from a geofence collection
|
|
537
|
+
* @param geofenceIds: string|string[]
|
|
538
|
+
* @param options?: GeofenceOptions
|
|
539
|
+
* @returns {Promise<DeleteGeofencesResults>} - Promise that resolves to an object with:
|
|
540
|
+
* successes: list of geofences successfully deleted
|
|
541
|
+
* errors: list of geofences that failed to delete
|
|
542
|
+
*/
|
|
543
|
+
public async deleteGeofences(
|
|
544
|
+
geofenceIds: string[],
|
|
545
|
+
options?: AmazonLocationServiceGeofenceOptions
|
|
546
|
+
): Promise<AmazonLocationServiceDeleteGeofencesResults> {
|
|
547
|
+
if (geofenceIds.length < 1) {
|
|
548
|
+
throw new Error('GeofenceId input array is empty');
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const credentialsOK = await this._ensureCredentials();
|
|
552
|
+
if (!credentialsOK) {
|
|
553
|
+
throw new Error('No credentials');
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
this._verifyGeofenceCollections(options?.collectionName);
|
|
557
|
+
|
|
558
|
+
// Validate all geofenceIds are valid
|
|
559
|
+
const badGeofenceIds = geofenceIds.filter(geofenceId => {
|
|
560
|
+
try {
|
|
561
|
+
validateGeofenceId(geofenceId);
|
|
562
|
+
} catch (error) {
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
if (badGeofenceIds.length > 0) {
|
|
567
|
+
throw new Error(`Invalid geofence ids: ${badGeofenceIds.join(', ')}`);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const results: AmazonLocationServiceDeleteGeofencesResults = {
|
|
571
|
+
successes: [],
|
|
572
|
+
errors: [],
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const batches = [];
|
|
576
|
+
|
|
577
|
+
let count = 0;
|
|
578
|
+
while (count < geofenceIds.length) {
|
|
579
|
+
batches.push(geofenceIds.slice(count, (count += 10)));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
await Promise.all(
|
|
583
|
+
batches.map(async batch => {
|
|
584
|
+
let response;
|
|
585
|
+
try {
|
|
586
|
+
response = await this._AmazonLocationServiceBatchDeleteGeofenceCall(
|
|
587
|
+
batch,
|
|
588
|
+
options?.collectionName || this._config.geofenceCollections.default
|
|
589
|
+
);
|
|
590
|
+
} catch (error) {
|
|
591
|
+
// If the API call fails, add the geofences to the errors array and move to next batch
|
|
592
|
+
batch.forEach(geofenceId => {
|
|
593
|
+
const errorObject = {
|
|
594
|
+
geofenceId,
|
|
595
|
+
error: {
|
|
596
|
+
code: error.message,
|
|
597
|
+
message: error.message,
|
|
598
|
+
},
|
|
599
|
+
};
|
|
600
|
+
results.errors.push(errorObject);
|
|
601
|
+
});
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const badGeofenceIds = response.Errors.map(
|
|
606
|
+
({ geofenceId }) => geofenceId
|
|
607
|
+
);
|
|
608
|
+
results.successes.push(
|
|
609
|
+
...batch.filter(Id => !badGeofenceIds.includes(Id))
|
|
610
|
+
);
|
|
611
|
+
})
|
|
612
|
+
);
|
|
613
|
+
return results;
|
|
614
|
+
}
|
|
615
|
+
|
|
336
616
|
/**
|
|
337
617
|
* @private
|
|
338
618
|
*/
|
|
@@ -345,7 +625,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
345
625
|
this._config.credentials = cred;
|
|
346
626
|
return true;
|
|
347
627
|
} catch (error) {
|
|
348
|
-
logger.
|
|
628
|
+
logger.debug('Ensure credentials error. Credentials are:', error);
|
|
349
629
|
return false;
|
|
350
630
|
}
|
|
351
631
|
}
|
|
@@ -353,14 +633,14 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
353
633
|
private _verifyMapResources() {
|
|
354
634
|
if (!this._config.maps) {
|
|
355
635
|
const errorString =
|
|
356
|
-
"No map resources found in amplify config, run 'amplify add geo' to create
|
|
357
|
-
logger.
|
|
636
|
+
"No map resources found in amplify config, run 'amplify add geo' to create one and run `amplify push` after";
|
|
637
|
+
logger.debug(errorString);
|
|
358
638
|
throw new Error(errorString);
|
|
359
639
|
}
|
|
360
640
|
if (!this._config.maps.default) {
|
|
361
641
|
const errorString =
|
|
362
642
|
"No default map resource found in amplify config, run 'amplify add geo' to create one and run `amplify push` after";
|
|
363
|
-
logger.
|
|
643
|
+
logger.debug(errorString);
|
|
364
644
|
throw new Error(errorString);
|
|
365
645
|
}
|
|
366
646
|
}
|
|
@@ -371,9 +651,76 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
371
651
|
!optionalSearchIndex
|
|
372
652
|
) {
|
|
373
653
|
const errorString =
|
|
374
|
-
'No Search Index found, please run `amplify add geo` to
|
|
375
|
-
logger.
|
|
654
|
+
'No Search Index found in amplify config, please run `amplify add geo` to create one and run `amplify push` after.';
|
|
655
|
+
logger.debug(errorString);
|
|
376
656
|
throw new Error(errorString);
|
|
377
657
|
}
|
|
378
658
|
}
|
|
659
|
+
|
|
660
|
+
private _verifyGeofenceCollections(optionalGeofenceCollectionName?: string) {
|
|
661
|
+
if (
|
|
662
|
+
(!this._config.geofenceCollections ||
|
|
663
|
+
!this._config.geofenceCollections.default) &&
|
|
664
|
+
!optionalGeofenceCollectionName
|
|
665
|
+
) {
|
|
666
|
+
const errorString =
|
|
667
|
+
'No Geofence Collections found, please run `amplify add geo` to create one and run `amplify push` after.';
|
|
668
|
+
logger.debug(errorString);
|
|
669
|
+
throw new Error(errorString);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
private async _AmazonLocationServiceBatchPutGeofenceCall(
|
|
674
|
+
PascalGeofences: BatchPutGeofenceRequestEntry[],
|
|
675
|
+
collectionName?: string
|
|
676
|
+
) {
|
|
677
|
+
// Create the BatchPutGeofence input
|
|
678
|
+
const geofenceInput: BatchPutGeofenceCommandInput = {
|
|
679
|
+
Entries: PascalGeofences,
|
|
680
|
+
CollectionName:
|
|
681
|
+
collectionName || this._config.geofenceCollections.default,
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
const client = new LocationClient({
|
|
685
|
+
credentials: this._config.credentials,
|
|
686
|
+
region: this._config.region,
|
|
687
|
+
customUserAgent: getAmplifyUserAgent(),
|
|
688
|
+
});
|
|
689
|
+
const command = new BatchPutGeofenceCommand(geofenceInput);
|
|
690
|
+
|
|
691
|
+
let response: BatchPutGeofenceCommandOutput;
|
|
692
|
+
try {
|
|
693
|
+
response = await client.send(command);
|
|
694
|
+
} catch (error) {
|
|
695
|
+
throw error;
|
|
696
|
+
}
|
|
697
|
+
return response;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
private async _AmazonLocationServiceBatchDeleteGeofenceCall(
|
|
701
|
+
geofenceIds: string[],
|
|
702
|
+
collectionName?: string
|
|
703
|
+
): Promise<BatchDeleteGeofenceCommandOutput> {
|
|
704
|
+
// Create the BatchDeleteGeofence input
|
|
705
|
+
const deleteGeofencesInput: BatchDeleteGeofenceCommandInput = {
|
|
706
|
+
GeofenceIds: geofenceIds,
|
|
707
|
+
CollectionName:
|
|
708
|
+
collectionName || this._config.geofenceCollections.default,
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
const client = new LocationClient({
|
|
712
|
+
credentials: this._config.credentials,
|
|
713
|
+
region: this._config.region,
|
|
714
|
+
customUserAgent: getAmplifyUserAgent(),
|
|
715
|
+
});
|
|
716
|
+
const command = new BatchDeleteGeofenceCommand(deleteGeofencesInput);
|
|
717
|
+
|
|
718
|
+
let response: BatchDeleteGeofenceCommandOutput;
|
|
719
|
+
try {
|
|
720
|
+
response = await client.send(command);
|
|
721
|
+
} catch (error) {
|
|
722
|
+
throw error;
|
|
723
|
+
}
|
|
724
|
+
return response;
|
|
725
|
+
}
|
|
379
726
|
}
|
|
@@ -1,5 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MapStyle,
|
|
3
|
+
GeofenceOptions,
|
|
4
|
+
ListGeofenceOptions,
|
|
5
|
+
Geofence,
|
|
6
|
+
DeleteGeofencesResults,
|
|
7
|
+
GeofenceError,
|
|
8
|
+
} from './Geo';
|
|
2
9
|
|
|
10
|
+
// Maps
|
|
3
11
|
export interface AmazonLocationServiceMapStyle extends MapStyle {
|
|
4
12
|
region: string;
|
|
5
13
|
}
|
|
14
|
+
|
|
15
|
+
// Geofences
|
|
16
|
+
export type AmazonLocationServiceGeofenceOptions = GeofenceOptions & {
|
|
17
|
+
collectionName?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Status types for Geofences
|
|
21
|
+
export type AmazonLocationServiceGeofenceStatus =
|
|
22
|
+
| 'ACTIVE'
|
|
23
|
+
| 'PENDING'
|
|
24
|
+
| 'FAILED'
|
|
25
|
+
| 'DELETED'
|
|
26
|
+
| 'DELETING';
|
|
27
|
+
|
|
28
|
+
export type AmazonLocationServiceGeofence = Omit<Geofence, 'status'> & {
|
|
29
|
+
status: AmazonLocationServiceGeofenceStatus;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// List Geofences
|
|
33
|
+
export type AmazonLocationServiceListGeofenceOptions = ListGeofenceOptions & {
|
|
34
|
+
collectionName?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Delete Geofences
|
|
38
|
+
export type AmazonLocationServiceBatchGeofenceErrorMessages =
|
|
39
|
+
| 'AccessDeniedException'
|
|
40
|
+
| 'InternalServerException'
|
|
41
|
+
| 'ResourceNotFoundException'
|
|
42
|
+
| 'ThrottlingException'
|
|
43
|
+
| 'ValidationException';
|
|
44
|
+
|
|
45
|
+
export type AmazonLocationServiceBatchGeofenceError = Omit<
|
|
46
|
+
GeofenceError,
|
|
47
|
+
'error'
|
|
48
|
+
> & {
|
|
49
|
+
error: {
|
|
50
|
+
code: string;
|
|
51
|
+
message: AmazonLocationServiceBatchGeofenceErrorMessages;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type AmazonLocationServiceDeleteGeofencesResults = Omit<
|
|
56
|
+
DeleteGeofencesResults,
|
|
57
|
+
'errors'
|
|
58
|
+
> & {
|
|
59
|
+
errors: AmazonLocationServiceBatchGeofenceError[];
|
|
60
|
+
};
|