UNPKG

architect-node-sdk

Version:

Essentialz Architect Nodejs SDK

98 lines (97 loc) 3.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; var node_fetch_1 = __importStar(require("node-fetch")); var types_1 = require("./types"); var const_1 = require("../../const"); var HttpClient = /** @class */ (function () { function HttpClient(baseUrl, config) { this.baseUrl = baseUrl; this.config = config; } HttpClient.prototype.get = function (url, init) { return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.get, init)) .then(HttpClient.handleResponse) .catch(HttpClient.handleError); }; HttpClient.prototype.post = function (url, data, init) { return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.post, init, data)) .then(HttpClient.handleResponse) .catch(HttpClient.handleError); }; HttpClient.prototype.delete = function (url, init) { return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.delete, init)) .then(HttpClient.handleResponse) .catch(HttpClient.handleError); }; HttpClient.prototype.put = function (url, data, init) { return this.handleRequest(url, this.getRequestInit(types_1.HttpMethod.put, init, data)) .then(HttpClient.handleResponse) .catch(HttpClient.handleError); }; HttpClient.prototype.getUrl = function (url) { return this.baseUrl + "/" + url; }; HttpClient.prototype.handleRequest = function (url, init) { return (0, node_fetch_1.default)(this.getUrl(url), init); }; HttpClient.handleResponse = function (response) { if (response.ok) { return response.json(); } 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; }; HttpClient.prototype.getHeaders = function () { var headers = {}; headers[const_1.HEADER_KEYS.CONTENT_TYPE] = const_1.CONTENT_TYPES.JSON; if (this.config.developerKey) { headers[const_1.HEADER_KEYS.DEVELOPER_KEY] = this.config.developerKey; } return headers; }; HttpClient.prototype.getBody = function (data, headersInit) { var headers = new node_fetch_1.Headers(headersInit); switch (headers.get(const_1.HEADER_KEYS.CONTENT_TYPE)) { case const_1.CONTENT_TYPES.JSON: { return JSON.stringify(data); } default: { return data; } } }; return HttpClient; }()); exports.HttpClient = HttpClient;