UNPKG

@quicore/resource-id

Version:

Deterministic and compact ID generation for API resources using pluggable hashing.

42 lines (38 loc) 1.44 kB
import { ResourceIDGenerator } from "./ResourceIDGenerator"; /** * Generates compact, deterministic IDs directly from FHIR resource objects. */ export class FHIRIDGenerator extends ResourceIDGenerator { /** * @param {object} resource - A valid FHIR resource containing `id` and `resourceType`. */ constructor(resource) { super(); this.resource = resource; this.setComponent('resourceId', this.extractResourceId()); this.setComponent('resourceType', this.extractResourceType()); } /** * Extracts and validates the resource ID from the FHIR resource. * @returns {string} The extracted ID. * @throws {Error} If ID is missing or invalid. */ extractResourceId() { if (!this.resource || !this.resource.id) { throw new Error(`FHIR Resource Id is required and cannot be omitted.`); } return this.normalizeId(this.resource.id); } /** * Extracts and validates the resource type from the FHIR resource. * @returns {string} The resource type (e.g., "Observation"). * @throws {Error} If resourceType is missing. */ extractResourceType() { const resourceType = this.resource.resourceType; if (!resourceType) { throw new Error(`FHIR Resource Type is required and cannot be omitted.`); } return resourceType; } }