@aws-amplify/geo 1.2.4 → 1.2.5-geo.11

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/dist/aws-amplify-geo.js +2020 -137
  2. package/dist/aws-amplify-geo.js.map +1 -1
  3. package/dist/aws-amplify-geo.min.js +6 -6
  4. package/dist/aws-amplify-geo.min.js.map +1 -1
  5. package/lib/Geo.d.ts +36 -10
  6. package/lib/Geo.js +140 -20
  7. package/lib/Geo.js.map +1 -1
  8. package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -8
  9. package/lib/Providers/AmazonLocationServiceProvider.js +405 -53
  10. package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
  11. package/lib/types/AmazonLocationServiceProvider.d.ts +21 -1
  12. package/lib/types/Geo.d.ts +48 -2
  13. package/lib/types/Provider.d.ts +5 -2
  14. package/lib/types/Provider.js +0 -12
  15. package/lib/types/Provider.js.map +1 -1
  16. package/lib/util.d.ts +6 -0
  17. package/lib/util.js +144 -0
  18. package/lib/util.js.map +1 -0
  19. package/lib-esm/Geo.d.ts +36 -10
  20. package/lib-esm/Geo.js +140 -20
  21. package/lib-esm/Geo.js.map +1 -1
  22. package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -8
  23. package/lib-esm/Providers/AmazonLocationServiceProvider.js +406 -54
  24. package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +1 -1
  25. package/lib-esm/types/AmazonLocationServiceProvider.d.ts +21 -1
  26. package/lib-esm/types/Geo.d.ts +48 -2
  27. package/lib-esm/types/Provider.d.ts +5 -2
  28. package/lib-esm/types/Provider.js +0 -12
  29. package/lib-esm/types/Provider.js.map +1 -1
  30. package/lib-esm/util.d.ts +6 -0
  31. package/lib-esm/util.js +134 -0
  32. package/lib-esm/util.js.map +1 -0
  33. package/package.json +6 -4
  34. package/src/Geo.ts +118 -20
  35. package/src/Providers/AmazonLocationServiceProvider.ts +422 -75
  36. package/src/types/AmazonLocationServiceProvider.ts +56 -1
  37. package/src/types/Geo.ts +71 -4
  38. package/src/types/Provider.ts +29 -6
  39. package/src/util.ts +173 -0
@@ -9,14 +9,18 @@ export interface GeoConfig {
9
9
  items: string[];
10
10
  default: string;
11
11
  };
12
+ geofenceCollections?: {
13
+ items: string[];
14
+ default: string;
15
+ };
12
16
  };
13
17
  }
14
18
  export interface MapStyle {
15
19
  mapName: string;
16
20
  style: string;
17
21
  }
18
- export declare type Latitude = number;
19
22
  export declare type Longitude = number;
23
+ export declare type Latitude = number;
20
24
  export declare type Coordinates = [Longitude, Latitude];
21
25
  export declare type SWLongitude = Longitude;
22
26
  export declare type SWLatitude = Latitude;
@@ -56,4 +60,46 @@ export interface Place {
56
60
  street?: string;
57
61
  subRegion?: string;
58
62
  }
59
- export declare type SearchForSuggestionsResults = string[];
63
+ export declare type LinearRing = Coordinates[];
64
+ export declare type GeofencePolygon = LinearRing[];
65
+ export declare type PolygonGeometry = {
66
+ polygon: GeofencePolygon;
67
+ };
68
+ export declare type GeofenceInput = {
69
+ geofenceId: string;
70
+ geometry: PolygonGeometry;
71
+ };
72
+ export declare type GeofenceOptions = {
73
+ providerName?: string;
74
+ };
75
+ export declare type GeofenceError = {
76
+ error: {
77
+ code: string;
78
+ message: string;
79
+ };
80
+ geofenceId: string;
81
+ };
82
+ declare type GeofenceBase = {
83
+ geofenceId: string;
84
+ createTime?: Date;
85
+ updateTime?: Date;
86
+ };
87
+ export declare type Geofence = GeofenceBase & {
88
+ geometry: PolygonGeometry;
89
+ };
90
+ export declare type SaveGeofencesResults = {
91
+ successes: GeofenceBase[];
92
+ errors: GeofenceError[];
93
+ };
94
+ export declare type ListGeofenceOptions = GeofenceOptions & {
95
+ nextToken?: string;
96
+ };
97
+ export declare type ListGeofenceResults = {
98
+ entries: Geofence[];
99
+ nextToken: string;
100
+ };
101
+ export declare type DeleteGeofencesResults = {
102
+ successes: string[];
103
+ errors: GeofenceError[];
104
+ };
105
+ export {};
@@ -1,4 +1,4 @@
1
- import { SearchByTextOptions, SearchByCoordinatesOptions, SearchForSuggestionsResults, Coordinates, Place, MapStyle } from './Geo';
1
+ import { SearchByTextOptions, SearchByCoordinatesOptions, Coordinates, Place, MapStyle, Geofence, GeofenceInput, GeofenceOptions, ListGeofenceOptions, ListGeofenceResults, SaveGeofencesResults, DeleteGeofencesResults } from './Geo';
2
2
  export interface GeoProvider {
3
3
  getCategory(): string;
4
4
  getProviderName(): string;
@@ -7,5 +7,8 @@ export interface GeoProvider {
7
7
  getDefaultMap(): MapStyle;
8
8
  searchByText(text: string, options?: SearchByTextOptions): Promise<Place[]>;
9
9
  searchByCoordinates(coordinates: Coordinates, options?: SearchByCoordinatesOptions): Promise<Place>;
10
- searchForSuggestions(text: string, options?: SearchByTextOptions): Promise<SearchForSuggestionsResults>;
10
+ saveGeofences(geofences: GeofenceInput[], options?: GeofenceOptions): Promise<SaveGeofencesResults>;
11
+ getGeofence(geofenceId: string, options?: ListGeofenceOptions): Promise<Geofence>;
12
+ listGeofences(options?: ListGeofenceOptions): Promise<ListGeofenceResults>;
13
+ deleteGeofences(geofenceIds: string[], options?: GeofenceOptions): Promise<DeleteGeofencesResults>;
11
14
  }
@@ -1,13 +1 @@
1
- /*
2
- * Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
- * the License. A copy of the License is located at
6
- *
7
- * http://aws.amazon.com/apache2.0/
8
- *
9
- * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
- * and limitations under the License.
12
- */
13
1
  //# sourceMappingURL=Provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../src/types/Provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
1
+ {"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../src/types/Provider.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ import { Longitude, Latitude, GeofenceInput, GeofencePolygon, LinearRing } from './types';
2
+ export declare function validateCoordinates(lng: Longitude, lat: Latitude): void;
3
+ export declare function validateGeofenceId(geofenceId: string): void;
4
+ export declare function validateLinearRing(linearRing: LinearRing, geofenceId?: string): void;
5
+ export declare function validatePolygon(polygon: GeofencePolygon, geofenceId?: string): void;
6
+ export declare function validateGeofencesInput(geofences: GeofenceInput[]): void;
@@ -0,0 +1,134 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ /*
18
+ * Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
19
+ *
20
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
21
+ * the License. A copy of the License is located at
22
+ *
23
+ * http://aws.amazon.com/apache2.0/
24
+ *
25
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
26
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
27
+ * and limitations under the License.
28
+ */
29
+ import booleanClockwise from '@turf/boolean-clockwise';
30
+ export function validateCoordinates(lng, lat) {
31
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
32
+ throw new Error("Invalid coordinates: [" + lng + "," + lat + "]");
33
+ }
34
+ if (lat < -90 || lat > 90) {
35
+ throw new Error('Latitude must be between -90 and 90 degrees inclusive.');
36
+ }
37
+ else if (lng < -180 || lng > 180) {
38
+ throw new Error('Longitude must be between -180 and 180 degrees inclusive.');
39
+ }
40
+ }
41
+ export function validateGeofenceId(geofenceId) {
42
+ var geofenceIdRegex = /^[-._\p{L}\p{N}]+$/iu;
43
+ // Check if geofenceId is valid
44
+ if (!geofenceIdRegex.test(geofenceId)) {
45
+ throw new Error("Invalid geofenceId: '" + geofenceId + "' - IDs can only contain alphanumeric characters, hyphens, underscores and periods.");
46
+ }
47
+ }
48
+ export function validateLinearRing(linearRing, geofenceId) {
49
+ var errorPrefix = geofenceId ? geofenceId + ": " : '';
50
+ // Validate LinearRing size, must be at least 4 points
51
+ if (linearRing.length < 4) {
52
+ throw new Error(errorPrefix + "LinearRing must contain 4 or more coordinates.");
53
+ }
54
+ // Validate all coordinates are valid, error with which ones are bad
55
+ var badCoordinates = [];
56
+ linearRing.forEach(function (coordinates) {
57
+ try {
58
+ validateCoordinates(coordinates[0], coordinates[1]);
59
+ }
60
+ catch (error) {
61
+ badCoordinates.push({ coordinates: coordinates, error: error.message });
62
+ }
63
+ });
64
+ if (badCoordinates.length > 0) {
65
+ throw new Error(errorPrefix + "One or more of the coordinates in the Polygon LinearRing are not valid: " + JSON.stringify(badCoordinates));
66
+ }
67
+ // Validate first and last coordinates are the same
68
+ var _a = __read(linearRing[0], 2), lngA = _a[0], latA = _a[1];
69
+ var _b = __read(linearRing[linearRing.length - 1], 2), lngB = _b[0], latB = _b[1];
70
+ if (lngA !== lngB || latA !== latB) {
71
+ throw new Error(errorPrefix + "LinearRing's first and last coordinates are not the same");
72
+ }
73
+ if (booleanClockwise(linearRing)) {
74
+ throw new Error(errorPrefix + "LinearRing coordinates must be wound counterclockwise");
75
+ }
76
+ }
77
+ export function validatePolygon(polygon, geofenceId) {
78
+ var errorPrefix = geofenceId ? geofenceId + ": " : '';
79
+ if (!Array.isArray(polygon)) {
80
+ throw new Error(errorPrefix + "Polygon is of incorrect structure. It should be an array of LinearRings");
81
+ }
82
+ if (polygon.length < 1) {
83
+ throw new Error(errorPrefix + "Polygon must have a single LinearRing array.");
84
+ }
85
+ if (polygon.length > 1) {
86
+ throw new Error(errorPrefix + "Polygon must have a single LinearRing array. Note: We do not currently support polygons with holes, multipolygons, polygons that are wound clockwise, or that cross the antimeridian.");
87
+ }
88
+ var verticesCount = polygon.reduce(function (prev, linearRing) { return prev + linearRing.length; }, 0);
89
+ if (verticesCount > 1000) {
90
+ throw new Error(errorPrefix + "Polygon has more than the maximum 1000 vertices.");
91
+ }
92
+ }
93
+ export function validateGeofencesInput(geofences) {
94
+ var geofenceIds = {};
95
+ geofences.forEach(function (geofence) {
96
+ // verify all required properties are present
97
+ // Validate geofenceId exists
98
+ if (!geofence.geofenceId) {
99
+ throw new Error("Geofence '" + geofence + "' is missing geofenceId");
100
+ }
101
+ var geofenceId = geofence.geofenceId;
102
+ validateGeofenceId(geofenceId);
103
+ // Validate geofenceId is unique
104
+ if (geofenceIds[geofenceId]) {
105
+ throw new Error("Duplicate geofenceId: " + geofenceId);
106
+ }
107
+ else {
108
+ geofenceIds[geofenceId] = true;
109
+ }
110
+ // Validate geometry exists
111
+ if (!geofence.geometry) {
112
+ throw new Error("Geofence '" + geofenceId + "' is missing geometry");
113
+ }
114
+ var geometry = geofence.geometry;
115
+ // Validate polygon exists
116
+ if (!geometry.polygon) {
117
+ throw new Error("Geofence '" + geofenceId + "' is missing geometry.polygon");
118
+ }
119
+ var polygon = geometry.polygon;
120
+ // Validate polygon length and structure
121
+ try {
122
+ validatePolygon(polygon, geofenceId);
123
+ }
124
+ catch (error) {
125
+ if (error.message === "Polygon has more than the maximum 1000 vertices.") {
126
+ throw new Error("Geofence '" + geofenceId + "' has more than the maximum of 1000 vertices");
127
+ }
128
+ }
129
+ // Validate LinearRing length, structure, and coordinates
130
+ var _a = __read(polygon, 1), linearRing = _a[0];
131
+ validateLinearRing(linearRing, geofenceId);
132
+ });
133
+ }
134
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","s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AAa,UAAU,iDAA8C,CACrE,CAAC;aACF;SACD;QAED,yDAAyD;QACnD,IAAA,uBAAsB,EAArB,kBAAqB,CAAC;QAC7B,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/geo",
3
- "version": "1.2.4",
3
+ "version": "1.2.5-geo.11+faba1c898",
4
4
  "description": "Geo category for aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -41,8 +41,9 @@
41
41
  },
42
42
  "homepage": "https://aws-amplify.github.io/",
43
43
  "dependencies": {
44
- "@aws-amplify/core": "4.5.0",
44
+ "@aws-amplify/core": "4.5.1-geo.11+faba1c898",
45
45
  "@aws-sdk/client-location": "3.48.0",
46
+ "@turf/boolean-clockwise": "6.5.0",
46
47
  "camelcase-keys": "6.2.2"
47
48
  },
48
49
  "jest": {
@@ -71,7 +72,8 @@
71
72
  "__tests__/model.ts",
72
73
  "__tests__/schema.ts",
73
74
  "__tests__/helpers.ts",
74
- "__tests__/data.ts"
75
+ "__tests__/testData.ts",
76
+ "__tests__/testUtils.ts"
75
77
  ],
76
78
  "moduleFileExtensions": [
77
79
  "ts",
@@ -97,5 +99,5 @@
97
99
  "lib-esm"
98
100
  ]
99
101
  },
100
- "gitHead": "88fa9322e9c3df523ebf52b9924dc46e7a976b53"
102
+ "gitHead": "faba1c8980ac344b42718bc666abccfe37db4a2a"
101
103
  }
package/src/Geo.ts CHANGED
@@ -17,14 +17,23 @@ import {
17
17
  } from '@aws-amplify/core';
18
18
  import { AmazonLocationServiceProvider } from './Providers/AmazonLocationServiceProvider';
19
19
 
20
+ import { validateCoordinates } from './util';
21
+
20
22
  import {
23
+ Place,
21
24
  GeoConfig,
22
25
  Coordinates,
23
26
  SearchByTextOptions,
24
- SearchForSuggestionsResults,
25
27
  SearchByCoordinatesOptions,
26
28
  GeoProvider,
27
29
  MapStyle,
30
+ GeofenceInput,
31
+ GeofenceOptions,
32
+ SaveGeofencesResults,
33
+ Geofence,
34
+ ListGeofenceOptions,
35
+ ListGeofenceResults,
36
+ DeleteGeofencesResults,
28
37
  } from './types';
29
38
 
30
39
  const logger = new Logger('Geo');
@@ -143,7 +152,10 @@ export class GeoClass {
143
152
  * @param {SearchByTextOptions} options? - Optional parameters to the search
144
153
  * @returns {Promise<Place[]>} - Promise resolves to a list of Places that match search parameters
145
154
  */
146
- public async searchByText(text: string, options?: SearchByTextOptions) {
155
+ public async searchByText(
156
+ text: string,
157
+ options?: SearchByTextOptions
158
+ ): Promise<Place[]> {
147
159
  const { providerName = DEFAULT_PROVIDER } = options || {};
148
160
  const prov = this.getPluggable(providerName);
149
161
 
@@ -156,20 +168,22 @@ export class GeoClass {
156
168
  }
157
169
 
158
170
  /**
159
- * Search for search term suggestions based on input text
160
- * @param {string} text - The text string that is to be search for
161
- * @param {SearchByTextOptions} options? - Optional parameters to the search
162
- * @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
171
+ * Reverse geocoding search via a coordinate point on the map
172
+ * @param coordinates - Coordinates array for the search input
173
+ * @param options - Options parameters for the search
174
+ * @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
163
175
  */
164
- public async searchForSuggestions(
165
- text: string,
166
- options?: SearchByTextOptions
167
- ) {
176
+ public async searchByCoordinates(
177
+ coordinates: Coordinates,
178
+ options?: SearchByCoordinatesOptions
179
+ ): Promise<Place> {
168
180
  const { providerName = DEFAULT_PROVIDER } = options || {};
169
181
  const prov = this.getPluggable(providerName);
170
182
 
183
+ const [lng, lat] = coordinates;
171
184
  try {
172
- return await prov.searchForSuggestions(text, options);
185
+ validateCoordinates(lng, lat);
186
+ return await prov.searchByCoordinates(coordinates, options);
173
187
  } catch (error) {
174
188
  logger.debug(error);
175
189
  throw error;
@@ -177,20 +191,104 @@ export class GeoClass {
177
191
  }
178
192
 
179
193
  /**
180
- * Reverse geocoding search via a coordinate point on the map
181
- * @param coordinates - Coordinates array for the search input
182
- * @param options - Options parameters for the search
183
- * @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
194
+ * Create geofences
195
+ * @param geofences - Single or array of geofence objects to create
196
+ * @param options? - Optional parameters for creating geofences
197
+ * @returns {Promise<SaveGeofencesResults>} - Promise that resolves to an object with:
198
+ * successes: list of geofences successfully created
199
+ * errors: list of geofences that failed to create
184
200
  */
185
- public async searchByCoordinates(
186
- coordinates: Coordinates,
187
- options?: SearchByCoordinatesOptions
188
- ) {
201
+ public async saveGeofences(
202
+ geofences: GeofenceInput | GeofenceInput[],
203
+ options?: GeofenceOptions
204
+ ): Promise<SaveGeofencesResults> {
189
205
  const { providerName = DEFAULT_PROVIDER } = options || {};
190
206
  const prov = this.getPluggable(providerName);
191
207
 
208
+ // If single geofence input, make it an array for batch call
209
+ let geofenceInputArray;
210
+ if (!Array.isArray(geofences)) {
211
+ geofenceInputArray = [geofences];
212
+ } else {
213
+ geofenceInputArray = geofences;
214
+ }
215
+
192
216
  try {
193
- return await prov.searchByCoordinates(coordinates, options);
217
+ return await prov.saveGeofences(geofenceInputArray, options);
218
+ } catch (error) {
219
+ logger.debug(error);
220
+ throw error;
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Get a single geofence by geofenceId
226
+ * @param geofenceId: string
227
+ * @param options?: GeofenceOptions
228
+ * @returns Promise<Geofence> - Promise that resolves to a geofence object
229
+ */
230
+ public async getGeofence(
231
+ geofenceId: string,
232
+ options?: GeofenceOptions
233
+ ): Promise<Geofence> {
234
+ const { providerName = DEFAULT_PROVIDER } = options || {};
235
+ const prov = this.getPluggable(providerName);
236
+
237
+ try {
238
+ return await prov.getGeofence(geofenceId, options);
239
+ } catch (error) {
240
+ logger.debug(error);
241
+ throw error;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * List geofences
247
+ * @param options?: ListGeofenceOptions
248
+ * @returns {Promise<ListGeofencesResults>} - Promise that resolves to an object with:
249
+ * entries: list of geofences - 100 geofences are listed per page
250
+ * nextToken: token for next page of geofences
251
+ */
252
+ public async listGeofences(
253
+ options?: ListGeofenceOptions
254
+ ): Promise<ListGeofenceResults> {
255
+ const { providerName = DEFAULT_PROVIDER } = options || {};
256
+ const prov = this.getPluggable(providerName);
257
+
258
+ try {
259
+ return await prov.listGeofences(options);
260
+ } catch (error) {
261
+ logger.debug(error);
262
+ throw error;
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Delete geofences
268
+ * @param geofenceIds: string|string[]
269
+ * @param options?: GeofenceOptions
270
+ * @returns {Promise<DeleteGeofencesResults>} - Promise that resolves to an object with:
271
+ * successes: list of geofences successfully deleted
272
+ * errors: list of geofences that failed to delete
273
+ */
274
+ public async deleteGeofences(
275
+ geofenceIds: string | string[],
276
+ options?: GeofenceOptions
277
+ ): Promise<DeleteGeofencesResults> {
278
+ const { providerName = DEFAULT_PROVIDER } = options || {};
279
+ const prov = this.getPluggable(providerName);
280
+
281
+ // If single geofence input, make it an array for batch call
282
+ let geofenceIdsInputArray;
283
+ if (!Array.isArray(geofenceIds)) {
284
+ geofenceIdsInputArray = [geofenceIds];
285
+ } else {
286
+ geofenceIdsInputArray = geofenceIds;
287
+ }
288
+
289
+ // Delete geofences
290
+ try {
291
+ return await prov.deleteGeofences(geofenceIdsInputArray, options);
194
292
  } catch (error) {
195
293
  logger.debug(error);
196
294
  throw error;