UNPKG

architect-sdk

Version:

Essentialz Architect SDK

103 lines (102 loc) 4.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; var snakecase_keys_1 = __importDefault(require("snakecase-keys")); var camelcase_keys_1 = __importDefault(require("camelcase-keys")); var types_1 = require("./types"); var HttpClient = /** @class */ (function () { function HttpClient(baseUrl, tokenManager, changeCase) { if (changeCase === void 0) { changeCase = true; } this.baseUrl = baseUrl; this.tokenManager = tokenManager; this.changeCase = changeCase; } HttpClient.prototype.get = function (url, init) { var _this = this; return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.get, init)) .then(function (response) { return _this.handleResponse(response); }) .catch(HttpClient.handleError); }; HttpClient.prototype.post = function (url, data, init) { var _this = this; return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.post, init, data)) .then(function (response) { return _this.handleResponse(response); }) .catch(HttpClient.handleError); }; HttpClient.prototype.delete = function (url, init) { var _this = this; return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.delete, init)) .then(function (response) { return _this.handleResponse(response); }) .catch(HttpClient.handleError); }; HttpClient.prototype.put = function (url, data, init) { var _this = this; return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.put, init, data)) .then(function (response) { return _this.handleResponse(response); }) .catch(HttpClient.handleError); }; HttpClient.prototype.getUrl = function (url) { return this.baseUrl + "/" + url; }; HttpClient.prototype.handleRequest = function (url, init) { return fetch(this.getUrl(url), init); }; HttpClient.prototype.handleResponse = function (response) { var _this = this; if (response.ok) { return response.json() .then(function (data) { return _this.prepareResponseData(data); }); } throw new Error(response.status.toString()); }; HttpClient.handleError = function (error) { throw error; }; HttpClient.prototype.getRequestInit = function (method, customInit, data) { var headers = (customInit === null || customInit === void 0 ? void 0 : customInit.headers) ? customInit.headers : this.getHeaders(); var init = { method: method, headers: headers }; if (data) { init.body = this.getBody(data, headers); } return init; }; // This could be extracted to application/json handler HttpClient.prototype.prepareResponseData = function (data) { return this.changeCase ? (0, camelcase_keys_1.default)(data, { deep: true }) : data; }; // This could be extracted to application/json handler HttpClient.prototype.prepareRequestData = function (data) { return this.changeCase ? (0, snakecase_keys_1.default)(data, { deep: true }) : data; }; HttpClient.prototype.getHeaders = function () { var headers = {}; headers['Content-Type'] = 'application/json'; var token = this.tokenManager.getToken(); if (token) { headers.Authorization = "Bearer " + token; } return headers; }; HttpClient.prototype.getBody = function (data, headersInit) { var headers = new Headers(headersInit); switch (headers.get('Content-Type')) { case 'application/json': { return JSON.stringify(this.prepareRequestData(data)); } default: { return data; } } }; return HttpClient; }()); exports.HttpClient = HttpClient;