ochre-sdk 1.0.12 → 1.0.13

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 CHANGED
@@ -47,7 +47,9 @@ present and `error` is `null`; on failure, the parsed value is `null` and
47
47
 
48
48
  - `fetchItem(uuid, options)` fetches and parses a single OCHRE item. Passing
49
49
  `category` narrows the returned TypeScript type, and `containedItemCategory`
50
- controls how nested Tree or Set contents are parsed.
50
+ controls how nested Tree or Set contents are parsed. For large Trees or Sets,
51
+ pass `shouldOmitEmbeddedItems: true` to fetch the top-level item without its
52
+ embedded `<items>` payload.
51
53
  - `fetchItemLinks(uuid, options)` fetches items linked from a source item and
52
54
  parses them as embedded OCHRE items.
53
55
  - `fetchGallery(params, options)` fetches paginated resource galleries with an
package/dist/index.d.mts CHANGED
@@ -1129,6 +1129,10 @@ type ItemLink<U extends ItemLinkCategory = ItemLinkCategory, T extends LanguageC
1129
1129
  * An Item in OCHRE (can be a tree, set, bibliography, concept, spatial unit, period, person, property value, property variable, or resource)
1130
1130
  */
1131
1131
  type Item<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes, W extends ItemPayloadKind = "topLevel"> = U extends ItemCategory ? U extends "tree" ? Tree<Extract<V, TreeItemCategory>, T, W> : U extends "set" ? Set<Extract<V, SetItemCategory>, T, W> : U extends "bibliography" ? Bibliography<T, W> : U extends "concept" ? Concept<T, W> : U extends "spatialUnit" ? SpatialUnit<T, W> : U extends "period" ? Period<T, W> : U extends "person" ? Person<T, W> : U extends "propertyVariable" ? PropertyVariable<T, W> : U extends "propertyValue" ? PropertyValue<T, W> : U extends "resource" ? Resource<T, W> : U extends "text" ? Text<T, W> : never : never;
1132
+ /**
1133
+ * A Tree or Set fetched without its embedded item hierarchy.
1134
+ */
1135
+ type ItemWithoutEmbeddedItems<U extends ItemContainerCategory = ItemContainerCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes, W extends ItemPayloadKind = "topLevel"> = U extends ItemContainerCategory ? U extends "tree" ? Prettify<Omit<Tree<Extract<V, TreeItemCategory>, T, W>, "items">> : U extends "set" ? Prettify<Omit<Set<Extract<V, SetItemCategory>, T, W>, "items">> : never : never;
1132
1136
  type TopLevelItem<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes> = Item<U, V, T, "topLevel">;
1133
1137
  type EmbeddedItem<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes> = Item<U, V, T, "embedded">;
1134
1138
  type AnyItem<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes> = Item<U, V, T, ItemPayloadKind>;
@@ -1585,7 +1589,21 @@ type FetchItemBaseOptions<TLanguages extends ReadonlyArray<string> | undefined =
1585
1589
  languages?: TLanguages;
1586
1590
  fetch?: FetchFunction$2;
1587
1591
  };
1592
+ type FetchItemNoOmitEmbeddedItemsOption = {
1593
+ shouldOmitEmbeddedItems?: false;
1594
+ };
1588
1595
  type FetchItemLanguages<TLanguages extends ReadonlyArray<string> | undefined> = TLanguages extends readonly [] ? ReadonlyArray<string> : TLanguages extends ReadonlyArray<string> ? TLanguages : ReadonlyArray<string>;
1596
+ type FetchItemSuccess<TItem> = {
1597
+ item: TItem;
1598
+ error: null;
1599
+ detailedError: null;
1600
+ };
1601
+ type FetchItemError = {
1602
+ item: null;
1603
+ error: string;
1604
+ detailedError: string;
1605
+ };
1606
+ type FetchItemResult<TItem> = Promise<FetchItemSuccess<TItem> | FetchItemError>;
1589
1607
  /**
1590
1608
  * Defines a reusable languages tuple with validation and literal type inference.
1591
1609
  *
@@ -1608,46 +1626,33 @@ declare function withLanguages<const TLanguages extends ReadonlyArray<string>>(l
1608
1626
  * @param options - Required options object
1609
1627
  * @param options.category - The category of the OCHRE item to fetch
1610
1628
  * @param options.containedItemCategory - The category of items inside the OCHRE item to fetch. Only valid for Trees and Sets. Tree accepts one category; Set accepts one category or an array.
1629
+ * @param options.shouldOmitEmbeddedItems - Whether to omit the embedded `<items>` node when fetching a Tree or Set.
1611
1630
  * @param options.languages - Language codes to parse. Inline arrays preserve literal types automatically.
1612
1631
  * @param options.fetch - Custom fetch function to use instead of the default fetch
1613
1632
  * @returns An object containing the parsed item
1614
1633
  */
1615
- declare function fetchItem<const TContainedItemCategory extends ContainedItemCategoryOption<ItemContainerCategory> | undefined = undefined, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options?: FetchItemBaseOptions<TLanguages> & {
1634
+ declare function fetchItem<const TContainedItemCategory extends ContainedItemCategoryOption<ItemContainerCategory> | undefined = undefined, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options?: FetchItemBaseOptions<TLanguages> & FetchItemNoOmitEmbeddedItemsOption & {
1616
1635
  category?: undefined;
1617
1636
  containedItemCategory?: TContainedItemCategory;
1618
- }): Promise<{
1619
- item: Item<ItemCategory, ContainedItemCategoryFromOption<ItemCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>;
1620
- error: null;
1621
- detailedError: null;
1622
- } | {
1623
- item: null;
1624
- error: string;
1625
- detailedError: string;
1626
- }>;
1637
+ }): FetchItemResult<Item<ItemCategory, ContainedItemCategoryFromOption<ItemCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>>;
1638
+ declare function fetchItem<const TContainedItemCategory extends ContainedItemCategoryOption<ItemContainerCategory> | undefined = undefined, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options: FetchItemBaseOptions<TLanguages> & {
1639
+ category?: undefined;
1640
+ containedItemCategory?: TContainedItemCategory;
1641
+ shouldOmitEmbeddedItems: true;
1642
+ }): FetchItemResult<ItemWithoutEmbeddedItems<ItemContainerCategory, ContainedItemCategoryFromOption<ItemContainerCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>>;
1643
+ declare function fetchItem<const TCategory extends ItemContainerCategory, const TContainedItemCategory extends ContainedItemCategoryOption<TCategory> | undefined = undefined, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options: FetchItemBaseOptions<TLanguages> & FetchItemNoOmitEmbeddedItemsOption & {
1644
+ category: TCategory;
1645
+ containedItemCategory?: TContainedItemCategory;
1646
+ }): FetchItemResult<Item<TCategory, ContainedItemCategoryFromOption<TCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>>;
1627
1647
  declare function fetchItem<const TCategory extends ItemContainerCategory, const TContainedItemCategory extends ContainedItemCategoryOption<TCategory> | undefined = undefined, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options: FetchItemBaseOptions<TLanguages> & {
1628
1648
  category: TCategory;
1629
1649
  containedItemCategory?: TContainedItemCategory;
1630
- }): Promise<{
1631
- item: Item<TCategory, ContainedItemCategoryFromOption<TCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>;
1632
- error: null;
1633
- detailedError: null;
1634
- } | {
1635
- item: null;
1636
- error: string;
1637
- detailedError: string;
1638
- }>;
1639
- declare function fetchItem<const TCategory extends ItemCategory, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options: FetchItemBaseOptions<TLanguages> & {
1650
+ shouldOmitEmbeddedItems: true;
1651
+ }): FetchItemResult<ItemWithoutEmbeddedItems<TCategory, ContainedItemCategoryFromOption<TCategory, TContainedItemCategory>, FetchItemLanguages<TLanguages>>>;
1652
+ declare function fetchItem<const TCategory extends ItemCategory, const TLanguages extends ReadonlyArray<string> | undefined = undefined>(uuid: string, options: FetchItemBaseOptions<TLanguages> & FetchItemNoOmitEmbeddedItemsOption & {
1640
1653
  category: TCategory;
1641
1654
  containedItemCategory?: never;
1642
- }): Promise<{
1643
- item: Item<TCategory, ContainedItemCategory<TCategory>, FetchItemLanguages<TLanguages>>;
1644
- error: null;
1645
- detailedError: null;
1646
- } | {
1647
- item: null;
1648
- error: string;
1649
- detailedError: string;
1650
- }>;
1655
+ }): FetchItemResult<Item<TCategory, ContainedItemCategory<TCategory>, FetchItemLanguages<TLanguages>>>;
1651
1656
  //#endregion
1652
1657
  //#region src/fetchers/set/items.d.ts
1653
1658
  type FetchFunction$1 = (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
@@ -1963,4 +1968,4 @@ declare const DEFAULT_PAGE_SIZE = 48;
1963
1968
  */
1964
1969
  declare function flattenItemProperties<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes, W extends ItemPayloadKind = "topLevel">(item: Item<U, V, T, W>): FlattenedItem<Item<U, V, T, W>, T>;
1965
1970
  //#endregion
1966
- export { AccordionWebBlock, AnyBibliography, AnyConcept, AnyItem, AnyPeriod, AnyPerson, AnyPropertyValue, AnyPropertyVariable, AnyResource, AnySet, AnySpatialUnit, AnyText, AnyTree, BaseItem, BaseItemLink, BelongsTo, Bibliography, BibliographyEntryInfo, BibliographyItemLink, BibliographySourceDocument, Concept, ConceptItemLink, ContainedItemCategory, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Context, ContextItem, ContextItemCategory, ContextNode, ContextTree, ContextTreeFilterLevel, ContextTreeLevel, ContextTreeLevelItem, Coordinates, CoordinatesSource, DEFAULT_PAGE_SIZE, DictionaryUnitItemLink, EmbeddedBibliography, EmbeddedConcept, EmbeddedItem, EmbeddedPeriod, EmbeddedPerson, EmbeddedPropertyValue, EmbeddedPropertyVariable, EmbeddedResource, EmbeddedSet, EmbeddedSpatialUnit, EmbeddedText, EmbeddedTree, Event, Gallery, Heading, HeadingItemCategory, Identification, Image, ImageMap, ImageMapArea, Interpretation, Item, ItemCategory, ItemContainerCategory, ItemLink, ItemLinkCategory, ItemLinks, ItemPayloadKind, ItemProperty, LanguageCodes, License, Metadata, type MultilingualOptions, MultilingualString, type MultilingualStringEntries, type MultilingualStringEntry, type MultilingualStringInput, type MultilingualStringJSON, type MultilingualStringObject, type MultilingualStringText, Note, Observation, Period, PeriodItemLink, Person, PersonItemLink, Property, PropertyLike, PropertyOptions, PropertyValue, PropertyValueContent, PropertyValueDataType, PropertyValueItemLink, PropertyValueQueryItem, PropertyVariable, PropertyVariableItemLink, Query, QueryGroup, QueryLeaf, QueryablePropertyValueDataType, RecursiveItemCategory, Resource, ResourceItemLink, Scope, Section, Set, SetAttributeValueQueryItem, SetBibliography, SetConcept, SetItem, SetItemCategory, SetItemLink, SetItemProperty, SetItemSimplifiedProperty, SetItemsSort, SetItemsSortDirection, SetPeriod, SetResource, SetSpatialUnit, SetTree, SimplifiedProperty, SpatialUnit, SpatialUnitItemLink, Style, StylesheetCategory, StylesheetItem, Text, TextItemLink, TopLevelItem, Tree, TreeItemCategory, TreeItemLink, WebBlock, WebBlockByLayout, WebBlockLayout, WebElement, WebElementComponent, WebElementComponentName, WebElementComponentOf, WebElementOf, WebImage, WebTitle, Webpage, Website, WebsitePropertyQuery, WebsitePropertyQueryNode, WebsiteSegment, WebsiteType, defineLanguages, fetchGallery, fetchItem, fetchItemLinks, fetchSetItems, fetchSetPropertyValues, fetchWebsite, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
1971
+ export { AccordionWebBlock, AnyBibliography, AnyConcept, AnyItem, AnyPeriod, AnyPerson, AnyPropertyValue, AnyPropertyVariable, AnyResource, AnySet, AnySpatialUnit, AnyText, AnyTree, BaseItem, BaseItemLink, BelongsTo, Bibliography, BibliographyEntryInfo, BibliographyItemLink, BibliographySourceDocument, Concept, ConceptItemLink, ContainedItemCategory, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Context, ContextItem, ContextItemCategory, ContextNode, ContextTree, ContextTreeFilterLevel, ContextTreeLevel, ContextTreeLevelItem, Coordinates, CoordinatesSource, DEFAULT_PAGE_SIZE, DictionaryUnitItemLink, EmbeddedBibliography, EmbeddedConcept, EmbeddedItem, EmbeddedPeriod, EmbeddedPerson, EmbeddedPropertyValue, EmbeddedPropertyVariable, EmbeddedResource, EmbeddedSet, EmbeddedSpatialUnit, EmbeddedText, EmbeddedTree, Event, Gallery, Heading, HeadingItemCategory, Identification, Image, ImageMap, ImageMapArea, Interpretation, Item, ItemCategory, ItemContainerCategory, ItemLink, ItemLinkCategory, ItemLinks, ItemPayloadKind, ItemProperty, ItemWithoutEmbeddedItems, LanguageCodes, License, Metadata, type MultilingualOptions, MultilingualString, type MultilingualStringEntries, type MultilingualStringEntry, type MultilingualStringInput, type MultilingualStringJSON, type MultilingualStringObject, type MultilingualStringText, Note, Observation, Period, PeriodItemLink, Person, PersonItemLink, Property, PropertyLike, PropertyOptions, PropertyValue, PropertyValueContent, PropertyValueDataType, PropertyValueItemLink, PropertyValueQueryItem, PropertyVariable, PropertyVariableItemLink, Query, QueryGroup, QueryLeaf, QueryablePropertyValueDataType, RecursiveItemCategory, Resource, ResourceItemLink, Scope, Section, Set, SetAttributeValueQueryItem, SetBibliography, SetConcept, SetItem, SetItemCategory, SetItemLink, SetItemProperty, SetItemSimplifiedProperty, SetItemsSort, SetItemsSortDirection, SetPeriod, SetResource, SetSpatialUnit, SetTree, SimplifiedProperty, SpatialUnit, SpatialUnitItemLink, Style, StylesheetCategory, StylesheetItem, Text, TextItemLink, TopLevelItem, Tree, TreeItemCategory, TreeItemLink, WebBlock, WebBlockByLayout, WebBlockLayout, WebElement, WebElementComponent, WebElementComponentName, WebElementComponentOf, WebElementOf, WebImage, WebTitle, Webpage, Website, WebsitePropertyQuery, WebsitePropertyQueryNode, WebsiteSegment, WebsiteType, defineLanguages, fetchGallery, fetchItem, fetchItemLinks, fetchSetItems, fetchSetPropertyValues, fetchWebsite, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
package/dist/index.mjs CHANGED
@@ -5484,10 +5484,17 @@ function parseWebsite(data, options) {
5484
5484
  function isItemContainerCategory(category) {
5485
5485
  return category === "tree" || category === "set";
5486
5486
  }
5487
+ function isItemContainer(item) {
5488
+ return isItemContainerCategory(item.category);
5489
+ }
5487
5490
  function assertItemCategoryAllowed(category, containedItemCategory) {
5488
5491
  if (category == null || containedItemCategory == null || isItemContainerCategory(category)) return;
5489
5492
  throw new Error(`containedItemCategory can only be used when category is "tree" or "set"; received category "${category}"`);
5490
5493
  }
5494
+ function assertShouldOmitEmbeddedItemsAllowed(category, shouldOmitEmbeddedItems) {
5495
+ if (!shouldOmitEmbeddedItems || category == null || isItemContainerCategory(category)) return;
5496
+ throw new Error(`shouldOmitEmbeddedItems can only be used when category is "tree" or "set"; received category "${category}"`);
5497
+ }
5491
5498
  function normalizeFetchedCategory(category) {
5492
5499
  switch (category) {
5493
5500
  case "tree":
@@ -5522,6 +5529,25 @@ function inferFetchItemCategory(rawOchre) {
5522
5529
  if ("set" in rawOchre) return "set";
5523
5530
  throw new Error("Could not infer OCHRE item category", { cause: rawOchre });
5524
5531
  }
5532
+ function buildOmitEmbeddedItemsXQuery(uuid) {
5533
+ return `xquery version "1.0-ml";
5534
+
5535
+ let $ochre := doc()/ochre[@uuid=${stringLiteral(uuid)}][1]
5536
+ return
5537
+ if (empty($ochre)) then ()
5538
+ else element ochre {
5539
+ $ochre/@*,
5540
+ for $node in $ochre/node()
5541
+ return
5542
+ if (local-name($node) = ("tree", "set"))
5543
+ then element { node-name($node) } { $node/@*, $node/node()[not(self::items)] }
5544
+ else $node
5545
+ }`;
5546
+ }
5547
+ function omitEmbeddedItems(item) {
5548
+ const { items: _items, ...itemWithoutEmbeddedItems } = item;
5549
+ return itemWithoutEmbeddedItems;
5550
+ }
5525
5551
  /**
5526
5552
  * Validate language codes while preserving literal tuple inference.
5527
5553
  */
@@ -5553,8 +5579,15 @@ async function fetchItem(uuid, options) {
5553
5579
  try {
5554
5580
  const parsedUuid = v.parse(uuidSchema, uuid);
5555
5581
  assertItemCategoryAllowed(options?.category, options?.containedItemCategory);
5582
+ const shouldOmitEmbeddedItems = options?.shouldOmitEmbeddedItems === true;
5583
+ assertShouldOmitEmbeddedItemsAllowed(options?.category, shouldOmitEmbeddedItems);
5556
5584
  const languages = options?.languages == null ? [] : parseLanguages$1(options.languages);
5557
- const response = await (options?.fetch ?? fetch)(`https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?uuid=${parsedUuid}&xsl=none&lang="*"`);
5585
+ const fetcher = options?.fetch ?? fetch;
5586
+ const response = shouldOmitEmbeddedItems ? await fetcher("https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?xquery&xsl=none&lang=\"*\"", {
5587
+ method: "POST",
5588
+ body: buildOmitEmbeddedItemsXQuery(parsedUuid),
5589
+ headers: { "Content-Type": "application/xquery" }
5590
+ }) : await fetcher(`https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?uuid=${parsedUuid}&xsl=none&lang="*"`);
5558
5591
  if (!response.ok) throw new Error("Failed to fetch OCHRE data", { cause: response.statusText });
5559
5592
  const dataRaw = await response.text();
5560
5593
  const data = new XMLParser(XML_PARSER_OPTIONS).parse(dataRaw);
@@ -5563,13 +5596,15 @@ async function fetchItem(uuid, options) {
5563
5596
  restoreXMLMetadata(output, data);
5564
5597
  const category = options?.category ?? inferFetchItemCategory(output.result.ochre);
5565
5598
  assertItemCategoryAllowed(category, options?.containedItemCategory);
5599
+ assertShouldOmitEmbeddedItemsAllowed(category, shouldOmitEmbeddedItems);
5600
+ const parsedItem = parseItem(output, {
5601
+ category,
5602
+ containedItemCategory: options?.containedItemCategory,
5603
+ languages,
5604
+ parseResourceView: (view, context) => parseWebpageView(view, { languages: context.metadata.languages }, context)
5605
+ });
5566
5606
  return {
5567
- item: parseItem(output, {
5568
- category,
5569
- containedItemCategory: options?.containedItemCategory,
5570
- languages,
5571
- parseResourceView: (view, context) => parseWebpageView(view, { languages: context.metadata.languages }, context)
5572
- }),
5607
+ item: shouldOmitEmbeddedItems && isItemContainer(parsedItem) ? omitEmbeddedItems(parsedItem) : parsedItem,
5573
5608
  error: null,
5574
5609
  detailedError: null
5575
5610
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
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",