forked from rdf-ext/sparql-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RawQuery.js
95 lines (82 loc) · 2.71 KB
/
RawQuery.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
86
87
88
89
90
91
92
93
94
95
/**
* A base query class which performs HTTP requests for the different SPARQL query forms
*/
class RawQuery {
/**
* @param {Object} init
* @param {Endpoint} init.endpoint
*/
constructor ({ endpoint }) {
/** @member {Endpoint} */
this.endpoint = endpoint
}
/**
* Performs an ASK query
* By default uses HTTP GET with query string
*
* @param {string} query SPARQL ASK query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<Response>}
*/
async ask (query, { headers, operation = 'get' } = {}) {
headers = this.endpoint.mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/sparql-results+json')
}
return this.endpoint[operation](query, { headers })
}
/**
* Performs a CONSTRUCT/DESCRIBE query
* By default uses HTTP GET with query string
*
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<Response>}
*/
async construct (query, { headers, operation = 'get' } = {}) {
headers = new this.endpoint.fetch.Headers(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/n-triples')
}
return this.endpoint[operation](query, { headers })
}
/**
* Performs a SELECT query
* By default uses HTTP GET with query string
*
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<Response>}
*/
async select (query, { headers, operation = 'get' } = {}) {
headers = this.endpoint.mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/sparql-results+json')
}
return this.endpoint[operation](query, { headers })
}
/**
* Performs a SELECT query
* By default uses HTTP POST with form-encoded body
*
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='postUrlencoded']
* @return {Promise<Response>}
*/
async update (query, { headers, operation = 'postUrlencoded' } = {}) {
headers = new this.endpoint.fetch.Headers(headers)
if (!headers.has('accept')) {
headers.set('accept', '*/*')
}
return this.endpoint[operation](query, { headers, update: true })
}
}
module.exports = RawQuery