@coasys/ad4m 0.12.0-rc1-dev.2 → 0.12.0-rc1-dev.4
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 +10 -3
- package/lib/index.cjs +6893 -5884
- package/lib/index.js +6889 -5872
- package/lib/src/Ad4mClient.d.ts +9 -16
- package/lib/src/Ad4mClient.js +46 -25
- package/lib/src/Literal.d.ts +1 -2
- package/lib/src/Literal.js +38 -35
- package/lib/src/SmartLiteral.d.ts +1 -2
- package/lib/src/SmartLiteral.js +30 -15
- package/lib/src/agent/AgentClient.d.ts +1 -4
- package/lib/src/agent/AgentClient.js +79 -99
- package/lib/src/ai/AIClient.d.ts +1 -2
- package/lib/src/ai/AIClient.js +224 -220
- package/lib/src/expression/ExpressionClient.d.ts +1 -1
- package/lib/src/expression/ExpressionClient.js +73 -86
- 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 +1351 -144
- package/lib/src/model/Ad4mModel.js +3760 -228
- package/lib/src/model/Ad4mModel.test.js +522 -979
- package/lib/src/model/Subject.d.ts +3 -3
- package/lib/src/model/Subject.js +25 -25
- package/lib/src/model/decorators.d.ts +444 -100
- package/lib/src/model/decorators.js +686 -321
- package/lib/src/model/util.d.ts +6 -6
- package/lib/src/model/util.js +24 -24
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
- package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveClient.js +240 -316
- package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
- package/lib/src/perspectives/PerspectiveProxy.js +485 -832
- package/lib/src/runtime/RuntimeClient.d.ts +1 -5
- package/lib/src/runtime/RuntimeClient.js +238 -412
- package/lib/src/shacl/SHACLFlow.test.js +105 -164
- package/lib/src/shacl/SHACLShape.d.ts +12 -17
- package/lib/src/shacl/SHACLShape.js +115 -128
- package/lib/src/shacl/SHACLShape.test.js +176 -244
- package/lib/src/shims/reflect-metadata.d.ts +0 -0
- package/lib/src/shims/type-graphql.d.ts +32 -0
- package/lib/src/src/Ad4mClient.d.ts +28 -0
- package/lib/src/src/Address.d.ts +1 -0
- package/lib/src/src/DID.d.ts +1 -0
- package/lib/src/src/Exception.d.ts +7 -0
- package/lib/src/src/Literal.d.ts +7 -0
- package/lib/src/src/PubSub.d.ts +17 -0
- package/lib/src/src/SmartLiteral.d.ts +12 -0
- package/lib/src/src/agent/Agent.d.ts +110 -0
- package/lib/src/src/agent/AgentClient.d.ts +62 -0
- package/lib/src/src/agent/AgentResolver.d.ts +30 -0
- package/lib/src/src/agent/AgentStatus.d.ts +8 -0
- package/lib/src/src/ai/AIClient.d.ts +32 -0
- package/lib/src/src/ai/AIResolver.d.ts +75 -0
- package/lib/src/src/ai/Tasks.d.ts +37 -0
- package/lib/src/src/buildSchema.d.ts +1 -0
- package/lib/src/src/expression/Expression.d.ts +28 -0
- package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
- package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
- package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
- package/lib/src/src/index.d.ts +35 -0
- package/lib/src/src/jestSetup.d.ts +1 -0
- package/lib/src/src/language/Icon.d.ts +4 -0
- package/lib/src/src/language/Language.d.ts +206 -0
- package/lib/src/src/language/LanguageClient.d.ts +17 -0
- package/lib/src/src/language/LanguageContext.d.ts +37 -0
- package/lib/src/src/language/LanguageHandle.d.ts +9 -0
- package/lib/src/src/language/LanguageMeta.d.ts +35 -0
- package/lib/src/src/language/LanguageRef.d.ts +6 -0
- package/lib/src/src/language/LanguageResolver.d.ts +19 -0
- package/lib/src/src/links/Links.d.ts +39 -0
- package/lib/src/src/links/Links.test.d.ts +1 -0
- package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
- package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
- package/lib/src/src/model/Subject.d.ts +25 -0
- package/lib/src/src/model/decorators.d.ts +545 -0
- package/lib/src/src/model/util.d.ts +12 -0
- package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
- package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
- package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
- package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
- package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
- package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
- package/lib/src/src/perspectives/Perspective.d.ts +38 -0
- package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
- package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
- package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
- package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
- package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
- package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
- package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
- package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
- package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
- package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
- package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
- package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
- package/lib/src/src/typeDefs.d.ts +1 -0
- package/lib/src/src/unwrapApolloResult.d.ts +2 -0
- package/lib/src/src/utils.d.ts +25 -0
- package/lib/src/utils.d.ts +0 -8
- package/lib/src/utils.js +7 -21
- package/package.json +15 -14
- package/src/Ad4mClient.test.ts +1315 -1564
- package/src/Ad4mClient.ts +54 -55
- package/src/Literal.test.ts +26 -45
- package/src/Literal.ts +66 -82
- package/src/SmartLiteral.ts +58 -74
- package/src/agent/AgentClient.ts +99 -144
- package/src/ai/AIClient.ts +359 -409
- package/src/expression/ExpressionClient.ts +96 -142
- package/src/index.ts +1 -1
- package/src/language/LanguageClient.ts +113 -137
- package/src/model/Ad4mModel.test.ts +616 -1114
- package/src/model/Ad4mModel.ts +4386 -451
- package/src/model/Subject.ts +159 -0
- package/src/model/decorators.ts +1168 -667
- package/src/model/util.ts +54 -53
- package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
- package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
- package/src/perspectives/PerspectiveClient.ts +540 -779
- package/src/perspectives/PerspectiveProxy.ts +2067 -2860
- package/src/runtime/RuntimeClient.ts +476 -775
- package/src/shacl/SHACLFlow.test.ts +129 -196
- package/src/shacl/SHACLShape.test.ts +190 -284
- package/src/shacl/SHACLShape.ts +200 -224
- package/src/utils.ts +17 -31
- package/tsconfig.json +10 -9
- package/src/model/IMPROVEMENTS.md +0 -482
- package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
- package/src/model/README.md +0 -317
- package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
- package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
- package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
- package/src/model/mutation.ts +0 -461
- package/src/model/parentUtils.ts +0 -91
- package/src/model/prolog/generatePrologFacts.ts +0 -176
- package/src/model/prolog/index.ts +0 -1
- package/src/model/query/ModelQueryBuilder.ts +0 -173
- package/src/model/query/fetchInstance.ts +0 -181
- package/src/model/query/hydration.ts +0 -273
- package/src/model/query/operations.ts +0 -510
- package/src/model/query/snapshot.ts +0 -126
- package/src/model/query/surrealCompiler.ts +0 -376
- package/src/model/schema/fromJSONSchema.ts +0 -411
- package/src/model/schema/metadata.ts +0 -148
- package/src/model/subscription.ts +0 -307
- package/src/model/transaction.ts +0 -60
- package/src/model/types.ts +0 -207
package/src/Ad4mClient.ts
CHANGED
|
@@ -1,73 +1,72 @@
|
|
|
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
|
+
#apolloClient: ApolloClient<any>
|
|
21
|
+
#agentClient: AgentClient
|
|
22
|
+
#expressionClient: ExpressionClient
|
|
23
|
+
#languageClient: LanguageClient
|
|
24
|
+
#neighbourhoodClient: NeighbourhoodClient
|
|
25
|
+
#perspectiveClient: PerspectiveClient
|
|
26
|
+
#runtimeClient: RuntimeClient
|
|
27
|
+
#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
|
-
}
|
|
45
29
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
constructor(client: ApolloClient<any>, subscribe: boolean = true) {
|
|
31
|
+
this.#apolloClient = client
|
|
32
|
+
this.#agentClient = new AgentClient(this.#apolloClient, subscribe)
|
|
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
|
+
}
|
|
49
44
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
get agent(): AgentClient {
|
|
46
|
+
return this.#agentClient
|
|
47
|
+
}
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
get expression(): ExpressionClient {
|
|
50
|
+
return this.#expressionClient
|
|
51
|
+
}
|
|
57
52
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
get languages(): LanguageClient {
|
|
54
|
+
return this.#languageClient
|
|
55
|
+
}
|
|
61
56
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
get neighbourhood(): NeighbourhoodClient {
|
|
58
|
+
return this.#neighbourhoodClient
|
|
59
|
+
}
|
|
65
60
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
get perspective(): PerspectiveClient {
|
|
62
|
+
return this.#perspectiveClient
|
|
63
|
+
}
|
|
69
64
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
get runtime(): RuntimeClient {
|
|
66
|
+
return this.#runtimeClient
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get ai(): AIClient {
|
|
70
|
+
return this.#aiClient
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/Literal.test.ts
CHANGED
|
@@ -1,50 +1,31 @@
|
|
|
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
|
-
|
|
24
|
-
});
|
|
18
|
+
it("can handle objects", () => {
|
|
19
|
+
const testObject = {testNumber: "1337", testString: "test"}
|
|
20
|
+
const testUrl = "literal://json:%7B%22testNumber%22%3A%221337%22%2C%22testString%22%3A%22test%22%7D"
|
|
21
|
+
expect(Literal.from(testObject).toUrl()).toBe(testUrl)
|
|
22
|
+
expect(Literal.fromUrl(testUrl).get()).toStrictEqual(testObject)
|
|
23
|
+
})
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
});
|
|
25
|
+
it("can handle special characters", () => {
|
|
26
|
+
const testString = "message(X) :- triple('ad4m://self', _, X)."
|
|
27
|
+
const testUrl = "literal://string:message%28X%29%20%3A-%20triple%28%27ad4m%3A%2F%2Fself%27%2C%20_%2C%20X%29."
|
|
28
|
+
expect(Literal.from(testString).toUrl()).toBe(testUrl)
|
|
29
|
+
expect(Literal.fromUrl(testUrl).get()).toBe(testString)
|
|
30
|
+
})
|
|
31
|
+
})
|
package/src/Literal.ts
CHANGED
|
@@ -1,100 +1,84 @@
|
|
|
1
1
|
function encodeRFC3986URIComponent(str) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
return encodeURIComponent(str)
|
|
3
|
+
.replace(
|
|
4
|
+
/[!'()*]/g,
|
|
5
|
+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
|
|
6
|
+
);
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
export class Literal {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
}
|
|
19
|
-
|
|
20
|
-
public static from(literal: any) {
|
|
21
|
-
const l = new Literal();
|
|
22
|
-
l._literal = literal;
|
|
23
|
-
return l;
|
|
24
|
-
}
|
|
10
|
+
#literal?: any
|
|
11
|
+
#url?: string
|
|
25
12
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
)
|
|
34
|
-
throw new Error("Can't turn empty Literal into URL");
|
|
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
|
+
}
|
|
35
20
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
);
|
|
21
|
+
public static from(literal: any) {
|
|
22
|
+
const l = new Literal()
|
|
23
|
+
l.#literal = literal
|
|
24
|
+
return l
|
|
54
25
|
}
|
|
55
26
|
|
|
56
|
-
|
|
57
|
-
|
|
27
|
+
toUrl(): string {
|
|
28
|
+
if(this.#url && !this.#literal)
|
|
29
|
+
return this.#url
|
|
30
|
+
if(!this.#url && (this.#literal === undefined || this.#literal === "" || this.#literal === null))
|
|
31
|
+
throw new Error("Can't turn empty Literal into URL")
|
|
58
32
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
33
|
+
let encoded
|
|
34
|
+
switch(typeof this.#literal) {
|
|
35
|
+
case 'string':
|
|
36
|
+
encoded = `string:${encodeRFC3986URIComponent(this.#literal)}`
|
|
37
|
+
break;
|
|
38
|
+
case 'number':
|
|
39
|
+
encoded = `number:${encodeRFC3986URIComponent(this.#literal)}`
|
|
40
|
+
break;
|
|
41
|
+
case 'boolean':
|
|
42
|
+
encoded = `boolean:${encodeRFC3986URIComponent(this.#literal)}`
|
|
43
|
+
break;
|
|
44
|
+
case 'object':
|
|
45
|
+
encoded = `json:${encodeRFC3986URIComponent(JSON.stringify(this.#literal))}`
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
66
48
|
|
|
67
|
-
|
|
49
|
+
return `literal://${encoded}`
|
|
50
|
+
}
|
|
68
51
|
|
|
69
|
-
|
|
70
|
-
|
|
52
|
+
get(): any {
|
|
53
|
+
if(this.#literal)
|
|
54
|
+
return this.#literal
|
|
55
|
+
|
|
56
|
+
if(!this.#url)
|
|
57
|
+
throw new Error("Can't render empty Literal")
|
|
71
58
|
|
|
72
|
-
|
|
73
|
-
|
|
59
|
+
if(!this.#url.startsWith("literal://"))
|
|
60
|
+
throw new Error("Can't render Literal from non-literal URL")
|
|
61
|
+
|
|
62
|
+
// get rid of "literal://"
|
|
63
|
+
const body = this.#url.substring(10)
|
|
64
|
+
|
|
74
65
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
66
|
+
if(body.startsWith("string:")) {
|
|
67
|
+
return decodeURIComponent(body.substring(7))
|
|
68
|
+
}
|
|
78
69
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
if(body.startsWith("number:")) {
|
|
71
|
+
const numberString = body.substring(7)
|
|
72
|
+
return parseFloat(numberString)
|
|
73
|
+
}
|
|
83
74
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
throw new Error(
|
|
89
|
-
`Literal.get(): malformed boolean payload "${boolStr}" — expected "true" or "false"`,
|
|
90
|
-
);
|
|
91
|
-
}
|
|
75
|
+
if(body.startsWith("json:")) {
|
|
76
|
+
const json = body.substring(5)
|
|
77
|
+
return JSON.parse(decodeURIComponent(json))
|
|
78
|
+
}
|
|
92
79
|
|
|
93
|
-
|
|
94
|
-
const json = body.substring(5);
|
|
95
|
-
return JSON.parse(decodeURIComponent(json));
|
|
80
|
+
throw new Error(`Can't parse unknown literal: ${body}`)
|
|
96
81
|
}
|
|
97
82
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
83
|
+
|
|
84
|
+
}
|
package/src/SmartLiteral.ts
CHANGED
|
@@ -3,89 +3,73 @@ 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"
|
|
6
|
+
export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content"
|
|
7
|
+
|
|
7
8
|
|
|
8
9
|
function makeRandomStringID(length: number): string {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
10
|
+
let result = '';
|
|
11
|
+
let characters = '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
|
-
|
|
22
|
-
|
|
23
|
-
constructor(perspective: PerspectiveProxy, base: string) {
|
|
24
|
-
this._perspective = perspective;
|
|
25
|
-
this._base = base;
|
|
26
|
-
}
|
|
20
|
+
#perspective: PerspectiveProxy
|
|
21
|
+
#base: string
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
}
|
|
23
|
+
constructor(perspective: PerspectiveProxy, base: string) {
|
|
24
|
+
this.#perspective = perspective
|
|
25
|
+
this.#base = base
|
|
26
|
+
}
|
|
41
27
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
}
|
|
28
|
+
get base() {
|
|
29
|
+
return this.#base
|
|
30
|
+
}
|
|
54
31
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}),
|
|
62
|
-
);
|
|
63
|
-
return links.map((link) => new SmartLiteral(perspective, link.data.source));
|
|
64
|
-
}
|
|
32
|
+
public static async create(perspective: PerspectiveProxy, literal: any): Promise<SmartLiteral> {
|
|
33
|
+
const base = Literal.from(makeRandomStringID(10)).toUrl()
|
|
34
|
+
const smartLiteral = new SmartLiteral(perspective, base)
|
|
35
|
+
await smartLiteral.set(literal)
|
|
36
|
+
return smartLiteral
|
|
37
|
+
}
|
|
65
38
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
39
|
+
public static async isSmartLiteralBase(perspective: PerspectiveProxy, base: string): Promise<boolean> {
|
|
40
|
+
let links = await perspective.get(new LinkQuery({
|
|
41
|
+
source: base,
|
|
42
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE
|
|
43
|
+
}))
|
|
44
|
+
return links.length > 0
|
|
45
|
+
}
|
|
73
46
|
|
|
74
|
-
|
|
75
|
-
|
|
47
|
+
public static async getAllSmartLiterals(perspective: PerspectiveProxy): Promise<SmartLiteral[]> {
|
|
48
|
+
let links = await perspective.get(new LinkQuery({
|
|
49
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE
|
|
50
|
+
}))
|
|
51
|
+
return links.map(link => new SmartLiteral(perspective, link.data.source))
|
|
76
52
|
}
|
|
77
53
|
|
|
78
|
-
|
|
79
|
-
|
|
54
|
+
async get(): Promise<any> {
|
|
55
|
+
let link = await this.#perspective.getSingleTarget(new LinkQuery({
|
|
56
|
+
source: this.#base,
|
|
57
|
+
predicate: SMART_LITERAL_CONTENT_PREDICATE
|
|
58
|
+
}))
|
|
59
|
+
|
|
60
|
+
if(!link) {
|
|
61
|
+
throw `No content for smart literal ${this.#base}`
|
|
62
|
+
}
|
|
80
63
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
64
|
+
return Literal.fromUrl(link).get()
|
|
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
|
+
}
|
|
75
|
+
}
|