@coasys/ad4m 0.12.0-rc1-dev.1 → 0.12.0-rc1-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +4 -4
- package/lib/index.cjs +5999 -6046
- package/lib/index.js +5977 -6040
- package/lib/src/Ad4mClient.d.ts +16 -9
- package/lib/src/Ad4mClient.js +25 -46
- package/lib/src/Ad4mClient.test.d.ts +1 -0
- package/lib/src/Ad4mClient.test.js +1204 -0
- package/lib/src/Literal.d.ts +2 -1
- package/lib/src/Literal.js +35 -38
- package/lib/src/Literal.test.d.ts +1 -0
- package/lib/src/Literal.test.js +41 -0
- package/lib/src/SmartLiteral.d.ts +2 -1
- package/lib/src/SmartLiteral.js +15 -30
- package/lib/src/agent/AgentClient.d.ts +4 -1
- package/lib/src/agent/AgentClient.js +99 -79
- package/lib/src/ai/AIClient.d.ts +2 -1
- package/lib/src/ai/AIClient.js +220 -224
- package/lib/src/expression/ExpressionClient.d.ts +1 -1
- package/lib/src/expression/ExpressionClient.js +86 -73
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/language/LanguageClient.d.ts +1 -1
- package/lib/src/language/LanguageClient.js +52 -52
- package/lib/src/model/Ad4mModel.d.ts +157 -937
- package/lib/src/model/Ad4mModel.js +212 -2799
- package/lib/src/model/Ad4mModel.test.js +1002 -561
- package/lib/src/model/decorators.d.ts +94 -426
- package/lib/src/model/decorators.js +333 -741
- package/lib/src/model/mutation.d.ts +81 -0
- package/lib/src/model/mutation.js +317 -0
- package/lib/src/model/parentUtils.d.ts +32 -0
- package/lib/src/model/parentUtils.js +64 -0
- package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
- package/lib/src/model/prolog/generatePrologFacts.js +139 -0
- package/lib/src/model/prolog/index.d.ts +1 -0
- package/lib/src/model/prolog/index.js +1 -0
- package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
- package/lib/src/model/query/ModelQueryBuilder.js +115 -0
- package/lib/src/model/query/QueryBuilder.d.ts +101 -0
- package/lib/src/model/query/QueryBuilder.js +193 -0
- package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
- package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
- package/lib/src/model/query/fetchInstance.d.ts +24 -0
- package/lib/src/model/query/fetchInstance.js +144 -0
- package/lib/src/model/query/hydration.d.ts +54 -0
- package/lib/src/model/query/hydration.js +214 -0
- package/lib/src/model/query/operations.d.ts +59 -0
- package/lib/src/model/query/operations.js +384 -0
- package/lib/src/model/query/snapshot.d.ts +59 -0
- package/lib/src/model/query/snapshot.js +106 -0
- package/lib/src/model/query/surrealCompiler.d.ts +42 -0
- package/lib/src/model/query/surrealCompiler.js +304 -0
- package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
- package/lib/src/model/schema/fromJSONSchema.js +264 -0
- package/lib/src/model/schema/metadata.d.ts +15 -0
- package/lib/src/model/schema/metadata.js +112 -0
- package/lib/src/model/subscription.d.ts +47 -0
- package/lib/src/model/subscription.js +231 -0
- package/lib/src/model/transaction.d.ts +27 -0
- package/lib/src/model/transaction.js +32 -0
- package/lib/src/model/types.d.ts +195 -0
- package/lib/src/model/types.js +8 -0
- package/lib/src/model/util.js +7 -7
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
- package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
- package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveClient.js +316 -240
- package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
- package/lib/src/perspectives/PerspectiveProxy.js +803 -444
- package/lib/src/runtime/RuntimeClient.d.ts +5 -1
- package/lib/src/runtime/RuntimeClient.js +412 -238
- package/lib/src/shacl/SHACLFlow.test.js +164 -105
- package/lib/src/shacl/SHACLShape.d.ts +17 -3
- package/lib/src/shacl/SHACLShape.js +127 -90
- package/lib/src/shacl/SHACLShape.test.js +244 -176
- package/lib/src/utils.d.ts +8 -0
- package/lib/src/utils.js +21 -7
- package/package.json +8 -13
- package/src/Ad4mClient.test.ts +1564 -1315
- package/src/Ad4mClient.ts +55 -54
- package/src/Literal.test.ts +45 -26
- package/src/Literal.ts +82 -66
- package/src/SmartLiteral.ts +74 -58
- package/src/agent/AgentClient.ts +144 -99
- package/src/ai/AIClient.ts +409 -359
- package/src/expression/ExpressionClient.ts +142 -96
- package/src/index.ts +1 -1
- package/src/language/LanguageClient.ts +137 -113
- package/src/model/Ad4mModel.test.ts +1128 -647
- package/src/model/Ad4mModel.ts +485 -3249
- package/src/model/IMPROVEMENTS.md +482 -0
- package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
- package/src/model/README.md +317 -0
- package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
- package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
- package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
- package/src/model/decorators.ts +689 -1046
- package/src/model/mutation.ts +461 -0
- package/src/model/parentUtils.ts +91 -0
- package/src/model/prolog/generatePrologFacts.ts +176 -0
- package/src/model/prolog/index.ts +1 -0
- package/src/model/query/ModelQueryBuilder.ts +173 -0
- package/src/model/query/fetchInstance.ts +181 -0
- package/src/model/query/hydration.ts +273 -0
- package/src/model/query/operations.ts +510 -0
- package/src/model/query/snapshot.ts +126 -0
- package/src/model/query/surrealCompiler.ts +376 -0
- package/src/model/schema/fromJSONSchema.ts +411 -0
- package/src/model/schema/metadata.ts +148 -0
- package/src/model/subscription.ts +307 -0
- package/src/model/transaction.ts +60 -0
- package/src/model/types.ts +207 -0
- package/src/model/util.ts +47 -48
- package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
- package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
- package/src/perspectives/PerspectiveClient.ts +779 -540
- package/src/perspectives/PerspectiveProxy.ts +2860 -2053
- package/src/runtime/RuntimeClient.ts +775 -476
- package/src/shacl/SHACLFlow.test.ts +196 -129
- package/src/shacl/SHACLShape.test.ts +284 -190
- package/src/shacl/SHACLShape.ts +225 -172
- package/src/utils.ts +31 -17
- package/tsconfig.json +9 -10
- package/src/model/Subject.ts +0 -159
package/src/Ad4mClient.ts
CHANGED
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
import { ApolloClient } from
|
|
2
|
-
import { AgentClient } from
|
|
3
|
-
import { LanguageClient } from
|
|
4
|
-
import { NeighbourhoodClient } from
|
|
5
|
-
import { PerspectiveClient } from
|
|
6
|
-
import { RuntimeClient } from
|
|
7
|
-
import { ExpressionClient } from
|
|
8
|
-
import { AIClient } from
|
|
1
|
+
import { ApolloClient } from "@apollo/client/core";
|
|
2
|
+
import { AgentClient } from "./agent/AgentClient";
|
|
3
|
+
import { LanguageClient } from "./language/LanguageClient";
|
|
4
|
+
import { NeighbourhoodClient } from "./neighbourhood/NeighbourhoodClient";
|
|
5
|
+
import { PerspectiveClient } from "./perspectives/PerspectiveClient";
|
|
6
|
+
import { RuntimeClient } from "./runtime/RuntimeClient";
|
|
7
|
+
import { ExpressionClient } from "./expression/ExpressionClient";
|
|
8
|
+
import { AIClient } from "./ai/AIClient";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Client for the Ad4m interface wrapping GraphQL queryies
|
|
12
12
|
* for convenient use in user facing code.
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
* Aggregates the six sub-clients:
|
|
15
15
|
* AgentClient, ExpressionClient, LanguageClient,
|
|
16
16
|
* NeighbourhoodClient, PerspectiveClient and RuntimeClient
|
|
17
17
|
* for the respective functionality.
|
|
18
18
|
*/
|
|
19
19
|
export class Ad4mClient {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
private _apolloClient: ApolloClient<any>;
|
|
21
|
+
private _agentClient: AgentClient;
|
|
22
|
+
private _expressionClient: ExpressionClient;
|
|
23
|
+
private _languageClient: LanguageClient;
|
|
24
|
+
private _neighbourhoodClient: NeighbourhoodClient;
|
|
25
|
+
private _perspectiveClient: PerspectiveClient;
|
|
26
|
+
private _runtimeClient: RuntimeClient;
|
|
27
|
+
private _aiClient: AIClient;
|
|
28
28
|
|
|
29
|
+
constructor(client: ApolloClient<any>, subscribe: boolean = true) {
|
|
30
|
+
this._apolloClient = client;
|
|
31
|
+
this._agentClient = new AgentClient(this._apolloClient, subscribe);
|
|
32
|
+
this._expressionClient = new ExpressionClient(this._apolloClient);
|
|
33
|
+
this._languageClient = new LanguageClient(this._apolloClient);
|
|
34
|
+
this._neighbourhoodClient = new NeighbourhoodClient(this._apolloClient);
|
|
35
|
+
this._aiClient = new AIClient(this._apolloClient, subscribe);
|
|
36
|
+
this._perspectiveClient = new PerspectiveClient(
|
|
37
|
+
this._apolloClient,
|
|
38
|
+
subscribe,
|
|
39
|
+
);
|
|
40
|
+
this._perspectiveClient.setExpressionClient(this._expressionClient);
|
|
41
|
+
this._perspectiveClient.setNeighbourhoodClient(this._neighbourhoodClient);
|
|
42
|
+
this._perspectiveClient.setAIClient(this._aiClient);
|
|
43
|
+
this._runtimeClient = new RuntimeClient(this._apolloClient, subscribe);
|
|
44
|
+
}
|
|
29
45
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.#expressionClient = new ExpressionClient(this.#apolloClient)
|
|
34
|
-
this.#languageClient = new LanguageClient(this.#apolloClient)
|
|
35
|
-
this.#neighbourhoodClient = new NeighbourhoodClient(this.#apolloClient)
|
|
36
|
-
this.#aiClient = new AIClient(this.#apolloClient, subscribe)
|
|
37
|
-
this.#perspectiveClient = new PerspectiveClient(this.#apolloClient, subscribe)
|
|
38
|
-
this.#perspectiveClient.setExpressionClient(this.#expressionClient)
|
|
39
|
-
this.#perspectiveClient.setNeighbourhoodClient(this.#neighbourhoodClient)
|
|
40
|
-
this.#perspectiveClient.setAIClient(this.#aiClient)
|
|
41
|
-
this.#runtimeClient = new RuntimeClient(this.#apolloClient, subscribe)
|
|
42
|
-
|
|
43
|
-
}
|
|
46
|
+
get agent(): AgentClient {
|
|
47
|
+
return this._agentClient;
|
|
48
|
+
}
|
|
44
49
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
get expression(): ExpressionClient {
|
|
51
|
+
return this._expressionClient;
|
|
52
|
+
}
|
|
48
53
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
get languages(): LanguageClient {
|
|
55
|
+
return this._languageClient;
|
|
56
|
+
}
|
|
52
57
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
get neighbourhood(): NeighbourhoodClient {
|
|
59
|
+
return this._neighbourhoodClient;
|
|
60
|
+
}
|
|
56
61
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
get perspective(): PerspectiveClient {
|
|
63
|
+
return this._perspectiveClient;
|
|
64
|
+
}
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
66
|
+
get runtime(): RuntimeClient {
|
|
67
|
+
return this._runtimeClient;
|
|
68
|
+
}
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
get ai(): AIClient {
|
|
70
|
-
return this.#aiClient
|
|
71
|
-
}
|
|
72
|
-
}
|
|
70
|
+
get ai(): AIClient {
|
|
71
|
+
return this._aiClient;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/Literal.test.ts
CHANGED
|
@@ -1,31 +1,50 @@
|
|
|
1
|
-
import { Literal } from
|
|
1
|
+
import { Literal } from "./Literal";
|
|
2
2
|
|
|
3
3
|
describe("Literal", () => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
it("can handle strings", () => {
|
|
5
|
+
const testString = "test string";
|
|
6
|
+
const testUrl = "literal://string:test%20string";
|
|
7
|
+
expect(Literal.from(testString).toUrl()).toBe(testUrl);
|
|
8
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(testString);
|
|
9
|
+
});
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
it("can handle numbers", () => {
|
|
12
|
+
const testNumber = 3.1415;
|
|
13
|
+
const testUrl = "literal://number:3.1415";
|
|
14
|
+
expect(Literal.from(testNumber).toUrl()).toBe(testUrl);
|
|
15
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(testNumber);
|
|
16
|
+
});
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
it("can handle objects", () => {
|
|
19
|
+
const testObject = { testNumber: "1337", testString: "test" };
|
|
20
|
+
const testUrl =
|
|
21
|
+
"literal://json:%7B%22testNumber%22%3A%221337%22%2C%22testString%22%3A%22test%22%7D";
|
|
22
|
+
expect(Literal.from(testObject).toUrl()).toBe(testUrl);
|
|
23
|
+
expect(Literal.fromUrl(testUrl).get()).toStrictEqual(testObject);
|
|
24
|
+
});
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
26
|
+
it("can handle special characters", () => {
|
|
27
|
+
const testString = "message(X) :- triple('ad4m://self', _, X).";
|
|
28
|
+
const testUrl =
|
|
29
|
+
"literal://string:message%28X%29%20%3A-%20triple%28%27ad4m%3A%2F%2Fself%27%2C%20_%2C%20X%29.";
|
|
30
|
+
expect(Literal.from(testString).toUrl()).toBe(testUrl);
|
|
31
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(testString);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("can handle boolean true", () => {
|
|
35
|
+
const testUrl = "literal://boolean:true";
|
|
36
|
+
expect(Literal.from(true).toUrl()).toBe(testUrl);
|
|
37
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("can handle boolean false", () => {
|
|
41
|
+
const testUrl = "literal://boolean:false";
|
|
42
|
+
expect(Literal.from(false).toUrl()).toBe(testUrl);
|
|
43
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(false);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("can handle zero", () => {
|
|
47
|
+
expect(Literal.from(0).toUrl()).toBe("literal://number:0");
|
|
48
|
+
expect(Literal.fromUrl("literal://number:0").get()).toBe(0);
|
|
49
|
+
});
|
|
50
|
+
});
|
package/src/Literal.ts
CHANGED
|
@@ -1,84 +1,100 @@
|
|
|
1
1
|
function encodeRFC3986URIComponent(str) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
);
|
|
2
|
+
return encodeURIComponent(str).replace(
|
|
3
|
+
/[!'()*]/g,
|
|
4
|
+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
5
|
+
);
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
export class Literal {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
public static fromUrl(url: string) {
|
|
14
|
-
if(!url || !url.startsWith("literal://"))
|
|
15
|
-
throw new Error("Can't create Literal from non-literal URL")
|
|
16
|
-
const l = new Literal()
|
|
17
|
-
l.#url = url
|
|
18
|
-
return l
|
|
19
|
-
}
|
|
9
|
+
private _literal?: any;
|
|
10
|
+
private _url?: string;
|
|
20
11
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
public static fromUrl(url: string) {
|
|
13
|
+
if (!url || !url.startsWith("literal://"))
|
|
14
|
+
throw new Error("Can't create Literal from non-literal URL");
|
|
15
|
+
const l = new Literal();
|
|
16
|
+
l._url = url;
|
|
17
|
+
return l;
|
|
18
|
+
}
|
|
26
19
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
public static from(literal: any) {
|
|
21
|
+
const l = new Literal();
|
|
22
|
+
l._literal = literal;
|
|
23
|
+
return l;
|
|
24
|
+
}
|
|
32
25
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
encoded = `boolean:${encodeRFC3986URIComponent(this.#literal)}`
|
|
43
|
-
break;
|
|
44
|
-
case 'object':
|
|
45
|
-
encoded = `json:${encodeRFC3986URIComponent(JSON.stringify(this.#literal))}`
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
26
|
+
toUrl(): string {
|
|
27
|
+
if (this._url && !this._literal) return this._url;
|
|
28
|
+
if (
|
|
29
|
+
!this._url &&
|
|
30
|
+
(this._literal === undefined ||
|
|
31
|
+
this._literal === "" ||
|
|
32
|
+
this._literal === null)
|
|
33
|
+
)
|
|
34
|
+
throw new Error("Can't turn empty Literal into URL");
|
|
48
35
|
|
|
49
|
-
|
|
36
|
+
let encoded;
|
|
37
|
+
switch (typeof this._literal) {
|
|
38
|
+
case "string":
|
|
39
|
+
encoded = `string:${encodeRFC3986URIComponent(this._literal)}`;
|
|
40
|
+
break;
|
|
41
|
+
case "number":
|
|
42
|
+
encoded = `number:${encodeRFC3986URIComponent(this._literal)}`;
|
|
43
|
+
break;
|
|
44
|
+
case "boolean":
|
|
45
|
+
encoded = `boolean:${encodeRFC3986URIComponent(this._literal)}`;
|
|
46
|
+
break;
|
|
47
|
+
case "object":
|
|
48
|
+
encoded = `json:${encodeRFC3986URIComponent(JSON.stringify(this._literal))}`;
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Literal.toUrl(): unsupported type "${typeof this._literal}" (value: ${String(this._literal)})`,
|
|
53
|
+
);
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return this.#literal
|
|
55
|
-
|
|
56
|
-
if(!this.#url)
|
|
57
|
-
throw new Error("Can't render empty Literal")
|
|
56
|
+
return `literal://${encoded}`;
|
|
57
|
+
}
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
get(): any {
|
|
60
|
+
if (
|
|
61
|
+
this._literal !== undefined &&
|
|
62
|
+
this._literal !== null &&
|
|
63
|
+
this._literal !== ""
|
|
64
|
+
)
|
|
65
|
+
return this._literal;
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
return decodeURIComponent(body.substring(7))
|
|
68
|
-
}
|
|
67
|
+
if (!this._url) throw new Error("Can't render empty Literal");
|
|
69
68
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return parseFloat(numberString)
|
|
73
|
-
}
|
|
69
|
+
if (!this._url.startsWith("literal://"))
|
|
70
|
+
throw new Error("Can't render Literal from non-literal URL");
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return JSON.parse(decodeURIComponent(json))
|
|
78
|
-
}
|
|
72
|
+
// get rid of "literal://"
|
|
73
|
+
const body = this._url.substring(10);
|
|
79
74
|
|
|
80
|
-
|
|
75
|
+
if (body.startsWith("string:")) {
|
|
76
|
+
return decodeURIComponent(body.substring(7));
|
|
81
77
|
}
|
|
82
78
|
|
|
79
|
+
if (body.startsWith("number:")) {
|
|
80
|
+
const numberString = body.substring(7);
|
|
81
|
+
return parseFloat(numberString);
|
|
82
|
+
}
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
if (body.startsWith("boolean:")) {
|
|
85
|
+
const boolStr = body.substring(8).trim();
|
|
86
|
+
if (boolStr === "true") return true;
|
|
87
|
+
if (boolStr === "false") return false;
|
|
88
|
+
throw new Error(
|
|
89
|
+
`Literal.get(): malformed boolean payload "${boolStr}" — expected "true" or "false"`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (body.startsWith("json:")) {
|
|
94
|
+
const json = body.substring(5);
|
|
95
|
+
return JSON.parse(decodeURIComponent(json));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new Error(`Can't parse unknown literal: ${body}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/SmartLiteral.ts
CHANGED
|
@@ -3,73 +3,89 @@ import { Literal } from "./Literal";
|
|
|
3
3
|
import { LinkQuery } from "./perspectives/LinkQuery";
|
|
4
4
|
import { PerspectiveProxy } from "./perspectives/PerspectiveProxy";
|
|
5
5
|
|
|
6
|
-
export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content"
|
|
7
|
-
|
|
6
|
+
export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content";
|
|
8
7
|
|
|
9
8
|
function makeRandomStringID(length: number): string {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
let result = "";
|
|
10
|
+
let characters =
|
|
11
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
12
|
+
let charactersLength = characters.length;
|
|
13
|
+
for (let i = 0; i < length; i++) {
|
|
14
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
18
|
|
|
19
19
|
export class SmartLiteral {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
private _perspective: PerspectiveProxy;
|
|
21
|
+
private _base: string;
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
constructor(perspective: PerspectiveProxy, base: string) {
|
|
24
|
+
this._perspective = perspective;
|
|
25
|
+
this._base = base;
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
get base() {
|
|
29
|
+
return this._base;
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
public static async create(
|
|
33
|
+
perspective: PerspectiveProxy,
|
|
34
|
+
literal: any,
|
|
35
|
+
): Promise<SmartLiteral> {
|
|
36
|
+
const base = Literal.from(makeRandomStringID(10)).toUrl();
|
|
37
|
+
const smartLiteral = new SmartLiteral(perspective, base);
|
|
38
|
+
await smartLiteral.set(literal);
|
|
39
|
+
return smartLiteral;
|
|
40
|
+
}
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
public static async isSmartLiteralBase(
|
|
43
|
+
perspective: PerspectiveProxy,
|
|
44
|
+
base: string,
|
|
45
|
+
): Promise<boolean> {
|
|
46
|
+
let links = await perspective.get(
|
|
47
|
+
new LinkQuery({
|
|
48
|
+
source: base,
|
|
49
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
return links.length > 0;
|
|
53
|
+
}
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
predicate: SMART_LITERAL_CONTENT_PREDICATE
|
|
58
|
-
}))
|
|
55
|
+
public static async getAllSmartLiterals(
|
|
56
|
+
perspective: PerspectiveProxy,
|
|
57
|
+
): Promise<SmartLiteral[]> {
|
|
58
|
+
let links = await perspective.get(
|
|
59
|
+
new LinkQuery({
|
|
60
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE,
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
return links.map((link) => new SmartLiteral(perspective, link.data.source));
|
|
64
|
+
}
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
async get(): Promise<any> {
|
|
67
|
+
let link = await this._perspective.getSingleTarget(
|
|
68
|
+
new LinkQuery({
|
|
69
|
+
source: this._base,
|
|
70
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE,
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
async set(content: any) {
|
|
68
|
-
let literal = Literal.from(content)
|
|
69
|
-
await this.#perspective.setSingleTarget(new Link({
|
|
70
|
-
source: this.#base,
|
|
71
|
-
predicate: SMART_LITERAL_CONTENT_PREDICATE,
|
|
72
|
-
target: literal.toUrl()
|
|
73
|
-
}))
|
|
74
|
+
if (!link) {
|
|
75
|
+
throw `No content for smart literal ${this._base}`;
|
|
74
76
|
}
|
|
75
|
-
|
|
77
|
+
|
|
78
|
+
return Literal.fromUrl(link).get();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async set(content: any) {
|
|
82
|
+
let literal = Literal.from(content);
|
|
83
|
+
await this._perspective.setSingleTarget(
|
|
84
|
+
new Link({
|
|
85
|
+
source: this._base,
|
|
86
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE,
|
|
87
|
+
target: literal.toUrl(),
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|