UNPKG

graphdb

Version:

Javascript client library supporting GraphDB and RDF4J REST API.

362 lines (335 loc) 8.91 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var StringUtils = require('../util/string-utils'); /** * Holds request information applicable to {@link HttpClient}. * * @class * * @author Mihail Radkov * @author Svilen Velikov */ var HttpRequestBuilder = /*#__PURE__*/function () { /** * Does default initialization of the configuration. */ function HttpRequestBuilder() { _classCallCheck(this, HttpRequestBuilder); this.config = {}; } /** * Prepares new builder for HTTP GET request against the provided URL. * * @static * @param {string} url * @return {HttpRequestBuilder} */ return _createClass(HttpRequestBuilder, [{ key: "addHeader", value: /** * Add a new http header entry. Blank values are skipped. * * @param {string} header type * @param {string} value the header value * @return {HttpRequestBuilder} */ function addHeader(header, value) { if (StringUtils.isBlank(value)) { return this; } if (!this.config.headers) { this.config.headers = {}; } this.config.headers[header] = value; return this; } /** * Sets the headers map. * * @param {Object<string, string>} headers the headers map * @return {HttpRequestBuilder} */ }, { key: "setHeaders", value: function setHeaders(headers) { this.config.headers = headers; return this; } /** * Returns the headers map. * * @return {Object<string, string>} */ }, { key: "getHeaders", value: function getHeaders() { return this.config.headers; } /** * Add a specific header of type <code>Accept</code> with the given value. * * @param {string} value * @return {HttpRequestBuilder} */ }, { key: "addAcceptHeader", value: function addAcceptHeader(value) { return this.addHeader('Accept', value); } /** * Add a specific header of type <code>Content-Type</code> with the given * value. * * @param {string} value * @return {HttpRequestBuilder} */ }, { key: "addContentTypeHeader", value: function addContentTypeHeader(value) { return this.addHeader('Content-Type', value); } /** * Add a custom GraphDB header which holds a user password for base * authentication. * * @param {string} value * @return {HttpRequestBuilder} */ }, { key: "addGraphDBPasswordHeader", value: function addGraphDBPasswordHeader(value) { return this.addHeader('x-graphdb-password', value); } /** * Add an Authorization header which holds an authorization token. * * @param {string} value * @return {HttpRequestBuilder} */ }, { key: "addAuthorizationHeader", value: function addAuthorizationHeader(value) { return this.addHeader('authorization', value); } /** * Set request parameters object. * * @param {Object} params * @return {HttpRequestBuilder} */ }, { key: "setParams", value: function setParams(params) { this.config.params = params; return this; } /** * Add a new request param. * * @param {string} param * @param {*} value * @return {HttpRequestBuilder} */ }, { key: "addParam", value: function addParam(param, value) { if (!value) { return this; } if (!this.config.params) { this.config.params = {}; } this.config.params[param] = value; return this; } /** * Returns the request parameters map. * * @return {Object<string, *>} */ }, { key: "getParams", value: function getParams() { return this.config.params; } /** * Set timeout configuration. * * @param {number} timeout in ms * @return {HttpRequestBuilder} */ }, { key: "setTimeout", value: function setTimeout(timeout) { this.config.timeout = timeout; return this; } /** * Returns the request timeout. * * @return {number} */ }, { key: "getTimeout", value: function getTimeout() { return this.config.timeout; } /** * Set a responseType config. * * @param {string} responseType * @return {HttpRequestBuilder} */ }, { key: "setResponseType", value: function setResponseType(responseType) { this.config.responseType = responseType; return this; } /** * Returns the request's response type. * * @return {string} */ }, { key: "getResponseType", value: function getResponseType() { return this.config.responseType; } /** * Sets the data to be sent as request payload. * * @param {*} data the payload * @return {HttpRequestBuilder} */ }, { key: "setData", value: function setData(data) { this.config.data = data; return this; } /** * Gets the data to be sent as payload. * * @return {*} */ }, { key: "getData", value: function getData() { return this.config.data; } /** * Sets the URL against which to perform the request. * * @param {string} url * @return {HttpRequestBuilder} */ }, { key: "setUrl", value: function setUrl(url) { this.config.url = url; return this; } /** * Gets the URL. * * @return {string} */ }, { key: "getUrl", value: function getUrl() { return this.config.url; } /** * Sets the HTTP method. * * @param {string} method * @return {HttpRequestBuilder} */ }, { key: "setMethod", value: function setMethod(method) { this.config.method = method; return this; } /** * Gets the HTTP method. * * @return {string} */ }, { key: "getMethod", value: function getMethod() { return this.config.method; } /** * Getter for the configuration. * @return {Object} */ }, { key: "get", value: function get() { return this.config; } }], [{ key: "httpGet", value: function httpGet(url) { return new HttpRequestBuilder().setMethod('get').setUrl(url); } /** * Prepares new builder for HTTP POST request against the provided URL. * * @static * @param {string} url * @return {HttpRequestBuilder} */ }, { key: "httpPost", value: function httpPost(url) { return new HttpRequestBuilder().setMethod('post').setUrl(url); } /** * Prepares new builder for HTTP PUT request against the provided URL. * * @static * @param {string} url * @return {HttpRequestBuilder} */ }, { key: "httpPut", value: function httpPut(url) { return new HttpRequestBuilder().setMethod('put').setUrl(url); } /** * Prepares new builder for HTTP PATCH request against the provided URL. * * @static * @param {string} url * @return {HttpRequestBuilder} */ }, { key: "httpPatch", value: function httpPatch(url) { return new HttpRequestBuilder().setMethod('patch').setUrl(url); } /** * Prepares new builder for HTTP DELETE request against the provided URL. * * @static * @param {string} url * @return {HttpRequestBuilder} */ }, { key: "httpDelete", value: function httpDelete(url) { return new HttpRequestBuilder().setMethod('delete').setUrl(url); } }]); }(); module.exports = HttpRequestBuilder;