-
Notifications
You must be signed in to change notification settings - Fork 0
/
solid-api.js
76 lines (67 loc) · 1.84 KB
/
solid-api.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
import {
fetch,
login,
handleIncomingRedirect,
getDefaultSession,
} from "https://cdn.skypack.dev/pin/@inrupt/[email protected]/dist=es2019,mode=imports/optimized/@inrupt/solid-client-authn-browser.js";
import {
Store,
Parser,
} from "https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/n3.js";
const solidAPI = {
isLoggedIn: false,
fetch: async (url, params) => {
params = Object.assign(
{
mode: "cors",
headers: {
Accept: "text/turtle",
},
},
params
);
let response = await fetch(url, params);
if (response.ok) {
return response.text();
} else {
throw new Error(response.status + ": " + response.statusText);
}
},
parse: (url, text) => {
const parser = new Parser({ blankNodePrefix: "", baseIRI: url });
return parser.parse(text);
},
list: async (url) => {
const turtle = await solidAPI.fetch(url);
const data = solidAPI.parse(url, turtle);
const store = new Store(data)
let result = [];
for (let quad of store.match(url, "http://www.w3.org/ns/ldp#contains")) {
result.push(quad.object.id);
}
return result;
},
get: async (url, store) => {
const turtle = await solidAPI.fetch(url);
const data = solidAPI.parse(url, turtle);
for (let quad of data) {
store.add(quad);
}
return data.size;
},
Store: Store,
signin: async (oidcIssuer, redirectUrl) => {
if (!getDefaultSession().info.isLoggedIn) {
await login({
oidcIssuer,
redirectUrl,
clientName: "Solid Demo Muze aan de UT",
});
}
},
handleIncomingRedirect: async () => {
await handleIncomingRedirect()
solidAPI.isLoggedIn = getDefaultSession().info.isLoggedIn
}
};
export default solidAPI;