-
Notifications
You must be signed in to change notification settings - Fork 7
/
containerLogic.ts
53 lines (48 loc) · 1.87 KB
/
containerLogic.ts
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
import { NamedNode, Statement, sym } from "rdflib";
/**
* Container-related class
*/
export function createContainerLogic(store) {
function getContainerElements(containerNode: NamedNode): NamedNode[] {
return store
.statementsMatching(
containerNode,
sym("http://www.w3.org/ns/ldp#contains"),
undefined
)
.map((st: Statement) => st.object as NamedNode);
}
function isContainer(url: NamedNode) {
const nodeToString = url.value;
return nodeToString.charAt(nodeToString.length - 1) === "/";
}
async function createContainer(url: string) {
const stringToNode = sym(url);
if (!isContainer(stringToNode)) {
throw new Error(`Not a container URL ${url}`);
}
// Copied from https://github.com/solidos/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
const result = await store.fetcher._fetch(url, {
method: "PUT",
headers: {
"Content-Type": "text/turtle",
"If-None-Match": "*",
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solidos/node-solid-server/issues/1465
},
body: " ", // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
});
if (result.status.toString()[0] !== '2') {
throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`);
}
}
async function getContainerMembers(containerUrl: NamedNode): Promise<NamedNode[]> {
await store.fetcher.load(containerUrl);
return getContainerElements(containerUrl);
}
return {
isContainer,
createContainer,
getContainerElements,
getContainerMembers
}
}