@twin.org/data-json-ld 0.0.3-next.15 → 0.0.3-next.17
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/README.md +1 -1
- package/dist/es/helpers/jsonLdHelperTypes.js.map +1 -1
- package/dist/types/helpers/jsonLdHelperTypes.d.ts +43 -6
- package/docs/changelog.md +29 -1
- package/docs/examples.md +190 -1
- package/docs/reference/index.md +7 -0
- package/docs/reference/type-aliases/JsonLdExistingPropertyEither.md +24 -0
- package/docs/reference/type-aliases/JsonLdObjectWithAtId.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithAtType.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithId.md +5 -5
- package/docs/reference/type-aliases/JsonLdObjectWithNoAtId.md +11 -0
- package/docs/reference/type-aliases/JsonLdObjectWithNoAtType.md +11 -0
- package/docs/reference/type-aliases/JsonLdObjectWithNoId.md +2 -2
- package/docs/reference/type-aliases/JsonLdObjectWithNoType.md +2 -2
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalAtId.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalAtType.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalId.md +5 -5
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalType.md +5 -5
- package/docs/reference/type-aliases/JsonLdObjectWithType.md +5 -5
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# TWIN JSON LD Data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package provides JSON-LD data models and helper utilities for working with linked data documents in a predictable and reusable way. It aligns package-level data structures with the [JSON-LD](https://json-ld.org/) ecosystem, making it easier to represent interoperable semantic data across services.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonLdHelperTypes.js","sourceRoot":"","sources":["../../../src/helpers/jsonLdHelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IJsonLdContextDefinitionRoot } from \"../models/IJsonLdContextDefinitionRoot.js\";\n\n/**\n * Extract the optional property names from a type.\n */\nexport type JsonLdOptionalKeys<T> = {\n\t[K in keyof T]-?: {} extends Pick<T, K> ? K : never;\n}[keyof T];\n\n/**\n * Extract the required property names from a type.\n */\nexport type JsonLdRequiredKeys<T> = Exclude<keyof T, JsonLdOptionalKeys<T>>;\n\n/**\n * Keep JSON-LD keys as-is and prefix non-JSON-LD keys.\n */\nexport type JsonLdAliasKey<K extends string, Prefix extends string> = K extends `@${string}`\n\t? K\n\t: `${Prefix}:${K}`;\n\n/**\n * Remap an object type so JSON-LD keys (\"@...\") are preserved and\n * non-JSON-LD keys are exposed as `Prefix:key` aliases, while preserving\n * each key's original required/optional status.\n */\nexport type JsonLdWithAliases<T extends object, Prefix extends string> = {\n\t[K in Extract<JsonLdRequiredKeys<T>, string> as JsonLdAliasKey<K, Prefix>]: T[K];\n} & {\n\t[K in Extract<JsonLdOptionalKeys<T>, string> as JsonLdAliasKey<K, Prefix>]?: T[K];\n};\n\n/**\n * Keep only JSON-LD keys (\"@...\") from a type.\n */\nexport type JsonLdKeys<T extends object> = Pick<T, Extract<keyof T, `@${string}`>>;\n\n/**\n * Create a JSON-LD object shape containing only JSON-LD keys plus aliased\n * non-JSON-LD keys.\n */\nexport type JsonLdObjectWithAliases<T extends object, Prefix extends string> = JsonLdKeys<T> &\n\tJsonLdWithAliases<T, Prefix>;\n\n/**\n * Add \"@context\" to a type.\n */\nexport type JsonLdObjectWithContext<T extends object, C = IJsonLdContextDefinitionRoot> = Omit<\n\tT,\n\t\"@context\"\n> & {\n\t\"@context\": C;\n};\n\n/**\n * Infer an existing property's type from a source type, or fall back to a default.\n */\nexport type JsonLdExistingProperty<T extends object, P extends PropertyKey, D> = T extends {\n\t[K in P]?: infer PropertyType;\n}\n\t? Exclude<PropertyType, undefined>\n\t: D;\n\n/**\n * Add optional \"@context\" to a type, inferring an existing context type from\n * the source type when available, otherwise using the provided default.\n */\nexport type JsonLdObjectWithOptionalContext<\n\tT extends object,\n\tC = JsonLdExistingProperty<T, \"@context\", IJsonLdContextDefinitionRoot>\n> = Omit<T, \"@context\"> & {\n\t\"@context\"?: C;\n};\n\n/**\n * Omit optional \"@context\" from a type, inferring an existing context type from\n * the source type when available, otherwise using the provided default.\n */\nexport type JsonLdObjectWithNoContext<T extends object> = Omit<T, \"@context\">;\n\n/**\n * Add \"
|
|
1
|
+
{"version":3,"file":"jsonLdHelperTypes.js","sourceRoot":"","sources":["../../../src/helpers/jsonLdHelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IJsonLdContextDefinitionRoot } from \"../models/IJsonLdContextDefinitionRoot.js\";\n\n/**\n * Extract the optional property names from a type.\n */\nexport type JsonLdOptionalKeys<T> = {\n\t[K in keyof T]-?: {} extends Pick<T, K> ? K : never;\n}[keyof T];\n\n/**\n * Extract the required property names from a type.\n */\nexport type JsonLdRequiredKeys<T> = Exclude<keyof T, JsonLdOptionalKeys<T>>;\n\n/**\n * Keep JSON-LD keys as-is and prefix non-JSON-LD keys.\n */\nexport type JsonLdAliasKey<K extends string, Prefix extends string> = K extends `@${string}`\n\t? K\n\t: `${Prefix}:${K}`;\n\n/**\n * Remap an object type so JSON-LD keys (\"@...\") are preserved and\n * non-JSON-LD keys are exposed as `Prefix:key` aliases, while preserving\n * each key's original required/optional status.\n */\nexport type JsonLdWithAliases<T extends object, Prefix extends string> = {\n\t[K in Extract<JsonLdRequiredKeys<T>, string> as JsonLdAliasKey<K, Prefix>]: T[K];\n} & {\n\t[K in Extract<JsonLdOptionalKeys<T>, string> as JsonLdAliasKey<K, Prefix>]?: T[K];\n};\n\n/**\n * Keep only JSON-LD keys (\"@...\") from a type.\n */\nexport type JsonLdKeys<T extends object> = Pick<T, Extract<keyof T, `@${string}`>>;\n\n/**\n * Create a JSON-LD object shape containing only JSON-LD keys plus aliased\n * non-JSON-LD keys.\n */\nexport type JsonLdObjectWithAliases<T extends object, Prefix extends string> = JsonLdKeys<T> &\n\tJsonLdWithAliases<T, Prefix>;\n\n/**\n * Add \"@context\" to a type.\n */\nexport type JsonLdObjectWithContext<T extends object, C = IJsonLdContextDefinitionRoot> = Omit<\n\tT,\n\t\"@context\"\n> & {\n\t\"@context\": C;\n};\n\n/**\n * Infer an existing property's type from a source type, or fall back to a default.\n */\nexport type JsonLdExistingProperty<T extends object, P extends PropertyKey, D> = T extends {\n\t[K in P]?: infer PropertyType;\n}\n\t? Exclude<PropertyType, undefined>\n\t: D;\n\n/**\n * Infer an existing property's type from either of two source properties,\n * or fall back to a default when neither exists.\n */\nexport type JsonLdExistingPropertyEither<\n\tT extends object,\n\tP1 extends PropertyKey,\n\tP2 extends PropertyKey,\n\tD\n> = [JsonLdExistingProperty<T, P1, never> | JsonLdExistingProperty<T, P2, never>] extends [never]\n\t? D\n\t: JsonLdExistingProperty<T, P1, never> | JsonLdExistingProperty<T, P2, never>;\n\n/**\n * Add optional \"@context\" to a type, inferring an existing context type from\n * the source type when available, otherwise using the provided default.\n */\nexport type JsonLdObjectWithOptionalContext<\n\tT extends object,\n\tC = JsonLdExistingProperty<T, \"@context\", IJsonLdContextDefinitionRoot>\n> = Omit<T, \"@context\"> & {\n\t\"@context\"?: C;\n};\n\n/**\n * Omit optional \"@context\" from a type, inferring an existing context type from\n * the source type when available, otherwise using the provided default.\n */\nexport type JsonLdObjectWithNoContext<T extends object> = Omit<T, \"@context\">;\n\n/**\n * Add \"type\" to a type.\n */\nexport type JsonLdObjectWithType<\n\tT extends object,\n\tTy = JsonLdExistingPropertyEither<T, \"type\", \"@type\", string | string[]>\n> = Omit<T, \"type\" | \"@type\"> & {\n\ttype: Ty;\n};\n\n/**\n * Add optional \"type\" to a type.\n */\nexport type JsonLdObjectWithOptionalType<\n\tT extends object,\n\tTy = JsonLdExistingPropertyEither<T, \"type\", \"@type\", string | string[]>\n> = Omit<T, \"type\" | \"@type\"> & {\n\ttype?: Ty;\n};\n\n/**\n * Omit \"type\" from a type.\n */\nexport type JsonLdObjectWithNoType<T extends object> = Omit<T, \"type\">;\n\n/**\n * Add \"@type\" to a type.\n */\nexport type JsonLdObjectWithAtType<\n\tT extends object,\n\tTy = JsonLdExistingPropertyEither<T, \"@type\", \"type\", string | string[]>\n> = Omit<T, \"@type\" | \"type\"> & {\n\t\"@type\": Ty;\n};\n\n/**\n * Add optional \"@type\" to a type.\n */\nexport type JsonLdObjectWithOptionalAtType<\n\tT extends object,\n\tTy = JsonLdExistingPropertyEither<T, \"@type\", \"type\", string | string[]>\n> = Omit<T, \"@type\" | \"type\"> & {\n\t\"@type\"?: Ty;\n};\n\n/**\n * Omit \"@type\" from a type.\n */\nexport type JsonLdObjectWithNoAtType<T extends object> = Omit<T, \"@type\">;\n\n/**\n * Add \"id\" to a type.\n */\nexport type JsonLdObjectWithId<\n\tT extends object,\n\tId = JsonLdExistingPropertyEither<T, \"id\", \"@id\", string>\n> = Omit<T, \"id\" | \"@id\"> & {\n\tid: Id;\n};\n\n/**\n * Add optional \"id\" to a type.\n */\nexport type JsonLdObjectWithOptionalId<\n\tT extends object,\n\tId = JsonLdExistingPropertyEither<T, \"id\", \"@id\", string>\n> = Omit<T, \"id\" | \"@id\"> & {\n\tid?: Id;\n};\n\n/**\n * Omit \"id\" from a type.\n */\nexport type JsonLdObjectWithNoId<T extends object> = Omit<T, \"id\">;\n\n/**\n * Add \"@id\" to a type.\n */\nexport type JsonLdObjectWithAtId<\n\tT extends object,\n\tId = JsonLdExistingPropertyEither<T, \"@id\", \"id\", string>\n> = Omit<T, \"@id\" | \"id\"> & {\n\t\"@id\": Id;\n};\n\n/**\n * Add optional \"@id\" to a type.\n */\nexport type JsonLdObjectWithOptionalAtId<\n\tT extends object,\n\tId = JsonLdExistingPropertyEither<T, \"@id\", \"id\", string>\n> = Omit<T, \"@id\" | \"id\"> & {\n\t\"@id\"?: Id;\n};\n\n/**\n * Omit \"@id\" from a type.\n */\nexport type JsonLdObjectWithNoAtId<T extends object> = Omit<T, \"@id\">;\n"]}
|
|
@@ -44,6 +44,11 @@ export type JsonLdObjectWithContext<T extends object, C = IJsonLdContextDefiniti
|
|
|
44
44
|
export type JsonLdExistingProperty<T extends object, P extends PropertyKey, D> = T extends {
|
|
45
45
|
[K in P]?: infer PropertyType;
|
|
46
46
|
} ? Exclude<PropertyType, undefined> : D;
|
|
47
|
+
/**
|
|
48
|
+
* Infer an existing property's type from either of two source properties,
|
|
49
|
+
* or fall back to a default when neither exists.
|
|
50
|
+
*/
|
|
51
|
+
export type JsonLdExistingPropertyEither<T extends object, P1 extends PropertyKey, P2 extends PropertyKey, D> = [JsonLdExistingProperty<T, P1, never> | JsonLdExistingProperty<T, P2, never>] extends [never] ? D : JsonLdExistingProperty<T, P1, never> | JsonLdExistingProperty<T, P2, never>;
|
|
47
52
|
/**
|
|
48
53
|
* Add optional "@context" to a type, inferring an existing context type from
|
|
49
54
|
* the source type when available, otherwise using the provided default.
|
|
@@ -56,35 +61,67 @@ export type JsonLdObjectWithOptionalContext<T extends object, C = JsonLdExisting
|
|
|
56
61
|
* the source type when available, otherwise using the provided default.
|
|
57
62
|
*/
|
|
58
63
|
export type JsonLdObjectWithNoContext<T extends object> = Omit<T, "@context">;
|
|
64
|
+
/**
|
|
65
|
+
* Add "type" to a type.
|
|
66
|
+
*/
|
|
67
|
+
export type JsonLdObjectWithType<T extends object, Ty = JsonLdExistingPropertyEither<T, "type", "@type", string | string[]>> = Omit<T, "type" | "@type"> & {
|
|
68
|
+
type: Ty;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Add optional "type" to a type.
|
|
72
|
+
*/
|
|
73
|
+
export type JsonLdObjectWithOptionalType<T extends object, Ty = JsonLdExistingPropertyEither<T, "type", "@type", string | string[]>> = Omit<T, "type" | "@type"> & {
|
|
74
|
+
type?: Ty;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Omit "type" from a type.
|
|
78
|
+
*/
|
|
79
|
+
export type JsonLdObjectWithNoType<T extends object> = Omit<T, "type">;
|
|
59
80
|
/**
|
|
60
81
|
* Add "@type" to a type.
|
|
61
82
|
*/
|
|
62
|
-
export type
|
|
83
|
+
export type JsonLdObjectWithAtType<T extends object, Ty = JsonLdExistingPropertyEither<T, "@type", "type", string | string[]>> = Omit<T, "@type" | "type"> & {
|
|
63
84
|
"@type": Ty;
|
|
64
85
|
};
|
|
65
86
|
/**
|
|
66
87
|
* Add optional "@type" to a type.
|
|
67
88
|
*/
|
|
68
|
-
export type
|
|
89
|
+
export type JsonLdObjectWithOptionalAtType<T extends object, Ty = JsonLdExistingPropertyEither<T, "@type", "type", string | string[]>> = Omit<T, "@type" | "type"> & {
|
|
69
90
|
"@type"?: Ty;
|
|
70
91
|
};
|
|
71
92
|
/**
|
|
72
93
|
* Omit "@type" from a type.
|
|
73
94
|
*/
|
|
74
|
-
export type
|
|
95
|
+
export type JsonLdObjectWithNoAtType<T extends object> = Omit<T, "@type">;
|
|
96
|
+
/**
|
|
97
|
+
* Add "id" to a type.
|
|
98
|
+
*/
|
|
99
|
+
export type JsonLdObjectWithId<T extends object, Id = JsonLdExistingPropertyEither<T, "id", "@id", string>> = Omit<T, "id" | "@id"> & {
|
|
100
|
+
id: Id;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Add optional "id" to a type.
|
|
104
|
+
*/
|
|
105
|
+
export type JsonLdObjectWithOptionalId<T extends object, Id = JsonLdExistingPropertyEither<T, "id", "@id", string>> = Omit<T, "id" | "@id"> & {
|
|
106
|
+
id?: Id;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Omit "id" from a type.
|
|
110
|
+
*/
|
|
111
|
+
export type JsonLdObjectWithNoId<T extends object> = Omit<T, "id">;
|
|
75
112
|
/**
|
|
76
113
|
* Add "@id" to a type.
|
|
77
114
|
*/
|
|
78
|
-
export type
|
|
115
|
+
export type JsonLdObjectWithAtId<T extends object, Id = JsonLdExistingPropertyEither<T, "@id", "id", string>> = Omit<T, "@id" | "id"> & {
|
|
79
116
|
"@id": Id;
|
|
80
117
|
};
|
|
81
118
|
/**
|
|
82
119
|
* Add optional "@id" to a type.
|
|
83
120
|
*/
|
|
84
|
-
export type
|
|
121
|
+
export type JsonLdObjectWithOptionalAtId<T extends object, Id = JsonLdExistingPropertyEither<T, "@id", "id", string>> = Omit<T, "@id" | "id"> & {
|
|
85
122
|
"@id"?: Id;
|
|
86
123
|
};
|
|
87
124
|
/**
|
|
88
125
|
* Omit "@id" from a type.
|
|
89
126
|
*/
|
|
90
|
-
export type
|
|
127
|
+
export type JsonLdObjectWithNoAtId<T extends object> = Omit<T, "@id">;
|
package/docs/changelog.md
CHANGED
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.3-next.17](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.16...data-json-ld-v0.0.3-next.17) (2026-03-12)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **data-json-ld:** Synchronize repo versions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/data-core bumped from 0.0.3-next.16 to 0.0.3-next.17
|
|
16
|
+
|
|
17
|
+
## [0.0.3-next.16](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.15...data-json-ld-v0.0.3-next.16) (2026-03-06)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* additional JSON-LD helper types ([575627c](https://github.com/twinfoundation/data/commit/575627c707e691086e07163dac44ba1396ac322e))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/data-core bumped from 0.0.3-next.15 to 0.0.3-next.16
|
|
2
30
|
|
|
3
31
|
## [0.0.3-next.15](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.14...data-json-ld-v0.0.3-next.15) (2026-02-27)
|
|
4
32
|
|
package/docs/examples.md
CHANGED
|
@@ -1 +1,190 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Data Json Ld Examples
|
|
2
|
+
|
|
3
|
+
These examples walk through context management, document processing, validation, and property access patterns for JSON-LD documents.
|
|
4
|
+
|
|
5
|
+
## JsonLdProcessor
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { JsonLdProcessor } from '@twin.org/data-json-ld';
|
|
9
|
+
|
|
10
|
+
JsonLdProcessor.setCacheLimit(900000);
|
|
11
|
+
console.log('Cache limit:', JsonLdProcessor.getCacheLimit());
|
|
12
|
+
|
|
13
|
+
JsonLdProcessor.setRedirects([
|
|
14
|
+
{
|
|
15
|
+
from: /^https:\/\/raw\.githubusercontent\.com\//,
|
|
16
|
+
to: 'https://cdn.example.org/github-content'
|
|
17
|
+
}
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
JsonLdProcessor.addRedirect(/^https:\/\/schema\.org\//, 'https://schema.org/');
|
|
21
|
+
console.log('Has redirects:', JsonLdProcessor.getRedirects().length > 0);
|
|
22
|
+
|
|
23
|
+
const loader = JsonLdProcessor.getDocumentLoader();
|
|
24
|
+
console.log('Loader type:', typeof loader);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { JsonLdProcessor, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
29
|
+
|
|
30
|
+
const compactedDoc = {
|
|
31
|
+
'@context': {
|
|
32
|
+
ex: 'https://example.org/vocab/',
|
|
33
|
+
name: 'ex:name'
|
|
34
|
+
},
|
|
35
|
+
'@id': 'urn:example:person:1',
|
|
36
|
+
name: 'Ada'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const expanded = await JsonLdProcessor.expand(compactedDoc);
|
|
40
|
+
const compactedAgain = await JsonLdProcessor.compact(expanded[0], compactedDoc['@context']);
|
|
41
|
+
|
|
42
|
+
console.log('Compacted id:', compactedAgain['@id']);
|
|
43
|
+
|
|
44
|
+
const canonical = await JsonLdProcessor.canonize(expanded[0] as IJsonLdNodeObject);
|
|
45
|
+
console.log('Canonical includes id:', canonical.includes('urn:example:person:1'));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { JsonLdProcessor, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
50
|
+
|
|
51
|
+
const contextA = 'https://example.org/context/base';
|
|
52
|
+
const contextB = {
|
|
53
|
+
ex: 'https://example.org/vocab/',
|
|
54
|
+
name: 'ex:name'
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const combined = JsonLdProcessor.combineContexts(contextA, contextB);
|
|
58
|
+
|
|
59
|
+
const documentWithNestedContexts: IJsonLdNodeObject = {
|
|
60
|
+
'@context': contextA,
|
|
61
|
+
'@graph': [
|
|
62
|
+
{
|
|
63
|
+
'@context': contextB,
|
|
64
|
+
'@id': 'urn:example:item:1',
|
|
65
|
+
'@type': 'ex:Person',
|
|
66
|
+
'ex:name': 'Ada'
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const gathered = JsonLdProcessor.gatherContexts(documentWithNestedContexts);
|
|
72
|
+
const filtered = JsonLdProcessor.removeContexts(gathered, [contextB]);
|
|
73
|
+
|
|
74
|
+
console.log('Combined is array:', Array.isArray(combined));
|
|
75
|
+
console.log('Filtered context equals contextA:', filtered === contextA);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { JsonLdProcessor } from '@twin.org/data-json-ld';
|
|
80
|
+
|
|
81
|
+
await JsonLdProcessor.documentCacheAdd('https://example.org/context', {
|
|
82
|
+
'@context': {
|
|
83
|
+
ex: 'https://example.org/vocab/'
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
await JsonLdProcessor.documentCacheRemove('https://example.org/context');
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## JsonLdHelper
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { type IValidationFailure } from '@twin.org/core';
|
|
94
|
+
import { ValidationMode } from '@twin.org/data-core';
|
|
95
|
+
import { JsonLdHelper, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
96
|
+
|
|
97
|
+
const compactedDocument = {
|
|
98
|
+
'@context': {
|
|
99
|
+
ex: 'https://example.org/vocab/',
|
|
100
|
+
id: '@id',
|
|
101
|
+
type: '@type',
|
|
102
|
+
name: 'ex:name'
|
|
103
|
+
},
|
|
104
|
+
id: 'urn:example:person:1',
|
|
105
|
+
type: 'ex:Person',
|
|
106
|
+
name: 'Ada'
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const failures: IValidationFailure[] = [];
|
|
110
|
+
await JsonLdHelper.validate(compactedDocument, failures, {
|
|
111
|
+
validationMode: ValidationMode.Either,
|
|
112
|
+
failOnMissingType: false
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const expanded = await JsonLdHelper.expand(compactedDocument);
|
|
116
|
+
const hasPersonType = await JsonLdHelper.isType(expanded, ['https://example.org/vocab/Person']);
|
|
117
|
+
const types = await JsonLdHelper.getType(expanded);
|
|
118
|
+
const id = await JsonLdHelper.getId(expanded);
|
|
119
|
+
|
|
120
|
+
console.log('Failure count:', failures.length);
|
|
121
|
+
console.log('Has person type:', hasPersonType);
|
|
122
|
+
console.log('Type list includes person:', types.includes('https://example.org/vocab/Person'));
|
|
123
|
+
console.log('Document id:', id);
|
|
124
|
+
|
|
125
|
+
const nodeObject = JsonLdHelper.toNodeObject(compactedDocument);
|
|
126
|
+
const structuredObject = JsonLdHelper.toStructuredObject<{ name: string }>(
|
|
127
|
+
nodeObject as IJsonLdNodeObject
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
console.log('Structured name:', structuredObject.name);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { JsonLdHelper } from '@twin.org/data-json-ld';
|
|
135
|
+
|
|
136
|
+
const expandedDocument = [
|
|
137
|
+
{
|
|
138
|
+
'@id': 'urn:example:article:1',
|
|
139
|
+
'https://schema.org/name': [
|
|
140
|
+
{ '@value': 'Hello', '@language': 'en' },
|
|
141
|
+
{ '@value': 'Hallo', '@language': 'de' }
|
|
142
|
+
],
|
|
143
|
+
'https://schema.org/wordCount': [{ '@value': 240 }]
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
const title = await JsonLdHelper.getPropertyValue(
|
|
148
|
+
expandedDocument,
|
|
149
|
+
'https://schema.org/name',
|
|
150
|
+
'en'
|
|
151
|
+
);
|
|
152
|
+
const [nameValues, wordCountValues] = await JsonLdHelper.getPropertyValues(expandedDocument, [
|
|
153
|
+
'https://schema.org/name',
|
|
154
|
+
'https://schema.org/wordCount'
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
console.log('English title:', title?.[0]);
|
|
158
|
+
console.log('Name value count:', nameValues?.length);
|
|
159
|
+
console.log('Word count value:', wordCountValues?.[0]);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { JsonLdHelper } from '@twin.org/data-json-ld';
|
|
164
|
+
|
|
165
|
+
const node = {
|
|
166
|
+
'@id': 'urn:example:item:1',
|
|
167
|
+
name: 'Ada',
|
|
168
|
+
role: 'Engineer'
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const prefixed = JsonLdHelper.prefixProperties(node, 'ex', ['name', 'role']);
|
|
172
|
+
const stripped = JsonLdHelper.stripPrefixProperties(prefixed, 'ex');
|
|
173
|
+
|
|
174
|
+
console.log('Prefixed name:', prefixed['ex:name']);
|
|
175
|
+
console.log('Stripped name:', stripped.name);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## JsonLdDataTypes
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { DataTypeHelper } from '@twin.org/data-core';
|
|
182
|
+
import { JsonLdContexts, JsonLdDataTypes, JsonLdTypes } from '@twin.org/data-json-ld';
|
|
183
|
+
|
|
184
|
+
JsonLdDataTypes.registerTypes();
|
|
185
|
+
|
|
186
|
+
const schema = await DataTypeHelper.getSchemaForType(
|
|
187
|
+
`${JsonLdContexts.Namespace}${JsonLdTypes.Document}`
|
|
188
|
+
);
|
|
189
|
+
console.log('Registered schema type:', schema?.type);
|
|
190
|
+
```
|
package/docs/reference/index.md
CHANGED
|
@@ -30,14 +30,21 @@
|
|
|
30
30
|
- [JsonLdObjectWithAliases](type-aliases/JsonLdObjectWithAliases.md)
|
|
31
31
|
- [JsonLdObjectWithContext](type-aliases/JsonLdObjectWithContext.md)
|
|
32
32
|
- [JsonLdExistingProperty](type-aliases/JsonLdExistingProperty.md)
|
|
33
|
+
- [JsonLdExistingPropertyEither](type-aliases/JsonLdExistingPropertyEither.md)
|
|
33
34
|
- [JsonLdObjectWithOptionalContext](type-aliases/JsonLdObjectWithOptionalContext.md)
|
|
34
35
|
- [JsonLdObjectWithNoContext](type-aliases/JsonLdObjectWithNoContext.md)
|
|
35
36
|
- [JsonLdObjectWithType](type-aliases/JsonLdObjectWithType.md)
|
|
36
37
|
- [JsonLdObjectWithOptionalType](type-aliases/JsonLdObjectWithOptionalType.md)
|
|
37
38
|
- [JsonLdObjectWithNoType](type-aliases/JsonLdObjectWithNoType.md)
|
|
39
|
+
- [JsonLdObjectWithAtType](type-aliases/JsonLdObjectWithAtType.md)
|
|
40
|
+
- [JsonLdObjectWithOptionalAtType](type-aliases/JsonLdObjectWithOptionalAtType.md)
|
|
41
|
+
- [JsonLdObjectWithNoAtType](type-aliases/JsonLdObjectWithNoAtType.md)
|
|
38
42
|
- [JsonLdObjectWithId](type-aliases/JsonLdObjectWithId.md)
|
|
39
43
|
- [JsonLdObjectWithOptionalId](type-aliases/JsonLdObjectWithOptionalId.md)
|
|
40
44
|
- [JsonLdObjectWithNoId](type-aliases/JsonLdObjectWithNoId.md)
|
|
45
|
+
- [JsonLdObjectWithAtId](type-aliases/JsonLdObjectWithAtId.md)
|
|
46
|
+
- [JsonLdObjectWithOptionalAtId](type-aliases/JsonLdObjectWithOptionalAtId.md)
|
|
47
|
+
- [JsonLdObjectWithNoAtId](type-aliases/JsonLdObjectWithNoAtId.md)
|
|
41
48
|
- [IJsonLdContainerType](type-aliases/IJsonLdContainerType.md)
|
|
42
49
|
- [IJsonLdContainerTypeArray](type-aliases/IJsonLdContainerTypeArray.md)
|
|
43
50
|
- [IJsonLdContextDefinitionElement](type-aliases/IJsonLdContextDefinitionElement.md)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Type Alias: JsonLdExistingPropertyEither\<T, P1, P2, D\>
|
|
2
|
+
|
|
3
|
+
> **JsonLdExistingPropertyEither**\<`T`, `P1`, `P2`, `D`\> = \[[`JsonLdExistingProperty`](JsonLdExistingProperty.md)\<`T`, `P1`, `never`\> \| [`JsonLdExistingProperty`](JsonLdExistingProperty.md)\<`T`, `P2`, `never`\>\] *extends* \[`never`\] ? `D` : [`JsonLdExistingProperty`](JsonLdExistingProperty.md)\<`T`, `P1`, `never`\> \| [`JsonLdExistingProperty`](JsonLdExistingProperty.md)\<`T`, `P2`, `never`\>
|
|
4
|
+
|
|
5
|
+
Infer an existing property's type from either of two source properties,
|
|
6
|
+
or fall back to a default when neither exists.
|
|
7
|
+
|
|
8
|
+
## Type Parameters
|
|
9
|
+
|
|
10
|
+
### T
|
|
11
|
+
|
|
12
|
+
`T` *extends* `object`
|
|
13
|
+
|
|
14
|
+
### P1
|
|
15
|
+
|
|
16
|
+
`P1` *extends* `PropertyKey`
|
|
17
|
+
|
|
18
|
+
### P2
|
|
19
|
+
|
|
20
|
+
`P2` *extends* `PropertyKey`
|
|
21
|
+
|
|
22
|
+
### D
|
|
23
|
+
|
|
24
|
+
`D`
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Type Alias: JsonLdObjectWithAtId\<T, Id\>
|
|
2
|
+
|
|
3
|
+
> **JsonLdObjectWithAtId**\<`T`, `Id`\> = `Omit`\<`T`, `"@id"` \| `"id"`\> & `object`
|
|
4
|
+
|
|
5
|
+
Add "@id" to a type.
|
|
6
|
+
|
|
7
|
+
## Type Declaration
|
|
8
|
+
|
|
9
|
+
### @id
|
|
10
|
+
|
|
11
|
+
> **@id**: `Id`
|
|
12
|
+
|
|
13
|
+
## Type Parameters
|
|
14
|
+
|
|
15
|
+
### T
|
|
16
|
+
|
|
17
|
+
`T` *extends* `object`
|
|
18
|
+
|
|
19
|
+
### Id
|
|
20
|
+
|
|
21
|
+
`Id` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"@id"`, `"id"`, `string`\>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Type Alias: JsonLdObjectWithAtType\<T, Ty\>
|
|
2
|
+
|
|
3
|
+
> **JsonLdObjectWithAtType**\<`T`, `Ty`\> = `Omit`\<`T`, `"@type"` \| `"type"`\> & `object`
|
|
4
|
+
|
|
5
|
+
Add "@type" to a type.
|
|
6
|
+
|
|
7
|
+
## Type Declaration
|
|
8
|
+
|
|
9
|
+
### @type
|
|
10
|
+
|
|
11
|
+
> **@type**: `Ty`
|
|
12
|
+
|
|
13
|
+
## Type Parameters
|
|
14
|
+
|
|
15
|
+
### T
|
|
16
|
+
|
|
17
|
+
`T` *extends* `object`
|
|
18
|
+
|
|
19
|
+
### Ty
|
|
20
|
+
|
|
21
|
+
`Ty` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"@type"`, `"type"`, `string` \| `string`[]\>
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Type Alias: JsonLdObjectWithId\<T, Id\>
|
|
2
2
|
|
|
3
|
-
> **JsonLdObjectWithId**\<`T`, `Id`\> = `Omit`\<`T`, `"@id"`\> & `object`
|
|
3
|
+
> **JsonLdObjectWithId**\<`T`, `Id`\> = `Omit`\<`T`, `"id"` \| `"@id"`\> & `object`
|
|
4
4
|
|
|
5
|
-
Add "
|
|
5
|
+
Add "id" to a type.
|
|
6
6
|
|
|
7
7
|
## Type Declaration
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### id
|
|
10
10
|
|
|
11
|
-
>
|
|
11
|
+
> **id**: `Id`
|
|
12
12
|
|
|
13
13
|
## Type Parameters
|
|
14
14
|
|
|
@@ -18,4 +18,4 @@ Add "@id" to a type.
|
|
|
18
18
|
|
|
19
19
|
### Id
|
|
20
20
|
|
|
21
|
-
`Id` = [`
|
|
21
|
+
`Id` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"id"`, `"@id"`, `string`\>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Type Alias: JsonLdObjectWithOptionalAtId\<T, Id\>
|
|
2
|
+
|
|
3
|
+
> **JsonLdObjectWithOptionalAtId**\<`T`, `Id`\> = `Omit`\<`T`, `"@id"` \| `"id"`\> & `object`
|
|
4
|
+
|
|
5
|
+
Add optional "@id" to a type.
|
|
6
|
+
|
|
7
|
+
## Type Declaration
|
|
8
|
+
|
|
9
|
+
### @id?
|
|
10
|
+
|
|
11
|
+
> `optional` **@id**: `Id`
|
|
12
|
+
|
|
13
|
+
## Type Parameters
|
|
14
|
+
|
|
15
|
+
### T
|
|
16
|
+
|
|
17
|
+
`T` *extends* `object`
|
|
18
|
+
|
|
19
|
+
### Id
|
|
20
|
+
|
|
21
|
+
`Id` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"@id"`, `"id"`, `string`\>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Type Alias: JsonLdObjectWithOptionalAtType\<T, Ty\>
|
|
2
|
+
|
|
3
|
+
> **JsonLdObjectWithOptionalAtType**\<`T`, `Ty`\> = `Omit`\<`T`, `"@type"` \| `"type"`\> & `object`
|
|
4
|
+
|
|
5
|
+
Add optional "@type" to a type.
|
|
6
|
+
|
|
7
|
+
## Type Declaration
|
|
8
|
+
|
|
9
|
+
### @type?
|
|
10
|
+
|
|
11
|
+
> `optional` **@type**: `Ty`
|
|
12
|
+
|
|
13
|
+
## Type Parameters
|
|
14
|
+
|
|
15
|
+
### T
|
|
16
|
+
|
|
17
|
+
`T` *extends* `object`
|
|
18
|
+
|
|
19
|
+
### Ty
|
|
20
|
+
|
|
21
|
+
`Ty` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"@type"`, `"type"`, `string` \| `string`[]\>
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Type Alias: JsonLdObjectWithOptionalId\<T, Id\>
|
|
2
2
|
|
|
3
|
-
> **JsonLdObjectWithOptionalId**\<`T`, `Id`\> = `Omit`\<`T`, `"@id"`\> & `object`
|
|
3
|
+
> **JsonLdObjectWithOptionalId**\<`T`, `Id`\> = `Omit`\<`T`, `"id"` \| `"@id"`\> & `object`
|
|
4
4
|
|
|
5
|
-
Add optional "
|
|
5
|
+
Add optional "id" to a type.
|
|
6
6
|
|
|
7
7
|
## Type Declaration
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### id?
|
|
10
10
|
|
|
11
|
-
> `optional`
|
|
11
|
+
> `optional` **id**: `Id`
|
|
12
12
|
|
|
13
13
|
## Type Parameters
|
|
14
14
|
|
|
@@ -18,4 +18,4 @@ Add optional "@id" to a type.
|
|
|
18
18
|
|
|
19
19
|
### Id
|
|
20
20
|
|
|
21
|
-
`Id` = [`
|
|
21
|
+
`Id` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"id"`, `"@id"`, `string`\>
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Type Alias: JsonLdObjectWithOptionalType\<T, Ty\>
|
|
2
2
|
|
|
3
|
-
> **JsonLdObjectWithOptionalType**\<`T`, `Ty`\> = `Omit`\<`T`, `"@type"`\> & `object`
|
|
3
|
+
> **JsonLdObjectWithOptionalType**\<`T`, `Ty`\> = `Omit`\<`T`, `"type"` \| `"@type"`\> & `object`
|
|
4
4
|
|
|
5
|
-
Add optional "
|
|
5
|
+
Add optional "type" to a type.
|
|
6
6
|
|
|
7
7
|
## Type Declaration
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### type?
|
|
10
10
|
|
|
11
|
-
> `optional`
|
|
11
|
+
> `optional` **type**: `Ty`
|
|
12
12
|
|
|
13
13
|
## Type Parameters
|
|
14
14
|
|
|
@@ -18,4 +18,4 @@ Add optional "@type" to a type.
|
|
|
18
18
|
|
|
19
19
|
### Ty
|
|
20
20
|
|
|
21
|
-
`Ty` = [`
|
|
21
|
+
`Ty` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"type"`, `"@type"`, `string` \| `string`[]\>
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Type Alias: JsonLdObjectWithType\<T, Ty\>
|
|
2
2
|
|
|
3
|
-
> **JsonLdObjectWithType**\<`T`, `Ty`\> = `Omit`\<`T`, `"@type"`\> & `object`
|
|
3
|
+
> **JsonLdObjectWithType**\<`T`, `Ty`\> = `Omit`\<`T`, `"type"` \| `"@type"`\> & `object`
|
|
4
4
|
|
|
5
|
-
Add "
|
|
5
|
+
Add "type" to a type.
|
|
6
6
|
|
|
7
7
|
## Type Declaration
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### type
|
|
10
10
|
|
|
11
|
-
>
|
|
11
|
+
> **type**: `Ty`
|
|
12
12
|
|
|
13
13
|
## Type Parameters
|
|
14
14
|
|
|
@@ -18,4 +18,4 @@ Add "@type" to a type.
|
|
|
18
18
|
|
|
19
19
|
### Ty
|
|
20
20
|
|
|
21
|
-
`Ty` = [`
|
|
21
|
+
`Ty` = [`JsonLdExistingPropertyEither`](JsonLdExistingPropertyEither.md)\<`T`, `"type"`, `"@type"`, `string` \| `string`[]\>
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.3-next.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3-next.17",
|
|
4
|
+
"description": "JSON-LD data models and helpers for working with linked data documents.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/twinfoundation/data.git",
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@twin.org/core": "next",
|
|
18
|
-
"@twin.org/data-core": "0.0.3-next.
|
|
18
|
+
"@twin.org/data-core": "0.0.3-next.17",
|
|
19
19
|
"@twin.org/entity": "next",
|
|
20
20
|
"@twin.org/nameof": "next",
|
|
21
21
|
"@twin.org/web": "next",
|
|
22
22
|
"@types/jsonld": "1.5.15",
|
|
23
|
-
"jsonld": "
|
|
23
|
+
"jsonld": "9.0.0"
|
|
24
24
|
},
|
|
25
25
|
"main": "./dist/es/index.js",
|
|
26
26
|
"types": "./dist/types/index.d.ts",
|