@allmaps/stdlib
Version:
Allmaps Standard Library
65 lines (64 loc) • 2.21 kB
JavaScript
import { fetchJson } from './fetch.js';
function fetchAnnotationsByIiifUrl(url) {
// TODO: move base URLs to env/config file
return fetchJson(`https://annotations.allmaps.org/?url=${url}`);
}
async function fetchAnnotationsForImage(parsedImage) {
try {
const annotations = await fetchAnnotationsByIiifUrl(`${parsedImage.uri}/info.json`);
return [annotations];
}
catch (err) {
return [];
}
}
async function fetchAnnotationsForManifest(parsedManifest) {
try {
const annotations = await fetchAnnotationsByIiifUrl(parsedManifest.uri);
return [annotations];
}
catch (err) {
const annotations = [];
for (const canvas of parsedManifest.canvases) {
const imageAnnotations = await fetchAnnotationsForImage(canvas.image);
annotations.push(...imageAnnotations);
}
return annotations;
}
}
async function fetchAnnotationsForCollection(parsedCollection) {
try {
const annotations = await fetchAnnotationsByIiifUrl(parsedCollection.uri);
return [annotations];
}
catch (err) {
const annotations = [];
if ('items' in parsedCollection) {
for (const item of parsedCollection.items) {
if (item.type === 'collection') {
const itemAnnotations = await fetchAnnotationsForCollection(item);
annotations.push(...itemAnnotations);
}
else if (item.type === 'manifest' && 'canvases' in item) {
const itemAnnotations = await fetchAnnotationsForManifest(item);
annotations.push(...itemAnnotations);
}
}
}
return annotations;
}
}
export function fetchAnnotationsFromApi(parsedIiif) {
if (parsedIiif.type === 'image') {
return fetchAnnotationsForImage(parsedIiif);
}
else if (parsedIiif.type === 'manifest') {
return fetchAnnotationsForManifest(parsedIiif);
}
else if (parsedIiif.type === 'collection') {
return fetchAnnotationsForCollection(parsedIiif);
}
else {
throw new Error('Unsupported IIIF resource');
}
}