mapbox
Version:
interface to mapbox services
57 lines (45 loc) • 1.7 kB
JavaScript
;
var invariant = require('../vendor/invariant');
var constants = require('./constants');
var client = require('./client');
var getUser = require('./get_user');
/**
* Services all have the same constructor pattern: you initialize them
* with an access token and options, and they validate those arguments
* in a predictable way. This is a constructor-generator that makes
* it possible to require each service's API individually.
*
* @private
* @param {string} name the name of the Mapbox API this class will access:
* this is set to the name of the function so it will show up in tracebacks
* @returns {Function} constructor function
*/
function makeService(name) {
function service(accessToken, options) {
this.name = name;
invariant(typeof accessToken === 'string',
'accessToken required to instantiate Mapbox client');
var endpoint = constants.DEFAULT_ENDPOINT;
if (options !== undefined) {
invariant(typeof options === 'object', 'options must be an object');
if (options.endpoint) {
invariant(typeof options.endpoint === 'string', 'endpoint must be a string');
endpoint = options.endpoint;
}
if (options.account) {
invariant(typeof options.account === 'string', 'account must be a string');
this.owner = options.account;
}
}
this.client = client({
endpoint: endpoint,
accessToken: accessToken
});
this.accessToken = accessToken;
this.endpoint = endpoint;
this.owner = this.owner || getUser(accessToken);
invariant(!!this.owner, 'could not determine account from provided accessToken');
}
return service;
}
module.exports = makeService;