UNPKG

@quicore/resource-id

Version:

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

75 lines (68 loc) 2.31 kB
import { IDGenerator } from "./IdGenerator"; /** * Generates deterministic, scoped IDs for FHIR-like resources using integration context. * Example format: `${integrationId}::${resourceType}::${resourceId}` */ export class ResourceIDGenerator extends IDGenerator { constructor() { super(); } /** * Sets the integration ID. * @param {string} integrationId - Integration ID for scoping. * @returns {this} */ setIntegrationId(integrationId) { if (!integrationId) { throw new Error(`Integration ID is required and cannot be omitted.`); } this.setComponent('integrationId', integrationId); return this; } /** * Sets the resource ID. * @param {string} resourceId - Unique identifier of the resource. * @returns {this} */ setResourceId(resourceId) { if (!resourceId) { throw new Error(`Resource ID is required and cannot be omitted.`); } this.setComponent('resourceId', resourceId); return this; } /** * Sets the resource type. * @param {string} resourceType - Type of the FHIR resource (e.g., "Patient"). * @returns {this} */ setResourceType(resourceType) { if (!resourceType) { throw new Error(`Resource Type is required and cannot be omitted.`); } this.setComponent('resourceType', resourceType); return this; } /** * Combines components in a canonical string format for hashing. * @returns {string} Canonical string. */ createCanonicalString() { return `${this.components.integrationId}::${this.components.resourceType}::${this.components.resourceId}`; } /** * Ensures all required components are defined. * @throws {Error} If any component is missing. */ validateComponents() { if (!this.components.resourceId) { throw new Error('Resource ID is required'); } if (!this.components.resourceType) { throw new Error('Resource Type is required'); } if (!this.components.integrationId) { throw new Error('Integration ID is required'); } } }