@digitalculture/ochre-sdk 0.6.1 → 0.6.2

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.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  fetchByUuid: () => fetchByUuid,
24
+ fetchGallery: () => fetchGallery,
24
25
  fetchItem: () => fetchItem,
25
26
  filterProperties: () => filterProperties,
26
27
  getAllPropertyLabels: () => getAllPropertyLabels,
@@ -158,28 +159,6 @@ var whitespaceSchema = import_zod.z.string().transform((str) => str.split(" ")).
158
159
  );
159
160
  var emailSchema = import_zod.z.string().email({ message: "Invalid email" });
160
161
 
161
- // src/utils/helpers.ts
162
- function getItemCategory(keys) {
163
- const categoryFound = keys.find(
164
- (key) => categorySchema.safeParse(key).success
165
- );
166
- if (!categoryFound) {
167
- const unknownKey = keys.find(
168
- (key) => ![
169
- "uuid",
170
- "uuidBelongsTo",
171
- "belongsTo",
172
- "publicationDateTime",
173
- "metadata",
174
- "languages"
175
- ].includes(key)
176
- );
177
- throw new Error(`Invalid OCHRE data; found unexpected "${unknownKey}" key`);
178
- }
179
- const categoryKey = categorySchema.parse(categoryFound);
180
- return categoryKey;
181
- }
182
-
183
162
  // src/utils/getters.ts
184
163
  var DEFAULT_OPTIONS = {
185
164
  searchNestedProperties: false
@@ -588,6 +567,164 @@ ${JSON.stringify(
588
567
  }
589
568
  }
590
569
 
570
+ // src/utils/helpers.ts
571
+ function getItemCategory(keys) {
572
+ const categoryFound = keys.find(
573
+ (key) => categorySchema.safeParse(key).success
574
+ );
575
+ if (!categoryFound) {
576
+ const unknownKey = keys.find(
577
+ (key) => ![
578
+ "uuid",
579
+ "uuidBelongsTo",
580
+ "belongsTo",
581
+ "publicationDateTime",
582
+ "metadata",
583
+ "languages"
584
+ ].includes(key)
585
+ );
586
+ throw new Error(`Invalid OCHRE data; found unexpected "${unknownKey}" key`);
587
+ }
588
+ const categoryKey = categorySchema.parse(categoryFound);
589
+ return categoryKey;
590
+ }
591
+
592
+ // src/utils/fetchers/uuid.ts
593
+ async function fetchByUuid(uuid) {
594
+ try {
595
+ const parsedUuid = uuidSchema.parse(uuid);
596
+ const response = await fetch(
597
+ `https://ochre.lib.uchicago.edu/ochre?uuid=${parsedUuid}&format=json&lang="*"`
598
+ );
599
+ if (!response.ok) {
600
+ throw new Error("Failed to fetch OCHRE data");
601
+ }
602
+ const dataRaw = await response.json();
603
+ if (!("ochre" in dataRaw)) {
604
+ throw new Error("Invalid OCHRE data: API response missing 'ochre' key");
605
+ }
606
+ return [null, dataRaw];
607
+ } catch (error) {
608
+ return [error instanceof Error ? error.message : "Unknown error", null];
609
+ }
610
+ }
611
+
612
+ // src/utils/fetchers/item.ts
613
+ async function fetchItem(uuid, category) {
614
+ try {
615
+ const [error, data] = await fetchByUuid(uuid);
616
+ if (error !== null) {
617
+ throw new Error(error);
618
+ }
619
+ const categoryKey = getItemCategory(Object.keys(data.ochre));
620
+ let item;
621
+ switch (categoryKey) {
622
+ case "resource": {
623
+ if (!("resource" in data.ochre)) {
624
+ throw new Error(
625
+ "Invalid OCHRE data: API response missing 'resource' key"
626
+ );
627
+ }
628
+ item = parseResource(data.ochre.resource);
629
+ break;
630
+ }
631
+ case "spatialUnit": {
632
+ if (!("spatialUnit" in data.ochre)) {
633
+ throw new Error(
634
+ "Invalid OCHRE data: API response missing 'spatialUnit' key"
635
+ );
636
+ }
637
+ item = parseSpatialUnit(data.ochre.spatialUnit);
638
+ break;
639
+ }
640
+ case "concept": {
641
+ if (!("concept" in data.ochre)) {
642
+ throw new Error(
643
+ "Invalid OCHRE data: API response missing 'concept' key"
644
+ );
645
+ }
646
+ item = parseConcept(data.ochre.concept);
647
+ break;
648
+ }
649
+ case "period": {
650
+ if (!("period" in data.ochre)) {
651
+ throw new Error(
652
+ "Invalid OCHRE data: API response missing 'period' key"
653
+ );
654
+ }
655
+ item = parsePeriod(data.ochre.period);
656
+ break;
657
+ }
658
+ case "bibliography": {
659
+ if (!("bibliography" in data.ochre)) {
660
+ throw new Error(
661
+ "Invalid OCHRE data: API response missing 'bibliography' key"
662
+ );
663
+ }
664
+ item = parseBibliography(data.ochre.bibliography);
665
+ break;
666
+ }
667
+ case "person": {
668
+ if (!("person" in data.ochre)) {
669
+ throw new Error(
670
+ "Invalid OCHRE data: API response missing 'person' key"
671
+ );
672
+ }
673
+ item = parsePerson(data.ochre.person);
674
+ break;
675
+ }
676
+ case "propertyValue": {
677
+ if (!("propertyValue" in data.ochre)) {
678
+ throw new Error(
679
+ "Invalid OCHRE data: API response missing 'propertyValue' key"
680
+ );
681
+ }
682
+ item = parsePropertyValue(data.ochre.propertyValue);
683
+ break;
684
+ }
685
+ case "set": {
686
+ if (!("set" in data.ochre)) {
687
+ throw new Error("Invalid OCHRE data: API response missing 'set' key");
688
+ }
689
+ item = parseSet(data.ochre.set);
690
+ break;
691
+ }
692
+ case "tree": {
693
+ if (!("tree" in data.ochre)) {
694
+ throw new Error(
695
+ "Invalid OCHRE data: API response missing 'tree' key"
696
+ );
697
+ }
698
+ item = parseTree(data.ochre.tree);
699
+ break;
700
+ }
701
+ default: {
702
+ throw new Error("Invalid category");
703
+ }
704
+ }
705
+ const metadata = parseMetadata(data.ochre.metadata);
706
+ const belongsTo = {
707
+ uuid: data.ochre.uuidBelongsTo,
708
+ abbreviation: parseFakeString(data.ochre.belongsTo)
709
+ };
710
+ return {
711
+ error: null,
712
+ metadata,
713
+ belongsTo,
714
+ item,
715
+ category
716
+ };
717
+ } catch (error) {
718
+ return {
719
+ error: error instanceof Error ? error.message : "Unknown error",
720
+ metadata: void 0,
721
+ belongsTo: void 0,
722
+ item: void 0,
723
+ category: void 0
724
+ };
725
+ }
726
+ }
727
+
591
728
  // src/utils/parse.ts
592
729
  function parseIdentification(identification) {
593
730
  try {
@@ -2480,144 +2617,59 @@ async function parseWebsite(websiteTree, projectName, website) {
2480
2617
  };
2481
2618
  }
2482
2619
 
2483
- // src/utils/fetchers/uuid.ts
2484
- async function fetchByUuid(uuid) {
2620
+ // src/utils/fetchers/gallery.ts
2621
+ async function fetchGallery(uuid, filter, page, perPage) {
2485
2622
  try {
2486
- const parsedUuid = uuidSchema.parse(uuid);
2623
+ const {
2624
+ uuid: parsedUuid,
2625
+ filter: parsedFilter,
2626
+ page: parsedPage,
2627
+ perPage: parsedPerPage
2628
+ } = gallerySchema.parse({ uuid, filter, page, perPage });
2487
2629
  const response = await fetch(
2488
- `https://ochre.lib.uchicago.edu/ochre?uuid=${parsedUuid}&format=json&lang="*"`
2630
+ `https://ochre.lib.uchicago.edu/ochre?xquery=${encodeURIComponent(`
2631
+ for $q in input()/ochre[@uuid='${parsedUuid}']
2632
+ let $filtered := $q/tree/items/resource[contains(lower-case(identification/label), lower-case('${parsedFilter}'))]
2633
+ let $maxLength := count($filtered)
2634
+ return <gallery maxLength='{$maxLength}'>
2635
+ {$q/metadata/project}
2636
+ {$q/metadata/item}
2637
+ {$filtered[position() >= ${((parsedPage - 1) * parsedPerPage + 1).toString()} and position() < ${(parsedPage * parsedPerPage + 1).toString()}]}
2638
+ </gallery>
2639
+ `)}&format=json`
2489
2640
  );
2490
2641
  if (!response.ok) {
2491
- throw new Error("Failed to fetch OCHRE data");
2642
+ throw new Error("Error fetching gallery items, please try again later.");
2492
2643
  }
2493
- const dataRaw = await response.json();
2494
- if (!("ochre" in dataRaw)) {
2495
- throw new Error("Invalid OCHRE data: API response missing 'ochre' key");
2644
+ const data = await response.json();
2645
+ if (!("gallery" in data.result)) {
2646
+ throw new Error("Failed to fetch gallery");
2496
2647
  }
2497
- return [null, dataRaw];
2498
- } catch (error) {
2499
- return [error instanceof Error ? error.message : "Unknown error", null];
2500
- }
2501
- }
2502
-
2503
- // src/utils/fetchers/item.ts
2504
- async function fetchItem(uuid, category) {
2505
- try {
2506
- const [error, data] = await fetchByUuid(uuid);
2507
- if (error !== null) {
2508
- throw new Error(error);
2509
- }
2510
- const categoryKey = getItemCategory(Object.keys(data.ochre));
2511
- let item;
2512
- switch (categoryKey) {
2513
- case "resource": {
2514
- if (!("resource" in data.ochre)) {
2515
- throw new Error(
2516
- "Invalid OCHRE data: API response missing 'resource' key"
2517
- );
2518
- }
2519
- item = parseResource(data.ochre.resource);
2520
- break;
2521
- }
2522
- case "spatialUnit": {
2523
- if (!("spatialUnit" in data.ochre)) {
2524
- throw new Error(
2525
- "Invalid OCHRE data: API response missing 'spatialUnit' key"
2526
- );
2527
- }
2528
- item = parseSpatialUnit(data.ochre.spatialUnit);
2529
- break;
2530
- }
2531
- case "concept": {
2532
- if (!("concept" in data.ochre)) {
2533
- throw new Error(
2534
- "Invalid OCHRE data: API response missing 'concept' key"
2535
- );
2536
- }
2537
- item = parseConcept(data.ochre.concept);
2538
- break;
2539
- }
2540
- case "period": {
2541
- if (!("period" in data.ochre)) {
2542
- throw new Error(
2543
- "Invalid OCHRE data: API response missing 'period' key"
2544
- );
2545
- }
2546
- item = parsePeriod(data.ochre.period);
2547
- break;
2548
- }
2549
- case "bibliography": {
2550
- if (!("bibliography" in data.ochre)) {
2551
- throw new Error(
2552
- "Invalid OCHRE data: API response missing 'bibliography' key"
2553
- );
2554
- }
2555
- item = parseBibliography(data.ochre.bibliography);
2556
- break;
2557
- }
2558
- case "person": {
2559
- if (!("person" in data.ochre)) {
2560
- throw new Error(
2561
- "Invalid OCHRE data: API response missing 'person' key"
2562
- );
2563
- }
2564
- item = parsePerson(data.ochre.person);
2565
- break;
2566
- }
2567
- case "propertyValue": {
2568
- if (!("propertyValue" in data.ochre)) {
2569
- throw new Error(
2570
- "Invalid OCHRE data: API response missing 'propertyValue' key"
2571
- );
2572
- }
2573
- item = parsePropertyValue(data.ochre.propertyValue);
2574
- break;
2575
- }
2576
- case "set": {
2577
- if (!("set" in data.ochre)) {
2578
- throw new Error("Invalid OCHRE data: API response missing 'set' key");
2579
- }
2580
- item = parseSet(data.ochre.set);
2581
- break;
2582
- }
2583
- case "tree": {
2584
- if (!("tree" in data.ochre)) {
2585
- throw new Error(
2586
- "Invalid OCHRE data: API response missing 'tree' key"
2587
- );
2588
- }
2589
- item = parseTree(data.ochre.tree);
2590
- break;
2591
- }
2592
- default: {
2593
- throw new Error("Invalid category");
2594
- }
2595
- }
2596
- const metadata = parseMetadata(data.ochre.metadata);
2597
- const belongsTo = {
2598
- uuid: data.ochre.uuidBelongsTo,
2599
- abbreviation: parseFakeString(data.ochre.belongsTo)
2600
- };
2601
- return {
2602
- error: null,
2603
- metadata,
2604
- belongsTo,
2605
- item,
2606
- category
2648
+ const galleryIdentification = parseIdentification(
2649
+ data.result.gallery.item.identification
2650
+ );
2651
+ const galleryProjectIdentification = parseIdentification(
2652
+ data.result.gallery.project.identification
2653
+ );
2654
+ const gallery = {
2655
+ identification: galleryIdentification,
2656
+ projectIdentification: galleryProjectIdentification,
2657
+ resources: data.result.gallery.resource ? Array.isArray(data.result.gallery.resource) ? parseResources(data.result.gallery.resource) : [parseResource(data.result.gallery.resource)] : [],
2658
+ maxLength: data.result.gallery.maxLength
2607
2659
  };
2660
+ return { item: gallery, error: null };
2608
2661
  } catch (error) {
2662
+ console.error(error);
2609
2663
  return {
2610
- error: error instanceof Error ? error.message : "Unknown error",
2611
- metadata: void 0,
2612
- belongsTo: void 0,
2613
- item: void 0,
2614
- category: void 0
2664
+ item: null,
2665
+ error: error instanceof Error ? error.message : "Failed to fetch gallery"
2615
2666
  };
2616
2667
  }
2617
2668
  }
2618
2669
  // Annotate the CommonJS export names for ESM import in node:
2619
2670
  0 && (module.exports = {
2620
2671
  fetchByUuid,
2672
+ fetchGallery,
2621
2673
  fetchItem,
2622
2674
  filterProperties,
2623
2675
  getAllPropertyLabels,
package/dist/index.d.cts CHANGED
@@ -619,6 +619,40 @@ type WebBlock = {
619
619
  cssStylesMobile: Array<Style>;
620
620
  };
621
621
 
622
+ /**
623
+ * Fetches and parses a gallery from the OCHRE API
624
+ *
625
+ * @param uuid - The UUID of the gallery
626
+ * @param filter - The filter to apply to the gallery
627
+ * @param page - The page number to fetch
628
+ * @param perPage - The number of items per page
629
+ * @returns The parsed gallery or null if the fetch/parse fails
630
+ *
631
+ * @example
632
+ * ```ts
633
+ * const gallery = await fetchGallery("9c4da06b-f15e-40af-a747-0933eaf3587e", "1978", 1, 12);
634
+ * if (gallery === null) {
635
+ * console.error("Failed to fetch gallery");
636
+ * return;
637
+ * }
638
+ * console.log(`Fetched gallery: ${gallery.identification.label}`);
639
+ * console.log(`Contains ${gallery.resources.length.toLocaleString()} resources`);
640
+ * ```
641
+ *
642
+ * @remarks
643
+ * The returned gallery includes:
644
+ * - Gallery metadata and identification
645
+ * - Project identification
646
+ * - Resources (gallery items)
647
+ */
648
+ declare function fetchGallery(uuid: string, filter: string, page: number, perPage: number): Promise<{
649
+ item: Gallery | null;
650
+ error: null;
651
+ } | {
652
+ item: null;
653
+ error: string;
654
+ }>;
655
+
622
656
  /**
623
657
  * Fetches and parses an OCHRE item from the OCHRE API
624
658
  *
@@ -1644,4 +1678,4 @@ declare function parseStringDocumentItem(item: OchreStringRichTextItem, footnote
1644
1678
  */
1645
1679
  declare function parseStringContent(content: OchreStringContent, language?: string): string;
1646
1680
 
1647
- export { type Bibliography, type Concept, type Context, type ContextItem, type ContextNode, type Coordinates, type Data, type DataCategory, type Document, type Event, type Footnote, type Gallery, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type License, type Link, type Metadata, type Note, type Observation, type Period, type Person, type Property, type PropertyValue, type PropertyValueContent, type PropertyValueContentType, type Resource, type Set, type SpatialUnit, type Style, type Tree, type WebBlock, type WebElement, type WebElementComponent, type WebImage, type Webpage, type WebpageProperties, type Website, type WebsiteProperties, fetchByUuid, fetchItem, filterProperties, getAllPropertyLabels, getPropertyByLabel, getPropertyValueByLabel, getPropertyValuesByLabel, parseBibliographies, parseBibliography, parseConcept, parseConcepts, parseContext, parseCoordinates, parseDocument, parseEmail, parseEvents, parseFakeString, parseIdentification, parseImage, parseImageMap, parseInterpretations, parseLanguages, parseLicense, parseLink, parseLinks, parseMetadata, parseNotes, parseObservation, parseObservations, parsePeriod, parsePeriods, parsePerson, parsePersons, parseProperties, parseProperty, parsePropertyValue, parsePropertyValues, parseResource, parseResources, parseSet, parseSpatialUnit, parseSpatialUnits, parseStringContent, parseStringDocumentItem, parseStringItem, parseTree, parseWebsite };
1681
+ export { type Bibliography, type Concept, type Context, type ContextItem, type ContextNode, type Coordinates, type Data, type DataCategory, type Document, type Event, type Footnote, type Gallery, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type License, type Link, type Metadata, type Note, type Observation, type Period, type Person, type Property, type PropertyValue, type PropertyValueContent, type PropertyValueContentType, type Resource, type Set, type SpatialUnit, type Style, type Tree, type WebBlock, type WebElement, type WebElementComponent, type WebImage, type Webpage, type WebpageProperties, type Website, type WebsiteProperties, fetchByUuid, fetchGallery, fetchItem, filterProperties, getAllPropertyLabels, getPropertyByLabel, getPropertyValueByLabel, getPropertyValuesByLabel, parseBibliographies, parseBibliography, parseConcept, parseConcepts, parseContext, parseCoordinates, parseDocument, parseEmail, parseEvents, parseFakeString, parseIdentification, parseImage, parseImageMap, parseInterpretations, parseLanguages, parseLicense, parseLink, parseLinks, parseMetadata, parseNotes, parseObservation, parseObservations, parsePeriod, parsePeriods, parsePerson, parsePersons, parseProperties, parseProperty, parsePropertyValue, parsePropertyValues, parseResource, parseResources, parseSet, parseSpatialUnit, parseSpatialUnits, parseStringContent, parseStringDocumentItem, parseStringItem, parseTree, parseWebsite };
package/dist/index.d.ts CHANGED
@@ -619,6 +619,40 @@ type WebBlock = {
619
619
  cssStylesMobile: Array<Style>;
620
620
  };
621
621
 
622
+ /**
623
+ * Fetches and parses a gallery from the OCHRE API
624
+ *
625
+ * @param uuid - The UUID of the gallery
626
+ * @param filter - The filter to apply to the gallery
627
+ * @param page - The page number to fetch
628
+ * @param perPage - The number of items per page
629
+ * @returns The parsed gallery or null if the fetch/parse fails
630
+ *
631
+ * @example
632
+ * ```ts
633
+ * const gallery = await fetchGallery("9c4da06b-f15e-40af-a747-0933eaf3587e", "1978", 1, 12);
634
+ * if (gallery === null) {
635
+ * console.error("Failed to fetch gallery");
636
+ * return;
637
+ * }
638
+ * console.log(`Fetched gallery: ${gallery.identification.label}`);
639
+ * console.log(`Contains ${gallery.resources.length.toLocaleString()} resources`);
640
+ * ```
641
+ *
642
+ * @remarks
643
+ * The returned gallery includes:
644
+ * - Gallery metadata and identification
645
+ * - Project identification
646
+ * - Resources (gallery items)
647
+ */
648
+ declare function fetchGallery(uuid: string, filter: string, page: number, perPage: number): Promise<{
649
+ item: Gallery | null;
650
+ error: null;
651
+ } | {
652
+ item: null;
653
+ error: string;
654
+ }>;
655
+
622
656
  /**
623
657
  * Fetches and parses an OCHRE item from the OCHRE API
624
658
  *
@@ -1644,4 +1678,4 @@ declare function parseStringDocumentItem(item: OchreStringRichTextItem, footnote
1644
1678
  */
1645
1679
  declare function parseStringContent(content: OchreStringContent, language?: string): string;
1646
1680
 
1647
- export { type Bibliography, type Concept, type Context, type ContextItem, type ContextNode, type Coordinates, type Data, type DataCategory, type Document, type Event, type Footnote, type Gallery, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type License, type Link, type Metadata, type Note, type Observation, type Period, type Person, type Property, type PropertyValue, type PropertyValueContent, type PropertyValueContentType, type Resource, type Set, type SpatialUnit, type Style, type Tree, type WebBlock, type WebElement, type WebElementComponent, type WebImage, type Webpage, type WebpageProperties, type Website, type WebsiteProperties, fetchByUuid, fetchItem, filterProperties, getAllPropertyLabels, getPropertyByLabel, getPropertyValueByLabel, getPropertyValuesByLabel, parseBibliographies, parseBibliography, parseConcept, parseConcepts, parseContext, parseCoordinates, parseDocument, parseEmail, parseEvents, parseFakeString, parseIdentification, parseImage, parseImageMap, parseInterpretations, parseLanguages, parseLicense, parseLink, parseLinks, parseMetadata, parseNotes, parseObservation, parseObservations, parsePeriod, parsePeriods, parsePerson, parsePersons, parseProperties, parseProperty, parsePropertyValue, parsePropertyValues, parseResource, parseResources, parseSet, parseSpatialUnit, parseSpatialUnits, parseStringContent, parseStringDocumentItem, parseStringItem, parseTree, parseWebsite };
1681
+ export { type Bibliography, type Concept, type Context, type ContextItem, type ContextNode, type Coordinates, type Data, type DataCategory, type Document, type Event, type Footnote, type Gallery, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type License, type Link, type Metadata, type Note, type Observation, type Period, type Person, type Property, type PropertyValue, type PropertyValueContent, type PropertyValueContentType, type Resource, type Set, type SpatialUnit, type Style, type Tree, type WebBlock, type WebElement, type WebElementComponent, type WebImage, type Webpage, type WebpageProperties, type Website, type WebsiteProperties, fetchByUuid, fetchGallery, fetchItem, filterProperties, getAllPropertyLabels, getPropertyByLabel, getPropertyValueByLabel, getPropertyValuesByLabel, parseBibliographies, parseBibliography, parseConcept, parseConcepts, parseContext, parseCoordinates, parseDocument, parseEmail, parseEvents, parseFakeString, parseIdentification, parseImage, parseImageMap, parseInterpretations, parseLanguages, parseLicense, parseLink, parseLinks, parseMetadata, parseNotes, parseObservation, parseObservations, parsePeriod, parsePeriods, parsePerson, parsePersons, parseProperties, parseProperty, parsePropertyValue, parsePropertyValues, parseResource, parseResources, parseSet, parseSpatialUnit, parseSpatialUnits, parseStringContent, parseStringDocumentItem, parseStringItem, parseTree, parseWebsite };
package/dist/index.js CHANGED
@@ -86,28 +86,6 @@ var whitespaceSchema = z.string().transform((str) => str.split(" ")).pipe(
86
86
  );
87
87
  var emailSchema = z.string().email({ message: "Invalid email" });
88
88
 
89
- // src/utils/helpers.ts
90
- function getItemCategory(keys) {
91
- const categoryFound = keys.find(
92
- (key) => categorySchema.safeParse(key).success
93
- );
94
- if (!categoryFound) {
95
- const unknownKey = keys.find(
96
- (key) => ![
97
- "uuid",
98
- "uuidBelongsTo",
99
- "belongsTo",
100
- "publicationDateTime",
101
- "metadata",
102
- "languages"
103
- ].includes(key)
104
- );
105
- throw new Error(`Invalid OCHRE data; found unexpected "${unknownKey}" key`);
106
- }
107
- const categoryKey = categorySchema.parse(categoryFound);
108
- return categoryKey;
109
- }
110
-
111
89
  // src/utils/getters.ts
112
90
  var DEFAULT_OPTIONS = {
113
91
  searchNestedProperties: false
@@ -516,6 +494,164 @@ ${JSON.stringify(
516
494
  }
517
495
  }
518
496
 
497
+ // src/utils/helpers.ts
498
+ function getItemCategory(keys) {
499
+ const categoryFound = keys.find(
500
+ (key) => categorySchema.safeParse(key).success
501
+ );
502
+ if (!categoryFound) {
503
+ const unknownKey = keys.find(
504
+ (key) => ![
505
+ "uuid",
506
+ "uuidBelongsTo",
507
+ "belongsTo",
508
+ "publicationDateTime",
509
+ "metadata",
510
+ "languages"
511
+ ].includes(key)
512
+ );
513
+ throw new Error(`Invalid OCHRE data; found unexpected "${unknownKey}" key`);
514
+ }
515
+ const categoryKey = categorySchema.parse(categoryFound);
516
+ return categoryKey;
517
+ }
518
+
519
+ // src/utils/fetchers/uuid.ts
520
+ async function fetchByUuid(uuid) {
521
+ try {
522
+ const parsedUuid = uuidSchema.parse(uuid);
523
+ const response = await fetch(
524
+ `https://ochre.lib.uchicago.edu/ochre?uuid=${parsedUuid}&format=json&lang="*"`
525
+ );
526
+ if (!response.ok) {
527
+ throw new Error("Failed to fetch OCHRE data");
528
+ }
529
+ const dataRaw = await response.json();
530
+ if (!("ochre" in dataRaw)) {
531
+ throw new Error("Invalid OCHRE data: API response missing 'ochre' key");
532
+ }
533
+ return [null, dataRaw];
534
+ } catch (error) {
535
+ return [error instanceof Error ? error.message : "Unknown error", null];
536
+ }
537
+ }
538
+
539
+ // src/utils/fetchers/item.ts
540
+ async function fetchItem(uuid, category) {
541
+ try {
542
+ const [error, data] = await fetchByUuid(uuid);
543
+ if (error !== null) {
544
+ throw new Error(error);
545
+ }
546
+ const categoryKey = getItemCategory(Object.keys(data.ochre));
547
+ let item;
548
+ switch (categoryKey) {
549
+ case "resource": {
550
+ if (!("resource" in data.ochre)) {
551
+ throw new Error(
552
+ "Invalid OCHRE data: API response missing 'resource' key"
553
+ );
554
+ }
555
+ item = parseResource(data.ochre.resource);
556
+ break;
557
+ }
558
+ case "spatialUnit": {
559
+ if (!("spatialUnit" in data.ochre)) {
560
+ throw new Error(
561
+ "Invalid OCHRE data: API response missing 'spatialUnit' key"
562
+ );
563
+ }
564
+ item = parseSpatialUnit(data.ochre.spatialUnit);
565
+ break;
566
+ }
567
+ case "concept": {
568
+ if (!("concept" in data.ochre)) {
569
+ throw new Error(
570
+ "Invalid OCHRE data: API response missing 'concept' key"
571
+ );
572
+ }
573
+ item = parseConcept(data.ochre.concept);
574
+ break;
575
+ }
576
+ case "period": {
577
+ if (!("period" in data.ochre)) {
578
+ throw new Error(
579
+ "Invalid OCHRE data: API response missing 'period' key"
580
+ );
581
+ }
582
+ item = parsePeriod(data.ochre.period);
583
+ break;
584
+ }
585
+ case "bibliography": {
586
+ if (!("bibliography" in data.ochre)) {
587
+ throw new Error(
588
+ "Invalid OCHRE data: API response missing 'bibliography' key"
589
+ );
590
+ }
591
+ item = parseBibliography(data.ochre.bibliography);
592
+ break;
593
+ }
594
+ case "person": {
595
+ if (!("person" in data.ochre)) {
596
+ throw new Error(
597
+ "Invalid OCHRE data: API response missing 'person' key"
598
+ );
599
+ }
600
+ item = parsePerson(data.ochre.person);
601
+ break;
602
+ }
603
+ case "propertyValue": {
604
+ if (!("propertyValue" in data.ochre)) {
605
+ throw new Error(
606
+ "Invalid OCHRE data: API response missing 'propertyValue' key"
607
+ );
608
+ }
609
+ item = parsePropertyValue(data.ochre.propertyValue);
610
+ break;
611
+ }
612
+ case "set": {
613
+ if (!("set" in data.ochre)) {
614
+ throw new Error("Invalid OCHRE data: API response missing 'set' key");
615
+ }
616
+ item = parseSet(data.ochre.set);
617
+ break;
618
+ }
619
+ case "tree": {
620
+ if (!("tree" in data.ochre)) {
621
+ throw new Error(
622
+ "Invalid OCHRE data: API response missing 'tree' key"
623
+ );
624
+ }
625
+ item = parseTree(data.ochre.tree);
626
+ break;
627
+ }
628
+ default: {
629
+ throw new Error("Invalid category");
630
+ }
631
+ }
632
+ const metadata = parseMetadata(data.ochre.metadata);
633
+ const belongsTo = {
634
+ uuid: data.ochre.uuidBelongsTo,
635
+ abbreviation: parseFakeString(data.ochre.belongsTo)
636
+ };
637
+ return {
638
+ error: null,
639
+ metadata,
640
+ belongsTo,
641
+ item,
642
+ category
643
+ };
644
+ } catch (error) {
645
+ return {
646
+ error: error instanceof Error ? error.message : "Unknown error",
647
+ metadata: void 0,
648
+ belongsTo: void 0,
649
+ item: void 0,
650
+ category: void 0
651
+ };
652
+ }
653
+ }
654
+
519
655
  // src/utils/parse.ts
520
656
  function parseIdentification(identification) {
521
657
  try {
@@ -2408,143 +2544,58 @@ async function parseWebsite(websiteTree, projectName, website) {
2408
2544
  };
2409
2545
  }
2410
2546
 
2411
- // src/utils/fetchers/uuid.ts
2412
- async function fetchByUuid(uuid) {
2547
+ // src/utils/fetchers/gallery.ts
2548
+ async function fetchGallery(uuid, filter, page, perPage) {
2413
2549
  try {
2414
- const parsedUuid = uuidSchema.parse(uuid);
2550
+ const {
2551
+ uuid: parsedUuid,
2552
+ filter: parsedFilter,
2553
+ page: parsedPage,
2554
+ perPage: parsedPerPage
2555
+ } = gallerySchema.parse({ uuid, filter, page, perPage });
2415
2556
  const response = await fetch(
2416
- `https://ochre.lib.uchicago.edu/ochre?uuid=${parsedUuid}&format=json&lang="*"`
2557
+ `https://ochre.lib.uchicago.edu/ochre?xquery=${encodeURIComponent(`
2558
+ for $q in input()/ochre[@uuid='${parsedUuid}']
2559
+ let $filtered := $q/tree/items/resource[contains(lower-case(identification/label), lower-case('${parsedFilter}'))]
2560
+ let $maxLength := count($filtered)
2561
+ return <gallery maxLength='{$maxLength}'>
2562
+ {$q/metadata/project}
2563
+ {$q/metadata/item}
2564
+ {$filtered[position() >= ${((parsedPage - 1) * parsedPerPage + 1).toString()} and position() < ${(parsedPage * parsedPerPage + 1).toString()}]}
2565
+ </gallery>
2566
+ `)}&format=json`
2417
2567
  );
2418
2568
  if (!response.ok) {
2419
- throw new Error("Failed to fetch OCHRE data");
2569
+ throw new Error("Error fetching gallery items, please try again later.");
2420
2570
  }
2421
- const dataRaw = await response.json();
2422
- if (!("ochre" in dataRaw)) {
2423
- throw new Error("Invalid OCHRE data: API response missing 'ochre' key");
2571
+ const data = await response.json();
2572
+ if (!("gallery" in data.result)) {
2573
+ throw new Error("Failed to fetch gallery");
2424
2574
  }
2425
- return [null, dataRaw];
2426
- } catch (error) {
2427
- return [error instanceof Error ? error.message : "Unknown error", null];
2428
- }
2429
- }
2430
-
2431
- // src/utils/fetchers/item.ts
2432
- async function fetchItem(uuid, category) {
2433
- try {
2434
- const [error, data] = await fetchByUuid(uuid);
2435
- if (error !== null) {
2436
- throw new Error(error);
2437
- }
2438
- const categoryKey = getItemCategory(Object.keys(data.ochre));
2439
- let item;
2440
- switch (categoryKey) {
2441
- case "resource": {
2442
- if (!("resource" in data.ochre)) {
2443
- throw new Error(
2444
- "Invalid OCHRE data: API response missing 'resource' key"
2445
- );
2446
- }
2447
- item = parseResource(data.ochre.resource);
2448
- break;
2449
- }
2450
- case "spatialUnit": {
2451
- if (!("spatialUnit" in data.ochre)) {
2452
- throw new Error(
2453
- "Invalid OCHRE data: API response missing 'spatialUnit' key"
2454
- );
2455
- }
2456
- item = parseSpatialUnit(data.ochre.spatialUnit);
2457
- break;
2458
- }
2459
- case "concept": {
2460
- if (!("concept" in data.ochre)) {
2461
- throw new Error(
2462
- "Invalid OCHRE data: API response missing 'concept' key"
2463
- );
2464
- }
2465
- item = parseConcept(data.ochre.concept);
2466
- break;
2467
- }
2468
- case "period": {
2469
- if (!("period" in data.ochre)) {
2470
- throw new Error(
2471
- "Invalid OCHRE data: API response missing 'period' key"
2472
- );
2473
- }
2474
- item = parsePeriod(data.ochre.period);
2475
- break;
2476
- }
2477
- case "bibliography": {
2478
- if (!("bibliography" in data.ochre)) {
2479
- throw new Error(
2480
- "Invalid OCHRE data: API response missing 'bibliography' key"
2481
- );
2482
- }
2483
- item = parseBibliography(data.ochre.bibliography);
2484
- break;
2485
- }
2486
- case "person": {
2487
- if (!("person" in data.ochre)) {
2488
- throw new Error(
2489
- "Invalid OCHRE data: API response missing 'person' key"
2490
- );
2491
- }
2492
- item = parsePerson(data.ochre.person);
2493
- break;
2494
- }
2495
- case "propertyValue": {
2496
- if (!("propertyValue" in data.ochre)) {
2497
- throw new Error(
2498
- "Invalid OCHRE data: API response missing 'propertyValue' key"
2499
- );
2500
- }
2501
- item = parsePropertyValue(data.ochre.propertyValue);
2502
- break;
2503
- }
2504
- case "set": {
2505
- if (!("set" in data.ochre)) {
2506
- throw new Error("Invalid OCHRE data: API response missing 'set' key");
2507
- }
2508
- item = parseSet(data.ochre.set);
2509
- break;
2510
- }
2511
- case "tree": {
2512
- if (!("tree" in data.ochre)) {
2513
- throw new Error(
2514
- "Invalid OCHRE data: API response missing 'tree' key"
2515
- );
2516
- }
2517
- item = parseTree(data.ochre.tree);
2518
- break;
2519
- }
2520
- default: {
2521
- throw new Error("Invalid category");
2522
- }
2523
- }
2524
- const metadata = parseMetadata(data.ochre.metadata);
2525
- const belongsTo = {
2526
- uuid: data.ochre.uuidBelongsTo,
2527
- abbreviation: parseFakeString(data.ochre.belongsTo)
2528
- };
2529
- return {
2530
- error: null,
2531
- metadata,
2532
- belongsTo,
2533
- item,
2534
- category
2575
+ const galleryIdentification = parseIdentification(
2576
+ data.result.gallery.item.identification
2577
+ );
2578
+ const galleryProjectIdentification = parseIdentification(
2579
+ data.result.gallery.project.identification
2580
+ );
2581
+ const gallery = {
2582
+ identification: galleryIdentification,
2583
+ projectIdentification: galleryProjectIdentification,
2584
+ resources: data.result.gallery.resource ? Array.isArray(data.result.gallery.resource) ? parseResources(data.result.gallery.resource) : [parseResource(data.result.gallery.resource)] : [],
2585
+ maxLength: data.result.gallery.maxLength
2535
2586
  };
2587
+ return { item: gallery, error: null };
2536
2588
  } catch (error) {
2589
+ console.error(error);
2537
2590
  return {
2538
- error: error instanceof Error ? error.message : "Unknown error",
2539
- metadata: void 0,
2540
- belongsTo: void 0,
2541
- item: void 0,
2542
- category: void 0
2591
+ item: null,
2592
+ error: error instanceof Error ? error.message : "Failed to fetch gallery"
2543
2593
  };
2544
2594
  }
2545
2595
  }
2546
2596
  export {
2547
2597
  fetchByUuid,
2598
+ fetchGallery,
2548
2599
  fetchItem,
2549
2600
  filterProperties,
2550
2601
  getAllPropertyLabels,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitalculture/ochre-sdk",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Node.js library for working with OCHRE (Online Cultural and Historical Research Environment) data",