node-heremaps
Version:
Here maps API wrapper for Node.js
90 lines (66 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (request, config, args, uri, callback) {
if (_invalidCredentials(config)) {
return _handleError('You must provide app_id and app_code credentials.');
}
_initConfigs(config, args);
var qString = _buildQString(args);
if (qString.length > REQUEST_MAX_LENGTH) {
return _handleError('Request too long, exceeds ' + REQUEST_MAX_LENGTH + ' characters.');
}
var options = {
uri: _buildURL(uri, qString, { version: config.version || API_VERSION })
};
if (typeof callback !== 'function') {
return options.uri;
}
request(options, function (error, res, data) {
return _handleRes(error, res, data, callback);
});
};
var _qs = require('qs');
var _qs2 = _interopRequireDefault(_qs);
var _checkTypes = require('check-types');
var _checkTypes2 = _interopRequireDefault(_checkTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var REQUEST_MAX_LENGTH = 2048;
var API_VERSION = 6.2;
var GEN = 8;
function _initConfigs(config, args) {
args.app_id = config.app_id;
args.app_code = config.app_code;
args.gen = config.gen || GEN;
}
function _handleRes(error, res, data, callback) {
if (error) {
return callback(error);
}
if (res.statusCode === 200) {
return callback(null, data);
}
var statusError = new Error(data);
statusError.code = res.statusCode;
return callback(statusError, data);
}
function _buildURL(uri, qString) {
return uri + '?' + qString;
}
function _buildQString(args) {
var qsConfig = { indices: false, arrayFormat: 'repeat' };
return _qs2.default.stringify(args, qsConfig);
}
function _handleError(message, callback) {
var error = new Error(message);
if (typeof callback === 'function') {
return callback(error);
}
throw error;
}
function _invalidCredentials(config) {
if (!_checkTypes2.default.assigned(config.app_id) || !_checkTypes2.default.assigned(config.app_code)) {
return true;
}
}