@digitalculture/ochre-sdk 0.14.11 → 0.15.0
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/index.d.mts +3 -3
- package/dist/index.mjs +24 -13
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -367,7 +367,7 @@ type Set<U extends DataCategory = DataCategory> = {
|
|
|
367
367
|
uuid: string;
|
|
368
368
|
category: "set";
|
|
369
369
|
metadata: Metadata | null;
|
|
370
|
-
|
|
370
|
+
itemCategories: Array<U>;
|
|
371
371
|
publicationDateTime: Date | null;
|
|
372
372
|
type: string;
|
|
373
373
|
number: number;
|
|
@@ -1017,7 +1017,7 @@ declare function fetchGallery(uuid: string, filter: string, page: number, perPag
|
|
|
1017
1017
|
*
|
|
1018
1018
|
* If the fetch/parse fails, the returned object will have an `error` property.
|
|
1019
1019
|
*/
|
|
1020
|
-
declare function fetchItem<T extends DataCategory, U extends DataCategory>(uuid: string, category?: T, itemCategory?: T extends "tree" ? Exclude<U, "tree"> : T extends "set" ? U : never, options?: {
|
|
1020
|
+
declare function fetchItem<T extends DataCategory, U extends DataCategory>(uuid: string, category?: T, itemCategory?: T extends "tree" ? Exclude<U, "tree"> : T extends "set" ? Array<U> : never, options?: {
|
|
1021
1021
|
customFetch?: (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
1022
1022
|
isVersion2: boolean;
|
|
1023
1023
|
}): Promise<{
|
|
@@ -1028,7 +1028,7 @@ declare function fetchItem<T extends DataCategory, U extends DataCategory>(uuid:
|
|
|
1028
1028
|
};
|
|
1029
1029
|
item: Item<T, U>;
|
|
1030
1030
|
category: T;
|
|
1031
|
-
itemCategory: T extends "tree" ? Exclude<U, "tree"> : T extends "set" ? U : never;
|
|
1031
|
+
itemCategory: T extends "tree" ? Exclude<U, "tree"> : T extends "set" ? Array<U> : never;
|
|
1032
1032
|
} | {
|
|
1033
1033
|
error: string;
|
|
1034
1034
|
belongsTo: never;
|
package/dist/index.mjs
CHANGED
|
@@ -771,6 +771,17 @@ function getItemCategory(keys) {
|
|
|
771
771
|
}
|
|
772
772
|
return categorySchema.parse(categoryFound);
|
|
773
773
|
}
|
|
774
|
+
/**
|
|
775
|
+
* Get the categories of items from the OCHRE API response
|
|
776
|
+
* @param keys - The keys of the OCHRE API response
|
|
777
|
+
* @returns The categories of the items
|
|
778
|
+
* @internal
|
|
779
|
+
*/
|
|
780
|
+
function getItemCategories(keys) {
|
|
781
|
+
const categories = keys.map((key) => categorySchema.safeParse(key));
|
|
782
|
+
if (categories.some((result) => !result.success)) throw new Error(`Invalid OCHRE data; found unexpected keys: ${categories.filter((result) => !result.success).map((result) => result.error.message).join(", ")}`);
|
|
783
|
+
return categories.filter((result) => result.success).map((result) => result.data);
|
|
784
|
+
}
|
|
774
785
|
|
|
775
786
|
//#endregion
|
|
776
787
|
//#region src/utils/parse.ts
|
|
@@ -1606,7 +1617,7 @@ function parseTree(tree, itemCategory, metadata) {
|
|
|
1606
1617
|
case "set": {
|
|
1607
1618
|
if (!("set" in tree.items)) throw new Error("Invalid OCHRE data: Tree has no sets");
|
|
1608
1619
|
const setItems = [];
|
|
1609
|
-
for (const item of Array.isArray(tree.items.set) ? tree.items.set : [tree.items.set]) setItems.push(parseSet(item, itemCategory));
|
|
1620
|
+
for (const item of Array.isArray(tree.items.set) ? tree.items.set : [tree.items.set]) setItems.push(parseSet(item, [itemCategory]));
|
|
1610
1621
|
items = setItems;
|
|
1611
1622
|
break;
|
|
1612
1623
|
}
|
|
@@ -1633,41 +1644,41 @@ function parseTree(tree, itemCategory, metadata) {
|
|
|
1633
1644
|
* @param set - Raw set data in OCHRE format
|
|
1634
1645
|
* @returns Parsed Set object
|
|
1635
1646
|
*/
|
|
1636
|
-
function parseSet(set,
|
|
1647
|
+
function parseSet(set, itemCategories, metadata) {
|
|
1637
1648
|
if (typeof set.items === "string") throw new TypeError("Invalid OCHRE data: Set has no items");
|
|
1638
|
-
const
|
|
1649
|
+
const parsedItemCategories = itemCategories ?? getItemCategories(Object.keys(set.items));
|
|
1639
1650
|
let items = [];
|
|
1640
|
-
switch (
|
|
1651
|
+
for (const itemCategory of parsedItemCategories) switch (itemCategory) {
|
|
1641
1652
|
case "resource":
|
|
1642
|
-
if (!("resource" in set.items)) throw new Error("Invalid OCHRE data: Set has no resources");
|
|
1653
|
+
if (!("resource" in set.items) || set.items.resource == null) throw new Error("Invalid OCHRE data: Set has no resources");
|
|
1643
1654
|
items = parseResources(Array.isArray(set.items.resource) ? set.items.resource : [set.items.resource]);
|
|
1644
1655
|
break;
|
|
1645
1656
|
case "spatialUnit":
|
|
1646
|
-
if (!("spatialUnit" in set.items)) throw new Error("Invalid OCHRE data: Set has no spatial units");
|
|
1657
|
+
if (!("spatialUnit" in set.items) || set.items.spatialUnit == null) throw new Error("Invalid OCHRE data: Set has no spatial units");
|
|
1647
1658
|
items = parseSpatialUnits(Array.isArray(set.items.spatialUnit) ? set.items.spatialUnit : [set.items.spatialUnit]);
|
|
1648
1659
|
break;
|
|
1649
1660
|
case "concept":
|
|
1650
|
-
if (!("concept" in set.items)) throw new Error("Invalid OCHRE data: Set has no concepts");
|
|
1661
|
+
if (!("concept" in set.items) || set.items.concept == null) throw new Error("Invalid OCHRE data: Set has no concepts");
|
|
1651
1662
|
items = parseConcepts(Array.isArray(set.items.concept) ? set.items.concept : [set.items.concept]);
|
|
1652
1663
|
break;
|
|
1653
1664
|
case "period":
|
|
1654
|
-
if (!("period" in set.items)) throw new Error("Invalid OCHRE data: Set has no periods");
|
|
1665
|
+
if (!("period" in set.items) || set.items.period == null) throw new Error("Invalid OCHRE data: Set has no periods");
|
|
1655
1666
|
items = parsePeriods(Array.isArray(set.items.period) ? set.items.period : [set.items.period]);
|
|
1656
1667
|
break;
|
|
1657
1668
|
case "bibliography":
|
|
1658
|
-
if (!("bibliography" in set.items)) throw new Error("Invalid OCHRE data: Set has no bibliographies");
|
|
1669
|
+
if (!("bibliography" in set.items) || set.items.bibliography == null) throw new Error("Invalid OCHRE data: Set has no bibliographies");
|
|
1659
1670
|
items = parseBibliographies(Array.isArray(set.items.bibliography) ? set.items.bibliography : [set.items.bibliography]);
|
|
1660
1671
|
break;
|
|
1661
1672
|
case "person":
|
|
1662
|
-
if (!("person" in set.items)) throw new Error("Invalid OCHRE data: Set has no persons");
|
|
1673
|
+
if (!("person" in set.items) || set.items.person == null) throw new Error("Invalid OCHRE data: Set has no persons");
|
|
1663
1674
|
items = parsePersons(Array.isArray(set.items.person) ? set.items.person : [set.items.person]);
|
|
1664
1675
|
break;
|
|
1665
1676
|
case "propertyValue":
|
|
1666
|
-
if (!("propertyValue" in set.items)) throw new Error("Invalid OCHRE data: Set has no property values");
|
|
1677
|
+
if (!("propertyValue" in set.items) || set.items.propertyValue == null) throw new Error("Invalid OCHRE data: Set has no property values");
|
|
1667
1678
|
items = parsePropertyValues(Array.isArray(set.items.propertyValue) ? set.items.propertyValue : [set.items.propertyValue]);
|
|
1668
1679
|
break;
|
|
1669
1680
|
case "text":
|
|
1670
|
-
if (!("text" in set.items)) throw new Error("Invalid OCHRE data: Set has no texts");
|
|
1681
|
+
if (!("text" in set.items) || set.items.text == null) throw new Error("Invalid OCHRE data: Set has no texts");
|
|
1671
1682
|
items = parseTexts(Array.isArray(set.items.text) ? set.items.text : [set.items.text]);
|
|
1672
1683
|
break;
|
|
1673
1684
|
default: throw new Error("Invalid OCHRE data: Set has no items or is malformed");
|
|
@@ -1676,7 +1687,7 @@ function parseSet(set, itemCategory, metadata) {
|
|
|
1676
1687
|
uuid: set.uuid,
|
|
1677
1688
|
category: "set",
|
|
1678
1689
|
metadata: metadata ?? null,
|
|
1679
|
-
|
|
1690
|
+
itemCategories: parsedItemCategories,
|
|
1680
1691
|
publicationDateTime: set.publicationDateTime ? new Date(set.publicationDateTime) : null,
|
|
1681
1692
|
date: set.date ?? null,
|
|
1682
1693
|
license: parseLicense(set.availability),
|
package/package.json
CHANGED