UNPKG

stash-connector

Version:

Module to handle and work with Atlassian Stash projects and repositories throug REST API. Admin your repositories and projects in Stash easy. This project is not an Atlassian official project but use the Atlassian Stash REST API

125 lines 4.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HTTP = exports.HTTPResponse = exports.HTTPRequest = void 0; var axios_1 = __importDefault(require("axios")); var types_1 = require("../types"); var utils_1 = require("./utils"); var HTTPRequest = /** @class */ (function () { function HTTPRequest(method, endpoint) { this.CONTENT_TYPE_HEADER = 'Content-Type'; this.JSON_CONTENT_TYPE = 'application/json'; this.FILE_TYPE = 'multipart/form-data'; this.method = 'GET'; this.endpoint = ''; this.method = method; this.endpoint = endpoint; } HTTPRequest.prototype.withBody = function (body) { this.body = body; return this; }; HTTPRequest.prototype.asJson = function () { this.addHeader(this.CONTENT_TYPE_HEADER, this.JSON_CONTENT_TYPE); return this; }; HTTPRequest.prototype.asFile = function () { this.addHeader(this.CONTENT_TYPE_HEADER, this.FILE_TYPE); return this; }; HTTPRequest.prototype.addHeader = function (key, value) { if (!this.headers) { this.headers = {}; } this.headers[key] = value; return this; }; HTTPRequest.prototype.addQueryParam = function (key, value) { if (!this.queryParams) { this.queryParams = {}; } this.queryParams[key] = value; return this; }; HTTPRequest.prototype.execute = function () { if (this.queryParams && utils_1.Utils.hasKeys(this.queryParams)) { this.endpoint += '?' + processQueryParams(this.queryParams); } var options = { method: this.method, url: this.endpoint, headers: this.headers, data: this.body, }; if (this.headers && this.headers[this.CONTENT_TYPE_HEADER] === this.FILE_TYPE) { var formData = new FormData(); formData.append("image", this.body); options.data = formData; } return makeRequest(options); }; return HTTPRequest; }()); exports.HTTPRequest = HTTPRequest; var HTTPResponse = /** @class */ (function () { function HTTPResponse() { this.status = 200; this.statusText = ''; } return HTTPResponse; }()); exports.HTTPResponse = HTTPResponse; var HTTP = /** @class */ (function () { function HTTP() { } HTTP.get = function (endpoint) { return new HTTPRequest('GET', endpoint); }; HTTP.post = function (endpoint) { return new HTTPRequest('POST', endpoint); }; HTTP.put = function (endpoint) { return new HTTPRequest('PUT', endpoint); }; HTTP.delete = function (endpoint) { return new HTTPRequest('DELETE', endpoint); }; return HTTP; }()); exports.HTTP = HTTP; function makeRequest(options) { return new Promise(function (resolve, reject) { axios_1.default.request(options).then(function (response) { var result = new HTTPResponse(); result.status = response.status; result.statusText = response.statusText; result.data = response.data; if (response.headers) { result.headers = {}; for (var _i = 0, _a = Object.keys(response.headers); _i < _a.length; _i++) { var headerKey = _a[_i]; result.headers[headerKey] = response.headers[headerKey]; } } resolve(result); }).catch(function (error) { var stashError = new types_1.StashError(error); reject(stashError); }); }); } function processQueryParams(data) { if (data) { var params = []; for (var _i = 0, _a = Object.keys(data); _i < _a.length; _i++) { var key = _a[_i]; var value = data[key]; params.push(key + '=' + encodeURIComponent(value)); } return params.join('&'); } return ''; } //# sourceMappingURL=http.js.map