-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathget.js
More file actions
35 lines (31 loc) · 957 Bytes
/
get.js
File metadata and controls
35 lines (31 loc) · 957 Bytes
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
module.exports = get
const fetch = require('node-fetch')
const url = require('url')
function get (webid, callback) {
let uri
try {
uri = new url.URL(webid)
} catch (err) {
return callback(new Error('Invalid WebID URI: ' + webid + ': ' + err.message))
}
const headers = {
Accept: 'text/turtle, application/ld+json'
}
fetch(uri.href, { method: 'GET', headers })
.then(async res => {
if (!res.ok) {
return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.status))
}
const contentType = res.headers.get('content-type')
let body
if (contentType && contentType.includes('json')) {
body = JSON.stringify(await res.json(), null, 2)
} else {
body = await res.text()
}
callback(null, body, contentType)
})
.catch(err => {
return callback(new Error('Failed to fetch profile from ' + uri.href + ': ' + err))
})
}