diff src/fetchAndParse.ts @ 95:47d3b5a5bd5e

refactor
author drewp@bigasterisk.com
date Wed, 12 Jan 2022 22:15:13 -0800
parents
children ab7dca42afbd
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fetchAndParse.ts	Wed Jan 12 22:15:13 2022 -0800
@@ -0,0 +1,21 @@
+import { Store, Parser } from "n3";
+
+export async function fetchAndParse(url: string): Promise<Store> {
+    const store = new Store();
+    const res = await fetch(url);
+    const body = await res.text();
+    const parser = new Parser({ format: "N3" });
+    await new Promise((done, rej) => {
+        parser.parse(body, (err, quad, prefixes) => {
+            if (err) {
+                throw err;
+            }
+            if (quad) {
+                store.addQuad(quad);
+            } else {
+                done(null);
+            }
+        });
+    });
+    return store;
+}