@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.
92 lines (91 loc) • 3.27 kB
JavaScript
/*!
*
* 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 { PIDRecord } from "./PIDRecord";
import { PIDDataType } from "./PIDDataType";
import { handleMap, unresolvables } from "../../utils/utils";
import { cachedFetch } from "../../utils/DataCache";
export class PID {
constructor(prefix, suffix) {
this._prefix = prefix;
this._suffix = suffix;
}
get prefix() {
return this._prefix;
}
get suffix() {
return this._suffix;
}
static isPID(text) {
return new RegExp('^([0-9A-Za-z])+.([0-9A-Za-z])+/([!-~])+$').test(text);
}
static getPIDFromString(pid) {
if (!PID.isPID(pid))
throw new Error('Invalid input');
const firstSlashIndex = pid.indexOf('/');
return new PID(pid.substring(0, firstSlashIndex), pid.substring(firstSlashIndex + 1));
}
static fromJSON(serialized) {
const data = JSON.parse(serialized);
return new PID(data.prefix, data.suffix);
}
toString() {
return `${this.prefix}/${this.suffix}`;
}
isResolvable() {
return !unresolvables.has(this) && !this.prefix.toUpperCase().match('^(0$|0\\.|HS_|10320$)');
}
async resolve() {
if (unresolvables.has(this))
return undefined;
else if (handleMap.has(this))
return handleMap.get(this);
else {
const rawJson = (await cachedFetch(`https://hdl.handle.net/api/handles/${this.prefix}/${this.suffix}#resolve`));
console.log(rawJson);
const valuePromises = rawJson.values.map(async (value) => {
const type = (async () => {
if (PID.isPID(value.type)) {
const pid = PID.getPIDFromString(value.type);
const dataType = await PIDDataType.resolveDataType(pid);
return dataType instanceof PIDDataType ? dataType : pid;
}
return value.type;
})();
return {
index: value.index,
type: await type,
data: value.data,
ttl: value.ttl,
timestamp: Date.parse(value.timestamp),
};
});
const values = await Promise.all(valuePromises);
const record = new PIDRecord(this, values);
handleMap.set(this, record);
return record;
}
}
toObject() {
return {
prefix: this.prefix,
suffix: this.suffix,
};
}
}
export const locationType = new PID('10320', 'loc');
//# sourceMappingURL=PID.js.map