-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
310 lines (244 loc) · 8.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
// Dependencies
const request = require('superagent');
// URLs
const MAPPIFY_BASE_URL = "https://mappify.io/api/rpc/";
const AUTOCOMPLETE_URL = MAPPIFY_BASE_URL + "address/autocomplete/";
const CLASSIFY_URL = MAPPIFY_BASE_URL + "coordinates/classify/";
const GEOCODE_URL = MAPPIFY_BASE_URL + "address/geocode/";
const REVERSE_GEOCODE_URL = MAPPIFY_BASE_URL + "coordinates/reversegeocode/";
const DRIVING_DIRECTIONS_URL = MAPPIFY_BASE_URL + "trip/directions/";
const DRIVING_STATISTICS_URL = MAPPIFY_BASE_URL + "trip/driveStats/";
module.exports.getClient = function(apiKey) {
let mappify = {};
let config = {};
if(apiKey && typeof apiKey === "string") {
config.apiKey = apiKey;
}
/* *** Geocoding *** */
mappify.autocomplete = function(addressSearchString, options, done) {
// Options are optional
if(arguments.length == 2 && (typeof arguments[1]) === "function") {
options = null;
done = arguments[1];
}
if(typeof addressSearchString !== "string") {
return done(new TypeError("Provided search value wasn't a string"));
}
if(addressSearchString === "") {
return done(new TypeError("Provided search string was empty"));
}
let postBody = {
streetAddress: addressSearchString,
apiKey: config.apiKey
};
if(options && (typeof options.boostPrefix) === 'boolean') {
postBody.boostPrefix = options.boostPrefix;
}
request
.post(AUTOCOMPLETE_URL)
.send(postBody)
.end((err, res) => {
if(err) {
return done(err);
}
done(null, res.body);
});
};
mappify.classifyCoordinates = function(encoding, lat, long, radius, done) {
// radius is optional
if(arguments.length == 4 && (typeof arguments[3]) === "function") {
radius = null;
done = arguments[3];
}
if(!encoding || !["LGA", "POA", "SA1", "SA2", "SA3", "SA4"].includes(encoding)) {
return done(new Error("Invalid encoding value"));
}
if(typeof lat !== "number") {
return done(new Error("Invalid latitude value"));
}
if(lat < -90 || lat > 90) {
return done(new Error("Latitude is out of range"));
}
if(typeof long !== "number") {
return done(new Error("Invalid longitude value"));
}
if(long < -180 || long > 180) {
return done(new Error("Longitude is out of range"));
}
if(radius !== null && (typeof radius !== "number")) {
return done(new Error("Invalid radius value"));
}
if((typeof radius === "number") && radius < 0) {
return done(new Error("Radius is out of range"));
}
let postBody = {
encoding: encoding,
lat: lat,
lon: long,
radius: radius,
apiKey: config.apiKey
};
request
.post(CLASSIFY_URL)
.send(postBody)
.end((err, res) => {
if(err) {
return done(err);
}
done(null, res.body);
});
};
mappify.geocode = function(streetAddress, postCode, suburb, state, done) {
if(typeof streetAddress !== "string") {
return done(new TypeError("Provided street address value wasn't a string"));
}
if(streetAddress === "") {
return done(new TypeError("Provided street address string was empty"));
}
if(postCode && (typeof postCode !== "string")) {
return done(new TypeError("Provided postcode wasn't a string"));
}
if(suburb && (typeof suburb !== "string")) {
return done(new TypeError("Provided suburb wasn't a string"));
}
if(state && (typeof state !== "string")) {
return done(new TypeError("Provided state wasn't a string"));
}
if(state && !["ACT", "NSW", "VIC", "QLD", "TAS", "SA", "WA", "NT"].includes(state)) {
return done(new Error("Provided state wasn't a valid state code"));
}
let postBody = {
streetAddress: streetAddress,
postCode: postCode,
suburb: suburb,
state: state,
apiKey: config.apiKey
};
request
.post(GEOCODE_URL)
.send(postBody)
.end((err, res) => {
if(err) {
return done(err);
}
done(null, res.body);
});
};
mappify.reverseGeocode = function(lat, long, radius, done) {
// radius is optional
if(arguments.length == 3 && (typeof arguments[2]) === "function") {
radius = null;
done = arguments[2];
}
if(typeof lat !== "number") {
return done(new Error("Latitude wasn't a number"));
}
if(lat < -90 || lat > 90) {
return done(new Error("Latitude is out of range"));
}
if(typeof long !== "number") {
return done(new Error("Longitude wasn't a number"));
}
if(long < -180 || long > 180) {
return done(new Error("Longitude is out of range"));
}
if(radius !== null && (typeof radius !== "number")) {
return done(new Error("Radius wasn't a number"));
}
if((typeof radius === "number") && radius < 0) {
return done(new Error("Radius is out of range"));
}
let postBody = {
lat: lat,
lon: long,
radius: radius,
apiKey: config.apiKey
};
request
.post(REVERSE_GEOCODE_URL)
.send(postBody)
.end((err, res) => {
if(err) {
return done(err);
}
done(null, res.body);
});
};
/* *** Routing *** */
mappify.drivingDirections = function(origin, destination, options, done) {
routingRequest(DRIVING_DIRECTIONS_URL, origin, destination, options, done);
};
mappify.drivingStatistics = function(origin, destination, options, done) {
routingRequest(DRIVING_STATISTICS_URL, origin, destination, options, done);
};
return mappify;
/* *** Helper functions *** */
function validatePointObject(point) {
let validationResults = {};
if(point === null || typeof point !== "object") {
validationResults.error = new Error("Not a valid object");
}
else if((typeof point.lat) !== "number") {
validationResults.error = new Error("'lat' is missing");
}
else if((typeof point.lon) !== "number") {
validationResults.error = new Error("'lon' is missing");
}
else if(point.lat < -90 || point.lat > 90) {
validationResults.error = new Error("Latitude is out of range");
}
else if(point.lon < -180 || point.lon > 180) {
validationResults.error = new Error("Longitude is out of range");
}
else {
validationResults.validPoint = {
lat: point.lat,
lon: point.lon
}
}
return validationResults;
}
function routingRequest(url, origin, destination, options, done) {
// Options are optional
if((typeof arguments[4]) === "undefined" && (typeof arguments[3]) === "function") {
options = null;
done = arguments[3];
}
let originValidationResults = validatePointObject(origin);
if(originValidationResults.error) {
return done(new Error(`Origin isn't a valid point. ${originValidationResults.error.message}.`));
}
let destinationValidationResults = validatePointObject(destination);
if(destinationValidationResults.error) {
return done(new Error(`Destination isn't a valid point. ${destinationValidationResults.error.message}.`));
}
if(typeof options !== "object") {
return done(new Error("Options isn't an object"));
}
let postBody = {
origin: originValidationResults.validPoint,
destination: destinationValidationResults.validPoint,
apiKey: config.apiKey
};
if(options) {
let validOptions = {};
if(options.prioritiseMinimumDistance) {
validOptions.prioritiseMinimumDistance = true;
}
if(options.ignoreDirectionality) {
validOptions.ignoreDirectionality = true;
}
postBody.options = validOptions || null;
}
request
.post(url)
.send(postBody)
.end((err, res) => {
if(err) {
return done(err);
}
done(null, res.body);
});
};
};