-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.js
More file actions
159 lines (145 loc) · 3.85 KB
/
main.js
File metadata and controls
159 lines (145 loc) · 3.85 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const lodashGet = require('lodash/get');
const { httpBody } = require('../response-transforms');
const { mimeType, queryType } = require('./utils');
const { encodeQueryString, encodeQueryStringParams } = require('../utils');
const dispatchQuery = (
conn,
config,
accept = config.accept,
params = {},
additionalHandlers = {}
) => {
const headers = conn.headers();
headers.set('Accept', accept);
headers.set('Content-Type', 'application/x-www-form-urlencoded');
const fetchPromise = fetch(
conn.request(
config.database,
`${config.resource}${encodeQueryString(params)}`
),
{
method: 'POST',
body: encodeQueryStringParams({ query: config.query }),
headers,
}
);
return fetchPromise.then(res => {
const shouldContinue = !additionalHandlers.onResponseStart
? true
: additionalHandlers.onResponseStart(res);
if (shouldContinue === false) {
return res;
}
return httpBody(res).then(bodyRes => {
// Paths queries will return duplicate variable names
// in body.head.vars (#135)
// e.g., `paths start ?x end ?y via ?p` will return
// ['x', 'x', 'p', 'y', 'y']. Use of a Set here
// simply eliminates the duplicates for things like Studio
if (bodyRes.body && bodyRes.body.head && bodyRes.body.head.vars) {
// eslint-disable-next-line no-param-reassign
bodyRes.body.head.vars = [...new Set(bodyRes.body.head.vars)];
}
return bodyRes;
});
});
};
const execute = (conn, database, query, accept, params, additionalHandlers) => {
const type = queryType(query);
return dispatchQuery(
conn,
{
database,
query,
accept: mimeType(query),
resource: type === 'update' ? 'update' : 'query',
},
accept,
params,
additionalHandlers
);
};
const executeInTransaction = (
conn,
database,
transactionId,
query,
options = {},
params = {}
) => {
const headers = conn.headers();
headers.set('Accept', options.accept || mimeType(query));
headers.set('Content-Type', 'application/x-www-form-urlencoded');
return fetch(
conn.request(
database,
transactionId,
`${queryType(query) === 'update' ? 'update' : 'query'}${encodeQueryString(
params
)}`
),
{
method: 'POST',
headers,
body: encodeQueryStringParams({ query }),
}
)
.then(httpBody)
.then(res => ({ ...res, transactionId }));
};
const property = (conn, database, config, params) =>
execute(
conn,
database,
`select * where {
${config.uri} ${config.property} ?val
}
`,
params
).then(res => {
const values = lodashGet(res, 'body.results.bindings', []);
if (values.length > 0) {
return { ...res, body: values[0].val.value };
}
return res;
});
const explain = (conn, database, query, accept = 'text/plain', params = {}) => {
const headers = conn.headers();
headers.set('Accept', accept);
headers.set('Content-Type', 'application/x-www-form-urlencoded');
return fetch(conn.request(database, `explain${encodeQueryString(params)}`), {
method: 'POST',
headers,
body: encodeQueryStringParams({ query }),
}).then(httpBody);
};
const list = conn => {
const headers = conn.headers();
headers.set('Accept', 'application/json');
return fetch(conn.request('admin', 'queries'), {
headers,
}).then(httpBody);
};
const kill = (conn, queryId) => {
const headers = conn.headers();
return fetch(conn.request('admin', 'queries', queryId), {
method: 'DELETE',
headers,
}).then(httpBody);
};
const get = (conn, queryId) => {
const headers = conn.headers();
headers.set('Accept', 'application/json');
return fetch(conn.request('admin', 'queries', queryId), {
headers,
}).then(httpBody);
};
module.exports = {
execute,
executeInTransaction,
property,
list,
kill,
get,
explain,
};