ochre-sdk 1.3.28 → 1.3.30

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.
@@ -23,7 +23,7 @@ single category when it is known, or an array when the item may be any
23
23
  category in that list.
24
24
  @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.
25
25
  @param options.shouldOmitEmbeddedItems - Whether to omit the embedded item hierarchy when fetching a recursive item category. Ignored when the fetched item does not expose recursive embedded items.
26
- @param options.website - The website the item is shown in, whose page and component settings a resource's view starts from, as the website's own pages do
26
+ @param options.website - The website the item is shown in, whose page and component settings a resource's or text's view and popupView start from, as the website's own pages do
27
27
  @param options.languages - Language codes to parse. Inline arrays preserve literal types automatically.
28
28
  @param options.fetch - Custom fetch function to use instead of the default fetch
29
29
  @returns An object containing the parsed item
@@ -85,7 +85,7 @@ async function fetchItem(uuid, options) {
85
85
  category: options?.category,
86
86
  containedItemCategory: options?.containedItemCategory,
87
87
  languages,
88
- parseResourceView: (view, context) => parseWebpageView(view, { languages: context.metadata.languages }, context, options?.website ?? null)
88
+ parseView: (view, context) => parseWebpageView(view, { languages: context.metadata.languages }, context, options?.website ?? null)
89
89
  });
90
90
  assertItemCategoryAllowed(parsedItem.category, options?.containedItemCategory);
91
91
  return {
@@ -1,9 +1,9 @@
1
1
  import { ParserOptions, getParserOptions, parseStringLike } from "./helpers.mjs";
2
2
  import { Bibliography, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Coordinates, Gallery, Identification, Item, ItemCategory, ItemCategoryFromOption, ItemCategoryOption, ItemContainerCategory, ItemLinks, Metadata, Note, Person, Property, Resource, SetItem, SetItemCategory, SimplifiedProperty, SpatialUnit } from "../types/index.mjs";
3
3
  import { Webpage } from "../types/website.mjs";
4
- import { XMLBibliography, XMLCoordinates, XMLData, XMLDataItem, XMLGalleryData, XMLIdentification, XMLItemLinks, XMLLink, XMLMetadata, XMLNote, XMLPerson, XMLProperty, XMLResource, XMLSetItems, XMLSimplifiedProperty, XMLSpatialUnit } from "../xml/types.mjs";
4
+ import { XMLBibliography, XMLCoordinates, XMLData, XMLDataItem, XMLGalleryData, XMLIdentification, XMLItemLinks, XMLItemView, XMLLink, XMLMetadata, XMLNote, XMLPerson, XMLProperty, XMLSetItems, XMLSimplifiedProperty, XMLSpatialUnit } from "../xml/types.mjs";
5
5
  export type RawOchre = XMLData["result"]["ochre"];
6
- type ResourceViewParser<T extends ReadonlyArray<string>> = (view: XMLResource["view"], context: Pick<Resource<T, "topLevel">, "belongsTo" | "metadata">) => Webpage<T> | null;
6
+ type ItemViewParser<T extends ReadonlyArray<string>> = (view: XMLItemView | undefined, context: Pick<Resource<T, "topLevel">, "belongsTo" | "metadata">) => Webpage<T> | null;
7
7
  type SetItemCategoryFromCategories<T extends ReadonlyArray<SetItemCategory> | undefined> = T extends ReadonlyArray<infer U> ? Extract<U, SetItemCategory> : SetItemCategory;
8
8
  export declare function parseIdentification<T extends ReadonlyArray<string>>(rawIdentification: XMLIdentification, options: ParserOptions<T>): Identification<T>;
9
9
  export declare function parseCoordinates<T extends ReadonlyArray<string>>(rawCoordinates: XMLCoordinates | undefined, options: ParserOptions<T>): Array<Coordinates<T>>;
@@ -39,18 +39,18 @@ export declare function parseItem<const TContainedItemCategory extends Contained
39
39
  category?: undefined;
40
40
  containedItemCategory?: TContainedItemCategory;
41
41
  languages: T;
42
- parseResourceView?: ResourceViewParser<T>;
42
+ parseView?: ItemViewParser<T>;
43
43
  }): Item<ItemCategory, ContainedItemCategoryFromOption<ItemCategory, TContainedItemCategory>, T>;
44
44
  export declare function parseItem<const TCategory extends ItemCategoryOption, const TContainedItemCategory extends ContainedItemCategoryOption<Extract<ItemCategoryFromOption<TCategory>, ItemContainerCategory>> | undefined = undefined, T extends ReadonlyArray<string> = ReadonlyArray<string>>(rawData: XMLData, options: {
45
45
  category: TCategory;
46
46
  containedItemCategory?: TContainedItemCategory;
47
47
  languages: T;
48
- parseResourceView?: ResourceViewParser<T>;
48
+ parseView?: ItemViewParser<T>;
49
49
  }): Item<ItemCategoryFromOption<TCategory>, ContainedItemCategoryFromOption<ItemCategoryFromOption<TCategory>, TContainedItemCategory>, T>;
50
50
  export declare function parseItem(rawData: XMLData, options: {
51
51
  category?: ItemCategoryOption;
52
52
  containedItemCategory?: ContainedItemCategoryOption<ItemCategory>;
53
53
  languages: ReadonlyArray<string>;
54
- parseResourceView?: ResourceViewParser<ReadonlyArray<string>>;
54
+ parseView?: ItemViewParser<ReadonlyArray<string>>;
55
55
  }): Item<ItemCategory, SetItemCategory, ReadonlyArray<string>>;
56
56
  export { type ParserOptions, getParserOptions, parseStringLike };
@@ -1232,17 +1232,19 @@ function parseItem(rawData, options) {
1232
1232
  const metadata = parseMetadata(rawOchre, parserOptions, defaultLanguage);
1233
1233
  const persistentUrl = rawOchre.persistentUrl ?? null;
1234
1234
  const item = parseTopLevelItem(rawOchre, category, parserOptions);
1235
- if (category === "resource") {
1236
- const rawResource = getSingleTopLevelRawItem("resource" in rawOchre ? rawOchre.resource : null, "resource");
1235
+ if (category === "resource" || category === "text") {
1236
+ const rawItem = category === "resource" ? getSingleTopLevelRawItem("resource" in rawOchre ? rawOchre.resource : null, "resource") : getSingleTopLevelRawItem("text" in rawOchre ? rawOchre.text : null, "text");
1237
+ const parseView = (view) => options.parseView?.(view, {
1238
+ belongsTo,
1239
+ metadata
1240
+ }) ?? null;
1237
1241
  return {
1238
1242
  ...item,
1239
1243
  belongsTo,
1240
1244
  metadata,
1241
1245
  persistentUrl,
1242
- view: options.parseResourceView?.(rawResource.view, {
1243
- belongsTo,
1244
- metadata
1245
- }) ?? null
1246
+ view: parseView(rawItem.view),
1247
+ popupView: parseView(rawItem.popupView)
1246
1248
  };
1247
1249
  }
1248
1250
  return {
@@ -18,13 +18,16 @@ export type WebElementContext<T extends ReadonlyArray<string>> = {
18
18
  /**
19
19
  Reads the "customize-special" "element" properties of a website, each
20
20
  holding "component" properties whose settings every element of that
21
- component starts from, except the ones in {@link ELEMENT_TARGET_LABELS}
21
+ component starts from
22
22
 
23
23
  @param websiteReader - Reader over the website's "presentation" properties
24
24
  @param parent - The defaults of the enclosing website, which these extend
25
+ @param websiteUuid - The website's UUID, for error messages
25
26
  @returns The component defaults in effect for the website's elements
27
+ @throws When a default sets one of {@link ELEMENT_TARGET_LABELS}, which
28
+ every element has to set itself
26
29
  */
27
- export declare function parseComponentDefaults<T extends ReadonlyArray<string>>(websiteReader: WebsitePresentationReader<T>, parent: WebComponentDefaults<T>): WebComponentDefaults<T>;
30
+ export declare function parseComponentDefaults<T extends ReadonlyArray<string>>(websiteReader: WebsitePresentationReader<T>, parent: WebComponentDefaults<T>, websiteUuid: string): WebComponentDefaults<T>;
28
31
  /**
29
32
  What every component parser is handed
30
33
  */
@@ -125,18 +125,23 @@ const ELEMENT_TARGET_LABELS = [
125
125
  /**
126
126
  Reads the "customize-special" "element" properties of a website, each
127
127
  holding "component" properties whose settings every element of that
128
- component starts from, except the ones in {@link ELEMENT_TARGET_LABELS}
128
+ component starts from
129
129
 
130
130
  @param websiteReader - Reader over the website's "presentation" properties
131
131
  @param parent - The defaults of the enclosing website, which these extend
132
+ @param websiteUuid - The website's UUID, for error messages
132
133
  @returns The component defaults in effect for the website's elements
134
+ @throws When a default sets one of {@link ELEMENT_TARGET_LABELS}, which
135
+ every element has to set itself
133
136
  */
134
- function parseComponentDefaults(websiteReader, parent) {
137
+ function parseComponentDefaults(websiteReader, parent, websiteUuid) {
135
138
  const elementReaders = websiteReader.nestedAllByValue("customize-special", "element");
136
139
  if (elementReaders.length === 0) return parent;
137
140
  const defaults = { ...parent };
138
141
  for (const component of componentSchema.options) for (const elementReader of elementReaders) {
139
- const componentProperties = elementReader.propertyByValue("component", component)?.properties.filter((property) => ELEMENT_TARGET_LABELS.every((label) => getProperty([property], { label }) == null));
142
+ const componentProperties = elementReader.propertyByValue("component", component)?.properties;
143
+ const targetLabel = ELEMENT_TARGET_LABELS.find((label) => getProperty(componentProperties ?? [], { label }) != null);
144
+ if (targetLabel != null) throw new Error(`Component defaults cannot set \u{201C}${targetLabel}\u{201D}, which each element sets itself (component \u{201C}${component}\u{201D}, website uuid \u{201C}${websiteUuid}\u{201D})`, { cause: componentProperties });
140
145
  if (componentProperties != null && componentProperties.length > 0) {
141
146
  defaults[component] = [componentProperties, ...parent[component] ?? []];
142
147
  break;
@@ -1,9 +1,7 @@
1
1
  import { ParserOptions } from "../helpers.mjs";
2
2
  import { Webpage, Website } from "../../types/website.mjs";
3
- import { XMLWebsiteData, XMLWebsiteResource } from "../../xml/types.mjs";
4
- export declare function parseWebpageView<T extends ReadonlyArray<string>>(view: {
5
- resource?: Array<XMLWebsiteResource>;
6
- } | undefined, options: ParserOptions<T>, context: Pick<Website<T>, "belongsTo" | "metadata">, website: Pick<Website<T>, "properties"> | null): Webpage<T> | null;
3
+ import { XMLItemView, XMLWebsiteData } from "../../xml/types.mjs";
4
+ export declare function parseWebpageView<T extends ReadonlyArray<string>>(view: XMLItemView | undefined, options: ParserOptions<T>, context: Pick<Website<T>, "belongsTo" | "metadata">, website: Pick<Website<T>, "properties"> | null): Webpage<T> | null;
7
5
  export declare function parseWebsite<const T extends ReadonlyArray<string> = ReadonlyArray<string>>(data: XMLWebsiteData, options?: {
8
6
  languages?: T;
9
7
  }): Website<T>;
@@ -302,7 +302,7 @@ function parseWebsiteTree(websiteTree, options, context, type, slugPrefix) {
302
302
  const treeContext = {
303
303
  ...context,
304
304
  pageSlugs: context.pageSlugs ?? collectWebsitePageSlugs(pages, options),
305
- componentDefaults: parseComponentDefaults(websiteReader, context.componentDefaults)
305
+ componentDefaults: parseComponentDefaults(websiteReader, context.componentDefaults, websiteTree.uuid)
306
306
  };
307
307
  const sidebar = parseSidebar(resources, options, treeContext);
308
308
  const properties = parseWebsiteProperties(websiteReader, websiteTree, sidebar, options, context.parentProperties ?? null, treeContext.componentDefaults);
@@ -817,6 +817,10 @@ export type PropertyValue<T extends LanguageCodes = LanguageCodes, U extends Ite
817
817
  properties: Array<Property<T>>;
818
818
  bibliographies: Array<Bibliography<T, "embedded">>;
819
819
  }>;
820
+ type ItemViews<T extends LanguageCodes, U extends ItemPayloadKind> = U extends "topLevel" ? {
821
+ view: Webpage<T> | null;
822
+ popupView: Webpage<T> | null;
823
+ } : unknown;
820
824
  /**
821
825
  Resource in OCHRE
822
826
  */
@@ -841,9 +845,7 @@ export type Resource<T extends LanguageCodes = LanguageCodes, U extends ItemPayl
841
845
  properties: Array<Property<T>>;
842
846
  bibliographies: Array<Bibliography<T, "embedded">>;
843
847
  items: RecursiveItemChildren<U, Resource<T, RecursiveChildPayloadKind<U>>>;
844
- } & (U extends "topLevel" ? {
845
- view: Webpage<T> | null;
846
- } : unknown)>;
848
+ } & ItemViews<T, U>>;
847
849
  /**
848
850
  Text in OCHRE
849
851
  */
@@ -860,7 +862,7 @@ export type Text<T extends LanguageCodes = LanguageCodes, U extends ItemPayloadK
860
862
  periods: Array<Period<T, "embedded">>;
861
863
  creators: Array<Person<T, "embedded">>;
862
864
  editions: Array<Person<T, "embedded">>;
863
- }>;
865
+ } & ItemViews<T, U>>;
864
866
  /**
865
867
  Section in OCHRE
866
868
  */
@@ -203,7 +203,7 @@ export type Website<T extends LanguageCodes = LanguageCodes> = {
203
203
  component name: the "component" properties of this website, then of each
204
204
  enclosing one, which an element overrides by setting the property itself.
205
205
  The settings naming what an element points at ("link-to", "navigate-to",
206
- and "bound-element") are left out, since every element sets its own.
206
+ and "bound-element") cannot be defaults, since every element sets its own.
207
207
  */
208
208
  componentDefaults: Partial<Record<WebElementComponentName, Array<Array<SimplifiedProperty<T>>>>>;
209
209
  itemPage: {
@@ -3786,6 +3786,9 @@ export declare const XMLGalleryData: v.ObjectSchema<{
3786
3786
  readonly view: v.OptionalSchema<v.ObjectSchema<{
3787
3787
  readonly resource: v.OptionalSchema<v.ArraySchema<v.LazySchema<v.GenericSchema<unknown, XMLWebsiteResource>>, undefined>, undefined>;
3788
3788
  }, undefined>, undefined>;
3789
+ readonly popupView: v.OptionalSchema<v.ObjectSchema<{
3790
+ readonly resource: v.OptionalSchema<v.ArraySchema<v.LazySchema<v.GenericSchema<unknown, XMLWebsiteResource>>, undefined>, undefined>;
3791
+ }, undefined>, undefined>;
3789
3792
  readonly lang: v.OptionalSchema<v.StringSchema<"XMLResource: lang is string and optional">, undefined>;
3790
3793
  readonly uuid: v.SchemaWithPipe<readonly [v.StringSchema<"XMLBaseItem: uuid is string and required">, v.CheckAction<string, "XMLBaseItem: uuid is not a valid pseudo-UUID">]>;
3791
3794
  readonly publicationDateTime: v.OptionalSchema<v.GenericSchema<unknown, Date>, undefined>;
@@ -667,6 +667,7 @@ const XMLPropertyVariable = v.object({
667
667
  notes: v.optional(v.object({ note: v.array(XMLNote) })),
668
668
  bibliographies: v.optional(v.object({ bibliography: v.array(XMLBibliography) }))
669
669
  }, "XMLPropertyVariable: Shape error");
670
+ const XMLItemView = v.object({ resource: v.optional(v.array(v.lazy(() => XMLWebsiteResource))) });
670
671
  const XMLResource = v.object({
671
672
  ...XMLBaseItem.entries,
672
673
  type: v.optional(v.string("XMLResource: type is string and optional")),
@@ -693,7 +694,8 @@ const XMLResource = v.object({
693
694
  properties: v.optional(v.object({ property: v.array(XMLProperty) })),
694
695
  bibliographies: v.optional(v.object({ bibliography: v.array(XMLBibliography) })),
695
696
  resource: v.optional(v.array(v.lazy(() => XMLResource))),
696
- view: v.optional(v.object({ resource: v.optional(v.array(v.lazy(() => XMLWebsiteResource))) })),
697
+ view: v.optional(XMLItemView),
698
+ popupView: v.optional(XMLItemView),
697
699
  lang: v.optional(v.string("XMLResource: lang is string and optional"))
698
700
  }, "XMLResource: Shape error");
699
701
  const XMLSection = v.object({
@@ -727,7 +729,9 @@ const XMLText = v.object({
727
729
  edition: v.optional(v.array(v.lazy(() => XMLPerson))),
728
730
  editor: v.optional(v.array(v.lazy(() => XMLPerson))),
729
731
  publisher: v.optional(v.array(v.lazy(() => XMLPerson)))
730
- }, "XMLText: editions is object with edition array of XMLPerson"))
732
+ }, "XMLText: editions is object with edition array of XMLPerson")),
733
+ view: v.optional(XMLItemView),
734
+ popupView: v.optional(XMLItemView)
731
735
  }, "XMLText: Shape error");
732
736
  const XMLLink = v.pipe(v.object({
733
737
  tree: v.optional(v.array(XMLLinkedTree)),
@@ -559,6 +559,13 @@ export type XMLPropertyValue = XMLBaseItem & {
559
559
  bibliography: Array<XMLBibliography>;
560
560
  };
561
561
  };
562
+ export type XMLItemView = {
563
+ resource?: Array<XMLWebsiteResource>;
564
+ };
565
+ type XMLItemViews = {
566
+ view?: XMLItemView;
567
+ popupView?: XMLItemView;
568
+ };
562
569
  export type XMLResource = XMLBaseItem & {
563
570
  lang?: string;
564
571
  type?: string;
@@ -589,10 +596,7 @@ export type XMLResource = XMLBaseItem & {
589
596
  bibliography: Array<XMLBibliography>;
590
597
  };
591
598
  resource?: Array<XMLResource>;
592
- view?: {
593
- resource?: Array<XMLWebsiteResource>;
594
- };
595
- };
599
+ } & XMLItemViews;
596
600
  export type XMLSection = {
597
601
  uuid: string;
598
602
  publicationDateTime?: Date;
@@ -632,7 +636,7 @@ export type XMLText = XMLBaseItem & {
632
636
  editor?: Array<XMLPerson>;
633
637
  publisher?: Array<XMLPerson>;
634
638
  };
635
- };
639
+ } & XMLItemViews;
636
640
  export type XMLHeading = {
637
641
  name: string;
638
642
  abbreviation?: string;
@@ -753,7 +757,7 @@ export type XMLWebsiteSegment = {
753
757
  publicationDateTime?: Date;
754
758
  };
755
759
  export type XMLWebsiteResourceItem = XMLWebsiteResource | XMLWebsiteResourceGroup | XMLWebsiteSegment;
756
- export type XMLWebsiteResource = Omit<XMLResource, "properties" | "resource" | "view"> & {
760
+ export type XMLWebsiteResource = Omit<XMLResource, "properties" | "resource" | "view" | "popupView"> & {
757
761
  format?: string;
758
762
  slug?: string;
759
763
  options?: XMLWebsiteOptions;
@@ -992,4 +996,5 @@ export type XMLWebsiteData = {
992
996
  };
993
997
  };
994
998
  };
995
- export type XMLHeadingItemCategory = Exclude<XMLItemCategory, "tree" | "bibliography" | "spatialUnit" | "concept" | "period">;
999
+ export type XMLHeadingItemCategory = Exclude<XMLItemCategory, "tree" | "bibliography" | "spatialUnit" | "concept" | "period">;
1000
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "1.3.28",
3
+ "version": "1.3.30",
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",