@lblod/mu-auth-sudo 0.3.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,4 +9,33 @@ npm install @lblod/mu-auth-sudo
9
9
  Include the following in your code
10
10
  ```
11
11
  import { querySudo as query, updateSudo as update } from '@lblod/mu-auth-sudo';
12
+
13
+
14
+ //Examples
15
+
16
+ // To run a normal query
17
+
18
+ const queryString = `SELECT * FROM { GRAPH ?g { ?s ?p ?o. } } LIMIT 1`;
19
+ await query(queryString);
20
+
21
+ // To pass extra headers
22
+
23
+ const updateString = `INSERT DATA { GRAPH <http://foo> { <http://bar> <http://baz> <http://boom>. } }`;
24
+ const extraHeaders = { 'mu-call-scope-id': 'http://foo/bar', 'other-info'; 'hello' };
25
+ await update(updateString, extraHeaders);
26
+
27
+ // With custom sparqlEndpoint (this should be exceptional, make sure you know what you're doing)
28
+
29
+ await update(updateString, extraHeaders, 'http://the.sparql.endpoint/sparql');
12
30
  ```
31
+
32
+ ## Logging
33
+
34
+ The verbosity of logging can be configured as in the [javascript template](https://github.com/mu-semtech/mu-javascript-template/blob/6ff43eaf51856783c6946e82344e31a3348ce4a3/README.md#logging) through following environment variables:
35
+
36
+ - `LOG_SPARQL_ALL`: Logging of all executed SPARQL queries, read as well as update (default `true`)
37
+ - `LOG_SPARQL_QUERIES`: Logging of executed SPARQL read queries (default: `undefined`). Overrules `LOG_SPARQL_ALL`.
38
+ - `LOG_SPARQL_UPDATES`: Logging of executed SPARQL update queries (default `undefined`). Overrules `LOG_SPARQL_ALL`.
39
+ - `DEBUG_AUTH_HEADERS`: Debugging of [mu-authorization](https://github.com/mu-semtech/mu-authorization) access-control related headers (default `true`)
40
+
41
+ Following values are considered true: [`"true"`, `"TRUE"`, `"1"`].
package/dist/auth-sudo.js CHANGED
@@ -23,7 +23,9 @@ var DEBUG_AUTH_HEADERS = _envVar2.default.get('DEBUG_AUTH_HEADERS').asBool();
23
23
 
24
24
  function sudoSparqlClient() {
25
25
  var extraHeaders = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
26
+ var sparqlEndpoint = arguments[1];
26
27
 
28
+ sparqlEndpoint = sparqlEndpoint || process.env.MU_SPARQL_ENDPOINT;
27
29
  var options = {
28
30
  requestDefaults: {
29
31
  headers: {
@@ -67,36 +69,44 @@ function sudoSparqlClient() {
67
69
  if (DEBUG_AUTH_HEADERS) {
68
70
  console.log('Headers set on SPARQL client: ' + JSON.stringify(options));
69
71
  }
70
- return new _sparqlClient.SparqlClient(process.env.MU_SPARQL_ENDPOINT, options);
72
+
73
+ return new _sparqlClient.SparqlClient(sparqlEndpoint, options);
71
74
  }
72
75
 
73
- function executeRawQuery(queryString) {
74
- return sudoSparqlClient().query(queryString).executeRaw().then(function (response) {
75
- function maybeParseJSON(body) {
76
- // Catch invalid JSON
77
- try {
78
- return JSON.parse(body);
79
- } catch (ex) {
80
- return null;
81
- }
82
- }
76
+ async function executeRawQuery(queryString) {
77
+ var extraHeaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
78
+ var sparqlEndpoint = arguments[2];
83
79
 
84
- return maybeParseJSON(response.body);
85
- });
86
- }
87
80
 
88
- function querySudo(queryString) {
89
81
  if (LOG_SPARQL_QUERIES) {
90
82
  console.log(queryString);
91
83
  }
92
- return executeRawQuery(queryString);
84
+
85
+ var response = await sudoSparqlClient(extraHeaders, sparqlEndpoint).query(queryString).executeRaw();
86
+ return maybeParseJSON(response.body);
87
+ }
88
+
89
+ function querySudo(queryString) {
90
+ var extraHeaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
91
+ var sparqlEndpoint = arguments[2];
92
+
93
+ return executeRawQuery(queryString, extraHeaders, sparqlEndpoint);
93
94
  }
94
95
 
95
96
  function updateSudo(queryString) {
96
- if (LOG_SPARQL_UPDATES) {
97
- console.log(queryString);
97
+ var extraHeaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
98
+ var sparqlEndpoint = arguments[2];
99
+
100
+ return executeRawQuery(queryString, extraHeaders, sparqlEndpoint);
101
+ }
102
+
103
+ function maybeParseJSON(body) {
104
+ // Catch invalid JSON
105
+ try {
106
+ return JSON.parse(body);
107
+ } catch (ex) {
108
+ return null;
98
109
  }
99
- return executeRawQuery(queryString);
100
110
  }
101
111
 
102
112
  var _exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lblod/mu-auth-sudo",
3
- "version": "0.3.1",
3
+ "version": "0.5.1",
4
4
  "description": "this package provides an alternative sparql client for the mu-javascript-template that has sudo rights.",
5
5
  "main": "dist/auth-sudo.js",
6
6
  "scripts": {
@@ -33,8 +33,8 @@
33
33
  "homepage": "https://github.com/lblod/mu-auth-sudo#readme",
34
34
  "devDependencies": {
35
35
  "babel-cli": "^6.26.0",
36
+ "babel-plugin-add-module-exports": "^1.0.4",
36
37
  "babel-preset-env": "^1.7.0",
37
- "babel-plugin-add-module-exports": "^1.0.0",
38
38
  "babel-preset-es2015": "^6.24.1",
39
39
  "babel-register": "^6.26.0"
40
40
  },