graphdb
Version:
Javascript client library supporting GraphDB and RDF4J REST API.
434 lines (413 loc) • 20.6 kB
JavaScript
;
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); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var LoggingUtils = require('../logging/logging-utils');
var MediaType = require('../http/media-type');
var HttpRequestBuilder = require('../http/http-request-builder');
var ObjectUtils = require('../util/object-utils');
var Server = require('./server');
var RepositoryClientConfig = require('../repository/repository-client-config');
var RDFRepositoryClient = require('../repository/rdf-repository-client');
// Imports used by TypeScript type generation
var ServerClientConfig = require('./server-client-config');
var RepositoryConfig = require('../repository/repository-config');
var RepositoryType = require('../repository/repository-type');
var HttpResponse = require('../http/http-response');
var AppSettings = require('./app-settings');
var REPOSITORY_SERVICE_URL = '/rest/repositories';
var SECURITY_SERVICE_URL = '/rest/security';
/**
* Extends the {@link Server} with GraphDB API provided by GraphDB.
*
* Used to automate the security user management API:
* add, edit, or remove users. Also used to add, edit,
* or remove a repository to/from any attached location.
* You can work with multiple remote locations from a
* single access point.
*
* @class
* @author Teodossi Dossev
*
*/
var GraphDBServerClient = /*#__PURE__*/function (_Server) {
function GraphDBServerClient() {
_classCallCheck(this, GraphDBServerClient);
return _callSuper(this, GraphDBServerClient, arguments);
}
_inherits(GraphDBServerClient, _Server);
return _createClass(GraphDBServerClient, [{
key: "getRepositoryIDs",
value:
/**
* Retrieves the list of repository IDs from the specified location.
*
* @param {string} [location] - Optional repository location. If provided,
* the request will be executed for
* the specified location.
* @return {Promise<string[]>} A promise that resolves to an array
* of repository IDs.
*/
function getRepositoryIDs(location) {
var locationParameter = this.getLocationParameter(location);
var url = "".concat(REPOSITORY_SERVICE_URL).concat(locationParameter);
var requestBuilder = HttpRequestBuilder.httpGet(url).addAcceptHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder).then(function (response) {
return response.getData().map(function (repository) {
return repository.id;
});
});
}
/**
* Checks if a repository with the provided ID exists.
*
* @param {string} id The ID of the repository to check.
* @param {string} [location] The location of the repository (optional).
*
* @return {Promise<boolean>} A promise that resolves with a boolean value
* indicating whether the repository exists.
*/
}, {
key: "hasRepository",
value: function hasRepository(id, location) {
if (!id) {
throw new Error('Repository id is required parameter!');
}
return this.getRepositoryIDs(location).then(function (repositories) {
return repositories.indexOf(id) !== -1;
});
}
/**
* Creates a repository client instance with the provided id and
* configuration.
* @param {string} id of the repository
* @param {RepositoryClientConfig} config for the overridable repository
* configuration.
* @return {Promise<RDFRepositoryClient>} promise which resolves with
* new RDFRepositoryClient instance.
*/
}, {
key: "getRepository",
value: function getRepository(id, config) {
var _this = this;
if (!id) {
throw new Error('Repository id is required parameter!');
}
if (!config || !(config instanceof RepositoryClientConfig)) {
throw new Error('RepositoryClientConfig is required parameter!');
}
return this.hasRepository(id).then(function (exists) {
if (exists) {
return new RDFRepositoryClient(config);
}
_this.logger.error({
repoId: id
}, 'Repository does not exist');
return Promise.reject(new Error("Repository with id ".concat(id, " does not exists.")));
});
}
/**
* Deletes the repository with the provided ID.
*
* @param {string} id The ID of the repository to delete.
* @param {string} [location] The location of the repository (optional).
*
* @return {Promise<any>} A promise that resolves with the result of
* the delete operation.
*/
}, {
key: "deleteRepository",
value: function deleteRepository(id, location) {
var _this2 = this;
if (!id) {
throw new Error('Repository id is required parameter!');
}
var loc = this.getLocationParameter(location);
var requestBuilder = HttpRequestBuilder.httpDelete("".concat(REPOSITORY_SERVICE_URL, "/").concat(id).concat(loc));
return this.execute(requestBuilder).then(function (response) {
_this2.logger.info(LoggingUtils.getLogPayload(response, {
repoId: id
}), 'Deleted repository');
});
}
/**
* Get the default repository configuration for the repository type
*
* @param {RepositoryType|String} repositoryType the type for which a
* default configuration is required
*
* @return {Promise<HttpResponse|Error>} a promise which resolves to response
* wrapper or rejects with error if thrown during execution.
*/
}, {
key: "getDefaultConfig",
value: function getDefaultConfig(repositoryType) {
if (!repositoryType) {
throw new Error('Repository type is required parameter!');
}
var requestBuilder = HttpRequestBuilder.httpGet("".concat(REPOSITORY_SERVICE_URL, "/default-config/").concat(repositoryType)).addAcceptHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder);
}
/**
* Retrieves the configuration of a repository.
*
* @param {string} repositoryId The ID of the repository whose configuration
* is to be retrieved.
* @param {string} [location] The optional location of the repository.
*
* @return {Promise<HttpResponse|string|Error>} A promise that resolves to
* the response wrapper, or rejects with an error if one occurs
* during the execution.
*/
}, {
key: "getRepositoryConfig",
value: function getRepositoryConfig(repositoryId, location) {
if (!repositoryId) {
throw new Error('Repository id is required parameter!');
}
var loc = this.getLocationParameter(location);
var requestBuilder = HttpRequestBuilder.httpGet("".concat(REPOSITORY_SERVICE_URL, "/").concat(repositoryId).concat(loc)).addContentTypeHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder);
}
/**
* Download the repository configuration in turtle format
*
* @param {string} repositoryId the repository id
* @param {string} [location] optional repository location
*
* @return {Promise<string | any>} a service request that will resolve to a
* readable stream to which the client can subscribe and consume the emitted
* strings as soon as they are available. Resolves to turtle format.
*/
}, {
key: "downloadRepositoryConfig",
value: function downloadRepositoryConfig(repositoryId, location) {
var _this3 = this;
var loc = this.getLocationParameter(location);
var requestBuilder = HttpRequestBuilder.httpGet("".concat(REPOSITORY_SERVICE_URL, "/").concat(repositoryId, "/download-ttl").concat(loc)).addContentTypeHeader(MediaType.TEXT_TURTLE).setResponseType('stream');
return this.execute(requestBuilder).then(function (response) {
_this3.logger.debug(LoggingUtils.getLogPayload(response, requestBuilder.getParams()), 'Downloaded data');
return response.getData();
});
}
/**
* Creates a repository based on the provided configuration.
*
* @param {RepositoryConfig} repositoryConfig The configuration of
* the repository to be created.
*
* @return {Promise<HttpResponse|Error>} A promise that resolves to
* the response wrapper, or rejects with an error if one occurs
* during execution.
*/
}, {
key: "createRepository",
value: function createRepository(repositoryConfig) {
var location = this.getLocationParameter(repositoryConfig.location);
var requestBuilder = HttpRequestBuilder.httpPut("".concat(REPOSITORY_SERVICE_URL, "/").concat(repositoryConfig.id).concat(location)).setData(this.objectToJson(repositoryConfig)).addContentTypeHeader(MediaType.APPLICATION_JSON).addAcceptHeader(MediaType.TEXT_PLAIN);
return this.execute(requestBuilder);
}
/**
* Checks if GraphDB security is enabled.
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "isSecurityEnabled",
value: function isSecurityEnabled() {
var requestBuilder = HttpRequestBuilder.httpGet(SECURITY_SERVICE_URL).addAcceptHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder);
}
/**
* Enable or disable GraphDB security.
* @param {boolean} enabled <code>true</code> if security is enabled and
* <code>false</code> otherwise.
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "toggleSecurity",
value: function toggleSecurity(enabled) {
var requestBuilder = HttpRequestBuilder.httpPost("".concat(SECURITY_SERVICE_URL, "?useSecurity=").concat(enabled)).addContentTypeHeader(MediaType.APPLICATION_JSON).setData("".concat(enabled));
return this.execute(requestBuilder);
}
/**
* Enable or disable access to a predefined set of functionalities
* without having to log in.
* To use free access, you must have security enabled.
* Use with extreme caution, as the changes that are made to the
* application settings may possibly change the behavior of the
* GraphDB Workbench for the logged-in user or for all users
* if logged in as admin.
* @param {boolean} enabled <code>true</code> if free access is enabled and
* <code>false</code> otherwise.
* @param {string[]} authorities Array of read and/or write access rights
* described in the following template:
* <code>READ_REPO_{repository ID}</code> to grant repository read rights
* <code>WRITE_REPO_{repository ID}</code> to grant repository write rights
* @param {AppSettings} appSettings configure the default behavior
* of the GraphDB Workbench
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "updateFreeAccess",
value: function updateFreeAccess(enabled, authorities, appSettings) {
var requestBuilder = HttpRequestBuilder.httpPost("".concat(SECURITY_SERVICE_URL, "/free-access")).addContentTypeHeader(MediaType.APPLICATION_JSON).addAcceptHeader(MediaType.TEXT_PLAIN).setData({
appSettings: this.objectToJson(appSettings),
authorities: authorities,
enabled: enabled
});
return this.execute(requestBuilder);
}
/**
* Check if free access is enabled
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "getFreeAccess",
value: function getFreeAccess() {
var requestBuilder = HttpRequestBuilder.httpGet("".concat(SECURITY_SERVICE_URL, "/free-access")).addContentTypeHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder);
}
/**
* Create a user
* @param {string} username User name
* @param {string} password User password
* @param {string[]} [grantedAuthorities] Array of read and/or write access
* rights described in the following template:
* <code>READ_REPO_{repository ID}</code> to grant repository read rights
* <code>WRITE_REPO_{repository ID}</code> to grant repository write rights
* @param {AppSettings} [appSettings] configure the default behavior
* of the GraphDB Workbench
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "createUser",
value: function createUser(username, password, grantedAuthorities, appSettings) {
var requestBuilder = HttpRequestBuilder.httpPost("".concat(SECURITY_SERVICE_URL, "/users/").concat(username)).addContentTypeHeader(MediaType.APPLICATION_JSON).addAcceptHeader(MediaType.TEXT_PLAIN).setData({
username: username,
password: password,
grantedAuthorities: grantedAuthorities,
appSettings: this.objectToJson(appSettings)
});
return this.execute(requestBuilder);
}
/**
* Edit user.
* Use with extreme caution, as the changes that are made to the
* application settings may possibly change the behavior of the
* GraphDB Workbench for the user.
* @param {string} username User name
* @param {string} [password] User password
* @param {string[]} [grantedAuthorities] Array of read and/or write access
* rights described in the following template:
* <code>READ_REPO_{repository ID}</code> to grant repository read rights
* <code>WRITE_REPO_{repository ID}</code> to grant repository write rights
* @param {AppSettings} [appSettings] configure the default behavior
* of the GraphDB Workbench
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "updateUser",
value: function updateUser(username, password, grantedAuthorities, appSettings) {
var requestBuilder = HttpRequestBuilder.httpPut("".concat(SECURITY_SERVICE_URL, "/users/").concat(username)).addContentTypeHeader(MediaType.APPLICATION_JSON).addAcceptHeader(MediaType.TEXT_PLAIN).setData({
username: username,
password: password,
grantedAuthorities: grantedAuthorities,
appSettings: this.objectToJson(appSettings)
});
return this.execute(requestBuilder);
}
/**
* Change setting for a logged user.
* Use with extreme caution, as the changes that are made to the
* application settings may possibly change the behavior of the
* GraphDB Workbench for the user.
* @param {string} username User name
* @param {string} [password] User password
* @param {AppSettings} [appSettings] configure the default behavior
* of the GraphDB Workbench
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "updateUserData",
value: function updateUserData(username, password, appSettings) {
var requestBuilder = HttpRequestBuilder.httpPatch("".concat(SECURITY_SERVICE_URL, "/users/").concat(username)).addContentTypeHeader(MediaType.APPLICATION_JSON).addAcceptHeader(MediaType.TEXT_PLAIN).setData({
username: username,
password: password,
appSettings: this.objectToJson(appSettings)
});
return this.execute(requestBuilder);
}
/**
* Get a user
* @param {string} username User name
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "getUser",
value: function getUser(username) {
var requestBuilder = HttpRequestBuilder.httpGet("".concat(SECURITY_SERVICE_URL, "/users/").concat(username)).addContentTypeHeader(MediaType.APPLICATION_JSON);
return this.execute(requestBuilder);
}
/**
* Deletes a user
* @param {string} username User name
* @return {Promise<HttpResponse|Error>} a promise which resolves
* to response wrapper or rejects with error if thrown during execution.
*/
}, {
key: "deleteUser",
value: function deleteUser(username) {
var requestBuilder = HttpRequestBuilder.httpDelete("".concat(SECURITY_SERVICE_URL, "/users/").concat(username)).addAcceptHeader(MediaType.TEXT_PLAIN);
return this.execute(requestBuilder);
}
/**
* @private
* @param {Object} object to get json from
* @return {string | {}} json representation of object
* or empty object if undefined
*/
}, {
key: "objectToJson",
value: function objectToJson(object) {
if (object && typeof object.toJson === 'function') {
return object.toJson();
}
return {};
}
/**
* Returns the query parameter for the repository location if it's provided,
* otherwise returns an empty string.
*
* @private
*
* @param {string} [location] The location of the repository.
*
* @return {string} The query string representing the location parameter,
* or an empty string if the location is null or undefined.
*/
}, {
key: "getLocationParameter",
value: function getLocationParameter(location) {
return ObjectUtils.isNullOrUndefined(location) ? '' : "?location=".concat(location);
}
}]);
}(Server);
module.exports = GraphDBServerClient;