forked from SolidOS/solid-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainerLogic.js
More file actions
45 lines (45 loc) · 1.73 KB
/
containerLogic.js
File metadata and controls
45 lines (45 loc) · 1.73 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
import { sym } from 'rdflib';
/**
* Container-related class
*/
export function createContainerLogic(store) {
function getContainerElements(containerNode) {
return store
.statementsMatching(containerNode, sym('http://www.w3.org/ns/ldp#contains'), undefined)
.map((st) => st.object);
}
function isContainer(url) {
const nodeToString = url.value;
return nodeToString.charAt(nodeToString.length - 1) === '/';
}
async function createContainer(url) {
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) {
await store.fetcher.load(containerUrl);
return getContainerElements(containerUrl);
}
return {
isContainer,
createContainer,
getContainerElements,
getContainerMembers
};
}
//# sourceMappingURL=containerLogic.js.map