@aws-amplify/geo 1.1.14-unstable.3 → 1.2.1-cloud-logging.3

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.
@@ -18,9 +18,11 @@ import {
18
18
  } from '@aws-amplify/core';
19
19
  import {
20
20
  Place as PlaceResult,
21
- SearchPlaceIndexForTextCommandInput,
22
21
  LocationClient,
23
22
  SearchPlaceIndexForTextCommand,
23
+ SearchPlaceIndexForTextCommandInput,
24
+ SearchPlaceIndexForSuggestionsCommand,
25
+ SearchPlaceIndexForSuggestionsCommandInput,
24
26
  SearchPlaceIndexForPositionCommand,
25
27
  SearchPlaceIndexForPositionCommandInput,
26
28
  } from '@aws-sdk/client-location';
@@ -33,6 +35,7 @@ import {
33
35
  Place,
34
36
  AmazonLocationServiceMapStyle,
35
37
  Coordinates,
38
+ SearchForSuggestionsResults,
36
39
  } from '../types';
37
40
 
38
41
  const logger = new Logger('AmazonLocationServiceProvider');
@@ -188,9 +191,85 @@ export class AmazonLocationServiceProvider implements GeoProvider {
188
191
  const PascalResults: PlaceResult[] = response.Results.map(
189
192
  result => result.Place
190
193
  );
191
- const results: Place[] = (camelcaseKeys(PascalResults, {
194
+ const results: Place[] = camelcaseKeys(PascalResults, {
192
195
  deep: true,
193
- }) as undefined) as Place[];
196
+ }) as undefined as Place[];
197
+
198
+ return results;
199
+ }
200
+
201
+ /**
202
+ * Search for suggestions based on the input text
203
+ * @param {string} text - The text string that is to be searched for
204
+ * @param {SearchByTextOptions} options? - Optional parameters to the search
205
+ * @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
206
+ */
207
+
208
+ public async searchForSuggestions(
209
+ text: string,
210
+ options?: SearchByTextOptions
211
+ ): Promise<SearchForSuggestionsResults> {
212
+ const credentialsOK = await this._ensureCredentials();
213
+ if (!credentialsOK) {
214
+ throw new Error('No credentials');
215
+ }
216
+
217
+ this._verifySearchIndex(options?.searchIndexName);
218
+
219
+ /**
220
+ * Setup the searchInput
221
+ */
222
+ const locationServiceInput: SearchPlaceIndexForSuggestionsCommandInput = {
223
+ Text: text,
224
+ IndexName: this._config.search_indices.default,
225
+ };
226
+
227
+ /**
228
+ * Map search options to Amazon Location Service input object
229
+ */
230
+ if (options) {
231
+ locationServiceInput.FilterCountries = options.countries;
232
+ locationServiceInput.MaxResults = options.maxResults;
233
+
234
+ if (options.searchIndexName) {
235
+ locationServiceInput.IndexName = options.searchIndexName;
236
+ }
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
+ }
250
+
251
+ const client = new LocationClient({
252
+ credentials: this._config.credentials,
253
+ region: this._config.region,
254
+ customUserAgent: getAmplifyUserAgent(),
255
+ });
256
+ const command = new SearchPlaceIndexForSuggestionsCommand(
257
+ locationServiceInput
258
+ );
259
+
260
+ let response;
261
+ try {
262
+ response = await client.send(command);
263
+ } catch (error) {
264
+ logger.debug(error);
265
+ throw error;
266
+ }
267
+
268
+ /**
269
+ * The response from Amazon Location Service is a "Results" array of objects with a single `Text` item.
270
+ * Here we want to flatten that to an array of just the strings from those `Text` items.
271
+ */
272
+ const results = response.Results.map(result => result.Text);
194
273
 
195
274
  return results;
196
275
  }
@@ -247,9 +326,9 @@ export class AmazonLocationServiceProvider implements GeoProvider {
247
326
  * Here we want to flatten that to an array of results and change them to camelCase
248
327
  */
249
328
  const PascalResults = response.Results.map(result => result.Place);
250
- const results: Place = (camelcaseKeys(PascalResults[0], {
329
+ const results: Place = camelcaseKeys(PascalResults[0], {
251
330
  deep: true,
252
- }) as any) as Place;
331
+ }) as any as Place;
253
332
 
254
333
  return results;
255
334
  }
package/src/types/Geo.ts CHANGED
@@ -97,3 +97,6 @@ export interface Place {
97
97
  street?: string;
98
98
  subRegion?: string;
99
99
  }
100
+
101
+ // Return type for searchForSuggestions
102
+ export type SearchForSuggestionsResults = string[];
@@ -14,6 +14,7 @@
14
14
  import {
15
15
  SearchByTextOptions,
16
16
  SearchByCoordinatesOptions,
17
+ SearchForSuggestionsResults,
17
18
  Coordinates,
18
19
  Place,
19
20
  MapStyle,
@@ -41,4 +42,9 @@ export interface GeoProvider {
41
42
  coordinates: Coordinates,
42
43
  options?: SearchByCoordinatesOptions
43
44
  ): Promise<Place>;
45
+
46
+ searchForSuggestions(
47
+ text: string,
48
+ options?: SearchByTextOptions
49
+ ): Promise<SearchForSuggestionsResults>;
44
50
  }