Skip to content

Commit c831dcb

Browse files
committed
Add more JSDOC
1 parent fe16751 commit c831dcb

File tree

3 files changed

+94
-35
lines changed

3 files changed

+94
-35
lines changed

helpers/mu/index.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
import { app, errorHandler, beforeExit, exitHandler, setExitHandler } from './server.js';
22
import sparql from './sparql.js';
3+
import { SPARQL, query, update, sparqlEscape, sparqlEscapeString, sparqlEscapeUri, sparqlEscapeDecimal, sparqlEscapeInt, sparqlEscapeFloat, sparqlEscapeDate, sparqlEscapeDateTime, sparqlEscapeBool } from './sparql.js';
34
import { v1 as uuidV1 } from 'uuid';
45

56
// generates a uuid
67
const uuid = uuidV1;
78

89
const mu = {
9-
app: app,
10-
sparql: sparql,
11-
SPARQL: sparql.sparql,
12-
query: sparql.query,
13-
update: sparql.update,
14-
sparqlEscape: sparql.sparqlEscape,
15-
sparqlEscapeString: sparql.sparqlEscapeString,
16-
sparqlEscapeUri: sparql.sparqlEscapeUri,
17-
sparqlEscapeDecimal: sparql.sparqlEscapeDecimal,
18-
sparqlEscapeInt: sparql.sparqlEscapeInt,
19-
sparqlEscapeFloat: sparql.sparqlEscapeFloat,
20-
sparqlEscapeDate: sparql.sparqlEscapeDate,
21-
sparqlEscapeDateTime: sparql.sparqlEscapeDateTime,
22-
sparqlEscapeBool: sparql.sparqlEscapeBool,
10+
app,
11+
sparql,
12+
2313
uuid,
2414
errorHandler,
2515
beforeExit,
2616
exitHandler,
27-
setExitHandler
28-
};
17+
setExitHandler,
2918

30-
const SPARQL = mu.SPARQL,
31-
query = mu.query,
32-
update = mu.update,
33-
sparqlEscape = mu.sparqlEscape,
34-
sparqlEscapeString = mu.sparqlEscapeString,
35-
sparqlEscapeUri = mu.sparqlEscapeUri,
36-
sparqlEscapeInt = mu.sparqlEscapeInt,
37-
sparqlEscapeDecimal = mu.sparqlEscapeDecimal,
38-
sparqlEscapeFloat = mu.sparqlEscapeFloat,
39-
sparqlEscapeDate = mu.sparqlEscapeDate,
40-
sparqlEscapeDateTime = mu.sparqlEscapeDateTime,
41-
sparqlEscapeBool = mu.sparqlEscapeBool;
19+
SPARQL,
20+
query,
21+
update,
22+
sparqlEscape,
23+
sparqlEscapeString,
24+
sparqlEscapeUri,
25+
sparqlEscapeDecimal,
26+
sparqlEscapeInt,
27+
sparqlEscapeFloat,
28+
sparqlEscapeDate,
29+
sparqlEscapeDateTime,
30+
sparqlEscapeBool
31+
};
4232

4333
export {
4434
app,

helpers/mu/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import httpContext from 'express-http-context';
22
import express from 'express';
33
import bodyParser from 'body-parser';
44

5+
/**
6+
* The express JS server
7+
* @type {express.Express}
8+
*/
59
var app = express();
610

711
var bodySizeLimit = process.env.MAX_BODY_SIZE || '100kb';

helpers/mu/sparql.js

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
5569
function 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
*/
6886
function 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+
*/
122148
function 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+
*/
126160
function 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+
*/
130170
function 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+
*/
134180
function 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+
*/
138190
function 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+
*/
142200
function 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
*/
153210
function 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+
*/
167232
function sparqlEscape( value, type ){
168233
switch(type) {
169234
case 'string':

0 commit comments

Comments
 (0)