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