@@ -51,7 +51,21 @@ function newSparqlClient(userOptions) {
5151 } ) ;
5252}
5353
54- // executes a query (you can use the template syntax)
54+ /**
55+ * @typedef {Object } QueryOptions
56+ * @property {boolean? } sudo Execute the query as sudo
57+ * @property {string? } scope URI of the scope with whith the query is executed. Use the environment variable `DEFAULT_MU_AUTH_SCOPE` if possible.
58+ */
59+
60+ /**
61+ * Execute a sparql QUERY. Intended for use with QUERY and ASK.
62+ *
63+ * See environment variables for logging: `LOG_SPARQL_ALL`, `LOG_SPARQL_QUERIES`, `DEBUG_AUTH_HEADERS`
64+ *
65+ * @param { string } queryString SPARQL query as a string.
66+ * @param { QueryOptions? } options Operational changes to the SPARQL query.
67+ * @return { Promise<object?> } The response is returned as a parsed JSON object, or null if the response could not be parsed as JSON.
68+ */
5569function query ( queryString , options ) {
5670 if ( LOG_SPARQL_QUERIES ) {
5771 console . log ( queryString ) ;
@@ -60,10 +74,14 @@ function query( queryString, options ) {
6074} ;
6175
6276/**
63- * Executes an update query
77+ * Execute a sparql QUERY.
78+ * Intended for use with `DELETE {} INSERT {} WHERE {}`, `INSERT DATA` and `DELETE DATA`.
79+ *
80+ * See environment variables for logging: `LOG_SPARQL_ALL`, `LOG_SPARQL_UPDATES`, `DEBUG_AUTH_HEADERS`
6481 *
65- * @param { string } queryString String containing SPARQL query for the backend.
66- * @param { object? } options Options to be sent to
82+ * @param { string } queryString SPARQL query as a string.
83+ * @param { QueryOptions? } options Operational changes to the SPARQL query.
84+ * @return { Promise<object?> } The response is returned as a parsed JSON object, or null if the response could not be parsed as JSON.
6785 */
6886function update ( queryString , options ) {
6987 if ( LOG_SPARQL_UPDATES ) {
@@ -119,35 +137,74 @@ function executeQuery( queryString, options ) {
119137 } ) ;
120138}
121139
140+ /**
141+ * Escapes a string for use in SPARQL.
142+ *
143+ * Wraps the string in quotes and escapes necessary characters.
144+ *
145+ * @param {string } value String to be escaped.
146+ * @return {string } Escaped string for use in SPARQL.
147+ */
122148function sparqlEscapeString ( value ) {
123149 return '"""' + value . replace ( / [ \\ " ] / g, function ( match ) { return '\\' + match ; } ) + '"""' ;
124150} ;
125151
152+ /**
153+ * Escapes a URI for use in SPARQL.
154+ *
155+ * Wraps the URI in < and > and escapes necessary characters.
156+ *
157+ * @param {string } value URI string to be escaped.
158+ * @return {string } Escaped URI string for use in SPARQL.
159+ */
126160function sparqlEscapeUri ( value ) {
127161 return '<' + value . replace ( / [ \\ " < > ] / g, function ( match ) { return '\\' + match ; } ) + '>' ;
128162} ;
129163
164+ /**
165+ * Escapes a float for use in SPARQL as xsd:decimal.
166+ *
167+ * @param {string|number } value Number string or value to be escaped.
168+ * @return {string } Escaped number for use in SPARQL.
169+ */
130170function sparqlEscapeDecimal ( value ) {
131171 return '"' + Number . parseFloat ( value ) + '"^^xsd:decimal' ;
132172} ;
133173
174+ /**
175+ * Escapes an integer for use in SPARQL as xsd:integer.
176+ *
177+ * @param {string|number } value Number string or value to be escaped.
178+ * @return {string } Escaped number for use in SPARQL.
179+ */
134180function sparqlEscapeInt ( value ) {
135181 return '"' + Number . parseInt ( value ) + '"^^xsd:integer' ;
136182} ;
137183
184+ /**
185+ * Escapes a number for use in SPARQL as xsd:float.
186+ *
187+ * @param {string|number } value Number string or value to be escaped.
188+ * @return {string } Escaped number for use in SPARQL.
189+ */
138190function sparqlEscapeFloat ( value ) {
139191 return '"' + Number . parseFloat ( value ) + '"^^xsd:float' ;
140192} ;
141193
194+ /**
195+ * Escapes a date string or date object into an xsd:date for use in SPARQL.
196+ *
197+ * @param {string|Date|number } value Number string or value to be escaped.
198+ * @return {string } Escaped number for use in SPARQL.
199+ */
142200function sparqlEscapeDate ( value ) {
143201 return '"' + new Date ( value ) . toISOString ( ) . substring ( 0 , 10 ) + '"^^xsd:date' ; // only keep 'YYYY-MM-DD' portion of the string
144202} ;
145203
146204/**
147- * Escape date string or date object into an xsd:dateTime for use in a SPARQL string .
205+ * Escapes a date string or date object into an xsd:dateTime for use in a SPARQL.
148206 *
149- * @param { Date | string | number } value Date representation
150- * (understood by `new Date`) to convert.
207+ * @param { Date | string | number } value Date representation (understood by `new Date`) to convert.
151208 * @return { string } Date representation for SPARQL query.
152209 */
153210function sparqlEscapeDateTime ( value ) {
@@ -164,6 +221,14 @@ function sparqlEscapeBool( value ){
164221 return value ? '"true"^^xsd:boolean' : '"false"^^xsd:boolean' ;
165222} ;
166223
224+ /**
225+ * Escapes a value based on the supplide type rather than the separately published functions. Prefer to use the
226+ * functions.
227+ *
228+ * @param { "string"|"uri"|"bool"|"decimal"|"int"|"float"|"date"|"dateTime" } type The value to be escaped.
229+ * @param {* } value The value to be escaped.
230+ * @return { string } Boolean representation for SPARQL query.
231+ */
167232function sparqlEscape ( value , type ) {
168233 switch ( type ) {
169234 case 'string' :
0 commit comments