@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.
Files changed (39) hide show
  1. package/dist/aws-amplify-geo.js +1062 -51
  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 -3
  6. package/lib/Geo.js +170 -5
  7. package/lib/Geo.js.map +1 -1
  8. package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -1
  9. package/lib/Providers/AmazonLocationServiceProvider.js +409 -8
  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 -1
  13. package/lib/types/Provider.d.ts +5 -1
  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 +163 -0
  18. package/lib/util.js.map +1 -0
  19. package/lib-esm/Geo.d.ts +36 -3
  20. package/lib-esm/Geo.js +170 -5
  21. package/lib-esm/Geo.js.map +1 -1
  22. package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -1
  23. package/lib-esm/Providers/AmazonLocationServiceProvider.js +410 -9
  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 -1
  27. package/lib-esm/types/Provider.d.ts +5 -1
  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 +156 -0
  32. package/lib-esm/util.js.map +1 -0
  33. package/package.json +5 -4
  34. package/src/Geo.ts +144 -2
  35. package/src/Providers/AmazonLocationServiceProvider.ts +409 -6
  36. package/src/types/AmazonLocationServiceProvider.ts +56 -1
  37. package/src/types/Geo.ts +72 -2
  38. package/src/types/Provider.ts +31 -1
  39. package/src/util.ts +182 -0
package/src/util.ts ADDED
@@ -0,0 +1,182 @@
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
+ import { ConsoleLogger as Logger } from '@aws-amplify/core';
14
+
15
+ import {
16
+ Longitude,
17
+ Latitude,
18
+ GeofenceInput,
19
+ GeofencePolygon,
20
+ LinearRing,
21
+ } from './types';
22
+
23
+ const logger = new Logger('Geo');
24
+
25
+ export function validateCoordinates(lng: Longitude, lat: Latitude): void {
26
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
27
+ throw new Error(`Invalid coordinates: [${lng},${lat}]`);
28
+ }
29
+ if (lat < -90 || lat > 90) {
30
+ const errorString =
31
+ 'Latitude must be between -90 and 90 degrees inclusive.';
32
+ logger.debug(errorString);
33
+ throw new Error(errorString);
34
+ } else if (lng < -180 || lng > 180) {
35
+ const errorString =
36
+ 'Longitude must be between -180 and 180 degrees inclusive.';
37
+ logger.debug(errorString);
38
+ throw new Error(errorString);
39
+ }
40
+ }
41
+
42
+ export function validateGeofenceId(geofenceId: string) {
43
+ const geofenceIdRegex = /^[-._\p{L}\p{N}]+$/iu;
44
+
45
+ // Check if geofenceId is valid
46
+ if (!geofenceIdRegex.test(geofenceId)) {
47
+ const errorString = `Invalid geofenceId: ${geofenceId} Ids can only contain alphanumeric characters, hyphens, underscores and periods.`;
48
+ logger.debug(errorString);
49
+ throw new Error(errorString);
50
+ }
51
+ }
52
+
53
+ export function validateLinearRing(linearRing: LinearRing) {
54
+ // Validate LinearRing size, must be at least 4 points
55
+ if (linearRing.length < 4) {
56
+ const errorString = 'LinearRing must contain 4 or more coordinates.';
57
+ logger.debug(errorString);
58
+ throw new Error(errorString);
59
+ }
60
+
61
+ // Validate all coordinates are valid, error with which ones are bad
62
+ const badCoordinates = [];
63
+ linearRing.forEach(coordinates => {
64
+ try {
65
+ validateCoordinates(coordinates[0], coordinates[1]);
66
+ } catch (error) {
67
+ badCoordinates.push({ coordinates, error: error.message });
68
+ }
69
+ });
70
+ if (badCoordinates.length > 0) {
71
+ const errorString = `One or more of the coordinates are not valid: ${JSON.stringify(
72
+ badCoordinates
73
+ )}`;
74
+ logger.debug(errorString);
75
+ throw new Error(errorString);
76
+ }
77
+
78
+ // Validate first and last coordinates are the same
79
+ const [lngA, latA] = linearRing[0];
80
+ const [lngB, latB] = linearRing[linearRing.length - 1];
81
+
82
+ if (lngA !== lngB || latA !== latB) {
83
+ const errorString = `LinearRing's first and last coordinates are not the same`;
84
+ logger.debug(errorString);
85
+ throw new Error(errorString);
86
+ }
87
+ }
88
+
89
+ export function validatePolygon(polygon: GeofencePolygon): void {
90
+ if (!Array.isArray(polygon)) {
91
+ const errorString = `Polygon ${JSON.stringify(
92
+ polygon
93
+ )} is of incorrect structure. It should be an array of 'LinearRing'`;
94
+ logger.debug(errorString);
95
+ throw new Error(errorString);
96
+ }
97
+ if (!(polygon.length === 1)) {
98
+ const errorString = `Polygon ${JSON.stringify(
99
+ polygon
100
+ )} geometry.polygon must have a single LinearRing array`;
101
+ logger.debug(errorString);
102
+ throw new Error(errorString);
103
+ }
104
+ const verticesCount = polygon.reduce(
105
+ (prev, linearRing) => prev + linearRing.length,
106
+ 0
107
+ );
108
+ if (verticesCount > 1000) {
109
+ const errorString = `Polygon has more than the maximum 1000 vertices.`;
110
+ logger.debug(errorString);
111
+ throw new Error(errorString);
112
+ }
113
+ }
114
+
115
+ export function validateGeofences(geofences: GeofenceInput[]) {
116
+ const geofenceIds = {};
117
+
118
+ geofences.forEach((geofence: GeofenceInput) => {
119
+ // verify all required properties are present
120
+ if (!geofence.geofenceId) {
121
+ const errorString = `Geofence ${JSON.stringify(
122
+ geofence
123
+ )} is missing geofenceId`;
124
+ logger.debug(errorString);
125
+ throw new Error(errorString);
126
+ }
127
+
128
+ if (!geofence.geometry) {
129
+ const errorString = `Geofence ${JSON.stringify(
130
+ geofence
131
+ )} is missing geometry`;
132
+ logger.debug(errorString);
133
+ throw new Error(errorString);
134
+ }
135
+
136
+ if (!geofence.geometry.polygon) {
137
+ const errorString = `Geofence ${JSON.stringify(
138
+ geofence
139
+ )} is missing geometry.polygon`;
140
+ logger.debug(errorString);
141
+ throw new Error(errorString);
142
+ }
143
+
144
+ const {
145
+ geofenceId,
146
+ geometry: { polygon },
147
+ } = geofence;
148
+
149
+ // Validate geofenceId is valid
150
+ try {
151
+ validateGeofenceId(geofenceId);
152
+ } catch (error) {
153
+ throw error;
154
+ }
155
+
156
+ // Validate geofenceId is unique
157
+ if (geofenceIds[geofenceId]) {
158
+ const errorString = `Duplicate geofenceId: ${geofenceId}`;
159
+ logger.debug(errorString);
160
+ throw new Error(errorString);
161
+ } else {
162
+ geofenceIds[geofenceId] = true;
163
+ }
164
+
165
+ // Validate polygon length and structure
166
+ try {
167
+ validatePolygon(polygon);
168
+ } catch (error) {
169
+ if (
170
+ error.message === `Polygon has more than the maximum 1000 vertices.`
171
+ ) {
172
+ const errorString = `Geofence ${geofenceId} has more than the maximum of 1000 vertices`;
173
+ logger.debug(errorString);
174
+ throw new Error(errorString);
175
+ }
176
+ }
177
+
178
+ // Validate LinearRing length, structure, and coordinates
179
+ const [linearRing] = polygon;
180
+ validateLinearRing(linearRing);
181
+ });
182
+ }