Mercurial > code > home > repos > front-door-display
diff src/parseRdf.ts @ 15:20d1fa4250c0
refactor
author | drewp@bigasterisk.com |
---|---|
date | Thu, 06 Jun 2024 17:52:28 -0700 |
parents | |
children | a90cb6927c7d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parseRdf.ts Thu Jun 06 17:52:28 2024 -0700 @@ -0,0 +1,40 @@ +import { Parser, Quad_Predicate, Quad_Subject, Store, Term } from "n3"; +export const EV = "http://bigasterisk.com/event#"; +export const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + +export function getLiteral(store: Store, graph: Term, subj: Quad_Subject, pred: Quad_Predicate, missing: string | null): string { + let out = null; + store.getObjects(subj, pred, graph).forEach((attr) => { + out = attr.value; + }); + if (!out) { + if (missing === null) { + throw new Error(); + } + return missing; + } + return out; +} + +export async function fetchGraph(url: string) { + return await fetch(url, { + method: "GET", + headers: { + Accept: "application/json", + "X-Pomerium-Authorization": document.cookie.substring(document.cookie.indexOf("=") + 1), + }, + }); +} + +export async function parseGraph(r: Response, done: (store: Store) => void) { + const store = new Store(); + const n3txt = await r.text(); + const parser = new Parser({ format: "application/trig" }); + parser.parse(n3txt, (error, quad, prefixes) => { + if (quad) { + store.addQuad(quad); + } else { + done(store); + } + }); +}