graphdb
Version:
Javascript client library supporting GraphDB and RDF4J REST API.
247 lines (233 loc) • 11.7 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 _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); 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 QueryPayload = require('../query/query-payload');
var QueryType = require('../query/query-type');
var QueryLanguage = require('../query/query-language');
var RDFMimeType = require('../http/rdf-mime-type');
var QueryContentType = require('../http/query-content-type');
var SELECT_QUERY_RESULT_TYPES = [RDFMimeType.SPARQL_RESULTS_XML, RDFMimeType.SPARQL_RESULTS_JSON, RDFMimeType.BINARY_RDF_RESULTS_TABLE, RDFMimeType.BOOLEAN_RESULT, RDFMimeType.SPARQL_STAR_RESULTS_JSON, RDFMimeType.SPARQL_STAR_RESULTS_TSV];
var ASK_QUERY_RESULT_TYPES = [RDFMimeType.SPARQL_RESULTS_XML, RDFMimeType.SPARQL_RESULTS_JSON, RDFMimeType.BOOLEAN_RESULT];
var RDF_FORMATS = [RDFMimeType.RDF_XML, RDFMimeType.N_TRIPLES, RDFMimeType.TURTLE, RDFMimeType.N3, RDFMimeType.N_QUADS, RDFMimeType.JSON_LD, RDFMimeType.RDF_JSON, RDFMimeType.TRIX, RDFMimeType.TRIG, RDFMimeType.BINARY_RDF, RDFMimeType.TURTLE_STAR, RDFMimeType.TRIG_STAR];
var QUERY_OPERATION_TYPES = [QueryContentType.X_WWW_FORM_URLENCODED, QueryContentType.SPARQL_QUERY];
var QUERY_TO_RESPONSE_TYPE_FORMATS_MAPPING = {
SELECT: SELECT_QUERY_RESULT_TYPES,
DESCRIBE: RDF_FORMATS,
CONSTRUCT: RDF_FORMATS,
ASK: ASK_QUERY_RESULT_TYPES
};
/**
* Payload object holding common request parameters applicable for
* the query endpoint and SPARQL query as well.
*
* The SPARQL query and parameters "queryType" and "responseType" are mandatory
*
* The content type parameter, which is used for setting the HTTP Content-Type
* header, can be one of the following:
* - <code>application/x-www-form-urlencoded</code>
* - <code>application/sparql-query</code>
*
* @class
* @author Mihail Radkov
* @author Svilen Velikov
*/
var GetQueryPayload = /*#__PURE__*/function (_QueryPayload) {
function GetQueryPayload() {
_classCallCheck(this, GetQueryPayload);
return _callSuper(this, GetQueryPayload, arguments);
}
_inherits(GetQueryPayload, _QueryPayload);
return _createClass(GetQueryPayload, [{
key: "setQueryLn",
value:
/**
* @param {string} [queryLn] the query language that is used for the query.
* @return {GetQueryPayload}
* @throws {Error} if the query language is not one of {@link QueryLanguage}
*/
function setQueryLn(queryLn) {
var supportedLanguages = Object.values(QueryLanguage);
if (typeof queryLn !== 'string' || supportedLanguages.indexOf(queryLn) === -1) {
throw new Error("Query language must be one of ".concat(supportedLanguages, "!"));
}
this.params.queryLn = queryLn;
return this;
}
/**
* Populates an optional $key:value binding in the payload. Existing bindings
* will be overridden.
*
* @param {string} [binding] A variable binding name which may appear in the
* query and can be bound to a specific value provided outside
* of the actual query.
* @param {string} [value] A variable's binding value. See the binding comment
* @return {GetQueryPayload}
* @throws {Error} if the binding or the value is not a string
*/
}, {
key: "addBinding",
value: function addBinding(binding, value) {
if (typeof binding !== 'string' || typeof value !== 'string') {
throw new Error('Binding and value must be strings!');
}
this.params[binding] = value;
return this;
}
/**
* @param {boolean} [distinct] Specifies if only distinct query solutions
* should be returned.
* @return {GetQueryPayload}
* @throws {Error} if the parameter is not a boolean
*/
}, {
key: "setDistinct",
value: function setDistinct(distinct) {
if (typeof distinct !== 'boolean') {
throw new Error('Distinct must be a boolean!');
}
this.params.distinct = distinct;
return this;
}
/**
* @param {number} limit specifies the maximum number of query solutions to
* return.
* @return {GetQueryPayload}
* @throws {Error} if the limit is not a non negative number
*/
}, {
key: "setLimit",
value: function setLimit(limit) {
if (typeof limit !== 'number' || limit < 0) {
throw new Error('Limit must be a non negative number!');
}
this.params.limit = limit;
return this;
}
/**
* @param {number} [offset] Specifies the number of query solutions to skip.
* @return {GetQueryPayload}
* @throws {Error} if the offset is not a non negative number
*/
}, {
key: "setOffset",
value: function setOffset(offset) {
if (typeof offset !== 'number' || offset < 0) {
throw new Error('Offset must be a non negative number!');
}
this.params.offset = offset;
return this;
}
/**
* @inheritDoc
*/
}, {
key: "validatePayload",
value: function validatePayload() {
_superPropGet(GetQueryPayload, "validatePayload", this, 3)([]);
if (!this.getQueryType()) {
throw new Error('Parameter queryType is mandatory!');
}
if (!this.getResponseType()) {
throw new Error('Parameter responseType is mandatory!');
}
var responseType = this.getResponseType();
var allowedFormats = QUERY_TO_RESPONSE_TYPE_FORMATS_MAPPING[this.getQueryType()];
if (!this.isResponseTypeSupported(responseType, allowedFormats)) {
throw new Error("Invalid responseType=".concat(responseType, "\n for ").concat(this.getQueryType(), " query! Must be one of ").concat(allowedFormats));
}
}
/**
* Verifies that responseType is one of the expected types.
*
* @private
* @param {string} responseType
* @param {Array<string>} formats
* @return {boolean} true if responseType is one of the expected types and
* false otherwise.
*/
}, {
key: "isResponseTypeSupported",
value: function isResponseTypeSupported(responseType, formats) {
return formats.indexOf(responseType) !== -1;
}
// -----------------------------------------------------
// Configuration properties get/set methods follow below
// -----------------------------------------------------
/**
* A mandatory parameter which is used for resolving the Accept http header
* required by the RDF store.
*
* @param {string} responseType
* @return {GetQueryPayload}
* @throws {Error} if the response type is not one of {@link RDFMimeType}
*/
}, {
key: "setResponseType",
value: function setResponseType(responseType) {
var supportedTypes = Object.values(RDFMimeType);
if (typeof responseType !== 'string' || supportedTypes.indexOf(responseType) === -1) {
throw new Error("Response type must be one of ".concat(supportedTypes, "!"));
}
this.responseType = responseType;
return this;
}
/**
* @return {string} response type which was populated in this payload.
*/
}, {
key: "getResponseType",
value: function getResponseType() {
return this.responseType;
}
/**
* A mandatory parameter used for resolving request headers and resolving
* the response parsers.
*
* @param {QueryType} queryType
* @return {GetQueryPayload}
* @throws {Error} if the query type is not one of {@link QueryType}
*/
}, {
key: "setQueryType",
value: function setQueryType(queryType) {
var supportedTypes = Object.values(QueryType);
if (typeof queryType !== 'string' || supportedTypes.indexOf(queryType) === -1) {
throw new Error("Query type must be one of ".concat(supportedTypes, "!"));
}
this.queryType = queryType;
return this;
}
/**
* @return {string} query type which was populated in this payload. The value
* is one of the {@link QueryType} enum values.
*/
}, {
key: "getQueryType",
value: function getQueryType() {
return this.queryType;
}
/**
* @inheritDoc
*/
}, {
key: "getSupportedContentTypes",
value: function getSupportedContentTypes() {
return QUERY_OPERATION_TYPES;
}
}]);
}(QueryPayload);
module.exports = GetQueryPayload;