@trimble-oss/trimble-id
Version:
Trimble Identity SDK for JavaScript/ TypeScript
76 lines (71 loc) • 2.62 kB
JavaScript
;
// implements IHttpClientProvider
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// AMD
define(['./HttpClient', './AnalyticsHttpClient'], (HttpClient, AnalyticsHttpClient) => {
return factory({
HttpClient: HttpClient,
AnalyticsHttpClient: AnalyticsHttpClient
});
});
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory({
HttpClient: require('./HttpClient'),
AnalyticsHttpClient: require('./AnalyticsHttpClient'),
});
} else {
// Browser globals (Note: root is window)
root.BearerTokenHttpClientProvider = factory(root);
}
}(this, function (imports) {
/**
* @description Http client provider for API using bearer token authorization
* @param tokenProvider can be a token provider like ClientCredentialTokenProvider
* @param {string} baseAddress
*/
var BearerTokenHttpClientProvider = function (tokenProvider, baseAddress) {
this._tokenProvider = tokenProvider;
this._baseAddress = baseAddress;
//Send analytics
this._analyticshttpclient = imports.AnalyticsHttpClient;
this._analyticshttpclient.sendInitEvent(this.constructor.name);
};
/**
* @function
* @name RetrieveClaimset
* @returns {PromiseLike<any>} A promise that retrieves the client on completion
*/
BearerTokenHttpClientProvider.prototype.RetrieveClient = function () {
//Send analytics
this._analyticshttpclient.sendMethodEvent("RetrieveClient");
var self = this;
return new Promise(function (resolve, reject) {
self._tokenProvider.RetrieveToken()
.then((token) => {
var settings = {
headers: {
Authorization: 'Bearer ' + token,
}
};
resolve(new imports.HttpClient(self._baseAddress, settings));
})
.catch(reject);
});
};
/**
* @function
* @name RetrieveConsumerKey
* @returns {string} It retrieves the consumer key of the application.
*/
BearerTokenHttpClientProvider.prototype.RetrieveConsumerKey = function () {
if (this._tokenProvider != null) {
return this._tokenProvider._consumerKey;
}
return null;
};
// Exposed public methods
return BearerTokenHttpClientProvider;
}));