comparison src/parseRdf.ts @ 15:20d1fa4250c0

refactor
author drewp@bigasterisk.com
date Thu, 06 Jun 2024 17:52:28 -0700
parents
children a90cb6927c7d
comparison
equal deleted inserted replaced
14:899f12179b85 15:20d1fa4250c0
1 import { Parser, Quad_Predicate, Quad_Subject, Store, Term } from "n3";
2 export const EV = "http://bigasterisk.com/event#";
3 export const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
4
5 export function getLiteral(store: Store, graph: Term, subj: Quad_Subject, pred: Quad_Predicate, missing: string | null): string {
6 let out = null;
7 store.getObjects(subj, pred, graph).forEach((attr) => {
8 out = attr.value;
9 });
10 if (!out) {
11 if (missing === null) {
12 throw new Error();
13 }
14 return missing;
15 }
16 return out;
17 }
18
19 export async function fetchGraph(url: string) {
20 return await fetch(url, {
21 method: "GET",
22 headers: {
23 Accept: "application/json",
24 "X-Pomerium-Authorization": document.cookie.substring(document.cookie.indexOf("=") + 1),
25 },
26 });
27 }
28
29 export async function parseGraph(r: Response, done: (store: Store) => void) {
30 const store = new Store();
31 const n3txt = await r.text();
32 const parser = new Parser({ format: "application/trig" });
33 parser.parse(n3txt, (error, quad, prefixes) => {
34 if (quad) {
35 store.addQuad(quad);
36 } else {
37 done(store);
38 }
39 });
40 }