UNPKG

@quicore/resource-id

Version:

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

47 lines (44 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FHIRIDGenerator = void 0; var _ResourceIDGenerator = require("./ResourceIDGenerator"); /** * Generates compact, deterministic IDs directly from FHIR resource objects. */ class FHIRIDGenerator extends _ResourceIDGenerator.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; } } exports.FHIRIDGenerator = FHIRIDGenerator;