Comparing version 1.1.0 to 2.0.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-request')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'd3-request'], factory) : | ||
(global = global || self, factory(global.d3 = {}, global.d3)); | ||
}(this, function (exports, d3Request) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.d3 = {})); | ||
}(this, function (exports) { 'use strict'; | ||
function sparql (endpoint, query, callback) { | ||
var fetch; | ||
if (typeof window !== 'undefined') { | ||
fetch = window.fetch; | ||
} else { | ||
fetch = require('isomorphic-fetch'); | ||
} | ||
function sparql (endpoint, query, options) { | ||
var url = endpoint + '?query=' + encodeURIComponent(query); | ||
var sparql = d3Request.request(url) | ||
.mimeType('application/sparql-results+json') | ||
.response(parseResponse); | ||
if (callback) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('invalid callback: ' + callback) | ||
var defaultOptions = { | ||
method: 'GET', | ||
headers: { | ||
'Accept': 'application/sparql-results+json' | ||
} | ||
}; | ||
sparql.get(callback); | ||
return | ||
} | ||
Object.assign(defaultOptions, options); | ||
return sparql | ||
return fetch(url, defaultOptions) | ||
.then(function (response) { | ||
return response.json() | ||
}) | ||
.then(parseResponse) | ||
} | ||
var xmlSchema = 'http://www.w3.org/2001/XMLSchema#'; | ||
function parseResponse (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText); | ||
} catch (e) { | ||
throw new Error('unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: ' + xhr.responseText) | ||
} | ||
function parseResponse (body) { | ||
return body.results.bindings.map(function (row) { | ||
@@ -34,0 +36,0 @@ var rowObject = {}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-request")):"function"==typeof define&&define.amd?define(["exports","d3-request"],t):t((e=e||self).d3={},e.d3)}(this,function(e,a){"use strict";var s="http://www.w3.org/2001/XMLSchema#";function o(t){try{var e=JSON.parse(t.responseText)}catch(e){throw new Error("unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: "+t.responseText)}return e.results.bindings.map(function(t){var n={};return Object.keys(t).forEach(function(e){n[e]=function(e){var t=e.value;if("string"==typeof e.datatype){var n=e.datatype.replace(s,"");switch(n){case"string":t=String(t);break;case"boolean":t=Boolean("false"!==t&&t);break;case"float":case"integer":case"long":case"double":case"decimal":case"nonPositiveInteger":case"nonNegativeInteger":case"negativeInteger":case"int":case"unsignedLong":case"positiveInteger":case"short":case"unsignedInt":case"byte":case"unsignedShort":case"unsignedByte":t=Number(t);break;case"date":case"time":case"dateTime":t=new Date(t)}}return t}(t[e])}),n})}e.sparql=function(e,t,n){var s=e+"?query="+encodeURIComponent(t),r=a.request(s).mimeType("application/sparql-results+json").response(o);if(!n)return r;if("function"!=typeof n)throw new Error("invalid callback: "+n);r.get(n)},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).d3={})}(this,function(e){"use strict";var o;o="undefined"!=typeof window?window.fetch:require("isomorphic-fetch");var a="http://www.w3.org/2001/XMLSchema#";function r(e){return e.results.bindings.map(function(n){var t={};return Object.keys(n).forEach(function(e){t[e]=function(e){var n=e.value;if("string"==typeof e.datatype){var t=e.datatype.replace(a,"");switch(t){case"string":n=String(n);break;case"boolean":n=Boolean("false"!==n&&n);break;case"float":case"integer":case"long":case"double":case"decimal":case"nonPositiveInteger":case"nonNegativeInteger":case"negativeInteger":case"int":case"unsignedLong":case"positiveInteger":case"short":case"unsignedInt":case"byte":case"unsignedShort":case"unsignedByte":n=Number(n);break;case"date":case"time":case"dateTime":n=new Date(n)}}return n}(n[e])}),t})}e.sparql=function(e,n,t){var a=e+"?query="+encodeURIComponent(n),s={method:"GET",headers:{Accept:"application/sparql-results+json"}};return Object.assign(s,t),o(a,s).then(function(e){return e.json()}).then(r)},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-sparql", | ||
"version": "1.1.0", | ||
"description": "Queries a SPARQL endpoint with a SELECT query and provides the data d3 “usable”", | ||
"version": "2.0.0", | ||
"description": "Queries a SPARQL endpoint with a SELECT query", | ||
"keywords": [ | ||
@@ -25,2 +25,3 @@ "d3", | ||
"devDependencies": { | ||
"isomorphic-fetch": "^2.2.1", | ||
"mkdirp": "^0.5.1", | ||
@@ -32,6 +33,3 @@ "rimraf": "^2.6.3", | ||
"uglify-js": "^3.5.4" | ||
}, | ||
"dependencies": { | ||
"d3-request": "^1.0.6" | ||
} | ||
} |
# d3-sparql | ||
This module allows to access data from [SPARQL](https://www.w3.org/TR/sparql11-query/) [Endpoints](https://www.w3.org/wiki/SparqlEndpoints) in the vein of [d3-csv](https://github.com/d3/d3-dsv) and friends. The access through a SPARQL Endpoint allows a faster and more efficient data preparation (, once you got [the hang of SPARQL and the RDF data model](https://www.youtube.com/watch?v=FvGndkpa4K0)). Ultimately it keeps visualizations up to date. Think of SPARQL Endpoints as the most flexible API imaginable. | ||
This module lets you request data from [SPARQL](https://www.w3.org/TR/sparql11-query/) [endpoints](https://www.w3.org/wiki/SparqlEndpoints) in the vein of [d3-csv](https://github.com/d3/d3-dsv) and friends. The access through a SPARQL endpoint allows a faster and more efficient data preparation (once you got [the hang of SPARQL and the RDF data model](https://www.youtube.com/watch?v=FvGndkpa4K0)). Ultimately it keeps visualizations up to date. Think of SPARQL endpoints as the most flexible API imaginable. | ||
Define the SPARQL Query and Endpoint: | ||
Define the SPARQL query and endpoint: | ||
@@ -18,7 +18,6 @@ ```js | ||
To query the Endpoint and return the result in a d3 ready data format: | ||
To query the endpoint and get the result: | ||
```js | ||
d3.sparql(wikidataUrl, mikeQuery, function(error, data) { | ||
if (error) throw error; | ||
d3.sparql(wikidataUrl, mikeQuery).then((data) => { | ||
console.log(data); // [{'developerName': 'Mike Bostock'}] | ||
@@ -32,6 +31,4 @@ }) | ||
- Based on [d3-require](https://github.com/d3/d3-require) to be in-line with other d3 request code (and assure the same compatibility with browsers.) | ||
- Transformation of [XSD Datatypes](https://www.w3.org/2011/rdf-wg/wiki/XSD_Datatypes) (e.g. `xsd:dateTime`, `xsd:boolean`, ...) to native JavaScript types. | ||
- Reformatting of the JSON Structure to a d3 style layout while using the provided variables names of the SPARQL Query. | ||
- A backport to a d3 v3 implementation is [provided](https://github.com/zazuko/d3-sparql/tree/v3). | ||
@@ -41,25 +38,22 @@ ## Limitations | ||
- Only `SELECT` queries are supported. (This provides a projection of the graph data onto a table structure used by d3.) | ||
- Currently only supports Endpoints which are able to respond with `application/sparql-results+json`. | ||
- Only `GET` requests can be issued in the current version. | ||
- Currently only supports endpoints which are able to respond with `application/sparql-results+json`. | ||
## Installing | ||
If you use NPM, `npm install d3-sparql`. Otherwise, download the [latest release](https://github.com/zazuko/d3-sparql/releases/latest). | ||
Using NPM: `npm install d3-sparql`. Otherwise, download the [latest release](https://github.com/zazuko/d3-sparql/releases/latest) or use a CDN, for instance <https://www.jsdelivr.com>. | ||
## API Reference | ||
The *sparql* request is based on a [*d3.request*](https://github.com/d3/d3-request/blob/master/README.md#request). It implements the [*sparql*](https://github.com/zazuko/d3-sparql/blob/master/README.md#sparql) function and sets an according [*request*.response](https://github.com/d3/d3-request/blob/master/README.md#request_response) which processes the response of the SPARQL endpoint. | ||
This package add a [*sparql*](https://github.com/zazuko/d3-sparql/blob/master/README.md#sparql) function to the global `d3` object: `d3.sparql(endpoint, query, options = {})`. | ||
<a name="request" href="#sparql">#</a> d3.<b> sparql </b>(<i>endpoint</i>, <i>query</i>[, <i>callback</i>]) [<>](https://github.com/zazuko/d3-sparql/blob/master/src/sparql.js#L5 "Source") | ||
<a name="request" href="#sparql">#</a> d3.<b> sparql </b>(<i>endpoint</i>, <i>query</i>[, <i>options = {}</i>]) [<>](https://github.com/zazuko/d3-sparql/blob/master/src/sparql.js#L5 "Source") | ||
Returns a new *sparql* request for the specified SPARQL *endpoint* with the specified SPARQL *query*. If no *callback* is specified, the returned *sparql* request is not yet issued and can be further configured. If a callback is specified, it is equivalent to calling sparql.get immediately after construction: | ||
`options` is an optional object that will get merged with the second argument of [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch). | ||
```js | ||
d3.sparql(endpoint, query) | ||
.get(callback); | ||
.then((data) => …); | ||
``` | ||
All other [*request*](https://github.com/d3/d3-request/blob/master/README.md) functions can be used as specified to influence the final request (, except [*request*.response](https://github.com/d3/d3-request/blob/master/README.md#request_response) and [*request*.mimeType](https://github.com/d3/d3-request/blob/master/README.md#request_mimeType) should not be changed, unless you know what you do, to assure correct working of the [*sparql*](https://github.com/zazuko/d3-sparql/blob/master/README.md#sparql) request). | ||
## Acknowledgement | ||
The initial development of this library by [Zazuko](http://www.zazuko.com) was supported by the [City of Zürich](https://www.stadt-zuerich.ch/). |
@@ -1,20 +0,25 @@ | ||
import { request } from 'd3-request' | ||
var fetch | ||
if (typeof window !== 'undefined') { | ||
fetch = window.fetch | ||
} else { | ||
fetch = require('isomorphic-fetch') | ||
} | ||
export default function (endpoint, query, callback) { | ||
export default function (endpoint, query, options) { | ||
var url = endpoint + '?query=' + encodeURIComponent(query) | ||
var sparql = request(url) | ||
.mimeType('application/sparql-results+json') | ||
.response(parseResponse) | ||
if (callback) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('invalid callback: ' + callback) | ||
var defaultOptions = { | ||
method: 'GET', | ||
headers: { | ||
'Accept': 'application/sparql-results+json' | ||
} | ||
sparql.get(callback) | ||
return | ||
} | ||
return sparql | ||
Object.assign(defaultOptions, options) | ||
return fetch(url, defaultOptions) | ||
.then(function (response) { | ||
return response.json() | ||
}) | ||
.then(parseResponse) | ||
}; | ||
@@ -24,8 +29,3 @@ | ||
function parseResponse (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText) | ||
} catch (e) { | ||
throw new Error('unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: ' + xhr.responseText) | ||
} | ||
function parseResponse (body) { | ||
return body.results.bindings.map(function (row) { | ||
@@ -32,0 +32,0 @@ var rowObject = {} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
13
139
14979
7
57
2
- Removedd3-request@^1.0.6
- Removedcommander@2.20.3(transitive)
- Removedd3-collection@1.0.7(transitive)
- Removedd3-dispatch@1.0.6(transitive)
- Removedd3-dsv@1.2.0(transitive)
- Removedd3-request@1.0.6(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedrw@1.3.3(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedxmlhttprequest@1.8.0(transitive)