@twin.org/data-json-ld 0.0.1-next.3 → 0.0.1-next.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/dist/cjs/index.cjs +87 -110
- package/dist/esm/index.mjs +88 -111
- package/dist/types/models/IJsonLdDocument.d.ts +9 -1
- package/dist/types/utils/jsonLdHelper.d.ts +16 -1
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/JsonLdHelper.md +56 -0
- package/docs/reference/index.md +2 -0
- package/docs/reference/interfaces/IJsonLdGraphObject.md +1 -1
- package/docs/reference/interfaces/IJsonLdNodeObject.md +1 -1
- package/docs/reference/type-aliases/IJsonLdContextDefinitionElement.md +5 -0
- package/docs/reference/type-aliases/IJsonLdContextDefinitionRoot.md +5 -0
- package/docs/reference/type-aliases/IJsonLdKeyword.md +1 -1
- package/locales/en.json +3 -2
- package/package.json +3 -33
package/dist/cjs/index.cjs
CHANGED
|
@@ -374,33 +374,7 @@ var anyOf$7 = [
|
|
|
374
374
|
type: "object",
|
|
375
375
|
properties: {
|
|
376
376
|
"@context": {
|
|
377
|
-
|
|
378
|
-
{
|
|
379
|
-
type: "null"
|
|
380
|
-
},
|
|
381
|
-
{
|
|
382
|
-
type: "string"
|
|
383
|
-
},
|
|
384
|
-
{
|
|
385
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
386
|
-
},
|
|
387
|
-
{
|
|
388
|
-
type: "array",
|
|
389
|
-
items: {
|
|
390
|
-
anyOf: [
|
|
391
|
-
{
|
|
392
|
-
type: "null"
|
|
393
|
-
},
|
|
394
|
-
{
|
|
395
|
-
type: "string"
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
399
|
-
}
|
|
400
|
-
]
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
]
|
|
377
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
404
378
|
},
|
|
405
379
|
"@graph": {
|
|
406
380
|
anyOf: [
|
|
@@ -562,33 +536,7 @@ var properties$4 = {
|
|
|
562
536
|
]
|
|
563
537
|
},
|
|
564
538
|
"@context": {
|
|
565
|
-
|
|
566
|
-
{
|
|
567
|
-
type: "null"
|
|
568
|
-
},
|
|
569
|
-
{
|
|
570
|
-
type: "string"
|
|
571
|
-
},
|
|
572
|
-
{
|
|
573
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
574
|
-
},
|
|
575
|
-
{
|
|
576
|
-
type: "array",
|
|
577
|
-
items: {
|
|
578
|
-
anyOf: [
|
|
579
|
-
{
|
|
580
|
-
type: "null"
|
|
581
|
-
},
|
|
582
|
-
{
|
|
583
|
-
type: "string"
|
|
584
|
-
},
|
|
585
|
-
{
|
|
586
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
587
|
-
}
|
|
588
|
-
]
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
]
|
|
539
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
592
540
|
}
|
|
593
541
|
};
|
|
594
542
|
var required$3 = [
|
|
@@ -784,33 +732,7 @@ var properties$3 = {
|
|
|
784
732
|
]
|
|
785
733
|
},
|
|
786
734
|
"@context": {
|
|
787
|
-
|
|
788
|
-
{
|
|
789
|
-
type: "null"
|
|
790
|
-
},
|
|
791
|
-
{
|
|
792
|
-
type: "string"
|
|
793
|
-
},
|
|
794
|
-
{
|
|
795
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
796
|
-
},
|
|
797
|
-
{
|
|
798
|
-
type: "array",
|
|
799
|
-
items: {
|
|
800
|
-
anyOf: [
|
|
801
|
-
{
|
|
802
|
-
type: "null"
|
|
803
|
-
},
|
|
804
|
-
{
|
|
805
|
-
type: "string"
|
|
806
|
-
},
|
|
807
|
-
{
|
|
808
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
809
|
-
}
|
|
810
|
-
]
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
]
|
|
735
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
814
736
|
},
|
|
815
737
|
"@direction": {
|
|
816
738
|
type: [
|
|
@@ -1062,33 +984,7 @@ var JsonLdListOrSetItemSchema = {
|
|
|
1062
984
|
var type$2 = "object";
|
|
1063
985
|
var properties$1 = {
|
|
1064
986
|
"@context": {
|
|
1065
|
-
|
|
1066
|
-
{
|
|
1067
|
-
type: "null"
|
|
1068
|
-
},
|
|
1069
|
-
{
|
|
1070
|
-
type: "string"
|
|
1071
|
-
},
|
|
1072
|
-
{
|
|
1073
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1074
|
-
},
|
|
1075
|
-
{
|
|
1076
|
-
type: "array",
|
|
1077
|
-
items: {
|
|
1078
|
-
anyOf: [
|
|
1079
|
-
{
|
|
1080
|
-
type: "null"
|
|
1081
|
-
},
|
|
1082
|
-
{
|
|
1083
|
-
type: "string"
|
|
1084
|
-
},
|
|
1085
|
-
{
|
|
1086
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1087
|
-
}
|
|
1088
|
-
]
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
]
|
|
987
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
1092
988
|
},
|
|
1093
989
|
"@id": {
|
|
1094
990
|
anyOf: [
|
|
@@ -1507,6 +1403,77 @@ class JsonLdHelper {
|
|
|
1507
1403
|
}
|
|
1508
1404
|
return validationFailures.length === 0;
|
|
1509
1405
|
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Extract a property from the JSON-LD.
|
|
1408
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
1409
|
+
* @param propertyNames The possible names for the property.
|
|
1410
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
1411
|
+
* @returns The properties if available.
|
|
1412
|
+
*/
|
|
1413
|
+
static extractProperty(nodeObject, propertyNames, deleteProperty = true) {
|
|
1414
|
+
let retVal;
|
|
1415
|
+
for (const prop of propertyNames) {
|
|
1416
|
+
retVal ??= nodeObject[prop];
|
|
1417
|
+
if (deleteProperty) {
|
|
1418
|
+
delete nodeObject[prop];
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return retVal;
|
|
1422
|
+
}
|
|
1423
|
+
/**
|
|
1424
|
+
* Combine contexts.
|
|
1425
|
+
* @param context1 The first JSON-LD context to combine.
|
|
1426
|
+
* @param context2 The second JSON-LD context to combine.
|
|
1427
|
+
* @returns The combined context.
|
|
1428
|
+
*/
|
|
1429
|
+
static async combineContexts(context1, context2) {
|
|
1430
|
+
const combinedContext = [];
|
|
1431
|
+
if (core.Is.string(context1)) {
|
|
1432
|
+
if (!combinedContext.includes(context1)) {
|
|
1433
|
+
combinedContext.push(context1);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
else if (core.Is.array(context1)) {
|
|
1437
|
+
for (const context of context1) {
|
|
1438
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context));
|
|
1439
|
+
if (!hasMatch) {
|
|
1440
|
+
combinedContext.push(context);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
else if (core.Is.object(context1)) {
|
|
1445
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context1));
|
|
1446
|
+
if (!hasMatch) {
|
|
1447
|
+
combinedContext.push(context1);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
if (core.Is.string(context2)) {
|
|
1451
|
+
if (!combinedContext.includes(context2)) {
|
|
1452
|
+
combinedContext.push(context2);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
else if (core.Is.array(context2)) {
|
|
1456
|
+
for (const context of context2) {
|
|
1457
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context));
|
|
1458
|
+
if (!hasMatch) {
|
|
1459
|
+
combinedContext.push(context);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
else if (core.Is.object(context2)) {
|
|
1464
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context2));
|
|
1465
|
+
if (!hasMatch) {
|
|
1466
|
+
combinedContext.push(context2);
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
if (combinedContext.length === 0) {
|
|
1470
|
+
return null;
|
|
1471
|
+
}
|
|
1472
|
+
if (combinedContext.length === 1) {
|
|
1473
|
+
return combinedContext[0];
|
|
1474
|
+
}
|
|
1475
|
+
return combinedContext;
|
|
1476
|
+
}
|
|
1510
1477
|
}
|
|
1511
1478
|
|
|
1512
1479
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -1536,11 +1503,16 @@ class JsonLdProcessor {
|
|
|
1536
1503
|
*/
|
|
1537
1504
|
static async compact(document, context) {
|
|
1538
1505
|
try {
|
|
1539
|
-
|
|
1506
|
+
const compacted = await jsonLd.compact(document, context ?? {}, {
|
|
1540
1507
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1541
1508
|
});
|
|
1509
|
+
return compacted;
|
|
1542
1510
|
}
|
|
1543
1511
|
catch (err) {
|
|
1512
|
+
if (core.Is.object(err) &&
|
|
1513
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1514
|
+
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1515
|
+
}
|
|
1544
1516
|
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "compact", undefined, err);
|
|
1545
1517
|
}
|
|
1546
1518
|
}
|
|
@@ -1551,11 +1523,16 @@ class JsonLdProcessor {
|
|
|
1551
1523
|
*/
|
|
1552
1524
|
static async expand(compacted) {
|
|
1553
1525
|
try {
|
|
1554
|
-
|
|
1526
|
+
const expanded = await jsonLd.expand(compacted, {
|
|
1555
1527
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1556
1528
|
});
|
|
1529
|
+
return expanded;
|
|
1557
1530
|
}
|
|
1558
1531
|
catch (err) {
|
|
1532
|
+
if (core.Is.object(err) &&
|
|
1533
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1534
|
+
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1535
|
+
}
|
|
1559
1536
|
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "expand", undefined, err);
|
|
1560
1537
|
}
|
|
1561
1538
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataTypeHandlerFactory, DataTypeHelper } from '@twin.org/data-core';
|
|
2
|
-
import { Is, GeneralError } from '@twin.org/core';
|
|
2
|
+
import { Is, ObjectHelper, GeneralError } from '@twin.org/core';
|
|
3
3
|
import { FetchHelper, HttpMethod, MimeTypes } from '@twin.org/web';
|
|
4
4
|
import jsonLd from 'jsonld';
|
|
5
5
|
|
|
@@ -372,33 +372,7 @@ var anyOf$7 = [
|
|
|
372
372
|
type: "object",
|
|
373
373
|
properties: {
|
|
374
374
|
"@context": {
|
|
375
|
-
|
|
376
|
-
{
|
|
377
|
-
type: "null"
|
|
378
|
-
},
|
|
379
|
-
{
|
|
380
|
-
type: "string"
|
|
381
|
-
},
|
|
382
|
-
{
|
|
383
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
type: "array",
|
|
387
|
-
items: {
|
|
388
|
-
anyOf: [
|
|
389
|
-
{
|
|
390
|
-
type: "null"
|
|
391
|
-
},
|
|
392
|
-
{
|
|
393
|
-
type: "string"
|
|
394
|
-
},
|
|
395
|
-
{
|
|
396
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
397
|
-
}
|
|
398
|
-
]
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
]
|
|
375
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
402
376
|
},
|
|
403
377
|
"@graph": {
|
|
404
378
|
anyOf: [
|
|
@@ -560,33 +534,7 @@ var properties$4 = {
|
|
|
560
534
|
]
|
|
561
535
|
},
|
|
562
536
|
"@context": {
|
|
563
|
-
|
|
564
|
-
{
|
|
565
|
-
type: "null"
|
|
566
|
-
},
|
|
567
|
-
{
|
|
568
|
-
type: "string"
|
|
569
|
-
},
|
|
570
|
-
{
|
|
571
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
572
|
-
},
|
|
573
|
-
{
|
|
574
|
-
type: "array",
|
|
575
|
-
items: {
|
|
576
|
-
anyOf: [
|
|
577
|
-
{
|
|
578
|
-
type: "null"
|
|
579
|
-
},
|
|
580
|
-
{
|
|
581
|
-
type: "string"
|
|
582
|
-
},
|
|
583
|
-
{
|
|
584
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
585
|
-
}
|
|
586
|
-
]
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
]
|
|
537
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
590
538
|
}
|
|
591
539
|
};
|
|
592
540
|
var required$3 = [
|
|
@@ -782,33 +730,7 @@ var properties$3 = {
|
|
|
782
730
|
]
|
|
783
731
|
},
|
|
784
732
|
"@context": {
|
|
785
|
-
|
|
786
|
-
{
|
|
787
|
-
type: "null"
|
|
788
|
-
},
|
|
789
|
-
{
|
|
790
|
-
type: "string"
|
|
791
|
-
},
|
|
792
|
-
{
|
|
793
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
794
|
-
},
|
|
795
|
-
{
|
|
796
|
-
type: "array",
|
|
797
|
-
items: {
|
|
798
|
-
anyOf: [
|
|
799
|
-
{
|
|
800
|
-
type: "null"
|
|
801
|
-
},
|
|
802
|
-
{
|
|
803
|
-
type: "string"
|
|
804
|
-
},
|
|
805
|
-
{
|
|
806
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
807
|
-
}
|
|
808
|
-
]
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
]
|
|
733
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
812
734
|
},
|
|
813
735
|
"@direction": {
|
|
814
736
|
type: [
|
|
@@ -1060,33 +982,7 @@ var JsonLdListOrSetItemSchema = {
|
|
|
1060
982
|
var type$2 = "object";
|
|
1061
983
|
var properties$1 = {
|
|
1062
984
|
"@context": {
|
|
1063
|
-
|
|
1064
|
-
{
|
|
1065
|
-
type: "null"
|
|
1066
|
-
},
|
|
1067
|
-
{
|
|
1068
|
-
type: "string"
|
|
1069
|
-
},
|
|
1070
|
-
{
|
|
1071
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1072
|
-
},
|
|
1073
|
-
{
|
|
1074
|
-
type: "array",
|
|
1075
|
-
items: {
|
|
1076
|
-
anyOf: [
|
|
1077
|
-
{
|
|
1078
|
-
type: "null"
|
|
1079
|
-
},
|
|
1080
|
-
{
|
|
1081
|
-
type: "string"
|
|
1082
|
-
},
|
|
1083
|
-
{
|
|
1084
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1085
|
-
}
|
|
1086
|
-
]
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
]
|
|
985
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
1090
986
|
},
|
|
1091
987
|
"@id": {
|
|
1092
988
|
anyOf: [
|
|
@@ -1505,6 +1401,77 @@ class JsonLdHelper {
|
|
|
1505
1401
|
}
|
|
1506
1402
|
return validationFailures.length === 0;
|
|
1507
1403
|
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Extract a property from the JSON-LD.
|
|
1406
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
1407
|
+
* @param propertyNames The possible names for the property.
|
|
1408
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
1409
|
+
* @returns The properties if available.
|
|
1410
|
+
*/
|
|
1411
|
+
static extractProperty(nodeObject, propertyNames, deleteProperty = true) {
|
|
1412
|
+
let retVal;
|
|
1413
|
+
for (const prop of propertyNames) {
|
|
1414
|
+
retVal ??= nodeObject[prop];
|
|
1415
|
+
if (deleteProperty) {
|
|
1416
|
+
delete nodeObject[prop];
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
return retVal;
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Combine contexts.
|
|
1423
|
+
* @param context1 The first JSON-LD context to combine.
|
|
1424
|
+
* @param context2 The second JSON-LD context to combine.
|
|
1425
|
+
* @returns The combined context.
|
|
1426
|
+
*/
|
|
1427
|
+
static async combineContexts(context1, context2) {
|
|
1428
|
+
const combinedContext = [];
|
|
1429
|
+
if (Is.string(context1)) {
|
|
1430
|
+
if (!combinedContext.includes(context1)) {
|
|
1431
|
+
combinedContext.push(context1);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
else if (Is.array(context1)) {
|
|
1435
|
+
for (const context of context1) {
|
|
1436
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context));
|
|
1437
|
+
if (!hasMatch) {
|
|
1438
|
+
combinedContext.push(context);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
else if (Is.object(context1)) {
|
|
1443
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context1));
|
|
1444
|
+
if (!hasMatch) {
|
|
1445
|
+
combinedContext.push(context1);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
if (Is.string(context2)) {
|
|
1449
|
+
if (!combinedContext.includes(context2)) {
|
|
1450
|
+
combinedContext.push(context2);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
else if (Is.array(context2)) {
|
|
1454
|
+
for (const context of context2) {
|
|
1455
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context));
|
|
1456
|
+
if (!hasMatch) {
|
|
1457
|
+
combinedContext.push(context);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
else if (Is.object(context2)) {
|
|
1462
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context2));
|
|
1463
|
+
if (!hasMatch) {
|
|
1464
|
+
combinedContext.push(context2);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
if (combinedContext.length === 0) {
|
|
1468
|
+
return null;
|
|
1469
|
+
}
|
|
1470
|
+
if (combinedContext.length === 1) {
|
|
1471
|
+
return combinedContext[0];
|
|
1472
|
+
}
|
|
1473
|
+
return combinedContext;
|
|
1474
|
+
}
|
|
1508
1475
|
}
|
|
1509
1476
|
|
|
1510
1477
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -1534,11 +1501,16 @@ class JsonLdProcessor {
|
|
|
1534
1501
|
*/
|
|
1535
1502
|
static async compact(document, context) {
|
|
1536
1503
|
try {
|
|
1537
|
-
|
|
1504
|
+
const compacted = await jsonLd.compact(document, context ?? {}, {
|
|
1538
1505
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1539
1506
|
});
|
|
1507
|
+
return compacted;
|
|
1540
1508
|
}
|
|
1541
1509
|
catch (err) {
|
|
1510
|
+
if (Is.object(err) &&
|
|
1511
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1512
|
+
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1513
|
+
}
|
|
1542
1514
|
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "compact", undefined, err);
|
|
1543
1515
|
}
|
|
1544
1516
|
}
|
|
@@ -1549,11 +1521,16 @@ class JsonLdProcessor {
|
|
|
1549
1521
|
*/
|
|
1550
1522
|
static async expand(compacted) {
|
|
1551
1523
|
try {
|
|
1552
|
-
|
|
1524
|
+
const expanded = await jsonLd.expand(compacted, {
|
|
1553
1525
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1554
1526
|
});
|
|
1527
|
+
return expanded;
|
|
1555
1528
|
}
|
|
1556
1529
|
catch (err) {
|
|
1530
|
+
if (Is.object(err) &&
|
|
1531
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1532
|
+
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1533
|
+
}
|
|
1557
1534
|
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "expand", undefined, err);
|
|
1558
1535
|
}
|
|
1559
1536
|
}
|
|
@@ -140,6 +140,14 @@ export interface IJsonLdContextDefinition {
|
|
|
140
140
|
"@version"?: IJsonLdKeyword["@version"] | undefined;
|
|
141
141
|
"@vocab"?: IJsonLdKeyword["@vocab"] | undefined;
|
|
142
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* A context definition element is used to define the types of a context definition.
|
|
145
|
+
*/
|
|
146
|
+
export type IJsonLdContextDefinitionElement = null | string | IJsonLdContextDefinition;
|
|
147
|
+
/**
|
|
148
|
+
* A context definition root is used to define the root of a context definition.
|
|
149
|
+
*/
|
|
150
|
+
export type IJsonLdContextDefinitionRoot = IJsonLdContextDefinitionElement | IJsonLdContextDefinitionElement[];
|
|
143
151
|
/**
|
|
144
152
|
* An expanded term definition is used to describe the mapping between a term
|
|
145
153
|
* and its expanded identifier, as well as other properties of the value
|
|
@@ -171,7 +179,7 @@ export type IJsonLdExpandedTermDefinition = {
|
|
|
171
179
|
export type IJsonLdKeyword = {
|
|
172
180
|
"@base": string | null;
|
|
173
181
|
"@container": ("@list" | "@set" | IJsonLdContainerType) | ("@list" | "@set" | IJsonLdContainerType)[] | IJsonLdContainerTypeArray | null;
|
|
174
|
-
"@context":
|
|
182
|
+
"@context": IJsonLdContextDefinitionRoot;
|
|
175
183
|
"@direction": "ltr" | "rtl" | null;
|
|
176
184
|
"@graph": IJsonLdValueObject | IJsonLdNodeObject | (IJsonLdValueObject | IJsonLdNodeObject)[];
|
|
177
185
|
"@id": string | string[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type IValidationFailure } from "@twin.org/core";
|
|
2
2
|
import { type ValidationMode } from "@twin.org/data-core";
|
|
3
|
-
import type { IJsonLdDocument } from "../models/IJsonLdDocument";
|
|
3
|
+
import type { IJsonLdContextDefinitionRoot, IJsonLdDocument, IJsonLdNodeObject } from "../models/IJsonLdDocument";
|
|
4
4
|
/**
|
|
5
5
|
* Class to help with JSON LD.
|
|
6
6
|
*/
|
|
@@ -13,4 +13,19 @@ export declare class JsonLdHelper {
|
|
|
13
13
|
* @returns True if the document was valid.
|
|
14
14
|
*/
|
|
15
15
|
static validate<T extends IJsonLdDocument = IJsonLdDocument>(document: T, validationFailures: IValidationFailure[], validationMode?: ValidationMode): Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Extract a property from the JSON-LD.
|
|
18
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
19
|
+
* @param propertyNames The possible names for the property.
|
|
20
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
21
|
+
* @returns The properties if available.
|
|
22
|
+
*/
|
|
23
|
+
static extractProperty<T>(nodeObject: IJsonLdNodeObject, propertyNames: string[], deleteProperty?: boolean): T | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Combine contexts.
|
|
26
|
+
* @param context1 The first JSON-LD context to combine.
|
|
27
|
+
* @param context2 The second JSON-LD context to combine.
|
|
28
|
+
* @returns The combined context.
|
|
29
|
+
*/
|
|
30
|
+
static combineContexts(context1: IJsonLdContextDefinitionRoot | undefined, context2: IJsonLdContextDefinitionRoot | undefined): Promise<IJsonLdContextDefinitionRoot | undefined>;
|
|
16
31
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -43,3 +43,59 @@ The validation mode to use, defaults to either.
|
|
|
43
43
|
`Promise`\<`boolean`\>
|
|
44
44
|
|
|
45
45
|
True if the document was valid.
|
|
46
|
+
|
|
47
|
+
***
|
|
48
|
+
|
|
49
|
+
### extractProperty()
|
|
50
|
+
|
|
51
|
+
> `static` **extractProperty**\<`T`\>(`nodeObject`, `propertyNames`, `deleteProperty`): `undefined` \| `T`
|
|
52
|
+
|
|
53
|
+
Extract a property from the JSON-LD.
|
|
54
|
+
|
|
55
|
+
#### Type Parameters
|
|
56
|
+
|
|
57
|
+
• **T**
|
|
58
|
+
|
|
59
|
+
#### Parameters
|
|
60
|
+
|
|
61
|
+
• **nodeObject**: [`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
|
|
62
|
+
|
|
63
|
+
The JSON-LD node object to extract from.
|
|
64
|
+
|
|
65
|
+
• **propertyNames**: `string`[]
|
|
66
|
+
|
|
67
|
+
The possible names for the property.
|
|
68
|
+
|
|
69
|
+
• **deleteProperty**: `boolean` = `true`
|
|
70
|
+
|
|
71
|
+
Delete the property from the object, defaults to true.
|
|
72
|
+
|
|
73
|
+
#### Returns
|
|
74
|
+
|
|
75
|
+
`undefined` \| `T`
|
|
76
|
+
|
|
77
|
+
The properties if available.
|
|
78
|
+
|
|
79
|
+
***
|
|
80
|
+
|
|
81
|
+
### combineContexts()
|
|
82
|
+
|
|
83
|
+
> `static` **combineContexts**(`context1`, `context2`): `Promise`\<`undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)\>
|
|
84
|
+
|
|
85
|
+
Combine contexts.
|
|
86
|
+
|
|
87
|
+
#### Parameters
|
|
88
|
+
|
|
89
|
+
• **context1**: `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
90
|
+
|
|
91
|
+
The first JSON-LD context to combine.
|
|
92
|
+
|
|
93
|
+
• **context2**: `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
94
|
+
|
|
95
|
+
The second JSON-LD context to combine.
|
|
96
|
+
|
|
97
|
+
#### Returns
|
|
98
|
+
|
|
99
|
+
`Promise`\<`undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)\>
|
|
100
|
+
|
|
101
|
+
The combined context.
|
package/docs/reference/index.md
CHANGED
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
- [IJsonLdValueObject](type-aliases/IJsonLdValueObject.md)
|
|
27
27
|
- [IJsonLdIndexMapItem](type-aliases/IJsonLdIndexMapItem.md)
|
|
28
28
|
- [IJsonLdIncludedBlock](type-aliases/IJsonLdIncludedBlock.md)
|
|
29
|
+
- [IJsonLdContextDefinitionElement](type-aliases/IJsonLdContextDefinitionElement.md)
|
|
30
|
+
- [IJsonLdContextDefinitionRoot](type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
29
31
|
- [IJsonLdExpandedTermDefinition](type-aliases/IJsonLdExpandedTermDefinition.md)
|
|
30
32
|
- [IJsonLdKeyword](type-aliases/IJsonLdKeyword.md)
|
|
31
33
|
- [IJsonLdListOrSetItem](type-aliases/IJsonLdListOrSetItem.md)
|
|
@@ -28,4 +28,4 @@ https://www.w3.org/TR/json-ld11/#graph-objects
|
|
|
28
28
|
|
|
29
29
|
### @context?
|
|
30
30
|
|
|
31
|
-
> `optional` **@context**:
|
|
31
|
+
> `optional` **@context**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
@@ -15,7 +15,7 @@ https://www.w3.org/TR/json-ld11/#node-objects
|
|
|
15
15
|
|
|
16
16
|
### @context?
|
|
17
17
|
|
|
18
|
-
> `optional` **@context**:
|
|
18
|
+
> `optional` **@context**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
19
19
|
|
|
20
20
|
***
|
|
21
21
|
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Type Alias: IJsonLdContextDefinitionRoot
|
|
2
|
+
|
|
3
|
+
> **IJsonLdContextDefinitionRoot**: [`IJsonLdContextDefinitionElement`](IJsonLdContextDefinitionElement.md) \| [`IJsonLdContextDefinitionElement`](IJsonLdContextDefinitionElement.md)[]
|
|
4
|
+
|
|
5
|
+
A context definition root is used to define the root of a context definition.
|
|
@@ -18,7 +18,7 @@ Not for export.
|
|
|
18
18
|
|
|
19
19
|
### @context
|
|
20
20
|
|
|
21
|
-
> **@context**:
|
|
21
|
+
> **@context**: [`IJsonLdContextDefinitionRoot`](IJsonLdContextDefinitionRoot.md)
|
|
22
22
|
|
|
23
23
|
### @direction
|
|
24
24
|
|
package/locales/en.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
2
|
+
"jsonLdProcessor": {
|
|
3
3
|
"compact": "The JSON-LD compaction failed",
|
|
4
|
-
"expand": "The JSON-LD expansion failed"
|
|
4
|
+
"expand": "The JSON-LD expansion failed",
|
|
5
|
+
"invalidUrl": "The JSON-LD processing failed to retrieve from the following url \"{url}\""
|
|
5
6
|
}
|
|
6
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.4",
|
|
4
4
|
"description": "Models which define the structure of JSON LD",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,46 +13,16 @@
|
|
|
13
13
|
"engines": {
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"clean": "rimraf dist coverage docs/reference src/schemas",
|
|
18
|
-
"build:schema": "ts-to-schema ./ts-to-schema.json ./src/schemas",
|
|
19
|
-
"build:compile": "tspc",
|
|
20
|
-
"build": "npm run build:schema && npm run build:compile",
|
|
21
|
-
"test": "vitest --run --config ./vitest.config.ts --no-cache",
|
|
22
|
-
"coverage": "vitest --run --coverage --config ./vitest.config.ts --no-cache",
|
|
23
|
-
"bundle:esm": "rollup --config rollup.config.mjs --environment MODULE:esm",
|
|
24
|
-
"bundle:cjs": "rollup --config rollup.config.mjs --environment MODULE:cjs",
|
|
25
|
-
"bundle": "npm run bundle:esm && npm run bundle:cjs",
|
|
26
|
-
"docs:clean": "rimraf docs/reference",
|
|
27
|
-
"docs:generate": "typedoc",
|
|
28
|
-
"docs": "npm run docs:clean && npm run docs:generate",
|
|
29
|
-
"dist": "npm run clean && npm run build && npm run test && npm run bundle && npm run docs"
|
|
30
|
-
},
|
|
31
16
|
"dependencies": {
|
|
32
17
|
"@twin.org/core": "next",
|
|
33
|
-
"@twin.org/data-core": "0.0.1-next.
|
|
18
|
+
"@twin.org/data-core": "0.0.1-next.4",
|
|
34
19
|
"@twin.org/entity": "next",
|
|
35
20
|
"@twin.org/nameof": "next",
|
|
36
21
|
"@twin.org/web": "next",
|
|
37
|
-
"@types/jsonld": "1.5.15",
|
|
38
22
|
"@types/json-schema": "7.0.15",
|
|
23
|
+
"@types/jsonld": "1.5.15",
|
|
39
24
|
"jsonld": "8.3.2"
|
|
40
25
|
},
|
|
41
|
-
"devDependencies": {
|
|
42
|
-
"@twin.org/nameof-transformer": "next",
|
|
43
|
-
"@twin.org/ts-to-schema": "next",
|
|
44
|
-
"@rollup/plugin-json": "6.1.0",
|
|
45
|
-
"@vitest/coverage-v8": "2.1.1",
|
|
46
|
-
"copyfiles": "2.4.1",
|
|
47
|
-
"rimraf": "6.0.1",
|
|
48
|
-
"rollup": "4.22.0",
|
|
49
|
-
"rollup-plugin-typescript2": "0.36.0",
|
|
50
|
-
"ts-patch": "3.2.1",
|
|
51
|
-
"typedoc": "0.26.7",
|
|
52
|
-
"typedoc-plugin-markdown": "4.2.7",
|
|
53
|
-
"typescript": "5.6.2",
|
|
54
|
-
"vitest": "2.1.1"
|
|
55
|
-
},
|
|
56
26
|
"main": "./dist/cjs/index.cjs",
|
|
57
27
|
"module": "./dist/esm/index.mjs",
|
|
58
28
|
"types": "./dist/types/index.d.ts",
|