distance.js
1.65 KB
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
/**
* Modules from the community: package.json
*/
var check = require('check-types');
/**
* Internal modules
*/
var _makeRequest = require('./utils/makeRequest');
var _assignParams = require('./utils/assignParams');
var _jsonParser = require('./utils/jsonParser');
var _travelUtils = require('./utils/travelUtils');
var _constants = require('./config/constants');
var ACCEPTED_PARAMS = _constants.ACCEPTED_PARAMS;
var GOOGLEMAPS_ENDPOINTS = _constants.GOOGLEMAPS_ENDPOINTS;
var MAX_REQUEST_LENGTHS = _constants.MAX_REQUEST_LENGTHS;
var METHOD_KEY = 'distance-matrix';
module.exports = function(params, callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be present');
}
if (!(this.config.google_client_id && this.config.google_private_key) && this.config.key == null) {
return callback(new Error('The distance API requires a key. You can add it to the config.'));
}
if (!check.object(params)) {
return callback(new TypeError('params must be an object'));
}
var args = _assignParams({}, params, ACCEPTED_PARAMS[METHOD_KEY]);
// validate distance matrix args
if (args.origins == null) {
return callback(new Error('params.origins is required'));
}
if (args.destinations == null) {
return callback(new Error('params.destinations is required'));
}
// validate common directions/distance-matrix args
try {
_travelUtils.validateCommonArgs(args);
} catch(e) {
return callback(e);
}
_travelUtils.convertTargetTimes(args);
return _makeRequest(this.request, this.config, GOOGLEMAPS_ENDPOINTS[METHOD_KEY], args, _jsonParser(callback), MAX_REQUEST_LENGTHS[METHOD_KEY]);
};