-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRawQuery.js
92 lines (78 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
import mergeHeaders from './lib/mergeHeaders.js'
/**
* A query implementation that prepares URLs and headers for SPARQL queries and returns the raw fetch response.
*/
class RawQuery {
/**
* @param {Object} options
* @param {SimpleClient} options.client client that provides the HTTP I/O
*/
constructor ({ client }) {
this.client = client
}
/**
* Sends a request for a ASK query
*
* @param {string} query ASK query
* @param {Object} [options]
* @param {Headers} [options.headers] additional request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [options.operation='get'] SPARQL Protocol operation
* @return {Promise<Response>}
*/
async ask (query, { headers, operation = 'get' } = {}) {
headers = mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/sparql-results+json')
}
return this.client[operation](query, { headers })
}
/**
* Sends a request for a CONSTRUCT or DESCRIBE query
*
* @param {string} query CONSTRUCT or DESCRIBE query
* @param {Object} [options]
* @param {Headers} [options.headers] additional request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [options.operation='get'] SPARQL Protocol operation
* @return {Promise<Response>}
*/
async construct (query, { headers, operation = 'get' } = {}) {
headers = mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/n-triples')
}
return this.client[operation](query, { headers })
}
/**
* Sends a request for a SELECT query
*
* @param {string} query SELECT query
* @param {Object} [options]
* @param {Headers} [options.headers] additional request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [options.operation='get'] SPARQL Protocol operation
* @return {Promise<Response>}
*/
async select (query, { headers, operation = 'get' } = {}) {
headers = mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/sparql-results+json')
}
return this.client[operation](query, { headers })
}
/**
* Sends a request for an update query
*
* @param {string} query update query
* @param {Object} [options]
* @param {Headers} [options.headers] additional request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [options.operation='postUrlencoded'] SPARQL Protocol operation
* @return {Promise<Response>}
*/
async update (query, { headers, operation = 'postUrlencoded' } = {}) {
headers = mergeHeaders(headers)
if (!headers.has('accept')) {
headers.set('accept', '*/*')
}
return this.client[operation](query, { headers, update: true })
}
}
export default RawQuery