UNPKG

@kit-data-manager/react-search-component

Version:

All-in-one component for rendering an elastic search UI for searching anything. Built-in support for visualizing related items in a graph and resolving unique identifiers.

60 lines 2 kB
import { PIDDataSchema } from "../lib/PIDDataType"; export class PidResolver { constructor() { this.resolverUrl = "https://hdl.handle.net/"; this.cache = new Map(); } async resolve(pid) { if (!PidResolver.isPID(pid)) { throw `Tried to resolve something that is not a pid ${pid}`; } if (this.cache.has(pid)) { return this.cache.get(pid); } else { const pidResolvePromise = new Promise((resolve, reject) => { this.resolveFromRemote(pid) .then((resolved) => { if (resolved) { this.cache.set(pid, resolved); resolve(resolved); } else { reject(); } }) .catch(reject); }); this.cache.set(pid, pidResolvePromise); return pidResolvePromise; } } static isPID(text) { return new RegExp("^([0-9A-Z])+.([0-9A-Z])+/([!-~])+$", "i").test(text); } async resolveFromRemote(pid) { try { const request = await fetch(this.resolverUrl + pid); if (request.ok) { const data = await request.json(); try { return PIDDataSchema.parse(data); } catch (e) { console.error(`Failed to parse response for resolve request of pid ${pid}`, e); return null; } } else { console.error(`Network request failed for parsing pid ${pid}`, request.status); return null; } } catch (e) { console.error(`Network request failed for parsing pid ${pid}`, e); return null; } } } export const pidResolver = new PidResolver(); //# sourceMappingURL=PidResolver.js.map