'use strict';
var BraintreeError = require('../lib/braintree-error');
var Client = require('./client');
var getConfiguration = require('./get-configuration').getConfiguration;
var VERSION = process.env.npm_package_version;
var Promise = require('../lib/promise');
var wrapPromise = require('@braintree/wrap-promise');
var sharedErrors = require('../lib/errors');
var cachedClients = {};
/** @module braintree-web/client */
/**
* @function create
* @description This function is the entry point for the <code>braintree.client</code> module. It is used for creating {@link Client} instances that service communication to Braintree servers.
* @param {object} options Object containing all {@link Client} options:
* @param {string} options.authorization A tokenizationKey or clientToken.
* @param {callback} [callback] The second argument, <code>data</code>, is the {@link Client} instance.
* @returns {Promise|void} Returns a promise if no callback is provided.
* @example
* var createClient = require('braintree-web/client').create;
*
* createClient({
* authorization: CLIENT_AUTHORIZATION
* }, function (createErr, clientInstance) {
* // ...
* });
* @static
*/
function create(options) {
if (!options.authorization) {
return Promise.reject(new BraintreeError({
type: sharedErrors.INSTANTIATION_OPTION_REQUIRED.type,
code: sharedErrors.INSTANTIATION_OPTION_REQUIRED.code,
message: 'options.authorization is required when instantiating a client.'
}));
}
if (cachedClients[options.authorization]) {
return Promise.resolve(cachedClients[options.authorization]);
}
return getConfiguration(options).then(function (configuration) {
var client;
if (options.debug) {
configuration.isDebug = true;
}
client = new Client(configuration);
cachedClients[options.authorization] = client;
return client;
});
}
// Primarily used for testing the client create call
function clearCache() {
cachedClients = {};
}
module.exports = {
create: wrapPromise(create),
/**
* @description The current version of the SDK, i.e. `{@pkg version}`.
* @type {string}
*/
VERSION: VERSION,
_clearCache: clearCache
};