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

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 +2024 -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 +49 -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 +147 -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 +49 -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 +137 -0
  32. package/lib-esm/util.js.map +1 -0
  33. package/package.json +6 -4
  34. package/src/Geo.ts +119 -20
  35. package/src/Providers/AmazonLocationServiceProvider.ts +422 -75
  36. package/src/types/AmazonLocationServiceProvider.ts +56 -1
  37. package/src/types/Geo.ts +73 -4
  38. package/src/types/Provider.ts +30 -6
  39. package/src/util.ts +180 -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,47 @@ 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 GeofenceId = string;
69
+ export declare type GeofenceInput = {
70
+ geofenceId: GeofenceId;
71
+ geometry: PolygonGeometry;
72
+ };
73
+ export declare type GeofenceOptions = {
74
+ providerName?: string;
75
+ };
76
+ export declare type GeofenceError = {
77
+ error: {
78
+ code: string;
79
+ message: string;
80
+ };
81
+ geofenceId: GeofenceId;
82
+ };
83
+ declare type GeofenceBase = {
84
+ geofenceId: GeofenceId;
85
+ createTime?: Date;
86
+ updateTime?: Date;
87
+ };
88
+ export declare type Geofence = GeofenceBase & {
89
+ geometry: PolygonGeometry;
90
+ };
91
+ export declare type SaveGeofencesResults = {
92
+ successes: GeofenceBase[];
93
+ errors: GeofenceError[];
94
+ };
95
+ export declare type ListGeofenceOptions = GeofenceOptions & {
96
+ nextToken?: string;
97
+ };
98
+ export declare type ListGeofenceResults = {
99
+ entries: Geofence[];
100
+ nextToken: string | undefined;
101
+ };
102
+ export declare type DeleteGeofencesResults = {
103
+ successes: GeofenceId[];
104
+ errors: GeofenceError[];
105
+ };
106
+ export {};
@@ -1,4 +1,4 @@
1
- import { SearchByTextOptions, SearchByCoordinatesOptions, SearchForSuggestionsResults, Coordinates, Place, MapStyle } from './Geo';
1
+ import { SearchByTextOptions, SearchByCoordinatesOptions, Coordinates, Place, MapStyle, Geofence, GeofenceId, 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: GeofenceId, 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, GeofenceId, GeofenceInput, GeofencePolygon, LinearRing } from './types';
2
+ export declare function validateCoordinates(lng: Longitude, lat: Latitude): void;
3
+ export declare function validateGeofenceId(geofenceId: GeofenceId): void;
4
+ export declare function validateLinearRing(linearRing: LinearRing, geofenceId?: GeofenceId): void;
5
+ export declare function validatePolygon(polygon: GeofencePolygon, geofenceId?: GeofenceId): void;
6
+ export declare function validateGeofencesInput(geofences: GeofenceInput[]): void;
@@ -0,0 +1,137 @@
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 || 90 < lat) {
35
+ throw new Error('Latitude must be between -90 and 90 degrees inclusive.');
36
+ }
37
+ else if (lng < -180 || 180 < lng) {
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
+ polygon.forEach(function (linearRing) {
93
+ validateLinearRing(linearRing, geofenceId);
94
+ });
95
+ }
96
+ export function validateGeofencesInput(geofences) {
97
+ var geofenceIds = {};
98
+ geofences.forEach(function (geofence) {
99
+ // verify all required properties are present
100
+ // Validate geofenceId exists
101
+ if (!geofence.geofenceId) {
102
+ throw new Error("Geofence '" + geofence + "' is missing geofenceId");
103
+ }
104
+ var geofenceId = geofence.geofenceId;
105
+ validateGeofenceId(geofenceId);
106
+ // Validate geofenceId is unique
107
+ if (geofenceIds[geofenceId]) {
108
+ throw new Error("Duplicate geofenceId: " + geofenceId);
109
+ }
110
+ else {
111
+ geofenceIds[geofenceId] = true;
112
+ }
113
+ // Validate geometry exists
114
+ if (!geofence.geometry) {
115
+ throw new Error("Geofence '" + geofenceId + "' is missing geometry");
116
+ }
117
+ var geometry = geofence.geometry;
118
+ // Validate polygon exists
119
+ if (!geometry.polygon) {
120
+ throw new Error("Geofence '" + geofenceId + "' is missing geometry.polygon");
121
+ }
122
+ var polygon = geometry.polygon;
123
+ // Validate polygon length and structure
124
+ try {
125
+ validatePolygon(polygon, geofenceId);
126
+ }
127
+ catch (error) {
128
+ if (error.message.includes('Polygon has more than the maximum 1000 vertices.')) {
129
+ throw new Error("Geofence '" + geofenceId + "' has more than the maximum of 1000 vertices");
130
+ }
131
+ }
132
+ // Validate LinearRing length, structure, and coordinates
133
+ var _a = __read(polygon, 1), linearRing = _a[0];
134
+ validateLinearRing(linearRing, geofenceId);
135
+ });
136
+ }
137
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;GAWG;AACH,OAAO,gBAAgB,MAAM,yBAAyB,CAAC;AAWvD,MAAM,UAAU,mBAAmB,CAAC,GAAc,EAAE,GAAa;IAChE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACnD,MAAM,IAAI,KAAK,CAAC,2BAAyB,GAAG,SAAI,GAAG,MAAG,CAAC,CAAC;KACxD;IACD,IAAI,GAAG,GAAG,CAAC,EAAE,IAAI,EAAE,GAAG,GAAG,EAAE;QAC1B,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;KAC1E;SAAM,IAAI,GAAG,GAAG,CAAC,GAAG,IAAI,GAAG,GAAG,GAAG,EAAE;QACnC,MAAM,IAAI,KAAK,CACd,2DAA2D,CAC3D,CAAC;KACF;AACF,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,UAAsB;IACxD,IAAM,eAAe,GAAG,sBAAsB,CAAC;IAE/C,+BAA+B;IAC/B,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;QACtC,MAAM,IAAI,KAAK,CACd,0BAAwB,UAAU,wFAAqF,CACvH,CAAC;KACF;AACF,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,UAAsB,EACtB,UAAuB;IAEvB,IAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAI,UAAU,OAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IACxD,sDAAsD;IACtD,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;QAC1B,MAAM,IAAI,KAAK,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;YACtB,MAAM,IAAI,KAAK,CAAC,eAAa,UAAU,kCAA+B,CAAC,CAAC;SACxE;QACO,IAAA,0BAAO,CAAc;QAE7B,wCAAwC;QACxC,IAAI;YACH,eAAe,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;SACrC;QAAC,OAAO,KAAK,EAAE;YACf,IACC,KAAK,CAAC,OAAO,CAAC,QAAQ,CACrB,kDAAkD,CAClD,EACA;gBACD,MAAM,IAAI,KAAK,CACd,eAAa,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.18+158505d48",
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.18+158505d48",
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": "158505d488c9c31b5b3954e891a15cef841f8bdb"
101
103
  }
package/src/Geo.ts CHANGED
@@ -17,14 +17,24 @@ 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
+ GeofenceId,
31
+ GeofenceInput,
32
+ GeofenceOptions,
33
+ SaveGeofencesResults,
34
+ Geofence,
35
+ ListGeofenceOptions,
36
+ ListGeofenceResults,
37
+ DeleteGeofencesResults,
28
38
  } from './types';
29
39
 
30
40
  const logger = new Logger('Geo');
@@ -143,7 +153,10 @@ export class GeoClass {
143
153
  * @param {SearchByTextOptions} options? - Optional parameters to the search
144
154
  * @returns {Promise<Place[]>} - Promise resolves to a list of Places that match search parameters
145
155
  */
146
- public async searchByText(text: string, options?: SearchByTextOptions) {
156
+ public async searchByText(
157
+ text: string,
158
+ options?: SearchByTextOptions
159
+ ): Promise<Place[]> {
147
160
  const { providerName = DEFAULT_PROVIDER } = options || {};
148
161
  const prov = this.getPluggable(providerName);
149
162
 
@@ -156,20 +169,22 @@ export class GeoClass {
156
169
  }
157
170
 
158
171
  /**
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
172
+ * Reverse geocoding search via a coordinate point on the map
173
+ * @param coordinates - Coordinates array for the search input
174
+ * @param options - Options parameters for the search
175
+ * @returns {Promise<Place>} - Promise that resolves to a place matching search coordinates
163
176
  */
164
- public async searchForSuggestions(
165
- text: string,
166
- options?: SearchByTextOptions
167
- ) {
177
+ public async searchByCoordinates(
178
+ coordinates: Coordinates,
179
+ options?: SearchByCoordinatesOptions
180
+ ): Promise<Place> {
168
181
  const { providerName = DEFAULT_PROVIDER } = options || {};
169
182
  const prov = this.getPluggable(providerName);
170
183
 
184
+ const [lng, lat] = coordinates;
171
185
  try {
172
- return await prov.searchForSuggestions(text, options);
186
+ validateCoordinates(lng, lat);
187
+ return await prov.searchByCoordinates(coordinates, options);
173
188
  } catch (error) {
174
189
  logger.debug(error);
175
190
  throw error;
@@ -177,20 +192,104 @@ export class GeoClass {
177
192
  }
178
193
 
179
194
  /**
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
195
+ * Create geofences
196
+ * @param geofences - Single or array of geofence objects to create
197
+ * @param options? - Optional parameters for creating geofences
198
+ * @returns {Promise<SaveGeofencesResults>} - Promise that resolves to an object with:
199
+ * successes: list of geofences successfully created
200
+ * errors: list of geofences that failed to create
184
201
  */
185
- public async searchByCoordinates(
186
- coordinates: Coordinates,
187
- options?: SearchByCoordinatesOptions
188
- ) {
202
+ public async saveGeofences(
203
+ geofences: GeofenceInput | GeofenceInput[],
204
+ options?: GeofenceOptions
205
+ ): Promise<SaveGeofencesResults> {
189
206
  const { providerName = DEFAULT_PROVIDER } = options || {};
190
207
  const prov = this.getPluggable(providerName);
191
208
 
209
+ // If single geofence input, make it an array for batch call
210
+ let geofenceInputArray;
211
+ if (!Array.isArray(geofences)) {
212
+ geofenceInputArray = [geofences];
213
+ } else {
214
+ geofenceInputArray = geofences;
215
+ }
216
+
192
217
  try {
193
- return await prov.searchByCoordinates(coordinates, options);
218
+ return await prov.saveGeofences(geofenceInputArray, options);
219
+ } catch (error) {
220
+ logger.debug(error);
221
+ throw error;
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Get a single geofence by geofenceId
227
+ * @param geofenceId: GeofenceId - The string id of the geofence to get
228
+ * @param options?: GeofenceOptions - Optional parameters for getting a geofence
229
+ * @returns Promise<Geofence> - Promise that resolves to a geofence object
230
+ */
231
+ public async getGeofence(
232
+ geofenceId: GeofenceId,
233
+ options?: GeofenceOptions
234
+ ): Promise<Geofence> {
235
+ const { providerName = DEFAULT_PROVIDER } = options || {};
236
+ const prov = this.getPluggable(providerName);
237
+
238
+ try {
239
+ return await prov.getGeofence(geofenceId, options);
240
+ } catch (error) {
241
+ logger.debug(error);
242
+ throw error;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * List geofences
248
+ * @param options?: ListGeofenceOptions
249
+ * @returns {Promise<ListGeofencesResults>} - Promise that resolves to an object with:
250
+ * entries: list of geofences - 100 geofences are listed per page
251
+ * nextToken: token for next page of geofences
252
+ */
253
+ public async listGeofences(
254
+ options?: ListGeofenceOptions
255
+ ): Promise<ListGeofenceResults> {
256
+ const { providerName = DEFAULT_PROVIDER } = options || {};
257
+ const prov = this.getPluggable(providerName);
258
+
259
+ try {
260
+ return await prov.listGeofences(options);
261
+ } catch (error) {
262
+ logger.debug(error);
263
+ throw error;
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Delete geofences
269
+ * @param geofenceIds: string|string[]
270
+ * @param options?: GeofenceOptions
271
+ * @returns {Promise<DeleteGeofencesResults>} - Promise that resolves to an object with:
272
+ * successes: list of geofences successfully deleted
273
+ * errors: list of geofences that failed to delete
274
+ */
275
+ public async deleteGeofences(
276
+ geofenceIds: string | string[],
277
+ options?: GeofenceOptions
278
+ ): Promise<DeleteGeofencesResults> {
279
+ const { providerName = DEFAULT_PROVIDER } = options || {};
280
+ const prov = this.getPluggable(providerName);
281
+
282
+ // If single geofence input, make it an array for batch call
283
+ let geofenceIdsInputArray;
284
+ if (!Array.isArray(geofenceIds)) {
285
+ geofenceIdsInputArray = [geofenceIds];
286
+ } else {
287
+ geofenceIdsInputArray = geofenceIds;
288
+ }
289
+
290
+ // Delete geofences
291
+ try {
292
+ return await prov.deleteGeofences(geofenceIdsInputArray, options);
194
293
  } catch (error) {
195
294
  logger.debug(error);
196
295
  throw error;