UNPKG

graphdb

Version:

Javascript client library supporting GraphDB and RDF4J REST API.

164 lines (152 loc) 5.42 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 QueryContentType = require('../http/query-content-type'); /** * Base class from which all query payload classes derives. * Subclasses must implement {@link #getSupportedContentTypes} * and may override {@link #validatePayload} if additional validation is needed * according to the subclass. * * @abstract * @class * @author Mihail Radkov * @author Svilen Velikov */ var QueryPayload = /*#__PURE__*/function () { /** * Does basic initialization. */ function QueryPayload() { _classCallCheck(this, QueryPayload); /** * Holds common request parameters applicable to the query endpoint. * * @type {Object} */ this.params = {}; /** * Holds the SPARQL query to be used in the request to the query endpoint. * * @type {string|undefined} */ this.query = undefined; /** * Holds the content type value to be used in the request to the query * endpoint. This value will be set as the HTTP 'Content-Type' header when * sending the request. The value is one of the {@link QueryContentType} * enum values. * * @type {string} */ this.contentType = QueryContentType.X_WWW_FORM_URLENCODED; } /** * @param {boolean} [inference] Specifies whether inferred statements should * be included in the query evaluation. * @return {QueryPayload} */ return _createClass(QueryPayload, [{ key: "setInference", value: function setInference(inference) { if (typeof inference !== 'boolean') { throw new Error('Inference must be a boolean!'); } this.params.infer = inference; return this; } /** * @param {number} [timeout] Specifies a maximum query execution time, in * whole seconds. * @return {QueryPayload} */ }, { key: "setTimeout", value: function setTimeout(timeout) { if (typeof timeout !== 'number') { throw new Error('Timeout must be a number!'); } this.params.timeout = timeout; return this; } /** * An optional parameter which is used for defining the request Content-Type. * * @param {string} [contentType] The value is one of the * {@link QueryContentType} enum values. * * @return {QueryPayload} */ }, { key: "setContentType", value: function setContentType(contentType) { var supportedTypes = this.getSupportedContentTypes(); if (typeof contentType !== 'string' || supportedTypes.indexOf(contentType) === -1) { throw new Error("Content type must be one of ".concat(supportedTypes, "!")); } this.contentType = contentType; return this; } /** * @return {string} content type which was populated in this payload. * The value is one of the {@link QueryContentType} enum values. */ }, { key: "getContentType", value: function getContentType() { return this.contentType; } /** * * @return {Object} the query payload that contains all parameters. */ }, { key: "getParams", value: function getParams() { return this.params; } /** * Sets the SPARQL query string to be used. * * @param {string} query - The query string to set. * @return {QueryPayload} */ }, { key: "setQuery", value: function setQuery(query) { if (typeof query !== 'string') { throw new Error('Query must be a string!'); } this.query = query; return this; } /** * Retrieves the current SPARQL query string. * * @return {string} The currently set SPARQL query string. */ }, { key: "getQuery", value: function getQuery() { return this.query; } /** * * Validates payload for mandatory and invalid parameters. * * @throws {Error} if the payload is misconfigured */ }, { key: "validatePayload", value: function validatePayload() { if (!this.query) { throw new Error('Parameter query is mandatory!'); } } }]); }(); module.exports = QueryPayload;