ochre-sdk 0.19.7 → 0.19.9
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 +20 -8
- package/dist/index.mjs +84 -72
- package/package.json +4 -2
package/dist/index.d.mts
CHANGED
|
@@ -1079,23 +1079,29 @@ type WebBlock<T extends WebBlockLayout = WebBlockLayout> = {
|
|
|
1079
1079
|
/**
|
|
1080
1080
|
* Fetches and parses a gallery from the OCHRE API
|
|
1081
1081
|
*
|
|
1082
|
-
* @param
|
|
1083
|
-
* @param
|
|
1084
|
-
* @param
|
|
1085
|
-
* @param
|
|
1082
|
+
* @param params - The parameters for the fetch
|
|
1083
|
+
* @param params.uuid - The UUID of the gallery
|
|
1084
|
+
* @param params.filter - The filter to apply to the gallery
|
|
1085
|
+
* @param params.page - The page number to fetch
|
|
1086
|
+
* @param params.pageSize - The number of items per page
|
|
1086
1087
|
* @param options - The options for the fetch
|
|
1087
1088
|
* @param options.fetch - The fetch function to use
|
|
1088
1089
|
* @param options.version - The version of the OCHRE API to use
|
|
1089
1090
|
* @returns The parsed gallery or an error message if the fetch/parse fails
|
|
1090
1091
|
*/
|
|
1091
|
-
declare function fetchGallery(
|
|
1092
|
+
declare function fetchGallery(params: {
|
|
1093
|
+
uuid: string;
|
|
1094
|
+
filter?: string;
|
|
1095
|
+
page: number;
|
|
1096
|
+
pageSize: number;
|
|
1097
|
+
}, options?: {
|
|
1092
1098
|
fetch?: (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
1093
1099
|
version?: ApiVersion;
|
|
1094
1100
|
}): Promise<{
|
|
1095
|
-
|
|
1101
|
+
gallery: Gallery | null;
|
|
1096
1102
|
error: null;
|
|
1097
1103
|
} | {
|
|
1098
|
-
|
|
1104
|
+
gallery: null;
|
|
1099
1105
|
error: string;
|
|
1100
1106
|
}>;
|
|
1101
1107
|
//#endregion
|
|
@@ -1208,7 +1214,13 @@ declare function fetchPropertyValuesByPropertyVariables(params: {
|
|
|
1208
1214
|
declare function fetchWebsite(abbreviation: string, options?: {
|
|
1209
1215
|
fetch?: (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
1210
1216
|
version?: ApiVersion;
|
|
1211
|
-
}): Promise<
|
|
1217
|
+
}): Promise<{
|
|
1218
|
+
error: null;
|
|
1219
|
+
website: Website;
|
|
1220
|
+
} | {
|
|
1221
|
+
error: string;
|
|
1222
|
+
website: null;
|
|
1223
|
+
}>;
|
|
1212
1224
|
//#endregion
|
|
1213
1225
|
//#region src/utils/getters.d.ts
|
|
1214
1226
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -543,6 +543,14 @@ function parseFakeStringOrContent(value) {
|
|
|
543
543
|
function parseOptionalDate(dateTime) {
|
|
544
544
|
return dateTime != null ? parseISO(dateTime) : null;
|
|
545
545
|
}
|
|
546
|
+
/**
|
|
547
|
+
* Cleans an object by removing null values
|
|
548
|
+
* @param object - The object to clean
|
|
549
|
+
* @returns The cleaned object
|
|
550
|
+
*/
|
|
551
|
+
function cleanObject(object) {
|
|
552
|
+
return Object.fromEntries(Object.entries(object).filter(([_, value]) => value != null));
|
|
553
|
+
}
|
|
546
554
|
|
|
547
555
|
//#endregion
|
|
548
556
|
//#region src/schemas.ts
|
|
@@ -662,16 +670,6 @@ const propertyValueContentTypeSchema = z.enum([
|
|
|
662
670
|
"IDREF"
|
|
663
671
|
]);
|
|
664
672
|
/**
|
|
665
|
-
* Schema for validating gallery parameters
|
|
666
|
-
* @internal
|
|
667
|
-
*/
|
|
668
|
-
const gallerySchema = z.object({
|
|
669
|
-
uuid: z.string().refine(isPseudoUuid, { error: "Invalid UUID" }),
|
|
670
|
-
filter: z.string().optional(),
|
|
671
|
-
page: z.number().positive({ error: "Page must be positive" }),
|
|
672
|
-
pageSize: z.number().positive({ error: "Page size must be positive" })
|
|
673
|
-
}).strict();
|
|
674
|
-
/**
|
|
675
673
|
* Schema for validating and parsing render options
|
|
676
674
|
* @internal
|
|
677
675
|
*/
|
|
@@ -721,6 +719,40 @@ const boundsSchema = z.string().transform((str, ctx) => {
|
|
|
721
719
|
}
|
|
722
720
|
}).pipe(z.tuple([z.tuple([z.number(), z.number()]), z.tuple([z.number(), z.number()])], { message: "Must contain exactly 2 coordinate pairs" }));
|
|
723
721
|
|
|
722
|
+
//#endregion
|
|
723
|
+
//#region src/utils/helpers.ts
|
|
724
|
+
/**
|
|
725
|
+
* The default API version to use
|
|
726
|
+
*
|
|
727
|
+
* @remarks
|
|
728
|
+
* Version 1 of the OCHRE API is deprecated and will be removed in the future.
|
|
729
|
+
* It points to the old Tamino server.
|
|
730
|
+
*
|
|
731
|
+
* Version 2 of the OCHRE API is the current version and is the default.
|
|
732
|
+
* It points to the new MarkLogic server.
|
|
733
|
+
*/
|
|
734
|
+
const DEFAULT_API_VERSION = 2;
|
|
735
|
+
/**
|
|
736
|
+
* The default page size to use for fetching paginated items
|
|
737
|
+
*/
|
|
738
|
+
const DEFAULT_PAGE_SIZE = 48;
|
|
739
|
+
/**
|
|
740
|
+
* Flatten the properties of an item
|
|
741
|
+
* @param item - The item whose properties to flatten
|
|
742
|
+
* @returns The item with the properties flattened
|
|
743
|
+
*/
|
|
744
|
+
function flattenItemProperties(item) {
|
|
745
|
+
const allProperties = [];
|
|
746
|
+
if ("properties" in item) allProperties.push(...item.properties);
|
|
747
|
+
if ("observations" in item) for (const observation of item.observations) allProperties.push(...observation.properties);
|
|
748
|
+
if ("interpretations" in item) for (const interpretation of item.interpretations) allProperties.push(...interpretation.properties);
|
|
749
|
+
if ("bibliographies" in item) for (const bibliography of item.bibliographies) allProperties.push(...bibliography.properties);
|
|
750
|
+
return {
|
|
751
|
+
...item,
|
|
752
|
+
properties: flattenProperties(allProperties)
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
|
|
724
756
|
//#endregion
|
|
725
757
|
//#region src/utils/parse/index.ts
|
|
726
758
|
/**
|
|
@@ -1795,90 +1827,62 @@ function parseConcepts(concepts) {
|
|
|
1795
1827
|
//#endregion
|
|
1796
1828
|
//#region src/utils/fetchers/gallery.ts
|
|
1797
1829
|
/**
|
|
1830
|
+
* Schema for validating gallery parameters
|
|
1831
|
+
*/
|
|
1832
|
+
const galleryParamsSchema = z.object({
|
|
1833
|
+
uuid: uuidSchema,
|
|
1834
|
+
filter: z.string().optional(),
|
|
1835
|
+
page: z.number().positive({ error: "Page must be positive" }),
|
|
1836
|
+
pageSize: z.number().positive({ error: "Page size must be positive" })
|
|
1837
|
+
});
|
|
1838
|
+
/**
|
|
1798
1839
|
* Fetches and parses a gallery from the OCHRE API
|
|
1799
1840
|
*
|
|
1800
|
-
* @param
|
|
1801
|
-
* @param
|
|
1802
|
-
* @param
|
|
1803
|
-
* @param
|
|
1841
|
+
* @param params - The parameters for the fetch
|
|
1842
|
+
* @param params.uuid - The UUID of the gallery
|
|
1843
|
+
* @param params.filter - The filter to apply to the gallery
|
|
1844
|
+
* @param params.page - The page number to fetch
|
|
1845
|
+
* @param params.pageSize - The number of items per page
|
|
1804
1846
|
* @param options - The options for the fetch
|
|
1805
1847
|
* @param options.fetch - The fetch function to use
|
|
1806
1848
|
* @param options.version - The version of the OCHRE API to use
|
|
1807
1849
|
* @returns The parsed gallery or an error message if the fetch/parse fails
|
|
1808
1850
|
*/
|
|
1809
|
-
async function fetchGallery(
|
|
1851
|
+
async function fetchGallery(params, options) {
|
|
1810
1852
|
try {
|
|
1811
|
-
const
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
});
|
|
1817
|
-
const response = await (options?.fetch ?? fetch)(`https://ochre.lib.uchicago.edu/ochre?xquery=${encodeURIComponent(`
|
|
1818
|
-
for $q in input()/ochre[@uuid='${parsedUuid}']
|
|
1819
|
-
let $filtered := $q//items/resource[contains(lower-case(identification/label), lower-case('${parsedFilter}'))]
|
|
1853
|
+
const version = options?.version ?? DEFAULT_API_VERSION;
|
|
1854
|
+
const { uuid, filter, page, pageSize } = galleryParamsSchema.parse(params);
|
|
1855
|
+
const response = await (options?.fetch ?? fetch)(`${version === 2 ? "https://ochre.lib.uchicago.edu/ochre/v2/ochre.php" : "https://ochre.lib.uchicago.edu/ochre"}?xquery=${encodeURIComponent(`<ochre>{
|
|
1856
|
+
for $q in ${version === 2 ? "doc()" : "input()"}/ochre[@uuid='${uuid}']
|
|
1857
|
+
let $filtered := $q//items/resource[contains(lower-case(identification/label), lower-case('${filter}'))]
|
|
1820
1858
|
let $maxLength := count($filtered)
|
|
1821
1859
|
return <gallery maxLength='{$maxLength}'>
|
|
1822
1860
|
{$q/metadata/project}
|
|
1823
1861
|
{$q/metadata/item}
|
|
1824
|
-
{$filtered[position() >= ${((
|
|
1862
|
+
{$filtered[position() >= ${((page - 1) * pageSize + 1).toString()} and position() < ${(page * pageSize + 1).toString()}]}
|
|
1825
1863
|
</gallery>
|
|
1826
|
-
|
|
1864
|
+
}</ochre>`)}&format=json`);
|
|
1827
1865
|
if (!response.ok) throw new Error("Error fetching gallery items, please try again later.");
|
|
1828
1866
|
const data = await response.json();
|
|
1829
|
-
if (!("gallery" in data.result)) throw new Error("Failed to fetch gallery");
|
|
1867
|
+
if (!("gallery" in data.result.ochre)) throw new Error("Failed to fetch gallery");
|
|
1830
1868
|
return {
|
|
1831
|
-
|
|
1832
|
-
identification: parseIdentification(data.result.gallery.item.identification),
|
|
1833
|
-
projectIdentification: parseIdentification(data.result.gallery.project.identification),
|
|
1834
|
-
resources:
|
|
1835
|
-
maxLength: data.result.gallery.maxLength
|
|
1869
|
+
gallery: {
|
|
1870
|
+
identification: parseIdentification(data.result.ochre.gallery.item.identification),
|
|
1871
|
+
projectIdentification: parseIdentification(data.result.ochre.gallery.project.identification),
|
|
1872
|
+
resources: data.result.ochre.gallery.resource ? parseResources(Array.isArray(data.result.ochre.gallery.resource) ? data.result.ochre.gallery.resource : [data.result.ochre.gallery.resource]) : [],
|
|
1873
|
+
maxLength: data.result.ochre.gallery.maxLength
|
|
1836
1874
|
},
|
|
1837
1875
|
error: null
|
|
1838
1876
|
};
|
|
1839
1877
|
} catch (error) {
|
|
1840
1878
|
console.error(error);
|
|
1841
1879
|
return {
|
|
1842
|
-
|
|
1880
|
+
gallery: null,
|
|
1843
1881
|
error: error instanceof Error ? error.message : "Failed to fetch gallery"
|
|
1844
1882
|
};
|
|
1845
1883
|
}
|
|
1846
1884
|
}
|
|
1847
1885
|
|
|
1848
|
-
//#endregion
|
|
1849
|
-
//#region src/utils/helpers.ts
|
|
1850
|
-
/**
|
|
1851
|
-
* The default API version to use
|
|
1852
|
-
*
|
|
1853
|
-
* @remarks
|
|
1854
|
-
* Version 1 of the OCHRE API is deprecated and will be removed in the future.
|
|
1855
|
-
* It points to the old Tamino server.
|
|
1856
|
-
*
|
|
1857
|
-
* Version 2 of the OCHRE API is the current version and is the default.
|
|
1858
|
-
* It points to the new MarkLogic server.
|
|
1859
|
-
*/
|
|
1860
|
-
const DEFAULT_API_VERSION = 2;
|
|
1861
|
-
/**
|
|
1862
|
-
* The default page size to use for fetching paginated items
|
|
1863
|
-
*/
|
|
1864
|
-
const DEFAULT_PAGE_SIZE = 48;
|
|
1865
|
-
/**
|
|
1866
|
-
* Flatten the properties of an item
|
|
1867
|
-
* @param item - The item whose properties to flatten
|
|
1868
|
-
* @returns The item with the properties flattened
|
|
1869
|
-
*/
|
|
1870
|
-
function flattenItemProperties(item) {
|
|
1871
|
-
const allProperties = [];
|
|
1872
|
-
if ("properties" in item) allProperties.push(...item.properties);
|
|
1873
|
-
if ("observations" in item) for (const observation of item.observations) allProperties.push(...observation.properties);
|
|
1874
|
-
if ("interpretations" in item) for (const interpretation of item.interpretations) allProperties.push(...interpretation.properties);
|
|
1875
|
-
if ("bibliographies" in item) for (const bibliography of item.bibliographies) allProperties.push(...bibliography.properties);
|
|
1876
|
-
return {
|
|
1877
|
-
...item,
|
|
1878
|
-
properties: flattenProperties(allProperties)
|
|
1879
|
-
};
|
|
1880
|
-
}
|
|
1881
|
-
|
|
1882
1886
|
//#endregion
|
|
1883
1887
|
//#region src/utils/fetchers/uuid.ts
|
|
1884
1888
|
/**
|
|
@@ -3569,9 +3573,10 @@ function parseWebBlock(blockResource) {
|
|
|
3569
3573
|
propertiesTablet.isAccordionExpandedByDefault = getPropertyValueByLabel(tabletOverwriteProperties, "accordion-expanded") ?? void 0;
|
|
3570
3574
|
propertiesTablet.isAccordionSidebarDisplayed = getPropertyValueByLabel(tabletOverwriteProperties, "accordion-sidebar-displayed") ?? void 0;
|
|
3571
3575
|
}
|
|
3572
|
-
|
|
3576
|
+
const cleanedPropertiesTablet = cleanObject(propertiesTablet);
|
|
3577
|
+
if (Object.keys(cleanedPropertiesTablet).length > 0) returnBlock.properties.tablet = cleanedPropertiesTablet;
|
|
3573
3578
|
}
|
|
3574
|
-
const mobileOverwriteProperty = getPropertyByLabel(blockMainProperties, "overwrite-
|
|
3579
|
+
const mobileOverwriteProperty = getPropertyByLabel(blockMainProperties, "overwrite-mobile");
|
|
3575
3580
|
if (mobileOverwriteProperty !== null) {
|
|
3576
3581
|
const mobileOverwriteProperties = mobileOverwriteProperty.properties;
|
|
3577
3582
|
const propertiesMobile = {
|
|
@@ -3587,7 +3592,8 @@ function parseWebBlock(blockResource) {
|
|
|
3587
3592
|
propertiesMobile.isAccordionExpandedByDefault = getPropertyValueByLabel(mobileOverwriteProperties, "accordion-expanded") ?? void 0;
|
|
3588
3593
|
propertiesMobile.isAccordionSidebarDisplayed = getPropertyValueByLabel(mobileOverwriteProperties, "accordion-sidebar-displayed") ?? void 0;
|
|
3589
3594
|
}
|
|
3590
|
-
|
|
3595
|
+
const cleanedPropertiesMobile = cleanObject(propertiesMobile);
|
|
3596
|
+
if (Object.keys(cleanedPropertiesMobile).length > 0) returnBlock.properties.mobile = cleanedPropertiesMobile;
|
|
3591
3597
|
}
|
|
3592
3598
|
}
|
|
3593
3599
|
const blockResources = blockResource.resource ? ensureArray(blockResource.resource) : [];
|
|
@@ -3843,10 +3849,16 @@ async function fetchWebsite(abbreviation, options) {
|
|
|
3843
3849
|
abbreviation: parseFakeString(result.ochre.belongsTo)
|
|
3844
3850
|
};
|
|
3845
3851
|
}
|
|
3846
|
-
return
|
|
3852
|
+
return {
|
|
3853
|
+
error: null,
|
|
3854
|
+
website: parseWebsite(tree, metadata, belongsTo, { version })
|
|
3855
|
+
};
|
|
3847
3856
|
} catch (error) {
|
|
3848
3857
|
console.error(error);
|
|
3849
|
-
return
|
|
3858
|
+
return {
|
|
3859
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
3860
|
+
website: null
|
|
3861
|
+
};
|
|
3850
3862
|
}
|
|
3851
3863
|
}
|
|
3852
3864
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ochre-sdk",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.9",
|
|
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",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"zod": "^4.3.6"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@antfu/eslint-config": "^7.4.
|
|
49
|
+
"@antfu/eslint-config": "^7.4.3",
|
|
50
50
|
"@types/node": "^24.10.13",
|
|
51
51
|
"bumpp": "^10.4.1",
|
|
52
52
|
"eslint": "^10.0.0",
|
|
@@ -60,6 +60,8 @@
|
|
|
60
60
|
"build": "tsdown",
|
|
61
61
|
"lint": "eslint --cache .",
|
|
62
62
|
"lint:fix": "eslint --cache . --fix",
|
|
63
|
+
"lint:nocache": "eslint .",
|
|
64
|
+
"lint:nocache:fix": "eslint . --fix",
|
|
63
65
|
"format": "prettier --cache --check .",
|
|
64
66
|
"format:fix": "prettier --cache --write --list-different .",
|
|
65
67
|
"check-types": "tsc --noEmit",
|