@aws-amplify/geo 1.2.4 → 1.2.5-geo.18
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 +2024 -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 +49 -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 +147 -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 +49 -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 +137 -0
- package/lib-esm/util.js.map +1 -0
- package/package.json +6 -4
- package/src/Geo.ts +119 -20
- package/src/Providers/AmazonLocationServiceProvider.ts +422 -75
- package/src/types/AmazonLocationServiceProvider.ts +56 -1
- package/src/types/Geo.ts +73 -4
- package/src/types/Provider.ts +30 -6
- package/src/util.ts +180 -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,16 @@ import {
|
|
|
35
49
|
Place,
|
|
36
50
|
AmazonLocationServiceMapStyle,
|
|
37
51
|
Coordinates,
|
|
38
|
-
|
|
52
|
+
GeofenceId,
|
|
53
|
+
GeofenceInput,
|
|
54
|
+
AmazonLocationServiceGeofenceOptions,
|
|
55
|
+
AmazonLocationServiceListGeofenceOptions,
|
|
56
|
+
ListGeofenceResults,
|
|
57
|
+
AmazonLocationServiceGeofenceStatus,
|
|
58
|
+
SaveGeofencesResults,
|
|
59
|
+
AmazonLocationServiceGeofence,
|
|
60
|
+
GeofencePolygon,
|
|
61
|
+
AmazonLocationServiceDeleteGeofencesResults,
|
|
39
62
|
} from '../types';
|
|
40
63
|
|
|
41
64
|
const logger = new Logger('AmazonLocationServiceProvider');
|
|
@@ -199,16 +222,15 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
199
222
|
}
|
|
200
223
|
|
|
201
224
|
/**
|
|
202
|
-
*
|
|
203
|
-
* @param
|
|
204
|
-
* @param
|
|
205
|
-
* @returns {Promise<
|
|
225
|
+
* Reverse geocoding search via a coordinate point on the map
|
|
226
|
+
* @param coordinates - Coordinates array for the search input
|
|
227
|
+
* @param options - Options parameters for the search
|
|
228
|
+
* @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
|
|
206
229
|
*/
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
): Promise<SearchForSuggestionsResults> {
|
|
230
|
+
public async searchByCoordinates(
|
|
231
|
+
coordinates: Coordinates,
|
|
232
|
+
options?: SearchByCoordinatesOptions
|
|
233
|
+
): Promise<Place> {
|
|
212
234
|
const credentialsOK = await this._ensureCredentials();
|
|
213
235
|
if (!credentialsOK) {
|
|
214
236
|
throw new Error('No credentials');
|
|
@@ -216,36 +238,16 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
216
238
|
|
|
217
239
|
this._verifySearchIndex(options?.searchIndexName);
|
|
218
240
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
*/
|
|
222
|
-
const locationServiceInput: SearchPlaceIndexForSuggestionsCommandInput = {
|
|
223
|
-
Text: text,
|
|
241
|
+
const locationServiceInput: SearchPlaceIndexForPositionCommandInput = {
|
|
242
|
+
Position: coordinates,
|
|
224
243
|
IndexName: this._config.search_indices.default,
|
|
225
244
|
};
|
|
226
245
|
|
|
227
|
-
/**
|
|
228
|
-
* Map search options to Amazon Location Service input object
|
|
229
|
-
*/
|
|
230
246
|
if (options) {
|
|
231
|
-
locationServiceInput.FilterCountries = options.countries;
|
|
232
|
-
locationServiceInput.MaxResults = options.maxResults;
|
|
233
|
-
|
|
234
247
|
if (options.searchIndexName) {
|
|
235
248
|
locationServiceInput.IndexName = options.searchIndexName;
|
|
236
249
|
}
|
|
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
|
-
}
|
|
250
|
+
locationServiceInput.MaxResults = options.maxResults;
|
|
249
251
|
}
|
|
250
252
|
|
|
251
253
|
const client = new LocationClient({
|
|
@@ -253,7 +255,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
253
255
|
region: this._config.region,
|
|
254
256
|
customUserAgent: getAmplifyUserAgent(),
|
|
255
257
|
});
|
|
256
|
-
const command = new
|
|
258
|
+
const command = new SearchPlaceIndexForPositionCommand(
|
|
257
259
|
locationServiceInput
|
|
258
260
|
);
|
|
259
261
|
|
|
@@ -266,53 +268,234 @@ export class AmazonLocationServiceProvider implements GeoProvider {
|
|
|
266
268
|
}
|
|
267
269
|
|
|
268
270
|
/**
|
|
269
|
-
* The response from Amazon Location Service is a "Results" array
|
|
270
|
-
*
|
|
271
|
+
* The response from Amazon Location Service is a "Results" array with a single `Place` object
|
|
272
|
+
* which are Place objects in PascalCase.
|
|
273
|
+
* Here we want to flatten that to an array of results and change them to camelCase
|
|
271
274
|
*/
|
|
272
|
-
const
|
|
275
|
+
const PascalResults = response.Results.map(result => result.Place);
|
|
276
|
+
const results: Place = camelcaseKeys(PascalResults[0], {
|
|
277
|
+
deep: true,
|
|
278
|
+
}) as any as Place;
|
|
273
279
|
|
|
274
280
|
return results;
|
|
275
281
|
}
|
|
276
282
|
|
|
277
283
|
/**
|
|
278
|
-
*
|
|
279
|
-
* @param
|
|
280
|
-
* @param options -
|
|
281
|
-
* @returns {Promise<
|
|
284
|
+
* Create geofences inside of a geofence collection
|
|
285
|
+
* @param geofences - Array of geofence objects to create
|
|
286
|
+
* @param options? - Optional parameters for creating geofences
|
|
287
|
+
* @returns {Promise<AmazonLocationServiceSaveGeofencesResults>} - Promise that resolves to an object with:
|
|
288
|
+
* successes: list of geofences successfully created
|
|
289
|
+
* errors: list of geofences that failed to create
|
|
282
290
|
*/
|
|
283
|
-
public async
|
|
284
|
-
|
|
285
|
-
options?:
|
|
286
|
-
): Promise<
|
|
291
|
+
public async saveGeofences(
|
|
292
|
+
geofences: GeofenceInput[],
|
|
293
|
+
options?: AmazonLocationServiceGeofenceOptions
|
|
294
|
+
): Promise<SaveGeofencesResults> {
|
|
295
|
+
if (geofences.length < 1) {
|
|
296
|
+
throw new Error('Geofence input array is empty');
|
|
297
|
+
}
|
|
298
|
+
|
|
287
299
|
const credentialsOK = await this._ensureCredentials();
|
|
288
300
|
if (!credentialsOK) {
|
|
289
301
|
throw new Error('No credentials');
|
|
290
302
|
}
|
|
291
303
|
|
|
292
|
-
|
|
304
|
+
// Verify geofence collection exists in aws-config.js
|
|
305
|
+
try {
|
|
306
|
+
this._verifyGeofenceCollections(options?.collectionName);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
logger.debug(error);
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
293
311
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
312
|
+
validateGeofencesInput(geofences);
|
|
313
|
+
|
|
314
|
+
// Convert geofences to PascalCase for Amazon Location Service format
|
|
315
|
+
const PascalGeofences: BatchPutGeofenceRequestEntry[] = geofences.map(
|
|
316
|
+
({ geofenceId, geometry: { polygon } }) => {
|
|
317
|
+
return {
|
|
318
|
+
GeofenceId: geofenceId,
|
|
319
|
+
Geometry: {
|
|
320
|
+
Polygon: polygon,
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
);
|
|
325
|
+
const results: SaveGeofencesResults = {
|
|
326
|
+
successes: [],
|
|
327
|
+
errors: [],
|
|
297
328
|
};
|
|
298
329
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
330
|
+
const geofenceBatches: BatchPutGeofenceRequestEntry[][] = [];
|
|
331
|
+
|
|
332
|
+
while (PascalGeofences.length > 0) {
|
|
333
|
+
// Splice off 10 geofences from input clone due to Amazon Location Service API limit
|
|
334
|
+
const apiLimit = 10;
|
|
335
|
+
geofenceBatches.push(PascalGeofences.splice(0, apiLimit));
|
|
304
336
|
}
|
|
305
337
|
|
|
338
|
+
await Promise.all(
|
|
339
|
+
geofenceBatches.map(async batch => {
|
|
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: GeofenceId,
|
|
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 geofenceIdBatches: string[][] = [];
|
|
576
|
+
|
|
577
|
+
let count = 0;
|
|
578
|
+
while (count < geofenceIds.length) {
|
|
579
|
+
geofenceIdBatches.push(geofenceIds.slice(count, (count += 10)));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
await Promise.all(
|
|
583
|
+
geofenceIdBatches.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
|
+
};
|