forked from zazuko/kopflos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.js
79 lines (63 loc) · 2.18 KB
/
Api.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const clownface = require('clownface')
const { fromFile } = require('rdf-utils-fs')
const { addAll, fromStream } = require('rdf-dataset-ext')
const rdf = { ...require('@rdfjs/data-model'), ...require('@rdfjs/dataset') }
const { replaceDatasetIRI } = require('./lib/replaceIRI')
const LoaderRegistry = require('rdf-loaders-registry')
const EcmaScriptLoader = require('rdf-loader-code/ecmaScript')
const EcmaScriptModuleLoader = require('rdf-loader-code/ecmaScriptModule')
const EcmaScriptLiteralLoader = require('rdf-loader-code/ecmaScriptLiteral')
const ns = require('@tpluscode/rdf-ns-builders')
class Api {
constructor ({ term, dataset, graph, path, codePath } = {}) {
this.term = term
this.dataset = dataset
this.graph = graph
this.path = path
this.codePath = codePath
this.loaderRegistry = new LoaderRegistry()
this.tasks = []
this.initialized = false
EcmaScriptLoader.register(this.loaderRegistry)
EcmaScriptModuleLoader.register(this.loaderRegistry)
EcmaScriptLiteralLoader.register(this.loaderRegistry)
}
async init () {
if (!this._initialization) {
this._initialization = this._beginInit()
}
return this._initialization
}
fromFile (filePath) {
this.tasks.push(async () => {
addAll(this.dataset, await fromStream(rdf.dataset(), fromFile(filePath)))
})
return this
}
rebase (fromBaseIRI, toBaseIRI) {
this.tasks.push(async () => {
this.dataset = replaceDatasetIRI(fromBaseIRI, toBaseIRI, this.dataset)
})
return this
}
static fromFile (filePath, options) {
const api = new Api(options)
return api.fromFile(filePath)
}
async _beginInit () {
if (!this.dataset) {
this.dataset = rdf.dataset()
}
for (const task of this.tasks) {
await task()
}
const apiDoc = clownface({ dataset: this.dataset, term: this.term, graph: this.graph })
if (apiDoc.has(ns.rdf.type, ns.hydra.ApiDocumentation).terms.length === 0) {
apiDoc.addOut(ns.rdf.type, ns.hydra.ApiDocumentation)
apiDoc.node().has(ns.rdf.type, ns.hydra.Class).forEach(supportedClass => {
apiDoc.addOut(ns.hydra.supportedClass, supportedClass)
})
}
}
}
module.exports = Api