-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParsingClient.js
85 lines (81 loc) · 2.59 KB
/
ParsingClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import DataModelFactory from '@rdfjs/data-model/Factory.js'
import DatasetFactory from '@rdfjs/dataset/Factory.js'
import Environment from '@rdfjs/environment'
import isDatasetCoreFactory from './lib/isDatasetCoreFactory.js'
import ParsingQuery from './ParsingQuery.js'
import SimpleClient from './SimpleClient.js'
const defaultFactory = new Environment([DataModelFactory, DatasetFactory])
/**
* A client implementation based on {@link ParsingQuery} that parses SPARQL results into RDF/JS DatasetCore objects
* (CONSTRUCT/DESCRIBE) or an array of objects (SELECT). It does not provide a store interface.
*
* @extends SimpleClient
* @property {ParsingQuery} query
*
* @example
* // read the height of the Eiffel Tower from Wikidata with a SELECT query
*
* import ParsingClient from 'sparql-http-client/ParsingClient.js'
*
* const endpointUrl = 'https://query.wikidata.org/sparql'
* const query = `
* PREFIX wd: <http://www.wikidata.org/entity/>
* PREFIX p: <http://www.wikidata.org/prop/>
* PREFIX ps: <http://www.wikidata.org/prop/statement/>
* PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
*
* SELECT ?value WHERE {
* wd:Q243 p:P2048 ?height.
*
* ?height pq:P518 wd:Q24192182;
* ps:P2048 ?value .
* }`
*
* const client = new ParsingClient({ endpointUrl })
* const result = await client.query.select(query)
*
* for (const row of result) {
* for (const [key, value] of Object.entries(row)) {
* console.log(`${key}: ${value.value} (${value.termType})`)
* }
* }
*/
class ParsingClient extends SimpleClient {
/**
* @param {Object} options
* @param {string} [options.endpointUrl] SPARQL query endpoint URL
* @param {factory} [options.factory] RDF/JS factory
* @param {fetch} [options.fetch=nodeify-fetch] fetch implementation
* @param {Headers} [options.headers] headers sent with every request
* @param {string} [options.password] password used for basic authentication
* @param {string} [options.storeUrl] SPARQL Graph Store URL
* @param {string} [options.updateUrl] SPARQL update endpoint URL
* @param {string} [options.user] user used for basic authentication
*/
constructor ({
endpointUrl,
factory = defaultFactory,
fetch,
headers,
password,
storeUrl,
updateUrl,
user
}) {
super({
endpointUrl,
factory,
fetch,
headers,
password,
storeUrl,
updateUrl,
user,
Query: ParsingQuery
})
if (!isDatasetCoreFactory(this.factory)) {
throw new Error('the given factory doesn\'t implement the DatasetCoreFactory interface')
}
}
}
export default ParsingClient