@dataswift/hat-js
Version:
HAT JavaScript SDK for web and Node.js
68 lines (67 loc) • 2.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HatClient = void 0;
var applications_1 = require("./api/applications");
var hat_data_1 = require("./api/hat-data");
var data_debits_1 = require("./api/data-debits");
var authentication_1 = require("./api/authentication");
var registration_1 = require("./api/registration");
var hat_files_1 = require("./api/hat-files");
var HatCustomError_1 = require("./utils/HatCustomError");
var state_1 = require("./state");
var HttpService_1 = require("./services/HttpService");
var HatClient = /** @class */ (function () {
function HatClient(config) {
this.mHeaders = { 'Content-type': 'application/json' };
this.state = new state_1.State(config);
this.httpSvc = new HttpService_1.HttpService(this.state);
this.state.addObserver(this);
if (config.token) {
this.mHeaders['X-Auth-Token'] = config.token;
}
}
HatClient.authenticate = function (token, secure) {
return new HatClient({
token: token,
apiVersion: 'v2.6',
secure: secure,
});
};
HatClient.prototype.update = function (newState) {
if (newState.token) {
this.mHeaders['X-Auth-Token'] = newState.token;
if (this.state.onTokenChange) {
this.state.onTokenChange(newState.token);
}
}
};
HatClient.prototype.auth = function () {
return authentication_1.authentication(this.state, this.mHeaders, this.httpSvc);
};
HatClient.prototype.registration = function () {
return registration_1.registration();
};
HatClient.prototype.applications = function () {
this.isAuthenticated();
return applications_1.applications(this.state, this.mHeaders, this.httpSvc);
};
HatClient.prototype.dataDebits = function () {
this.isAuthenticated();
return data_debits_1.dataDebits(this.state, this.mHeaders, this.httpSvc);
};
HatClient.prototype.hatData = function () {
this.isAuthenticated();
return hat_data_1.hatData(this.state, this.mHeaders, this.httpSvc);
};
HatClient.prototype.files = function () {
this.isAuthenticated();
return hat_files_1.hatFiles(this.state, this.mHeaders, this.httpSvc);
};
HatClient.prototype.isAuthenticated = function () {
if (!this.state.authenticated) {
throw new HatCustomError_1.HatCustomError(HatCustomError_1.HatCustomErrorCauses.hatClientValidation, HatCustomError_1.HatCustomErrorMessages.notAuthenticated);
}
};
return HatClient;
}());
exports.HatClient = HatClient;