UNPKG

hat-javascript-sdk

Version:

HAT JavaScript SDK

66 lines (65 loc) 2.55 kB
"use strict"; // import 'whatwg-fetch'; // import 'promise-polyfill/src/polyfill'; Object.defineProperty(exports, "__esModule", { value: true }); var applications_1 = require("./api/applications"); var jwt_1 = require("./auth/jwt"); var authentication_1 = require("./api/authentication"); var registration_1 = require("./api/registration"); var hat_data_1 = require("./api/hat-data"); var data_debits_1 = require("./api/data-debits"); var HatClient = /** @class */ (function () { function HatClient(config) { this.state = { apiVersion: '', hatDomain: '', secure: true, authenticated: false }; this.mHeaders = [['Content-Type', 'application/json']]; var apiVersion = config.apiVersion, token = config.token, secure = config.secure; if (!token) { throw new Error('Token is undefined'); } if (!apiVersion) { throw new Error('Api version is undefined'); } this.state.apiVersion = apiVersion; var decodedToken = jwt_1.Jwt.decodeToken(token); if (jwt_1.Jwt.isExpired(decodedToken)) { throw new Error('Token has expired'); } else { this.state.authenticated = true; } if (!decodedToken.iss) { throw new Error('Issuer is undefined'); } else { if (secure === false) { } var httpProtocol = secure === false ? 'http://' : 'https://'; this.state.hatDomain = "" + (httpProtocol + decodedToken.iss); } this.mHeaders.push(['X-Auth-Token', token]); this.mHeaders.push(['Host', decodedToken.iss]); } HatClient.authenticate = function (token) { return new HatClient({ token: token, apiVersion: 'v2.6', }); }; HatClient.prototype.applications = function () { return applications_1.applications(this.state, this.mHeaders); }; HatClient.prototype.authentication = function () { return authentication_1.authentication(this.state, this.mHeaders); }; HatClient.prototype.dataDebits = function () { return data_debits_1.dataDebits(this.state, this.mHeaders); }; HatClient.prototype.registration = function () { return registration_1.registration(); }; HatClient.prototype.hatData = function () { return hat_data_1.hatData(this.state, this.mHeaders); }; return HatClient; }()); exports.HatClient = HatClient;