react-native-google-maps-services
Version:
React Native client library for Google Maps API Web Services
143 lines (131 loc) • 5.87 kB
JavaScript
/**
* @license
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Google Maps Service module.
* @module @google/maps
*/
/**
* Creates a Google Maps client. The client object contains all the API methods.
*
* @param {Object} options
* @param {string} options.key API key (required, unless clientID and
* clientSecret provided).
* @param {string=} options.clientId Maps API for Work client ID.
* @param {string=} options.clientSecret Maps API for Work client secret (a.k.a.
* private key).
* @param {string=} options.channel Maps API for Work channel.
* @param {number=} options.timeout Timeout in milliseconds.
* (Default: 60 * 1000 ms)
* @param {string=} options.language Default language for all queries.
See https://developers.google.com/maps/faq#languagesupport
* @param {number=} options.rate.limit Controls rate-limiting of requests.
* Maximum number of requests per period. (Default: 50)
* @param {number=} options.rate.period Period for rate limit, in milliseconds.
* (Default: 1000 ms)
* @param {number=} options.retryOptions.interval If a transient server error
* occurs, how long to wait before retrying the request, in milliseconds.
* (Default: 500 ms)
* @param {Function=} options.Promise - Promise constructor (optional).
* @return {GoogleMapsClient} The client object containing all API methods.
*/
exports.createClient = function(options) {
options = options || {};
var makeApiCall = require('./internal/make-api-call').inject(options);
/**
* Modified version of util.deprecate from core Node.js modules:
* https://github.com/nodejs/node/blob/a16081cbad61e53f839c261f3aef11a249018653/lib/internal/util.js#L35
* For usage, see the Node.js docs:
* https://nodejs.org/api/util.html#util_util_deprecate_function_string
* In place of process.emitWarning, we use console.warn:
* https://nodejs.org/api/process.html#process_process_emitwarning_warning_type_code_ctor
* @param {Function=} fn The function that is deprecated.
* @param {string=} msg The deprecation warning to emit.
* @param {string=} code A unique identifier for the warning instance being emitted.
*/
var deprecate = function (fn, msg, code) {
if (code !== undefined && typeof code !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'string');
function deprecated(...args) {
if (code !== undefined) {
console.warn(`[${code}] DeprecationWarning: ${msg}`);
} else {
console.warn(`DeprecationWarning: ${msg}`);
}
if (new.target) {
return Reflect.construct(fn, args, new.target);
}
return fn.apply(this, args);
}
// The wrapper will keep the same prototype as fn to maintain prototype chain
Object.setPrototypeOf(deprecated, fn);
if (fn.prototype) {
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
}
return deprecated;
}
var makeApiMethod = function(apiConfig) {
return function(query, callback, customParams) {
query = apiConfig.validator(query);
query.supportsClientId = apiConfig.supportsClientId !== false;
query.options = apiConfig.options;
if (options.language && !query.language) {
query.language = options.language;
}
// Merge query and customParams.
var finalQuery = {};
customParams = customParams || {};
[customParams, query].map(function(obj) {
Object.keys(obj).sort().map(function(key) {
finalQuery[key] = obj[key];
});
});
return makeApiCall(apiConfig, finalQuery, callback);
};
};
var geocode = require('./apis/geocode');
var geolocation = require('./apis/geolocation');
var timezone = require('./apis/timezone');
var directions = require('./apis/directions');
var distanceMatrix = require('./apis/distance-matrix');
var elevation = require('./apis/elevation');
var roads = require('./apis/roads');
var places = require('./apis/places');
return {
directions: makeApiMethod(directions.directions),
distanceMatrix: makeApiMethod(distanceMatrix.distanceMatrix),
elevation: makeApiMethod(elevation.elevation),
elevationAlongPath: makeApiMethod(elevation.elevationAlongPath),
geocode: makeApiMethod(geocode.geocode),
geolocate: makeApiMethod(geolocation.geolocate),
reverseGeocode: makeApiMethod(geocode.reverseGeocode),
places: makeApiMethod(places.places),
placesNearby: makeApiMethod(places.placesNearby),
placesRadar: deprecate(makeApiMethod(places.placesRadar), 'placesRadar is deprecated, see http://goo.gl/BGiumE'),
place: makeApiMethod(places.place),
placesPhoto: makeApiMethod(places.placesPhoto),
placesAutoComplete: makeApiMethod(places.placesAutoComplete),
placesQueryAutoComplete: makeApiMethod(places.placesQueryAutoComplete),
snapToRoads: makeApiMethod(roads.snapToRoads),
nearestRoads: makeApiMethod(roads.nearestRoads),
speedLimits: makeApiMethod(roads.speedLimits),
snappedSpeedLimits: makeApiMethod(roads.snappedSpeedLimits),
timezone: makeApiMethod(timezone.timezone)
};
};