src/lib/solid.ts (view raw)
1import {
2 getSolidDataset,
3 getStringNoLocale,
4 getThingAll,
5 getUrl,
6 toRdfJsDataset
7} from '@inrupt/solid-client';
8import { FOAF, VCARD } from '@inrupt/vocab-common-rdf';
9import jsonld from 'jsonld';
10import { JsonLdSerializer } from 'jsonld-streaming-serializer';
11import d from 'stream-to-string';
12
13export const fetchPod: typeof fetch = async (resource: URL | RequestInfo) => {
14 const response = await fetch(`/solid/fetch?resource=${encodeURIComponent(resource.toString())}`);
15 Object.defineProperty(response, 'url', { value: decodeURIComponent(resource.toString()) });
16 return response;
17};
18
19export const getAllSubjects = async (iri: string) => {
20 const dataset = await getSolidDataset(iri, { fetch: fetchPod });
21 const subjects = getThingAll(dataset);
22 return subjects;
23};
24
25export const getProfile = async (webId: string) => {
26 const subjects = getAllSubjects(webId);
27
28 const findValue = (predicate: string) =>
29 subjects
30 .map((subject) => getStringNoLocale(subject, predicate) || getUrl(subject, predicate))
31 ?.filter((i) => !!i)[0];
32
33 return {
34 name: findValue(FOAF.name),
35 organization: findValue(VCARD.organization_name),
36 image: findValue(VCARD.hasPhoto)
37 };
38};