@aws-amplify/geo 1.2.1 → 1.2.2-geo.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.
- package/dist/aws-amplify-geo.js +1062 -51
- package/dist/aws-amplify-geo.js.map +1 -1
- package/dist/aws-amplify-geo.min.js +6 -6
- package/dist/aws-amplify-geo.min.js.map +1 -1
- package/lib/Geo.d.ts +36 -3
- package/lib/Geo.js +170 -5
- package/lib/Geo.js.map +1 -1
- package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib/Providers/AmazonLocationServiceProvider.js +409 -8
- package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib/types/Geo.d.ts +48 -1
- package/lib/types/Provider.d.ts +5 -1
- package/lib/types/Provider.js +0 -12
- package/lib/types/Provider.js.map +1 -1
- package/lib/util.d.ts +6 -0
- package/lib/util.js +163 -0
- package/lib/util.js.map +1 -0
- package/lib-esm/Geo.d.ts +36 -3
- package/lib-esm/Geo.js +170 -5
- package/lib-esm/Geo.js.map +1 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.js +410 -9
- package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib-esm/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib-esm/types/Geo.d.ts +48 -1
- package/lib-esm/types/Provider.d.ts +5 -1
- package/lib-esm/types/Provider.js +0 -12
- package/lib-esm/types/Provider.js.map +1 -1
- package/lib-esm/util.d.ts +6 -0
- package/lib-esm/util.js +156 -0
- package/lib-esm/util.js.map +1 -0
- package/package.json +5 -4
- package/src/Geo.ts +144 -2
- package/src/Providers/AmazonLocationServiceProvider.ts +409 -6
- package/src/types/AmazonLocationServiceProvider.ts +56 -1
- package/src/types/Geo.ts +72 -2
- package/src/types/Provider.ts +31 -1
- package/src/util.ts +182 -0
package/lib-esm/util.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
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 { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
30
|
+
var logger = new Logger('Geo');
|
|
31
|
+
export function validateCoordinates(lng, lat) {
|
|
32
|
+
if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
|
|
33
|
+
throw new Error("Invalid coordinates: [" + lng + "," + lat + "]");
|
|
34
|
+
}
|
|
35
|
+
if (lat < -90 || lat > 90) {
|
|
36
|
+
var errorString = 'Latitude must be between -90 and 90 degrees inclusive.';
|
|
37
|
+
logger.debug(errorString);
|
|
38
|
+
throw new Error(errorString);
|
|
39
|
+
}
|
|
40
|
+
else if (lng < -180 || lng > 180) {
|
|
41
|
+
var errorString = 'Longitude must be between -180 and 180 degrees inclusive.';
|
|
42
|
+
logger.debug(errorString);
|
|
43
|
+
throw new Error(errorString);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function validateGeofenceId(geofenceId) {
|
|
47
|
+
var geofenceIdRegex = /^[-._\p{L}\p{N}]+$/iu;
|
|
48
|
+
// Check if geofenceId is valid
|
|
49
|
+
if (!geofenceIdRegex.test(geofenceId)) {
|
|
50
|
+
var errorString = "Invalid geofenceId: " + geofenceId + " Ids can only contain alphanumeric characters, hyphens, underscores and periods.";
|
|
51
|
+
logger.debug(errorString);
|
|
52
|
+
throw new Error(errorString);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function validateLinearRing(linearRing) {
|
|
56
|
+
// Validate LinearRing size, must be at least 4 points
|
|
57
|
+
if (linearRing.length < 4) {
|
|
58
|
+
var errorString = 'LinearRing must contain 4 or more coordinates.';
|
|
59
|
+
logger.debug(errorString);
|
|
60
|
+
throw new Error(errorString);
|
|
61
|
+
}
|
|
62
|
+
// Validate all coordinates are valid, error with which ones are bad
|
|
63
|
+
var badCoordinates = [];
|
|
64
|
+
linearRing.forEach(function (coordinates) {
|
|
65
|
+
try {
|
|
66
|
+
validateCoordinates(coordinates[0], coordinates[1]);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
badCoordinates.push({ coordinates: coordinates, error: error.message });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
if (badCoordinates.length > 0) {
|
|
73
|
+
var errorString = "One or more of the coordinates are not valid: " + JSON.stringify(badCoordinates);
|
|
74
|
+
logger.debug(errorString);
|
|
75
|
+
throw new Error(errorString);
|
|
76
|
+
}
|
|
77
|
+
// Validate first and last coordinates are the same
|
|
78
|
+
var _a = __read(linearRing[0], 2), lngA = _a[0], latA = _a[1];
|
|
79
|
+
var _b = __read(linearRing[linearRing.length - 1], 2), lngB = _b[0], latB = _b[1];
|
|
80
|
+
if (lngA !== lngB || latA !== latB) {
|
|
81
|
+
var errorString = "LinearRing's first and last coordinates are not the same";
|
|
82
|
+
logger.debug(errorString);
|
|
83
|
+
throw new Error(errorString);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function validatePolygon(polygon) {
|
|
87
|
+
if (!Array.isArray(polygon)) {
|
|
88
|
+
var errorString = "Polygon " + JSON.stringify(polygon) + " is of incorrect structure. It should be an array of 'LinearRing'";
|
|
89
|
+
logger.debug(errorString);
|
|
90
|
+
throw new Error(errorString);
|
|
91
|
+
}
|
|
92
|
+
if (!(polygon.length === 1)) {
|
|
93
|
+
var errorString = "Polygon " + JSON.stringify(polygon) + " geometry.polygon must have a single LinearRing array";
|
|
94
|
+
logger.debug(errorString);
|
|
95
|
+
throw new Error(errorString);
|
|
96
|
+
}
|
|
97
|
+
var verticesCount = polygon.reduce(function (prev, linearRing) { return prev + linearRing.length; }, 0);
|
|
98
|
+
if (verticesCount > 1000) {
|
|
99
|
+
var errorString = "Polygon has more than the maximum 1000 vertices.";
|
|
100
|
+
logger.debug(errorString);
|
|
101
|
+
throw new Error(errorString);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function validateGeofences(geofences) {
|
|
105
|
+
var geofenceIds = {};
|
|
106
|
+
geofences.forEach(function (geofence) {
|
|
107
|
+
// verify all required properties are present
|
|
108
|
+
if (!geofence.geofenceId) {
|
|
109
|
+
var errorString = "Geofence " + JSON.stringify(geofence) + " is missing geofenceId";
|
|
110
|
+
logger.debug(errorString);
|
|
111
|
+
throw new Error(errorString);
|
|
112
|
+
}
|
|
113
|
+
if (!geofence.geometry) {
|
|
114
|
+
var errorString = "Geofence " + JSON.stringify(geofence) + " is missing geometry";
|
|
115
|
+
logger.debug(errorString);
|
|
116
|
+
throw new Error(errorString);
|
|
117
|
+
}
|
|
118
|
+
if (!geofence.geometry.polygon) {
|
|
119
|
+
var errorString = "Geofence " + JSON.stringify(geofence) + " is missing geometry.polygon";
|
|
120
|
+
logger.debug(errorString);
|
|
121
|
+
throw new Error(errorString);
|
|
122
|
+
}
|
|
123
|
+
var geofenceId = geofence.geofenceId, polygon = geofence.geometry.polygon;
|
|
124
|
+
// Validate geofenceId is valid
|
|
125
|
+
try {
|
|
126
|
+
validateGeofenceId(geofenceId);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
// Validate geofenceId is unique
|
|
132
|
+
if (geofenceIds[geofenceId]) {
|
|
133
|
+
var errorString = "Duplicate geofenceId: " + geofenceId;
|
|
134
|
+
logger.debug(errorString);
|
|
135
|
+
throw new Error(errorString);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
geofenceIds[geofenceId] = true;
|
|
139
|
+
}
|
|
140
|
+
// Validate polygon length and structure
|
|
141
|
+
try {
|
|
142
|
+
validatePolygon(polygon);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (error.message === "Polygon has more than the maximum 1000 vertices.") {
|
|
146
|
+
var errorString = "Geofence " + geofenceId + " has more than the maximum of 1000 vertices";
|
|
147
|
+
logger.debug(errorString);
|
|
148
|
+
throw new Error(errorString);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Validate LinearRing length, structure, and coordinates
|
|
152
|
+
var _a = __read(polygon, 1), linearRing = _a[0];
|
|
153
|
+
validateLinearRing(linearRing);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,aAAa,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAU5D,IAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CA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
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"homepage": "https://aws-amplify.github.io/",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@aws-amplify/core": "4.4.
|
|
44
|
+
"@aws-amplify/core": "4.4.1-geo.3+8413d3deb",
|
|
45
45
|
"@aws-sdk/client-location": "3.48.0",
|
|
46
46
|
"camelcase-keys": "6.2.2"
|
|
47
47
|
},
|
|
@@ -71,7 +71,8 @@
|
|
|
71
71
|
"__tests__/model.ts",
|
|
72
72
|
"__tests__/schema.ts",
|
|
73
73
|
"__tests__/helpers.ts",
|
|
74
|
-
"__tests__/
|
|
74
|
+
"__tests__/testData.ts",
|
|
75
|
+
"__tests__/testUtils.ts"
|
|
75
76
|
],
|
|
76
77
|
"moduleFileExtensions": [
|
|
77
78
|
"ts",
|
|
@@ -97,5 +98,5 @@
|
|
|
97
98
|
"lib-esm"
|
|
98
99
|
]
|
|
99
100
|
},
|
|
100
|
-
"gitHead": "
|
|
101
|
+
"gitHead": "8413d3deb36406ea9a716e55db8f864bf8b6056c"
|
|
101
102
|
}
|
package/src/Geo.ts
CHANGED
|
@@ -18,6 +18,13 @@ import {
|
|
|
18
18
|
import { AmazonLocationServiceProvider } from './Providers/AmazonLocationServiceProvider';
|
|
19
19
|
|
|
20
20
|
import {
|
|
21
|
+
validateCoordinates,
|
|
22
|
+
validateGeofences,
|
|
23
|
+
validateGeofenceId,
|
|
24
|
+
} from './util';
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
Place,
|
|
21
28
|
GeoConfig,
|
|
22
29
|
Coordinates,
|
|
23
30
|
SearchByTextOptions,
|
|
@@ -25,6 +32,13 @@ import {
|
|
|
25
32
|
SearchByCoordinatesOptions,
|
|
26
33
|
GeoProvider,
|
|
27
34
|
MapStyle,
|
|
35
|
+
GeofenceInput,
|
|
36
|
+
GeofenceOptions,
|
|
37
|
+
SaveGeofencesResults,
|
|
38
|
+
Geofence,
|
|
39
|
+
ListGeofenceOptions,
|
|
40
|
+
ListGeofenceResults,
|
|
41
|
+
DeleteGeofencesResults,
|
|
28
42
|
} from './types';
|
|
29
43
|
|
|
30
44
|
const logger = new Logger('Geo');
|
|
@@ -143,7 +157,10 @@ export class GeoClass {
|
|
|
143
157
|
* @param {SearchByTextOptions} options? - Optional parameters to the search
|
|
144
158
|
* @returns {Promise<Place[]>} - Promise resolves to a list of Places that match search parameters
|
|
145
159
|
*/
|
|
146
|
-
public async searchByText(
|
|
160
|
+
public async searchByText(
|
|
161
|
+
text: string,
|
|
162
|
+
options?: SearchByTextOptions
|
|
163
|
+
): Promise<Place[]> {
|
|
147
164
|
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
148
165
|
const prov = this.getPluggable(providerName);
|
|
149
166
|
|
|
@@ -185,17 +202,142 @@ export class GeoClass {
|
|
|
185
202
|
public async searchByCoordinates(
|
|
186
203
|
coordinates: Coordinates,
|
|
187
204
|
options?: SearchByCoordinatesOptions
|
|
188
|
-
) {
|
|
205
|
+
): Promise<Place> {
|
|
189
206
|
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
190
207
|
const prov = this.getPluggable(providerName);
|
|
191
208
|
|
|
209
|
+
const [lng, lat] = coordinates;
|
|
192
210
|
try {
|
|
211
|
+
validateCoordinates(lng, lat);
|
|
193
212
|
return await prov.searchByCoordinates(coordinates, options);
|
|
194
213
|
} catch (error) {
|
|
195
214
|
logger.debug(error);
|
|
196
215
|
throw error;
|
|
197
216
|
}
|
|
198
217
|
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Create geofences
|
|
221
|
+
* @param geofences - Single or array of geofence objects to create
|
|
222
|
+
* @param options? - Optional parameters for creating geofences
|
|
223
|
+
* @returns {Promise<SaveGeofencesResults>} - Promise that resolves to an object with:
|
|
224
|
+
* successes: list of geofences successfully created
|
|
225
|
+
* errors: list of geofences that failed to create
|
|
226
|
+
*/
|
|
227
|
+
public async saveGeofences(
|
|
228
|
+
geofences: GeofenceInput | GeofenceInput[],
|
|
229
|
+
options?: GeofenceOptions
|
|
230
|
+
): Promise<SaveGeofencesResults> {
|
|
231
|
+
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
232
|
+
const prov = this.getPluggable(providerName);
|
|
233
|
+
|
|
234
|
+
// If single geofence input, make it an array for batch call
|
|
235
|
+
let geofenceInputArray;
|
|
236
|
+
if (!Array.isArray(geofences)) {
|
|
237
|
+
geofenceInputArray = [geofences];
|
|
238
|
+
} else {
|
|
239
|
+
geofenceInputArray = geofences;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
// Validate all geofences are unique and valid before calling Provider
|
|
244
|
+
validateGeofences(geofenceInputArray);
|
|
245
|
+
return await prov.saveGeofences(geofenceInputArray, options);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
logger.debug(error);
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Get a single geofence by geofenceId
|
|
254
|
+
* @param geofenceId: string
|
|
255
|
+
* @param options?: GeofenceOptions
|
|
256
|
+
* @returns Promise<Geofence> - Promise that resolves to a geofence object
|
|
257
|
+
*/
|
|
258
|
+
public async getGeofence(
|
|
259
|
+
geofenceId: string,
|
|
260
|
+
options?: GeofenceOptions
|
|
261
|
+
): Promise<Geofence> {
|
|
262
|
+
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
263
|
+
const prov = this.getPluggable(providerName);
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
// Validate geofenceId is valid before calling Provider
|
|
267
|
+
validateGeofenceId(geofenceId);
|
|
268
|
+
return await prov.getGeofence(geofenceId, options);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
logger.debug(error);
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* List geofences
|
|
277
|
+
* @param options?: ListGeofenceOptions
|
|
278
|
+
* @returns {Promise<ListGeofencesResults>} - Promise that resolves to an object with:
|
|
279
|
+
* entries: list of geofences - 100 geofences are listed per page
|
|
280
|
+
* nextToken: token for next page of geofences
|
|
281
|
+
*/
|
|
282
|
+
public async listGeofences(
|
|
283
|
+
options?: ListGeofenceOptions
|
|
284
|
+
): Promise<ListGeofenceResults> {
|
|
285
|
+
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
286
|
+
const prov = this.getPluggable(providerName);
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
return await prov.listGeofences(options);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
logger.debug(error);
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Delete geofences
|
|
298
|
+
* @param geofenceIds: string|string[]
|
|
299
|
+
* @param options?: GeofenceOptions
|
|
300
|
+
* @returns {Promise<DeleteGeofencesResults>} - Promise that resolves to an object with:
|
|
301
|
+
* successes: list of geofences successfully deleted
|
|
302
|
+
* errors: list of geofences that failed to delete
|
|
303
|
+
*/
|
|
304
|
+
public async deleteGeofences(
|
|
305
|
+
geofenceIds: string | string[],
|
|
306
|
+
options?: GeofenceOptions
|
|
307
|
+
): Promise<DeleteGeofencesResults> {
|
|
308
|
+
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
309
|
+
const prov = this.getPluggable(providerName);
|
|
310
|
+
|
|
311
|
+
// If single geofence input, make it an array for batch call
|
|
312
|
+
let geofenceIdsInputArray;
|
|
313
|
+
if (!Array.isArray(geofenceIds)) {
|
|
314
|
+
geofenceIdsInputArray = [geofenceIds];
|
|
315
|
+
} else {
|
|
316
|
+
geofenceIdsInputArray = geofenceIds;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Validate all geofenceIds are valid
|
|
320
|
+
const badGeofenceIds = geofenceIdsInputArray.filter(geofenceId => {
|
|
321
|
+
try {
|
|
322
|
+
validateGeofenceId(geofenceId);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
if (badGeofenceIds.length > 0) {
|
|
328
|
+
const errorString = `Invalid geofence ids: ${badGeofenceIds}`;
|
|
329
|
+
logger.debug(errorString);
|
|
330
|
+
throw new Error(errorString);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Delete geofences
|
|
334
|
+
try {
|
|
335
|
+
return await prov.deleteGeofences(geofenceIdsInputArray, options);
|
|
336
|
+
} catch (error) {
|
|
337
|
+
logger.debug(error);
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
199
341
|
}
|
|
200
342
|
|
|
201
343
|
export const Geo = new GeoClass();
|