-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcayley.ts
137 lines (128 loc) · 3.37 KB
/
cayley.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import NamedNode = require("@rdfjs/data-model/lib/named-node");
import BlankNode = require("@rdfjs/data-model/lib/blank-node");
import Literal = require("@rdfjs/data-model/lib/literal");
import * as N3 from "./n3";
import Path, { Identifier } from "./path";
import "isomorphic-fetch";
export { NamedNode, BlankNode, Literal, Path, Identifier };
export enum Language {
LinkedQL = "linkedql",
Gizmo = "gizmo",
GraphQL = "graphql",
MQL = "mql"
}
export enum Format {
NQuads = "application/n-quads",
Turtle = "text/turtle",
JsonLD = "application/ld+json"
}
export enum QueryResultFormat {
JSON = "application/json",
JsonLD = "application/ld+json"
}
export default class Client {
url: string;
constructor(url: string = "http://localhost:64210") {
this.url = url;
}
/**
* Reads all quads from the database
* @param subject Subjects to filter quads by
* @param predicate Predicates to filter quads by
* @param object Objects to filter quads by
* @param label Labels to filter quads by
* @param format Data encoder to use for response.
* @returns {Promise.<Response>}
*/
async read(
subject?: Identifier,
predicate?: NamedNode,
object?: Identifier | Literal,
label?: Identifier,
format: Format = Format.JsonLD
): Promise<Response> {
const searchParams = new URLSearchParams();
if (subject) {
searchParams.append("sub", N3.serializeTerm(subject));
}
if (predicate) {
searchParams.append("pred", N3.serializeTerm(predicate));
}
if (object) {
searchParams.append("obj", N3.serializeTerm(object));
}
if (label) {
searchParams.append("label", N3.serializeTerm(label));
}
return fetch(`${this.url}/api/v2/read?${searchParams}`, {
headers: {
Accept: format
}
});
}
/**
* Writes quads to the database
* @param content Content in format specified
* @param format Data decoder to use for request
*/
async write(
content: string,
format: Format = Format.JsonLD
): Promise<string> {
const res = await fetch(`${this.url}/api/v2/write`, {
method: "POST",
headers: {
"Content-Type": format
},
body: content
});
if (res.status !== 200) {
throw new Error(await res.text());
}
const { error, result } = await res.json();
if (error) {
throw new Error(error);
}
return result;
}
/**
* Removes a node add all associated quads
* @param content Content in format specified
* @param format Data decoder to use for request
*/
delete(content: string, format: Format = Format.JsonLD): Promise<Response> {
return fetch(`${this.url}/api/v2/delete`, {
method: "POST",
headers: {
"Content-Type": format
},
body: content
});
}
/**
* Query the graph
* @param query Query text
* @param language Query language to use
* @param limit Globally limit the number of results
*/
query(
query: string | Path,
limit: number = 100,
language: Language = Language.LinkedQL,
format: QueryResultFormat = QueryResultFormat.JsonLD
): Promise<Response> {
return fetch(
`${this.url}/api/v2/query?${new URLSearchParams({
lang: language,
limit: String(limit)
})}`,
{
method: "POST",
headers: {
Accept: format
},
body: String(query)
}
);
}
}