UNPKG

fhir-snapshot-generator

Version:
118 lines (112 loc) 4.66 kB
import { FhirPackageExplorer } from 'fhir-package-explorer'; import { FhirVersion, Logger, ElementDefinition, FhirPackageIdentifier, FileIndexEntryWithPkg } from '@outburn/types'; /** * © Copyright Outburn Ltd. 2022-2025 All Rights Reserved * Project name: fhir-snapshot-generator */ type Prethrower = (msg: Error | any) => Error; interface FhirTreeNode { id: string; path: string; definition?: ElementDefinition; children: FhirTreeNode[]; idSegments: string[]; pathSegments: string[]; nodeType: 'element' | 'array' | 'poly' | 'slice' | 'resliced' | 'headslice'; sliceName?: string; } /** * Snapshot caching strategy. * * - `'lazy'`: Default. Generate each snapshot on demand and cache it afterward. * - `'ensure'`: Proactively generate and cache all **missing** snapshots. * - `'rebuild'`: Regenerate **all** snapshots and overwrite existing cache entries. * - `'none'`: Fully bypass the cache. Always regenerate snapshots and do not write to cache. */ type SnapshotCacheMode = 'lazy' | 'ensure' | 'rebuild' | 'none'; type SnapshotGeneratorConfig = { /** * The FhirPackageExplorer instance to use for resolving FHIR resources. * This allows sharing a single FPE instance across multiple modules (e.g., FSG and FTR). */ fpe: FhirPackageExplorer; /** * The FHIR version to use for the snapshot generation. * This is used to determine the FHIR core package to use when fetching base FHIR types. */ fhirVersion: FhirVersion; /** * Determines how snapshot caching is handled. * Defaults to `'lazy'` if not specified. */ cacheMode?: SnapshotCacheMode; /** * Optional logger instance for custom logging. */ logger?: Logger; }; type SnapshotFetcher = (url: string) => Promise<ElementDefinition[]>; /** * © Copyright Outburn Ltd. 2022-2025 All Rights Reserved * Project name: fhir-snapshot-generator */ declare class FhirSnapshotGenerator { private fpe; private logger; private prethrow; private cachePath; private cacheMode; private fhirVersion; private fhirCorePackage; private resolvedBasePackages; private constructor(); /** * Creates a new instance of the FhirSnapshotGenerator class. * @param config - the configuration object including the FhirPackageExplorer instance * @returns - a promise that resolves to a new instance of the FhirSnapshotGenerator class */ static create(config: SnapshotGeneratorConfig): Promise<FhirSnapshotGenerator>; getLogger(): Logger; getCachePath(): string; getCacheMode(): SnapshotCacheMode; getFhirVersion(): FhirVersion; getFpe(): FhirPackageExplorer; /** * Get the core FHIR package for a specific FHIR package. * Will try to resolve the core package based on the direct dependencies of the source package or its fhirVersions array. * Defaults to the FHIR package of this instance's fhirVersion if no base package can be determined. * @param sourcePackage The source package identifier (e.g., { id: 'hl7.fhir.us.core', version: '6.1.0' }). * @returns The core FHIR package identifier (e.g., { id: 'hl7.fhir.r4.core', version: '4.0.1' }). */ private getCorePackage; /** * Get an original StructureDefinition from a specific package by filename. * After resolving the metadata using any other identifier (id, url, name), filename (combind with the package id and version) * is the most reliable way to get to a specific StructureDefinition, and is also used as the basis for the cache. */ private getStructureDefinitionByFileName; private getCacheFilePath; /** * Try to get an existing cached StructureDefinition snapshot. If not found, return undefined. */ private getSnapshotFromCache; private saveSnapshotToCache; /** * Fetch StructureDefinition metadata by any identifier (id, url, name) - FSH style. */ getMetadata(identifier: string, packageFilter?: FhirPackageIdentifier): Promise<FileIndexEntryWithPkg>; /** * Generate a snapshot for a StructureDefinition. */ private generate; /** * Get snapshot by metadata. */ private getSnapshotByMeta; private ensureSnapshotCached; /** * Get snapshot by any FSH style identifier (id, url or name), or by a metadata object. */ getSnapshot(identifier: string | FileIndexEntryWithPkg, packageFilter?: FhirPackageIdentifier): Promise<any>; } export { FhirSnapshotGenerator, type FhirTreeNode, type Prethrower, type SnapshotCacheMode, type SnapshotFetcher, type SnapshotGeneratorConfig };