-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (48 loc) · 1.05 KB
/
index.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
export default function ({
$config
}, inject) {
const config = $config.kirby
inject('kirby', {
find
})
const headers = {
Authorization: 'Basic ' + Buffer.from(config.username + ':' + config.password).toString('base64'),
'Content-Type': 'application/json'
}
async function find ({
request
}) {
try {
return unWrap(await fetch(`${config.url}/api/query`, {
headers,
method: 'POST',
body: JSON.stringify(request),
mode: 'cors'
}))
} catch (error) {
return getErrorResponse(error)
}
}
async function unWrap (response) {
const jsonResponse = await response.json()
const {
ok,
status,
statusText
} = response
return {
json: jsonResponse.result,
ok,
status,
statusText
}
}
function getErrorResponse (error) {
return {
ok: false,
status: 500,
statusText: error.message,
json: {}
}
}
}