@digitalculture/ochre-sdk 0.13.2 → 0.13.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/index.d.mts +11 -11
- package/dist/index.mjs +11 -18
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Represents the core data structure containing item information and metadata
|
|
4
4
|
*/
|
|
5
|
-
type Data<T extends DataCategory = DataCategory, U extends DataCategory = (T extends "tree"
|
|
5
|
+
type Data<T extends DataCategory = DataCategory, U extends DataCategory = (T extends "tree" ? Exclude<DataCategory, "tree"> : T extends "set" ? DataCategory : never)> = {
|
|
6
6
|
uuid: string;
|
|
7
7
|
belongsTo: {
|
|
8
8
|
uuid: string;
|
|
@@ -16,9 +16,9 @@ type Data<T extends DataCategory = DataCategory, U extends DataCategory = (T ext
|
|
|
16
16
|
*/
|
|
17
17
|
type DataCategory = "tree" | "set" | "resource" | "spatialUnit" | "concept" | "period" | "bibliography" | "person" | "propertyValue";
|
|
18
18
|
/**
|
|
19
|
-
* Represents the item of the data
|
|
19
|
+
* Represents the item of the data, with proper type narrowing based on category
|
|
20
20
|
*/
|
|
21
|
-
type Item<T extends DataCategory = DataCategory, U extends DataCategory = (T extends "tree"
|
|
21
|
+
type Item<T extends DataCategory = DataCategory, U extends DataCategory = (T extends "tree" ? Exclude<DataCategory, "tree"> : T extends "set" ? DataCategory : never)> = T extends "resource" ? Resource : T extends "spatialUnit" ? SpatialUnit : T extends "concept" ? Concept : T extends "period" ? Period : T extends "bibliography" ? Bibliography : T extends "person" ? Person : T extends "propertyValue" ? PropertyValue : T extends "tree" ? Tree<Exclude<U, "tree">> : T extends "set" ? Set<U> : Resource | SpatialUnit | Concept | Period | Bibliography | Person | PropertyValue | Tree<Exclude<U, "tree">> | Set<U>;
|
|
22
22
|
/**
|
|
23
23
|
* Basic identification information used across multiple types
|
|
24
24
|
*/
|
|
@@ -354,11 +354,11 @@ type Concept = {
|
|
|
354
354
|
/**
|
|
355
355
|
* Represents a set that can contain resources, spatial units and concepts
|
|
356
356
|
*/
|
|
357
|
-
type Set<
|
|
357
|
+
type Set<U extends DataCategory = DataCategory> = {
|
|
358
358
|
uuid: string;
|
|
359
359
|
category: "set";
|
|
360
360
|
metadata: Metadata | null;
|
|
361
|
-
itemCategory:
|
|
361
|
+
itemCategory: U;
|
|
362
362
|
publicationDateTime: Date | null;
|
|
363
363
|
type: string;
|
|
364
364
|
number: number;
|
|
@@ -368,7 +368,7 @@ type Set<T extends DataCategory> = {
|
|
|
368
368
|
isSuppressingBlanks: boolean;
|
|
369
369
|
description: string;
|
|
370
370
|
creators: Array<Person>;
|
|
371
|
-
items:
|
|
371
|
+
items: [DataCategory] extends [U] ? Array<Item> : U extends "resource" ? Array<Resource> : U extends "spatialUnit" ? Array<SpatialUnit> : U extends "concept" ? Array<Concept> : U extends "period" ? Array<Period> : U extends "bibliography" ? Array<Bibliography> : U extends "person" ? Array<Person> : U extends "propertyValue" ? Array<PropertyValue> : U extends "tree" ? Array<Tree<Exclude<DataCategory, "tree">>> : U extends "set" ? Array<Set<DataCategory>> : Array<Item>;
|
|
372
372
|
};
|
|
373
373
|
/**
|
|
374
374
|
* Represents a bibliography entry with citation and publication information
|
|
@@ -467,7 +467,7 @@ type Property<T extends PropertyValueContentType = PropertyValueContentType> = {
|
|
|
467
467
|
/**
|
|
468
468
|
* Represents a tree structure containing resources, spatial units and concepts
|
|
469
469
|
*/
|
|
470
|
-
type Tree<
|
|
470
|
+
type Tree<U extends Exclude<DataCategory, "tree"> = Exclude<DataCategory, "tree">> = {
|
|
471
471
|
uuid: string;
|
|
472
472
|
category: "tree";
|
|
473
473
|
metadata: Metadata | null;
|
|
@@ -479,7 +479,7 @@ type Tree<T extends DataCategory, U extends DataCategory> = {
|
|
|
479
479
|
identification: Identification;
|
|
480
480
|
creators: Array<Person>;
|
|
481
481
|
properties: Array<Property>;
|
|
482
|
-
items:
|
|
482
|
+
items: [Exclude<DataCategory, "tree">] extends [U] ? Array<Item> : U extends "resource" ? Array<Resource> : U extends "spatialUnit" ? Array<SpatialUnit> : U extends "concept" ? Array<Concept> : U extends "period" ? Array<Period> : U extends "bibliography" ? Array<Bibliography> : U extends "person" ? Array<Person> : U extends "propertyValue" ? Array<PropertyValue> : U extends "set" ? Array<Set<DataCategory>> : Array<Item>;
|
|
483
483
|
};
|
|
484
484
|
/**
|
|
485
485
|
* Represents a gallery with its identification, project identification, resources and max length
|
|
@@ -959,24 +959,24 @@ declare function fetchGallery(uuid: string, filter: string, page: number, perPag
|
|
|
959
959
|
*
|
|
960
960
|
* If the fetch/parse fails, the returned object will have an `error` property.
|
|
961
961
|
*/
|
|
962
|
-
declare function fetchItem<T extends DataCategory, U extends DataCategory>(uuid: string, category?: T,
|
|
962
|
+
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?: {
|
|
963
963
|
customFetch?: (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
964
964
|
isVersion2: boolean;
|
|
965
965
|
}): Promise<{
|
|
966
966
|
error: null;
|
|
967
|
-
metadata: Metadata;
|
|
968
967
|
belongsTo: {
|
|
969
968
|
uuid: string;
|
|
970
969
|
abbreviation: string;
|
|
971
970
|
};
|
|
972
971
|
item: Item<T, U>;
|
|
973
972
|
category: T;
|
|
973
|
+
itemCategory: T extends "tree" ? Exclude<U, "tree"> : T extends "set" ? U : never;
|
|
974
974
|
} | {
|
|
975
975
|
error: string;
|
|
976
|
-
metadata: never;
|
|
977
976
|
belongsTo: never;
|
|
978
977
|
item: never;
|
|
979
978
|
category: never;
|
|
979
|
+
itemCategory: never;
|
|
980
980
|
}>;
|
|
981
981
|
//#endregion
|
|
982
982
|
//#region src/utils/fetchers/property-query.d.ts
|
package/dist/index.mjs
CHANGED
|
@@ -1404,13 +1404,13 @@ function parsePropertyValues(propertyValues) {
|
|
|
1404
1404
|
* @param tree - Raw tree data in OCHRE format
|
|
1405
1405
|
* @returns Parsed Tree object or null if invalid
|
|
1406
1406
|
*/
|
|
1407
|
-
function parseTree(tree, itemCategory,
|
|
1407
|
+
function parseTree(tree, itemCategory, metadata) {
|
|
1408
1408
|
if (typeof tree.items === "string") throw new TypeError("Invalid OCHRE data: Tree has no items");
|
|
1409
1409
|
let creators = [];
|
|
1410
1410
|
if (tree.creators) creators = parsePersons(Array.isArray(tree.creators.creator) ? tree.creators.creator : [tree.creators.creator]);
|
|
1411
1411
|
let date = null;
|
|
1412
1412
|
if (tree.date != null) date = tree.date;
|
|
1413
|
-
const parsedItemCategory =
|
|
1413
|
+
const parsedItemCategory = itemCategory ?? getItemCategory(Object.keys(tree.items));
|
|
1414
1414
|
let items = [];
|
|
1415
1415
|
switch (parsedItemCategory) {
|
|
1416
1416
|
case "resource":
|
|
@@ -1444,7 +1444,7 @@ function parseTree(tree, itemCategory, itemSubCategory, metadata) {
|
|
|
1444
1444
|
case "set": {
|
|
1445
1445
|
if (!("set" in tree.items)) throw new Error("Invalid OCHRE data: Tree has no sets");
|
|
1446
1446
|
const setItems = [];
|
|
1447
|
-
for (const item of Array.isArray(tree.items.set) ? tree.items.set : [tree.items.set]) setItems.push(parseSet(item,
|
|
1447
|
+
for (const item of Array.isArray(tree.items.set) ? tree.items.set : [tree.items.set]) setItems.push(parseSet(item, itemCategory));
|
|
1448
1448
|
items = setItems;
|
|
1449
1449
|
break;
|
|
1450
1450
|
}
|
|
@@ -2920,7 +2920,7 @@ async function fetchByUuid(uuid, options) {
|
|
|
2920
2920
|
*
|
|
2921
2921
|
* If the fetch/parse fails, the returned object will have an `error` property.
|
|
2922
2922
|
*/
|
|
2923
|
-
async function fetchItem(uuid, category,
|
|
2923
|
+
async function fetchItem(uuid, category, itemCategory, options) {
|
|
2924
2924
|
try {
|
|
2925
2925
|
const customFetch = options?.customFetch;
|
|
2926
2926
|
const [error, data] = await fetchByUuid(uuid, {
|
|
@@ -2962,42 +2962,35 @@ async function fetchItem(uuid, category, setCategory, options) {
|
|
|
2962
2962
|
break;
|
|
2963
2963
|
case "set":
|
|
2964
2964
|
if (!("set" in data.ochre)) throw new Error("Invalid OCHRE data: API response missing 'set' key");
|
|
2965
|
-
item = parseSet(data.ochre.set,
|
|
2965
|
+
item = parseSet(data.ochre.set, itemCategory, metadata);
|
|
2966
2966
|
break;
|
|
2967
2967
|
case "tree":
|
|
2968
2968
|
if (!("tree" in data.ochre)) throw new Error("Invalid OCHRE data: API response missing 'tree' key");
|
|
2969
|
-
item = parseTree(data.ochre.tree,
|
|
2969
|
+
item = parseTree(data.ochre.tree, itemCategory, metadata);
|
|
2970
2970
|
break;
|
|
2971
2971
|
default: throw new Error("Invalid category");
|
|
2972
2972
|
}
|
|
2973
2973
|
return {
|
|
2974
2974
|
error: null,
|
|
2975
|
-
metadata,
|
|
2976
2975
|
belongsTo: {
|
|
2977
2976
|
uuid: data.ochre.uuidBelongsTo,
|
|
2978
2977
|
abbreviation: parseFakeString(data.ochre.belongsTo)
|
|
2979
2978
|
},
|
|
2980
2979
|
item,
|
|
2981
|
-
category
|
|
2980
|
+
category,
|
|
2981
|
+
itemCategory
|
|
2982
2982
|
};
|
|
2983
2983
|
} catch (error) {
|
|
2984
2984
|
return {
|
|
2985
2985
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
2986
|
-
metadata: void 0,
|
|
2987
2986
|
belongsTo: void 0,
|
|
2988
2987
|
item: void 0,
|
|
2989
|
-
category: void 0
|
|
2988
|
+
category: void 0,
|
|
2989
|
+
itemCategory: void 0
|
|
2990
2990
|
};
|
|
2991
2991
|
}
|
|
2992
2992
|
}
|
|
2993
2993
|
|
|
2994
|
-
//#endregion
|
|
2995
|
-
//#region src/constants.ts
|
|
2996
|
-
/**
|
|
2997
|
-
* The UUID of the `<unassigned>` property value in OCHRE
|
|
2998
|
-
*/
|
|
2999
|
-
const UNASSIGNED_UUID = "e28e29af-b663-c0ac-ceb6-11a688fca0dd";
|
|
3000
|
-
|
|
3001
2994
|
//#endregion
|
|
3002
2995
|
//#region src/utils/fetchers/property-query.ts
|
|
3003
2996
|
const PROJECT_SCOPE = "0c0aae37-7246-495b-9547-e25dbf5b99a3";
|
|
@@ -3104,7 +3097,7 @@ async function fetchPropertyQuery(scopeUuids, propertyUuids, options) {
|
|
|
3104
3097
|
};
|
|
3105
3098
|
}
|
|
3106
3099
|
return {
|
|
3107
|
-
items: Object.values(items).
|
|
3100
|
+
items: Object.values(items).toSorted((a, b) => {
|
|
3108
3101
|
const aValue = a.value.label ?? a.value.content;
|
|
3109
3102
|
const bValue = b.value.label ?? b.value.content;
|
|
3110
3103
|
return aValue.localeCompare(bValue, "en-US");
|
package/package.json
CHANGED