@aws-amplify/geo 1.2.5-unstable.7 → 1.3.0
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/CHANGELOG.md +16 -0
- package/dist/aws-amplify-geo.js +2097 -73
- 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 +153 -5
- package/lib/Geo.js.map +1 -1
- package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib/Providers/AmazonLocationServiceProvider.js +425 -8
- package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib/types/Geo.d.ts +49 -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 +147 -0
- package/lib/util.js.map +1 -0
- package/lib-esm/Geo.d.ts +36 -3
- package/lib-esm/Geo.js +153 -5
- package/lib-esm/Geo.js.map +1 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.js +426 -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 +49 -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 +137 -0
- package/lib-esm/util.js.map +1 -0
- package/package.json +6 -4
- package/src/Geo.ts +123 -2
- package/src/Providers/AmazonLocationServiceProvider.ts +432 -6
- package/src/types/AmazonLocationServiceProvider.ts +56 -1
- package/src/types/Geo.ts +74 -2
- package/src/types/Provider.ts +31 -1
- package/src/util.ts +180 -0
|
@@ -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;
|
package/lib-esm/util.js
ADDED
|
@@ -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,gBAAg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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.
|
|
3
|
+
"version": "1.3.0",
|
|
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.1
|
|
44
|
+
"@aws-amplify/core": "4.5.1",
|
|
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__/
|
|
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": "
|
|
102
|
+
"gitHead": "ccbe8c71a31a79827783d9834cf50fd8aec843af"
|
|
101
103
|
}
|
package/src/Geo.ts
CHANGED
|
@@ -17,13 +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
27
|
SearchByCoordinatesOptions,
|
|
25
28
|
GeoProvider,
|
|
26
29
|
MapStyle,
|
|
30
|
+
GeofenceId,
|
|
31
|
+
GeofenceInput,
|
|
32
|
+
GeofenceOptions,
|
|
33
|
+
SaveGeofencesResults,
|
|
34
|
+
Geofence,
|
|
35
|
+
ListGeofenceOptions,
|
|
36
|
+
ListGeofenceResults,
|
|
37
|
+
DeleteGeofencesResults,
|
|
27
38
|
} from './types';
|
|
28
39
|
|
|
29
40
|
const logger = new Logger('Geo');
|
|
@@ -142,7 +153,10 @@ export class GeoClass {
|
|
|
142
153
|
* @param {SearchByTextOptions} options? - Optional parameters to the search
|
|
143
154
|
* @returns {Promise<Place[]>} - Promise resolves to a list of Places that match search parameters
|
|
144
155
|
*/
|
|
145
|
-
public async searchByText(
|
|
156
|
+
public async searchByText(
|
|
157
|
+
text: string,
|
|
158
|
+
options?: SearchByTextOptions
|
|
159
|
+
): Promise<Place[]> {
|
|
146
160
|
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
147
161
|
const prov = this.getPluggable(providerName);
|
|
148
162
|
|
|
@@ -163,17 +177,124 @@ export class GeoClass {
|
|
|
163
177
|
public async searchByCoordinates(
|
|
164
178
|
coordinates: Coordinates,
|
|
165
179
|
options?: SearchByCoordinatesOptions
|
|
166
|
-
) {
|
|
180
|
+
): Promise<Place> {
|
|
167
181
|
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
168
182
|
const prov = this.getPluggable(providerName);
|
|
169
183
|
|
|
184
|
+
const [lng, lat] = coordinates;
|
|
170
185
|
try {
|
|
186
|
+
validateCoordinates(lng, lat);
|
|
171
187
|
return await prov.searchByCoordinates(coordinates, options);
|
|
172
188
|
} catch (error) {
|
|
173
189
|
logger.debug(error);
|
|
174
190
|
throw error;
|
|
175
191
|
}
|
|
176
192
|
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
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
|
|
201
|
+
*/
|
|
202
|
+
public async saveGeofences(
|
|
203
|
+
geofences: GeofenceInput | GeofenceInput[],
|
|
204
|
+
options?: GeofenceOptions
|
|
205
|
+
): Promise<SaveGeofencesResults> {
|
|
206
|
+
const { providerName = DEFAULT_PROVIDER } = options || {};
|
|
207
|
+
const prov = this.getPluggable(providerName);
|
|
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
|
+
|
|
217
|
+
try {
|
|
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);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
logger.debug(error);
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
177
298
|
}
|
|
178
299
|
|
|
179
300
|
export const Geo = new GeoClass();
|