baasic-sdk-javascript
Version:
JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).
171 lines (170 loc) • 7.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var inversify_1 = require("inversify");
var _1 = require("./");
var contracts_1 = require("../core/contracts");
var common_1 = require("../common");
var ApiClient = /** @class */ (function () {
function ApiClient(appOptions, httpClient, tokenHandler, halParser) {
this.appOptions = appOptions;
this.httpClient = httpClient;
this.tokenHandler = tokenHandler;
this.halParser = halParser;
this.wwwAuthenticateTokenizer = (function () {
var ws = '(?:(?:\\r\\n)?[ \\t])+', token = '(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2E\\x30-\\x39\\x3F\\x41-\\x5A\\x5E-\\x7A\\x7C\\x7E]+)', quotedString = '"(?:[\\x00-\\x0B\\x0D-\\x21\\x23-\\x5B\\\\x5D-\\x7F]|' + ws + '|\\\\[\\x00-\\x7F])*"';
return new RegExp(token + '(?:=(?:' + quotedString + '|' + token + '))?', 'g');
})();
this.createPromise = httpClient.createPromise;
}
ApiClient_1 = ApiClient;
ApiClient.prototype.request = function (request) {
var _this = this;
if (request && request.url) {
request.url = this.compileUrl(request.url);
}
var headers = request.headers || (request.headers = {});
var authToken = this.tokenHandler.get();
var syncToken = false;
if (authToken) {
syncToken = authToken.sliding_window !== undefined;
/*jshint camelcase: false */
headers["AUTHORIZATION"] = "BEARER " + authToken.token;
}
if (request.data && !this.headerExists(headers, 'Content-Type')) {
if (!(request.data instanceof FormData)) {
headers['Content-Type'] = 'application/json; charset=UTF-8';
}
}
if (this.appOptions.enableHALJSON) {
if (!this.headerExists(headers, 'Accept')) {
headers["Accept"] = 'application/hal+json; charset=UTF-8';
}
}
var promise = this.httpClient.request(request);
promise.then(function (data) {
if (syncToken)
_this.tokenHandler.store(authToken);
var contentType = _this.getHeader(data.headers, 'Content-Type');
if (contentType && contentType.toLowerCase().indexOf('application/hal+json') !== -1) {
data.data = _this.halParser.parse(data.data);
}
return data;
}, function (response) {
if (response.statusCode === 401) {
var err = response.data;
if (err && err.details) {
switch (err.details) {
case 'invalid_token':
_this.tokenHandler.store(null);
break;
case 'invalid_request':
/*jshint camelcase: false */
switch (err.message) {
/*jshint camelcase: true */
case 'Missing or invalid session':
_this.tokenHandler.store(null);
break;
}
break;
}
}
}
return response;
});
return promise;
};
ApiClient.prototype.get = function (url, headers, abortSignal) {
return this.internalRequest(url, "GET", undefined, headers, abortSignal);
};
ApiClient.prototype.delete = function (url, headers, data, abortSignal) {
return this.internalRequest(url, "DELETE", data, headers, abortSignal);
};
ApiClient.prototype.post = function (url, data, headers, abortSignal) {
return this.internalRequest(url, "POST", data, headers, abortSignal);
};
ApiClient.prototype.put = function (url, data, headers, abortSignal) {
return this.internalRequest(url, "PUT", data, headers, abortSignal);
};
ApiClient.prototype.patch = function (url, data, headers, abortSignal) {
return this.internalRequest(url, "PATCH", data, headers, abortSignal);
};
ApiClient.prototype.compileUrl = function (url) {
if (!ApiClient_1.httpTest.test(url)) {
var rootUrl = this.appOptions.apiUrl;
if (url.indexOf(rootUrl) < 0) {
return "" + rootUrl + url;
}
}
return url;
};
ApiClient.prototype.internalRequest = function (url, method, data, headers, abortSignal) {
url = this.compileUrl(url);
var request = {
url: url,
method: method
};
if (data) {
request.data = data;
}
if (headers) {
request.headers = headers;
}
if (abortSignal) {
request.abortSignal = abortSignal;
}
return this.request(request);
};
ApiClient.prototype.unquote = function (quotedString) {
return quotedString.substr(1, quotedString.length - 2).replace(/(?:(?:\r\n)?[ \t])+/g, ' ');
};
ApiClient.prototype.headerExists = function (headers, key) {
return headers && (headers.hasOwnProperty(key) || headers.hasOwnProperty(key.toLowerCase()));
};
ApiClient.prototype.getHeader = function (headers, key) {
if (headers) {
var header = headers[key] || headers[key.toLowerCase()];
if (Array.isArray(header)) {
header = header.join(';');
}
return header;
}
return undefined;
};
ApiClient.prototype.parseWWWAuthenticateHeader = function (value) {
var tokens;
if (typeof value === 'string') {
tokens = value.match(this.wwwAuthenticateTokenizer);
}
else if (Array.isArray(value)) {
tokens = value[0].split(' ').concat(value.slice(1));
}
if (tokens && tokens.length > 0) {
var wwwAutheniticate = {
scheme: tokens[0]
};
if (tokens.length > 1) {
var details = {};
for (var i = 1, l = tokens.length; i < l; i++) {
var values = tokens[i].split('=');
details[values[0]] = this.unquote(values[1]);
}
wwwAutheniticate.details = details;
}
return wwwAutheniticate;
}
};
var ApiClient_1;
ApiClient.httpTest = new RegExp("^(http|https)://", "i");
ApiClient = ApiClient_1 = tslib_1.__decorate([
inversify_1.injectable(),
tslib_1.__param(0, inversify_1.inject(contracts_1.TYPES.IAppOptions)),
tslib_1.__param(1, inversify_1.inject(_1.httpTYPES.IHttpClient)),
tslib_1.__param(2, inversify_1.inject(contracts_1.TYPES.ITokenHandler)),
tslib_1.__param(3, inversify_1.inject(common_1.TYPES.IHALParser)),
tslib_1.__metadata("design:paramtypes", [Object, Object, Object, Object])
], ApiClient);
return ApiClient;
}());
exports.ApiClient = ApiClient;
;