-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (139 loc) · 4.25 KB
/
index.js
File metadata and controls
189 lines (139 loc) · 4.25 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as A from "@inrupt/solid-client-authn-browser"
import * as M from "@solid/object"
import * as W from "@rdfjs/wrapper"
import * as N from "n3"
//#region Elements
const editorForm = document.querySelector("form")
const titleInput = document.querySelector("input.title")
const nameInput = document.querySelector("input.name")
const loadButton = document.querySelector("button.load")
const removeButton = document.querySelector("button.remove")
//#endregion
const session = await authenticate()
//#region Event handlers
editorForm.addEventListener("submit", onSave)
loadButton.addEventListener("click", onLoad)
removeButton.addEventListener("click", onRemove)
async function onLoad() {
populate(normalize(parse(await load(await identify()), await identify())))
}
async function onSave(e) {
e.preventDefault()
await save(await identify(), await serialize(extract()))
}
async function onRemove() {
await remove(await identify())
editorForm.reset()
}
//#endregion
//#region Functional operations
async function identify() {
return new URL("bookData", await storage()).toString()
}
function populate(data) {
titleInput.value = data.book.title
nameInput.value = data.book.author.name
}
function extract() {
const data = normalize()
data.book.title = titleInput.value
data.book.author.name = nameInput.value
return data
}
function normalize(data) {
return data ?? parse(`
BASE <http://example.com/>
[
<title> "" ;
<author> [
<name> ""
]
] .
`)
}
async function authenticate() {
await A.handleIncomingRedirect({restorePreviousSession: true})
const session = A.getDefaultSession()
if (!session.info.isLoggedIn)
await A.login({oidcIssuer: "https://login.inrupt.com"})
// await A.login({oidcIssuer: "https://solidcommunity.net"})
return session
}
async function storage() {
const profile = await webId()
for (const storage of profile.pimStorage) return storage
}
async function webId() {
const webIdUrl = session.info.webId
const webIdResponse = await fetch(webIdUrl)
const webIdText = await webIdResponse.text()
const webIdDataset = parseRdf(webIdText, webIdUrl)
const webId = new M.WebIdDataset(webIdDataset, N.DataFactory)
return webId.mainSubject
}
//#endregion
//#region Data operations
async function load(id) {
const response = await session.fetch(id)
if (!response.ok) return undefined
return await response.text()
}
async function save(id, data) {
await session.fetch(id, {method: "PUT", body: data, headers: {"Content-Type": "text/turtle"}})
}
async function remove(id) {
await session.fetch(id, {method: "DELETE"})
}
function parse(data, baseIRI) {
return data === undefined ? undefined : new Data(parseRdf(data, baseIRI), N.DataFactory)
}
function serialize(data) {
return new Promise((resolve, reject) => {
const writer = new N.Writer
writer.addQuads([...data])
writer.end((error, result) => {
if (error) reject(error)
else resolve(result)
})
})
}
function parseRdf(rdf, baseIRI) {
const store = new N.Store
store.addQuads(new N.Parser({baseIRI}).parse(rdf));
return store
}
//#endregion
//#region Mapping
const EX = {
author: "http://example.com/author",
name: "http://example.com/name",
title: "http://example.com/title",
}
class Data extends W.DatasetWrapper {
get book() {
for (const result of this.subjectsOf(EX.title, Book)) return result
}
}
class Book extends W.TermWrapper {
get title() {
return W.OptionalFrom.subjectPredicate(this, EX.title, W.LiteralAs.string)
}
set title(value) {
W.OptionalAs.object(this, EX.title, value, W.LiteralFrom.string)
}
get author() {
return W.OptionalFrom.subjectPredicate(this, EX.author, W.TermAs.instance(Person))
}
set author(value) {
W.OptionalAs.object(this, EX.author, value, W.TermFrom.instance)
}
}
class Person extends W.TermWrapper {
get name() {
return W.OptionalFrom.subjectPredicate(this, EX.name, W.LiteralAs.string)
}
set name(value) {
W.OptionalAs.object(this, EX.name, value, W.LiteralFrom.string)
}
}
//#endregion