UNPKG

@kit-data-manager/pid-component

Version:

The PID-Component is a web component that can be used to evaluate and display FAIR Digital Objects, PIDs, ORCiDs, and possibly other identifiers in a user-friendly way. It is easily extensible to support other identifier types.

214 lines (213 loc) 10 kB
/*! * * Copyright 2024-2026 Karlsruhe Institute of Technology. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import { cachedFetch } from "../../../utils/DataCache"; import { FoldableItem } from "../../../utils/FoldableItem"; import { beautifyResourceType } from "../ResourceTypeIcons"; export class DataCiteInfo { constructor(doi, response) { var _a; this._doi = doi; this._rawMetadata = response; this._attributes = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.attributes) || {}; } get title() { const titles = this._attributes.titles || []; const mainTitle = titles.find((t) => { if (typeof t === 'string') return true; return !t.titleType || t.titleType === 'Title'; }); if (typeof mainTitle === 'string') return mainTitle; return (mainTitle === null || mainTitle === void 0 ? void 0 : mainTitle.title) || ''; } get creators() { const creators = this._attributes.creators || []; return creators.map((creator) => { var _a, _b; if (typeof creator === 'string') { return { name: creator }; } const result = { name: creator.name || '', givenName: creator.givenName, familyName: creator.familyName, }; if (!result.name && creator.givenName && creator.familyName) { result.name = `${creator.givenName} ${creator.familyName}`; } else if (!result.name) { result.name = creator.givenName || creator.familyName || ''; } const orcidIdentifier = (_a = creator.nameIdentifiers) === null || _a === void 0 ? void 0 : _a.find((id) => { var _a; return ((_a = id.nameIdentifierScheme) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'orcid'; }); if (orcidIdentifier === null || orcidIdentifier === void 0 ? void 0 : orcidIdentifier.nameIdentifier) { result.orcid = orcidIdentifier.nameIdentifier .replace(/^https?:\/\/orcid\.org\//i, '') .replace(/^orcid:/i, ''); } if (creator.affiliation && creator.affiliation.length > 0) { const primaryAffiliation = creator.affiliation[0]; result.affiliation = primaryAffiliation.name; const rorIdentifier = primaryAffiliation.affiliationIdentifier; if (rorIdentifier && ((_b = primaryAffiliation.affiliationIdentifierScheme) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'ror') { result.ror = rorIdentifier.replace(/^https?:\/\/ror\.org\//i, ''); } } return result; }).filter((c) => c.name); } get correspondingAuthor() { var _a, _b; const contributors = this._attributes.contributors || []; const corresponding = contributors.find((c) => { var _a; return ((_a = c.contributorType) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'contactperson'; }); if (!corresponding) return undefined; const result = { name: corresponding.name || '', givenName: corresponding.givenName, familyName: corresponding.familyName, isCorresponding: true, }; if (!result.name && corresponding.givenName && corresponding.familyName) { result.name = `${corresponding.givenName} ${corresponding.familyName}`; } const orcidIdentifier = (_a = corresponding.nameIdentifiers) === null || _a === void 0 ? void 0 : _a.find((id) => { var _a; return ((_a = id.nameIdentifierScheme) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'orcid'; }); if (orcidIdentifier === null || orcidIdentifier === void 0 ? void 0 : orcidIdentifier.nameIdentifier) { result.orcid = orcidIdentifier.nameIdentifier .replace(/^https?:\/\/orcid\.org\//i, '') .replace(/^orcid:/i, ''); } if (corresponding.affiliation && corresponding.affiliation.length > 0) { const primaryAffiliation = corresponding.affiliation[0]; result.affiliation = primaryAffiliation.name; const rorIdentifier = primaryAffiliation.affiliationIdentifier; if (rorIdentifier && ((_b = primaryAffiliation.affiliationIdentifierScheme) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'ror') { result.ror = rorIdentifier.replace(/^https?:\/\/ror\.org\//i, ''); } } return result; } get publisher() { return this._attributes.publisher; } get publicationDate() { const dates = this._attributes.dates || []; const issued = dates.find((d) => { var _a; return ((_a = d.dateType) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'issued'; }); if (issued === null || issued === void 0 ? void 0 : issued.date) return issued.date; if (this._attributes.publicationYear) { return `${this._attributes.publicationYear}`; } return undefined; } get resourceType() { var _a; return ((_a = this._attributes.types) === null || _a === void 0 ? void 0 : _a.resourceTypeGeneral) || this._attributes.resourceTypeGeneral; } get resourceTypeSpecific() { var _a; return (_a = this._attributes.types) === null || _a === void 0 ? void 0 : _a.resourceType; } get description() { const descriptions = this._attributes.descriptions || []; const abstract = descriptions.find((d) => { var _a; if (typeof d === 'string') return true; return ((_a = d.descriptionType) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'abstract' || !d.descriptionType; }); if (typeof abstract === 'string') return abstract; return abstract === null || abstract === void 0 ? void 0 : abstract.description; } get url() { return this._attributes.url || this._doi.toURL(); } get subjects() { const subjects = this._attributes.subjects || []; return subjects.map((s) => { if (typeof s === 'string') return s; return s.subject || ''; }).filter(Boolean); } get rawMetadata() { return this._rawMetadata; } static async fetch(doi) { const apiUrl = `https://api.datacite.org/dois/${encodeURIComponent(doi.toString())}`; try { const response = (await cachedFetch(apiUrl, { headers: { Accept: 'application/vnd.api+json', }, })); if (!response || !response.data) return null; return new DataCiteInfo(doi, response); } catch (error) { console.debug('DataCite API error:', error); return null; } } static fromObject(doiObj, obj) { return new DataCiteInfo(doiObj, obj.rawMetadata); } generateItems() { const items = []; let index = 10; if (this.title) { items.push(new FoldableItem(index++, 'Title', this.title, 'The title of the resource.', undefined, undefined, false)); } const correspondingAuthor = this.correspondingAuthor; if (correspondingAuthor) { items.push(new FoldableItem(index++, 'Corresponding Author', correspondingAuthor.orcid || `${correspondingAuthor.name}${correspondingAuthor.affiliation ? ` (${correspondingAuthor.affiliation})` : ''}`, `This field indicates the first author of the resource, who is often the corresponding author.`)); } const creators = this.creators; creators.forEach((creator, idx) => { if (idx === 0 && correspondingAuthor) return; items.push(new FoldableItem(index++, `Author`, creator.orcid || `${creator.name}${creator.affiliation ? ` (${creator.affiliation})` : ''}`, 'A creator/author of the resource.', creator.orcid ? `https://orcid.org/${creator.orcid}` : undefined)); }); if (this.publisher) { items.push(new FoldableItem(index++, 'Publisher', this.publisher, 'The publisher of the resource.')); } if (this.publicationDate) { items.push(new FoldableItem(index++, 'Publication Date', this.publicationDate, 'The publication date in ISO 8601 format.')); } if (this.resourceType) { const displayType = this.resourceTypeSpecific || this.resourceType; items.push(new FoldableItem(index++, 'Resource Type', beautifyResourceType(displayType), 'The type of the resource.')); } if (this.description) { items.push(new FoldableItem(index++, 'Description', this.description, 'The description or abstract of the resource.', undefined, undefined, false)); } this.subjects.forEach((subject) => { items.push(new FoldableItem(index++, 'Subject', subject, 'A subject area or keyword associated with the resource.')); }); return items; } toObject() { return { doi: JSON.stringify(this._doi.toObject()), rawMetadata: this._rawMetadata, }; } } //# sourceMappingURL=DataCiteInfo.js.map