@aws-amplify/geo 1.3.12-next.13 → 1.3.12-next.32

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/lib/Geo.js +91 -86
  3. package/lib/Geo.js.map +1 -1
  4. package/lib/Providers/AmazonLocationServiceProvider.js +160 -125
  5. package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
  6. package/lib/util.js +24 -23
  7. package/lib/util.js.map +1 -1
  8. package/lib-esm/Geo.d.ts +15 -1
  9. package/lib-esm/Geo.js +79 -74
  10. package/lib-esm/Geo.js.map +1 -1
  11. package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +10 -1
  12. package/lib-esm/Providers/AmazonLocationServiceProvider.js +138 -100
  13. package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +1 -1
  14. package/lib-esm/types/Geo.d.ts +8 -0
  15. package/lib-esm/types/Provider.d.ts +3 -1
  16. package/lib-esm/util.d.ts +1 -0
  17. package/lib-esm/util.js +19 -16
  18. package/lib-esm/util.js.map +1 -1
  19. package/package.json +11 -5
  20. package/src/Geo.ts +45 -2
  21. package/src/Providers/AmazonLocationServiceProvider.ts +131 -21
  22. package/src/types/Geo.ts +12 -0
  23. package/src/types/Provider.ts +12 -0
  24. package/src/util.ts +23 -0
  25. package/build.js +0 -5
  26. package/dist/aws-amplify-geo.js +0 -29948
  27. package/dist/aws-amplify-geo.js.map +0 -1
  28. package/dist/aws-amplify-geo.min.js +0 -56
  29. package/dist/aws-amplify-geo.min.js.map +0 -1
  30. package/index.js +0 -7
  31. package/lib/Geo.d.ts +0 -96
  32. package/lib/Providers/AmazonLocationServiceProvider.d.ts +0 -96
  33. package/lib/index.d.ts +0 -2
  34. package/lib/types/AmazonLocationServiceProvider.d.ts +0 -24
  35. package/lib/types/Geo.d.ts +0 -106
  36. package/lib/types/Provider.d.ts +0 -14
  37. package/lib/types/index.d.ts +0 -3
  38. package/lib/util.d.ts +0 -6
  39. package/webpack.config.dev.js +0 -6
package/src/Geo.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  import {
14
14
  Amplify,
15
15
  ConsoleLogger as Logger,
16
- parseMobileHubConfig,
16
+ parseAWSExports,
17
17
  } from '@aws-amplify/core';
18
18
  import { AmazonLocationServiceProvider } from './Providers/AmazonLocationServiceProvider';
19
19
 
@@ -35,6 +35,7 @@ import {
35
35
  ListGeofenceOptions,
36
36
  ListGeofenceResults,
37
37
  DeleteGeofencesResults,
38
+ searchByPlaceIdOptions,
38
39
  } from './types';
39
40
 
40
41
  const logger = new Logger('Geo');
@@ -112,7 +113,7 @@ export class GeoClass {
112
113
 
113
114
  if (!config) return this._config;
114
115
 
115
- const amplifyConfig = parseMobileHubConfig(config);
116
+ const amplifyConfig = parseAWSExports(config);
116
117
  this._config = Object.assign({}, this._config, amplifyConfig.Geo, config);
117
118
 
118
119
  this._pluggables.forEach(pluggable => {
@@ -168,6 +169,48 @@ export class GeoClass {
168
169
  }
169
170
  }
170
171
 
172
+ /**
173
+ * Search for search term suggestions based on input text
174
+ * @param {string} text - The text string that is to be search for
175
+ * @param {SearchByTextOptions} options? - Optional parameters to the search
176
+ * @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
177
+ */
178
+ public async searchForSuggestions(
179
+ text: string,
180
+ options?: SearchByTextOptions
181
+ ) {
182
+ const { providerName = DEFAULT_PROVIDER } = options || {};
183
+ const prov = this.getPluggable(providerName);
184
+
185
+ try {
186
+ return await prov.searchForSuggestions(text, options);
187
+ } catch (error) {
188
+ logger.debug(error);
189
+ throw error;
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Search for location by unique ID
195
+ * @param {string} placeId - Unique ID of the location that is to be searched for
196
+ * @param {searchByPlaceIdOptions} options? - Optional parameters to the search
197
+ * @returns {Promise<Place>} - Resolves to a place with the given placeId
198
+ */
199
+ public async searchByPlaceId(
200
+ placeId: string,
201
+ options?: searchByPlaceIdOptions
202
+ ) {
203
+ const providerName = DEFAULT_PROVIDER;
204
+ const prov = this.getPluggable(providerName);
205
+
206
+ try {
207
+ return await prov.searchByPlaceId(placeId, options);
208
+ } catch (error) {
209
+ logger.debug(error);
210
+ throw error;
211
+ }
212
+ }
213
+
171
214
  /**
172
215
  * Reverse geocoding search via a coordinate point on the map
173
216
  * @param coordinates - Coordinates array for the search input
@@ -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
+ }
package/build.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- const build = require('../../scripts/build');
4
-
5
- build(process.argv[2], process.argv[3]);