UNPKG

mnubo-sdk

Version:
189 lines (188 loc) 7.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var underscore_1 = require("./utils/underscore"); var http_1 = require("./http/http"); var promiseRetry = require("promise-retry"); var owners_1 = require("./ingestion/owners"); var objects_1 = require("./ingestion/objects"); var events_1 = require("./ingestion/events"); var search_1 = require("./restitution/search"); var bigdata_1 = require("./bigdata/bigdata"); var model_1 = require("./model/model"); var version; try { version = require('../package.json').version; } catch (e) { version = 'unknown'; } var OAuth2Scopes; (function (OAuth2Scopes) { OAuth2Scopes[OAuth2Scopes["ALL"] = 0] = "ALL"; OAuth2Scopes[OAuth2Scopes["LIMITED"] = 1] = "LIMITED"; OAuth2Scopes[OAuth2Scopes["READ"] = 2] = "READ"; OAuth2Scopes[OAuth2Scopes["WRITE"] = 3] = "WRITE"; })(OAuth2Scopes = exports.OAuth2Scopes || (exports.OAuth2Scopes = {})); function isClientCompression(object) { return object.requests !== undefined || object.responses !== undefined; } var AccessToken = /** @class */ (function () { function AccessToken(value, type, expiresIn, jti) { this.value = value; this.type = type; this.expiresIn = expiresIn; this.jti = jti; this.requestedAt = new Date(); } AccessToken.prototype.isValid = function () { var now = new Date(); return now.getTime() < this.requestedAt.getTime() + this.expiresIn * 1000; }; return AccessToken; }()); var Client = /** @class */ (function () { function Client(options) { this.options = options; this.defaultNumberOfAttempts = 5; this.defaultInitialDelay = 500; if (options.env === undefined) { options.env = 'sandbox'; } if (!options.httpOptions) { options.httpOptions = { protocol: 'https', hostname: this.hostname(), port: 443, }; } if (!options.compression) { options.compression = false; } this.owners = new owners_1.Owners(this); this.objects = new objects_1.Objects(this); this.events = new events_1.Events(this); this.search = new search_1.Search(this); this.bigdata = new bigdata_1.Bigdata(this); this.model = new model_1.Model(this); } Client.prototype.hostname = function () { var part = 'sandbox'; if (this.options.env === 'production') { part = 'api'; } return "rest." + part + ".mnubo.com"; }; Client.prototype.getAccessToken = function (scope) { var _this = this; scope = scope || OAuth2Scopes.ALL; var id = this.options.id; var secret = this.options.secret; var payload = "grant_type=client_credentials&scope=" + OAuth2Scopes[scope].toUpperCase(); var options = { path: '/oauth/token', headers: new Map(), }; options.headers.set('Authorization', "Basic " + underscore_1.base64Encode(id + ':' + secret)); options.headers.set('Content-Type', 'application/x-www-form-urlencoded'); options.headers.set('X-MNUBO-SDK', "JavaScript/" + version); Object.assign(options, this.options.httpOptions); return http_1.http.post(options, payload).then(function (data) { _this.token = new AccessToken(data.access_token, data.token_type, data.expires_in, data.jti); }); }; /** * Is the access token still valid? * @return {boolean} false if there is no access token or if it has expired. */ Client.prototype.isAccessTokenValid = function () { return this.token && this.token.isValid(); }; Client.prototype.authenticate = function () { var _this = this; if (this.isAccessTokenValid()) { return Promise.resolve(this.token); } else { return this.getAccessToken().then(function () { return _this.token; }); } }; Client.prototype.buildHttpOptions = function (path, contentType) { var options = { path: path, headers: new Map(), }; var compressionAlgorithm = 'gzip'; var compression = this.options.compression; contentType = contentType || 'application/json'; var token = this.options.token ? this.options.token : this.token.value; options.headers.set('Authorization', "Bearer " + token); options.headers.set('Content-Type', "" + contentType); if (isClientCompression(compression)) { if (compression.requests === true) { options.headers.set('Content-Encoding', compressionAlgorithm); } if (compression.responses === true) { options.headers.set('Accept-Encoding', compressionAlgorithm); } } else if (compression === true) { options.headers.set('Accept-Encoding', compressionAlgorithm); options.headers.set('Content-Encoding', compressionAlgorithm); } Object.assign(options, this.options.httpOptions); return options; }; Client.prototype.possiblyRetry = function (f) { var exponentialBackoff = this.options.exponentialBackoff; if (!exponentialBackoff) { return f().catch(function (err) { return Promise.reject(err.payload); }); } else { var retries = exponentialBackoff.numberOfAttempts ? exponentialBackoff.numberOfAttempts : this.defaultNumberOfAttempts; var minTimeout = exponentialBackoff.initialDelayInMillis ? exponentialBackoff.initialDelayInMillis : this.defaultInitialDelay; var opts = { retries: retries, minTimeout: minTimeout, randomize: true, }; return promiseRetry(function (retry, attempt) { return f().catch(function (err) { if (err.statusCode === 503) { if (exponentialBackoff.onRetry && attempt <= exponentialBackoff.numberOfAttempts) { exponentialBackoff.onRetry(attempt); } return retry(); } else { return Promise.reject(err.payload); } }); }, opts); } }; Client.prototype.get = function (path, contentType) { var _this = this; return this.possiblyRetry(function () { return http_1.http.get(_this.buildHttpOptions(path, contentType)); }); }; Client.prototype.post = function (path, payload, contentType) { var _this = this; return this.possiblyRetry(function () { return http_1.http.post(_this.buildHttpOptions(path, contentType), payload); }); }; Client.prototype.put = function (path, payload, contentType) { var _this = this; return this.possiblyRetry(function () { return http_1.http.put(_this.buildHttpOptions(path, contentType), payload); }); }; Client.prototype.delete = function (path, contentType) { var _this = this; return this.possiblyRetry(function () { return http_1.http.delete(_this.buildHttpOptions(path, contentType)); }); }; return Client; }()); exports.Client = Client; //# sourceMappingURL=mnubo.js.map