graphdb
Version:
Javascript client library supporting GraphDB and RDF4J REST API.
211 lines (194 loc) • 6.3 kB
JavaScript
"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 BASIC_AUTH = 'BASIC';
var GDB_TOKEN = 'GDB_TOKEN';
var OFF = 'OFF';
/**
* Abstract configuration wrapper used for initialization of concrete
* Client instances. Concrete client configuration wrappers must extend
* this class and override it's methods if necessary.
*
* @abstract
* @author Mihail Radkov
* @author Svilen Velikov
* @author Teodossi Dossev
*/
var ClientConfig = /*#__PURE__*/function () {
/**
* Client configuration constructor.
*
* @param {string} endpoint server base URL that will be prepend
* to all server requests
*/
function ClientConfig(endpoint) {
_classCallCheck(this, ClientConfig);
this.setEndpoint(endpoint);
}
/**
* Sets the default headers map for each HTTP request.
*
* @param {Object<string, string>} headers the default headers
* @return {this} the concrete configuration config for method chaining
*/
return _createClass(ClientConfig, [{
key: "setHeaders",
value: function setHeaders(headers) {
this.headers = headers;
return this;
}
/**
* Returns the default headers for each HTTP request.
*
* @return {Object<string, string>} the default headers map
*/
}, {
key: "getHeaders",
value: function getHeaders() {
return this.headers;
}
/**
* @return {string} the username
*/
}, {
key: "getUsername",
value: function getUsername() {
return this.username;
}
/**
* @return {string} the user password
*/
}, {
key: "getPass",
value: function getPass() {
return this.pass;
}
/**
* @return {boolean} if the user should be re-logged in after token expires
*/
}, {
key: "getKeepAlive",
value: function getKeepAlive() {
return this.keepAlive;
}
/**
* @param {boolean} keepAlive
* @return {this} the concrete configuration config for method chaining
*/
}, {
key: "setKeepAlive",
value: function setKeepAlive(keepAlive) {
this.keepAlive = keepAlive;
return this;
}
/**
* Username and password for user logging setter.
* Sets basic authentication as client authentication type.
*
* @param {string} [username]
* @param {string} [pass]
*
* @return {this} the concrete configuration config for method chaining
*/
}, {
key: "useBasicAuthentication",
value: function useBasicAuthentication(username, pass) {
this.username = username;
this.pass = pass;
this.switchAuthentication(BASIC_AUTH);
return this;
}
/**
* @return {boolean} [basicAuth] if use Basic Auth
*/
}, {
key: "getBasicAuthentication",
value: function getBasicAuthentication() {
return this.basicAuth;
}
/**
* @private
* @param {string} auth authentication type
*/
}, {
key: "switchAuthentication",
value: function switchAuthentication(auth) {
this.basicAuth = auth === BASIC_AUTH;
this.gdbTokenAuth = auth === GDB_TOKEN;
}
/**
* @return {boolean} [gdbTokenAuth] if use Gdb Token Auth
*/
}, {
key: "getGdbTokenAuthentication",
value: function getGdbTokenAuthentication() {
return this.gdbTokenAuth;
}
/**
* Username and password for user logging setter.
* Sets gdb token authentication as client authentication type.
* *
* @param {string} [username]
* @param {string} [pass]
*
* @return {this} the concrete configuration config for method chaining
*/
}, {
key: "useGdbTokenAuthentication",
value: function useGdbTokenAuthentication(username, pass) {
this.username = username;
this.pass = pass;
this.switchAuthentication(GDB_TOKEN);
return this;
}
/**
* Disables authentication.
*/
}, {
key: "disableAuthentication",
value: function disableAuthentication() {
this.switchAuthentication(OFF);
}
/**
* Sets the server's endpoint URL.
*
* @param {string} endpoint the endpoint URL
* @return {this} the current config for method chaining
*/
}, {
key: "setEndpoint",
value: function setEndpoint(endpoint) {
if (endpoint && (typeof endpoint === 'string' || endpoint instanceof String)) {
this.endpoint = endpoint;
return this;
} else {
throw new Error('Invalid Endpoint parameter!');
}
}
/**
* Returns the server's endpoint URL.
* @return {string} the endpoint URL
*/
}, {
key: "getEndpoint",
value: function getEndpoint() {
return this.endpoint;
}
/**
* Returns <code>true</code> if basic or gdb token authentication
* is enabled. <code>false</code> otherwise.
*
* @return {boolean} is authentication enabled
*/
}, {
key: "shouldAuthenticate",
value: function shouldAuthenticate() {
return this.basicAuth || this.gdbTokenAuth;
}
}]);
}();
module.exports = ClientConfig;