| 1 | import { URL } from 'url' |
| 2 | |
| 3 | export default function get (webid, callback) { |
| 4 | let uri |
| 5 | try { |
| 6 | uri = new URL(webid) |
| 7 | } catch (err) { |
| 8 | return callback(new Error('Invalid WebID URI: ' + webid + ': ' + err.message)) |
| 9 | } |
| 10 | const headers = { |
| 11 | Accept: 'text/turtle, application/ld+json' |
| 12 | } |
| 13 | fetch(uri.href, { method: 'GET', headers }) |
| 14 | .then(async res => { |
| 15 | if (!res.ok) { |
| 16 | return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.status)) |
| 17 | } |
| 18 | const contentType = res.headers.get('content-type') |
| 19 | let body |
| 20 | if (contentType && contentType.includes('json')) { |
| 21 | body = JSON.stringify(await res.json(), null, 2) |
| 22 | } else { |
| 23 | body = await res.text() |
| 24 | } |
| 25 | callback(null, body, contentType) |
| 26 | }) |
| 27 | .catch(err => { |
| 28 | return callback(new Error('Failed to fetch profile from ' + uri.href + ': ' + err)) |
| 29 | }) |
| 30 | } |