Skip to content

Commit 8f56009

Browse files
committed
Apply SPARQL escape helpers on interpolated template variables
1 parent 47224bf commit 8f56009

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM semtech/mu-javascript-template:1.0.0
1+
FROM semtech/mu-javascript-template:1.1.0
22

33
MAINTAINER Erika Pauwels <erika.pauwels@gmail.com>
44

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Each element in the array will become an API endpoint in the microservice. An ex
3939
## Using variables in the export query
4040
You can also define variables in your SPARQL export query which will be replaced with query param values at runtime. Therefore, we use [ES6's tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals).
4141

42-
Import the `template` tag function from `/app/template.js` in your `export.js` and apply the tag function on your query. You can then define variables in your SPARQL query using `${'myVariable'}` (note the quotes around the variable name). At runtime the variable will be replaced with the value provided in the `myVariable` query param of the request.
42+
Import the `template` tag function from `/app/template.js` in your `export.js` and apply the tag function on your query. You can then define variables in your SPARQL query using `${['myVariable', 'type']}`. `myVariable` is the name of the variable that must be replaced. `type` is the datatype of the variable. This must be one of `'string'`, `'uri'`, `'int'`, `'float'`, `'date'`, `'dateTime'`, `'bool'`. At runtime the variable will be replaced with the value provided in the `myVariable` query param of the request.
4343

4444
An example `export.js` including variables file may look as follows:
4545

app.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,40 @@ import request from 'request';
44
import queries from '/config/export';
55

66
queries.map(function(config) {
7-
app.get(config.path, function(req, res, next) {
8-
const options = {
9-
method: 'POST',
10-
url: process.env.MU_SPARQL_ENDPOINT,
11-
encoding: 'utf8',
12-
headers: {
13-
'Accept': config.format
14-
},
15-
qs: {
16-
format: config.format,
17-
// replace placeholders in query with query params if it's a template
18-
query: typeof(config.query) === 'string' ? config.query : config.query(req.query)
19-
}
20-
};
21-
22-
request(options, function(error, response, body) {
23-
if (error) {
24-
console.error(`Something went wrong executing the SPARQL query: ${JSON.stringify(error)}`);
25-
next(error);
26-
} else if (response.statusCode == 200) {
27-
const filename = req.query.file || config.file;
28-
if (filename) { res.attachment(filename); }
29-
res.send(body);
30-
} else {
31-
next(new Error(body));
32-
}
33-
});
34-
} );
7+
app.get(config.path, function(req, res, next) {
8+
let query = '';
9+
try {
10+
// replace placeholders in query with query params if it's a template
11+
query = typeof(config.query) === 'string' ? config.query : config.query(req.query);
12+
} catch (err) {
13+
err.status = 400;
14+
return next(err);
15+
}
16+
17+
const options = {
18+
method: 'POST',
19+
url: process.env.MU_SPARQL_ENDPOINT,
20+
encoding: 'utf8',
21+
headers: {
22+
'Accept': config.format
23+
},
24+
qs: {
25+
format: config.format,
26+
query: query
27+
}
28+
};
29+
30+
request(options, function(error, response, body) {
31+
if (error) {
32+
console.error(`Something went wrong executing the SPARQL query: ${JSON.stringify(error)}`);
33+
return next(error);
34+
} else if (response.statusCode == 200) {
35+
const filename = req.query.file || config.file;
36+
if (filename) { res.attachment(filename); }
37+
res.send(body);
38+
} else {
39+
return next(new Error(body));
40+
}
41+
});
42+
} );
3543
});

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "mu-export-js-template",
3-
"version": "0.2.0",
2+
"name": "export-service",
3+
"version": "1.0.0",
44
"description": "Microservice to export data using custom defined SPARQL queries",
55
"repository": {
66
"type": "git",
7-
"url": "git+https://github.com/mu-semtech/mu-export-js-template.git"
7+
"url": "git+https://github.com/mu-semtech/export-service.git"
88
},
99
"keywords": [
1010
"mu-semtech"

template.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
export default function template(strings, ...keys) {
2-
return (function(values) {
3-
var result = [strings[0]];
4-
keys.forEach(function(key, i) {
5-
var value = values[key];
6-
result.push(value, strings[i + 1]);
7-
});
8-
return result.join('');
1+
import { sparqlEscape } from 'mu';
2+
3+
export default function template(strings, ...replacements) {
4+
return (function(values) { // object with query param values to inline
5+
const result = [strings[0]]; // string parts without variables
6+
replacements.forEach(function(replacement, i) { // replacement = [key, type]
7+
const value = values[replacement[0]];
8+
if (value == null) {
9+
throw new Error(`No value provided for key ${replacement[0]}`);
10+
}
11+
const escapedValue = sparqlEscape(value, replacement[1]);
12+
result.push(escapedValue, strings[i + 1]);
913
});
14+
return result.join('');
15+
});
1016
}

0 commit comments

Comments
 (0)