babelfhir-ts 1.0.17 → 1.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -77
- package/bin/babelfhir-ts.js +2 -10
- package/out/fhir-r4.d.ts +676 -0
- package/out/generator/classGenerator.js +2 -2
- package/out/generator/fhir-r4.d.ts +676 -0
- package/out/generator/index.js +164 -12
- package/out/generator/interfaceGenerator.js +4 -32
- package/out/generator/sdParser.js +71 -5
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionAppointment.ts +94 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionAppointmentClass.ts +194 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionCheckIn.ts +29 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionCheckInClass.ts +194 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionCheckInNoCode.ts +29 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/AdmissionCheckInNoCodeClass.ts +194 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/RandomSupport.ts +26 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/valuesets/ValueSet-AdmissionFlowIds.ts +38 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/valuesets/ValueSet-AdmissionReasonCodeGroup.ts +42 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/generated/valuesets/index.ts +18 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/.index.db +0 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/.index.json +234 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/CodeSystem-admission-flow-ids.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/CodeSystem-admission-reason-codes.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/ImplementationGuide-pink.admission.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/SearchParameter-SearchParameter-Appointment-AdmissionCheckIn.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/SearchParameter-SearchParameter-Appointment-AdmissionCheckInNoCode.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/SearchParameter-SearchParameter-Appointment-AdmissionUrl.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/SearchParameter-SearchParameter-Appointment-AdmissionUuid.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionAppointment.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionCheckIn.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionCheckInNoCode.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionDataFlow.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionDevice.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionDocumentMetadata.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionFormData.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionFormUrl.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionFormUuid.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionIntegrationPoints.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionPatient.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionPatientData.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-AdmissionRoutingRule.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-LocationTomlConfiguration.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-Pink-Location-With-Toml.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/StructureDefinition-PinkLocation.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/ValueSet-AdmissionFlowIdsValueSet.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/ValueSet-AdmissionReasonCodeGroup.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/AllergyIntolerance-Example-PenicillinAllergy.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/Appointment-Example-AdmissionAppointment.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/Condition-Example-DiabetesCondition.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/Device-Example-Surgery-Room.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/DocumentReference-Example-PreOperativeEvaluation.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/Location-Example-Location-With-Toml.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/Patient-Example-Patient.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/example/QuestionnaireResponse-Example-AdmissionFormResponse.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/other/spec.internals +313 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/other/validation-oo.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/other/validation-summary.json +1 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/package.json +23 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionAppointment.sch +22 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionCheckIn.sch +18 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionCheckInNoCode.sch +18 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionDevice.sch +12 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionFormUrl.sch +18 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionFormUuid.sch +18 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-AdmissionPatient.sch +12 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-Pink-Location-With-Toml.sch +26 -0
- package/out/generator/temp/1761563744855-kptdikzwryj/package/xml/StructureDefinition-PinkLocation.sch +12 -0
- package/out/main.js +11 -8
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -2,23 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
**BabelFHIR-TS** transforms FHIR® StructureDefinitions into production-ready TypeScript code with full type safety and built-in validation. Unlike generic FHIR type definitions, BabelFHIR-TS generates **profile-aware** interfaces that understand your Implementation Guide's constraints, extensions, and slicing rules.
|
|
4
4
|
|
|
5
|
-
## Why BabelFHIR-TS?
|
|
6
|
-
|
|
7
|
-
**The FHIR Challenge**: Implementation Guides define strict profiles that constrain base FHIR resources with required or must-support elements, custom extensions, value set bindings, and cardinality rules. Existing TypeScript libraries can't capture these requirements when you need profile-specific types, leading to an overhead when using TypeScript to build apps that interact with FHIR servers.
|
|
8
|
-
|
|
9
|
-
**The BabelFHIR-TS Solution**: Automatically generates TypeScript interfaces and validation logic directly from StructureDefinition JSON. Your IDE autocompletes required fields, flags missing extensions at compile-time, and validates FHIRPath invariants at runtime.
|
|
10
|
-
|
|
11
5
|
### What you get
|
|
12
6
|
|
|
13
|
-
- **Strongly typed interfaces** that merge profile constraints with base FHIR types (
|
|
7
|
+
- **Strongly typed interfaces** that merge profile constraints with base FHIR types (types come from `@types/fhir`)
|
|
8
|
+
- **Compiled output by default** — packages ship JavaScript (`.js`) plus TypeScript declarations (`.d.ts`)
|
|
14
9
|
- **Runtime validation** using FHIRPath expressions from the profile—no external validator required for basic checks
|
|
15
10
|
- **Type-safe extension handling** with proper slicing and nested extension support
|
|
16
11
|
- **Random data builders** for testing and development (when class generation is enabled)
|
|
17
12
|
- **Zero manual mapping**—consume any FHIR package or Implementation Guide directly from registries
|
|
18
|
-
- **Fast and lightweight**—
|
|
19
|
-
- **Install any FHIR profile as a node module**—use `babelfhir-ts install` to add Implementation Guides directly to your
|
|
20
|
-
|
|
21
|
-
Use it to build FHIR-compliant APIs, validate incoming resources against profiles, or generate type-safe client SDKs from Implementation Guides.
|
|
13
|
+
- **Fast and lightweight**—minimal runtime deps; only `fhirpath` is required for validators
|
|
14
|
+
- **Install any FHIR profile as a node module**—use `babelfhir-ts install` to add Implementation Guides directly to your project
|
|
22
15
|
|
|
23
16
|
## Installation
|
|
24
17
|
|
|
@@ -71,6 +64,42 @@ const patient = USCorePatientClass.random();
|
|
|
71
64
|
const { errors, warnings } = await patient.validate();
|
|
72
65
|
```
|
|
73
66
|
|
|
67
|
+
## Using the generated code in your project
|
|
68
|
+
|
|
69
|
+
Generated profile packages installed via `babelfhir-ts install` are published as **compiled JavaScript with TypeScript declarations**:
|
|
70
|
+
|
|
71
|
+
- JavaScript for runtime: `index.js`, `*.js`
|
|
72
|
+
- Type declarations for IDE/TS: `index.d.ts`, `*.d.ts`
|
|
73
|
+
- Dependencies:
|
|
74
|
+
- `@types/fhir` is included as a dependency of the generated package (no extra setup in your app)
|
|
75
|
+
- `fhirpath` is a peer dependency (required only if you use the generated validators/classes)
|
|
76
|
+
|
|
77
|
+
Install `fhirpath` in your app if you plan to call `.validate()` or use the generated classes.
|
|
78
|
+
|
|
79
|
+
### Module resolution
|
|
80
|
+
|
|
81
|
+
Generated packages use **`moduleResolution: "node16"`** in their `tsconfig.json` to ensure proper ESM compatibility. This works with all modern TypeScript setups including:
|
|
82
|
+
|
|
83
|
+
- **Node.js 18+** with `"type": "module"` in `package.json`
|
|
84
|
+
- **Bundlers** (Vite, esbuild, Webpack) with ESM output
|
|
85
|
+
- **TypeScript 5.0+** with `node16`, `nodenext`, or `bundler` module resolution
|
|
86
|
+
|
|
87
|
+
#### FHIR type imports
|
|
88
|
+
|
|
89
|
+
Generated code imports base FHIR types from `"fhir/r4"`:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { Appointment, Extension, CodeableConcept } from "fhir/r4";
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Since `@types/fhir` doesn't provide a `package.json` `exports` field, BabelFHIR-TS includes an **ambient module declaration** (`fhir-r4.d.ts`) in every generated package. This declaration file:
|
|
96
|
+
|
|
97
|
+
- Maps `"fhir/r4"` imports to the actual `@types/fhir` type definitions
|
|
98
|
+
- Enables strict `node16`/`nodenext` module resolution without path configuration
|
|
99
|
+
- Is automatically picked up by TypeScript—no `tsconfig.json` changes needed in your project
|
|
100
|
+
|
|
101
|
+
> **Note**: If you're using `moduleResolution: "bundler"` in your own project, the ambient module declaration is still compatible and won't cause any issues.
|
|
102
|
+
|
|
74
103
|
## CLI reference
|
|
75
104
|
|
|
76
105
|
```
|
|
@@ -94,72 +123,7 @@ babelfhir-ts [options] [<input> [output]]
|
|
|
94
123
|
|
|
95
124
|
- **Directory** – scan all `.tgz`, `.zip`, or `.json` files inside the folder
|
|
96
125
|
- **Archive** – process a FHIR NPM package in `.tgz` or `.zip` format
|
|
97
|
-
- **StructureDefinition JSON** – generate code for a single profile
|
|
98
|
-
|
|
99
|
-
## Generated output
|
|
100
|
-
|
|
101
|
-
For each StructureDefinition the generator produces:
|
|
102
|
-
|
|
103
|
-
- `*.ts` interface definitions (extending the canonical FHIR types)
|
|
104
|
-
- `*Class.ts` companions that offer:
|
|
105
|
-
- constructor helpers (`empty`, `random`, `randomClass`)
|
|
106
|
-
- deterministic field filling for required and must-support elements
|
|
107
|
-
- `validate()` that evaluates FHIR invariant expressions via `fhirpath`
|
|
108
|
-
- optional validation parity artefacts when you run the test suite (`npm test validatorParity`)
|
|
109
|
-
|
|
110
|
-
The validator relies on `fhirpath.evaluate(...)` to enforce profile invariants (including min cardinalities, slices, and custom expressions) without requiring a full FHIR server. It intentionally performs **high-level** checks only: terminology expansion, reference resolution, and other server-backed logic are out of scope, so keep a downstream validator (e.g. the HL7 Java validator CLI) in your QA pipeline for full conformance.
|
|
111
|
-
|
|
112
|
-
### Example: Generated US Core Encounter Profile
|
|
113
|
-
|
|
114
|
-
Given the US Core Encounter profile, BabelFHIR-TS generates TypeScript interfaces that capture all must-support elements and profile extensions:
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
import { Encounter, Meta, Extension, Identifier, Coding, CodeableConcept,
|
|
118
|
-
Reference, EncounterParticipant, Period, EncounterHospitalization,
|
|
119
|
-
EncounterLocation } from "fhir/r4";
|
|
120
|
-
|
|
121
|
-
// Extension interface with type-safe URL
|
|
122
|
-
export interface UsCoreInterpreterNeeded extends Extension {
|
|
123
|
-
url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-interpreter-needed'
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Profile interface extending base Encounter with must-support constraints
|
|
127
|
-
export interface USCoreEncounterProfile extends Encounter {
|
|
128
|
-
/** Must Support */
|
|
129
|
-
meta?: Meta;
|
|
130
|
-
/** Must Support */
|
|
131
|
-
identifier?: Identifier[];
|
|
132
|
-
/** Must Support */
|
|
133
|
-
class: Coding;
|
|
134
|
-
/** Must Support */
|
|
135
|
-
type: CodeableConcept[];
|
|
136
|
-
/** Must Support */
|
|
137
|
-
subject: Reference;
|
|
138
|
-
/** Must Support */
|
|
139
|
-
participant?: EncounterParticipant[];
|
|
140
|
-
/** Must Support */
|
|
141
|
-
period?: Period;
|
|
142
|
-
/** Must Support */
|
|
143
|
-
reasonCode?: CodeableConcept[];
|
|
144
|
-
/** Must Support */
|
|
145
|
-
reasonReference?: Reference[];
|
|
146
|
-
/** Must Support */
|
|
147
|
-
hospitalization?: EncounterHospitalization;
|
|
148
|
-
/** Must Support */
|
|
149
|
-
location?: EncounterLocation[];
|
|
150
|
-
/** Must Support */
|
|
151
|
-
serviceProvider?: Reference;
|
|
152
|
-
|
|
153
|
-
// Typed extension slicing
|
|
154
|
-
extension?: (Extension | UsCoreInterpreterNeeded)[];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Generated validator function
|
|
158
|
-
export async function validateUSCoreEncounterProfile(
|
|
159
|
-
resource: USCoreEncounterProfile
|
|
160
|
-
): Promise<{ errors: string[], warnings: string[] }>;
|
|
161
|
-
|
|
162
|
-
```
|
|
126
|
+
- **StructureDefinition JSON** – generate code for a single profile definitio
|
|
163
127
|
|
|
164
128
|
## Scripts for contributors
|
|
165
129
|
|
|
@@ -174,6 +138,12 @@ export async function validateUSCoreEncounterProfile(
|
|
|
174
138
|
|
|
175
139
|
The generator caches downloaded StructureDefinitions and packages inside `.cache/`. When you need a clean run, pass `--no-cache` or manually remove the folder. Temporary downloads land in `.temp-*` directories and are cleaned up automatically.
|
|
176
140
|
|
|
141
|
+
## Why BabelFHIR-TS?
|
|
142
|
+
|
|
143
|
+
**The FHIR Challenge**: Implementation Guides define strict profiles that constrain base FHIR resources with required or must-support elements, custom extensions, value set bindings, and cardinality rules. Existing TypeScript libraries can't capture these requirements when you need profile-specific types, leading to an overhead when using TypeScript to build apps that interact with FHIR servers.
|
|
144
|
+
|
|
145
|
+
**The BabelFHIR-TS Solution**: Automatically generates TypeScript interfaces and validation logic directly from StructureDefinition JSON. Your IDE autocompletes required fields, flags missing extensions at compile-time, and validates FHIRPath invariants at runtime.
|
|
146
|
+
|
|
177
147
|
## Limitations
|
|
178
148
|
|
|
179
149
|
BabelFHIR-TS is a code generation tool that parses FHIR StructureDefinitions and produces TypeScript interfaces and validators. While it handles many common FHIR profiling patterns, there are important limitations to be aware of:
|
|
@@ -207,6 +177,7 @@ BabelFHIR-TS is a code generation tool that parses FHIR StructureDefinitions and
|
|
|
207
177
|
### Reporting Issues
|
|
208
178
|
|
|
209
179
|
If you encounter an Implementation Guide that doesn't generate correctly, please [open an issue](https://github.com/quotentiroler/BabelFHIR-ts/issues) with:
|
|
180
|
+
|
|
210
181
|
- The package name and version
|
|
211
182
|
- The specific StructureDefinition URL
|
|
212
183
|
- Expected vs. actual generated output
|
package/bin/babelfhir-ts.js
CHANGED
|
@@ -9,24 +9,16 @@ import fs from 'fs';
|
|
|
9
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
10
10
|
const __dirname = dirname(__filename);
|
|
11
11
|
|
|
12
|
-
// Use
|
|
13
|
-
const tsxBin = process.platform === 'win32' ? 'tsx.cmd' : 'tsx';
|
|
14
|
-
const tsxPath = join(__dirname, '..', 'node_modules', '.bin', tsxBin);
|
|
12
|
+
// Use node directly to run the compiled JavaScript
|
|
15
13
|
const mainJsPath = join(__dirname, '..', 'out', 'main.js');
|
|
16
14
|
|
|
17
|
-
// Check if tsx exists
|
|
18
|
-
if (!fs.existsSync(tsxPath)) {
|
|
19
|
-
console.error('tsx not found. Please install dependencies with: npm install');
|
|
20
|
-
process.exit(1);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
15
|
// Check if the compiled file exists
|
|
24
16
|
if (!fs.existsSync(mainJsPath)) {
|
|
25
17
|
console.error('Compiled main.js not found. Please ensure the package was built correctly.');
|
|
26
18
|
process.exit(1);
|
|
27
19
|
}
|
|
28
20
|
|
|
29
|
-
const child = spawn(
|
|
21
|
+
const child = spawn('node', [mainJsPath, ...process.argv.slice(2)], {
|
|
30
22
|
stdio: 'inherit',
|
|
31
23
|
cwd: process.cwd(), // Use the current working directory where the command was invoked
|
|
32
24
|
shell: process.platform === 'win32'
|