@aws-amplify/geo 1.3.19-unstable.2 → 1.3.19

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.
@@ -19,15 +19,20 @@ import {
19
19
  } from '@aws-amplify/core';
20
20
  import {
21
21
  Place as PlaceResult,
22
- SearchPlaceIndexForTextCommandInput,
23
22
  LocationClient,
24
23
  SearchPlaceIndexForTextCommand,
24
+ SearchPlaceIndexForTextCommandInput,
25
+ SearchPlaceIndexForSuggestionsCommand,
26
+ SearchPlaceIndexForSuggestionsCommandInput,
25
27
  SearchPlaceIndexForPositionCommand,
26
28
  SearchPlaceIndexForPositionCommandInput,
27
29
  BatchPutGeofenceCommand,
28
30
  BatchPutGeofenceCommandInput,
29
31
  BatchPutGeofenceRequestEntry,
30
32
  BatchPutGeofenceCommandOutput,
33
+ GetPlaceCommand,
34
+ GetPlaceCommandInput,
35
+ GetPlaceCommandOutput,
31
36
  GetGeofenceCommand,
32
37
  GetGeofenceCommandInput,
33
38
  GetGeofenceCommandOutput,
@@ -39,7 +44,11 @@ import {
39
44
  BatchDeleteGeofenceCommandOutput,
40
45
  } from '@aws-sdk/client-location';
41
46
 
42
- import { validateGeofenceId, validateGeofencesInput } from '../util';
47
+ import {
48
+ mapSearchOptions,
49
+ validateGeofenceId,
50
+ validateGeofencesInput,
51
+ } from '../util';
43
52
 
44
53
  import {
45
54
  GeoConfig,
@@ -49,6 +58,7 @@ import {
49
58
  Place,
50
59
  AmazonLocationServiceMapStyle,
51
60
  Coordinates,
61
+ SearchForSuggestionsResults,
52
62
  GeofenceId,
53
63
  GeofenceInput,
54
64
  AmazonLocationServiceGeofenceOptions,
@@ -59,6 +69,7 @@ import {
59
69
  AmazonLocationServiceGeofence,
60
70
  GeofencePolygon,
61
71
  AmazonLocationServiceDeleteGeofencesResults,
72
+ searchByPlaceIdOptions,
62
73
  } from '../types';
63
74
 
64
75
  const logger = new Logger('AmazonLocationServiceProvider');
@@ -162,7 +173,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
162
173
  /**
163
174
  * Setup the searchInput
164
175
  */
165
- const locationServiceInput: SearchPlaceIndexForTextCommandInput = {
176
+ let locationServiceInput: SearchPlaceIndexForTextCommandInput = {
166
177
  Text: text,
167
178
  IndexName: this._config.search_indices.default,
168
179
  };
@@ -171,24 +182,10 @@ export class AmazonLocationServiceProvider implements GeoProvider {
171
182
  * Map search options to Amazon Location Service input object
172
183
  */
173
184
  if (options) {
174
- locationServiceInput.FilterCountries = options.countries;
175
- locationServiceInput.MaxResults = options.maxResults;
176
-
177
- if (options.searchIndexName) {
178
- locationServiceInput.IndexName = options.searchIndexName;
179
- }
180
-
181
- if (options['biasPosition'] && options['searchAreaConstraints']) {
182
- throw new Error(
183
- 'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
184
- );
185
- }
186
- if (options['biasPosition']) {
187
- locationServiceInput.BiasPosition = options['biasPosition'];
188
- }
189
- if (options['searchAreaConstraints']) {
190
- locationServiceInput.FilterBBox = options['searchAreaConstraints'];
191
- }
185
+ locationServiceInput = {
186
+ ...locationServiceInput,
187
+ ...mapSearchOptions(options, locationServiceInput),
188
+ };
192
189
  }
193
190
 
194
191
  const client = new LocationClient({
@@ -221,6 +218,119 @@ export class AmazonLocationServiceProvider implements GeoProvider {
221
218
  return results;
222
219
  }
223
220
 
221
+ /**
222
+ * Search for suggestions based on the input text
223
+ * @param {string} text - The text string that is to be searched for
224
+ * @param {SearchByTextOptions} options? - Optional parameters to the search
225
+ * @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
226
+ */
227
+
228
+ public async searchForSuggestions(
229
+ text: string,
230
+ options?: SearchByTextOptions
231
+ ): Promise<SearchForSuggestionsResults> {
232
+ const credentialsOK = await this._ensureCredentials();
233
+ if (!credentialsOK) {
234
+ throw new Error('No credentials');
235
+ }
236
+
237
+ this._verifySearchIndex(options?.searchIndexName);
238
+
239
+ /**
240
+ * Setup the searchInput
241
+ */
242
+ let locationServiceInput: SearchPlaceIndexForSuggestionsCommandInput = {
243
+ Text: text,
244
+ IndexName: this._config.search_indices.default,
245
+ };
246
+
247
+ /**
248
+ * Map search options to Amazon Location Service input object
249
+ */
250
+ if (options) {
251
+ locationServiceInput = {
252
+ ...locationServiceInput,
253
+ ...mapSearchOptions(options, locationServiceInput),
254
+ };
255
+ }
256
+
257
+ const client = new LocationClient({
258
+ credentials: this._config.credentials,
259
+ region: this._config.region,
260
+ customUserAgent: getAmplifyUserAgent(),
261
+ });
262
+ const command = new SearchPlaceIndexForSuggestionsCommand(
263
+ locationServiceInput
264
+ );
265
+
266
+ let response;
267
+ try {
268
+ response = await client.send(command);
269
+ } catch (error) {
270
+ logger.debug(error);
271
+ throw error;
272
+ }
273
+
274
+ /**
275
+ * The response from Amazon Location Service is a "Results" array of objects with `Text` and `PlaceId`.
276
+ */
277
+ const results = response.Results.map(result => ({
278
+ text: result.Text,
279
+ placeId: result.PlaceId,
280
+ }));
281
+
282
+ return results;
283
+ }
284
+
285
+ private _verifyPlaceId(placeId: string) {
286
+ if (placeId.length === 0) {
287
+ const errorString = 'PlaceId cannot be an empty string.';
288
+ logger.debug(errorString);
289
+ throw new Error(errorString);
290
+ }
291
+ }
292
+
293
+ public async searchByPlaceId(
294
+ placeId: string,
295
+ options?: searchByPlaceIdOptions
296
+ ): Promise<Place | undefined> {
297
+ const credentialsOK = await this._ensureCredentials();
298
+ if (!credentialsOK) {
299
+ throw new Error('No credentials');
300
+ }
301
+
302
+ this._verifySearchIndex(options?.searchIndexName);
303
+ this._verifyPlaceId(placeId);
304
+
305
+ const client = new LocationClient({
306
+ credentials: this._config.credentials,
307
+ region: this._config.region,
308
+ customUserAgent: getAmplifyUserAgent(),
309
+ });
310
+
311
+ const searchByPlaceIdInput: GetPlaceCommandInput = {
312
+ PlaceId: placeId,
313
+ IndexName:
314
+ options?.searchIndexName || this._config.search_indices.default,
315
+ };
316
+ const command = new GetPlaceCommand(searchByPlaceIdInput);
317
+
318
+ let response: GetPlaceCommandOutput;
319
+ try {
320
+ response = await client.send(command);
321
+ } catch (error) {
322
+ logger.debug(error);
323
+ throw error;
324
+ }
325
+
326
+ const place: PlaceResult | undefined = response.Place;
327
+
328
+ if (place) {
329
+ return camelcaseKeys(place, { deep: true }) as unknown as Place;
330
+ }
331
+ return;
332
+ }
333
+
224
334
  /**
225
335
  * Reverse geocoding search via a coordinate point on the map
226
336
  * @param coordinates - Coordinates array for the search input
package/src/types/Geo.ts CHANGED
@@ -84,6 +84,10 @@ export type SearchByCoordinatesOptions = {
84
84
  providerName?: string;
85
85
  };
86
86
 
87
+ export type searchByPlaceIdOptions = {
88
+ searchIndexName?: string;
89
+ };
90
+
87
91
  // Geometry object for Place points
88
92
  export type PlaceGeometry = {
89
93
  point: Coordinates;
@@ -169,3 +173,11 @@ export type DeleteGeofencesResults = {
169
173
  successes: GeofenceId[];
170
174
  errors: GeofenceError[];
171
175
  };
176
+
177
+ // Return type for searchForSuggestions
178
+ export type SearchForSuggestionsResults = SearchForSuggestionsResult[];
179
+
180
+ export type SearchForSuggestionsResult = {
181
+ text: string;
182
+ placeId?: string;
183
+ };
@@ -13,6 +13,7 @@
13
13
  import {
14
14
  SearchByTextOptions,
15
15
  SearchByCoordinatesOptions,
16
+ SearchForSuggestionsResults,
16
17
  Coordinates,
17
18
  Place,
18
19
  MapStyle,
@@ -24,6 +25,7 @@ import {
24
25
  ListGeofenceResults,
25
26
  SaveGeofencesResults,
26
27
  DeleteGeofencesResults,
28
+ searchByPlaceIdOptions,
27
29
  } from './Geo';
28
30
 
29
31
  export interface GeoProvider {
@@ -51,6 +53,16 @@ export interface GeoProvider {
51
53
  options?: SearchByCoordinatesOptions
52
54
  ): Promise<Place>;
53
55
 
56
+ searchForSuggestions(
57
+ text: string,
58
+ options?: SearchByTextOptions
59
+ ): Promise<SearchForSuggestionsResults>;
60
+
61
+ searchByPlaceId(
62
+ placeId: string,
63
+ options?: searchByPlaceIdOptions
64
+ ): Promise<Place | undefined>;
65
+
54
66
  // create geofences
55
67
  saveGeofences(
56
68
  geofences: GeofenceInput[],
package/src/util.ts CHANGED
@@ -178,3 +178,26 @@ export function validateGeofencesInput(geofences: GeofenceInput[]) {
178
178
  validateLinearRing(linearRing, geofenceId);
179
179
  });
180
180
  }
181
+
182
+ export function mapSearchOptions(options, locationServiceInput) {
183
+ const locationServiceModifiedInput = { ...locationServiceInput };
184
+ locationServiceModifiedInput.FilterCountries = options.countries;
185
+ locationServiceModifiedInput.MaxResults = options.maxResults;
186
+
187
+ if (options.searchIndexName) {
188
+ locationServiceModifiedInput.IndexName = options.searchIndexName;
189
+ }
190
+
191
+ if (options['biasPosition'] && options['searchAreaConstraints']) {
192
+ throw new Error(
193
+ 'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
194
+ );
195
+ }
196
+ if (options['biasPosition']) {
197
+ locationServiceModifiedInput.BiasPosition = options['biasPosition'];
198
+ }
199
+ if (options['searchAreaConstraints']) {
200
+ locationServiceModifiedInput.FilterBBox = options['searchAreaConstraints'];
201
+ }
202
+ return locationServiceModifiedInput;
203
+ }