deno.land / x / shacl_to_type@v0.0.9 / frontend / createModel.ts

createModel.ts
نووسراو ببینە
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
46
47
48
49
50
51
52
53
54
import { responseToObjects } from './responseToObjects.ts'import { Prefixes, Meta } from './types.ts'import { Context } from './Context.ts'
export const createModel = <Type>(endpoint: string, { prefixes, meta, query }: { prefixes: Prefixes, meta: Meta, query: string }) => {
const objectCache: Map<string, Type> = new Map() const context = new Context(prefixes)
function get (iris: Array<string>): Promise<Array<Type>> function get (iri: string): Promise<Type> async function get (input: Array<string> | string) { const iris = typeof input === 'string' ? [input] : input const compactedIris = iris.map(iri => context.compactIri(iri))
const uncachedIris = compactedIris.filter(iri => !objectCache.has(iri)) const cachedIris = compactedIris.filter(iri => objectCache.has(iri))
const cachedObjects = cachedIris.map(iri => objectCache.get(iri))
const values = uncachedIris.map(iri => iri.includes('://') ? `<${iri}>` : iri)
let newObjects = []
if (uncachedIris.length) { const preparedQuery = query.replace('VALUES ?s { }', `VALUES ?s { ${values.join(' ')} }`)
const body = new FormData() body.set('query', preparedQuery) const response = await fetch(endpoint, { method: 'POST', headers: { accept: 'application/rdf+json' }, body })
const results = await response.json() newObjects = responseToObjects(results, context, meta) for (const object of newObjects) { Object.freeze(object) objectCache.set(object.id, object) } } const output = [ ...newObjects, ...cachedObjects ]
return typeof input === 'string' ? output.pop() : output }
return { get }}
shacl_to_type

Version Info

Tagged at
a year ago