This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Development
bun install # Install dependencies
bun test # Run all tests
bun test --watch # Run tests in watch mode
bun test --coverage # Run tests with coverage
bun run typecheck # Type check the codebase
bun run lint # Lint and format code with Biome
# Building
bun run build # Build the project (creates dist/)
bun run cli # Run CLI in development mode
# CLI Usage
bun run cli typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson
bun run cli generate typescript -i schemas.ndjson -o ./typesThis is a FHIR code generation toolkit with a three-stage pipeline:
- Parser (
parser.ts): Reads TypeSchema documents from files - Generator (
generator.ts): Converts FHIR packages to TypeSchema format - Core processors (
core/): Handle FHIR schema transformationtransformer.ts: Main FHIR-to-TypeSchema conversionfield-builder.ts: Builds TypeSchema fields from FHIR elementsbinding.ts: Processes FHIR value bindings and enumsnested-types.ts: Handles nested type dependencies
- APIBuilder (
builder.ts): Fluent interface for chaining operations - Generators (
writer-generator/): Language-specific code generatorsintrospection.ts: Generates introspection data like TypeSchematypescript.ts: Generates TypeScript interfaces and typespython.ts: Generates Python/Pydantic modelscsharp.ts: Generates C# classes
- Commands (
commands/):typeschema: Generate and validate TypeSchema from FHIR packagesgenerate: Generate code from TypeSchema (TypeScript, Python, C#)
- Main entry (
index.ts): CLI setup with yargs
FHIR Package → TypeSchema Generator → TypeSchema Format → Code Generators → Output Files
- Main config:
atomic-codegen.config.ts(TypeScript configuration file) - Package config: Uses
Configtype fromsrc/config.ts - Default packages:
hl7.fhir.r4.core@4.0.1 - Output dir:
./generatedby default - Cache:
.typeschema-cache/for performance optimization
- TypeSchema types: Defined in
src/typeschema/types.ts - Tests: Located in
test/unit/with mirrors tosrc/structure - Generated code: Output goes to
generated/directory - Utilities: Common functions in
src/utils.tsandsrc/typeschema/utils.ts
- Uses strict TypeScript with latest ESNext features
- Module format: ESM with
"type": "module"in package.json - Build target: Node.js with Bun bundler
- Biome for linting/formatting (tabs, double quotes)
- Uses Bun's built-in test runner
- Unit tests for core functionality (transformers, builders)
- Tests mirror source structure in
test/unit/ - API tests for high-level generators
@atomic-ehr/fhir-canonical-manager: FHIR package management@atomic-ehr/fhirschema: FHIR schema definitionsyargs: CLI argument parsingajv: JSON schema validation
- Supports FHIR R4 packages (R5 in progress)
- Handles profiles and extensions (US Core in development)
- Caches parsed schemas for performance
- Multi-package dependency resolution via Canonical Manager
- Intermediate representation between FHIR and target languages
- Enables multi-language code generation
- Supports field validation and constraints
- Handles nested types and references
- Flattens FHIR's hierarchical structure for easier generation
- Modular generator system via APIBuilder
- Language-specific writers in
src/api/writer-generator/ - TypeScript generator creates interfaces with proper inheritance
- Extensible architecture for new languages
- Supports custom naming conventions and output formats
The APIBuilder class (src/api/builder.ts) provides the fluent API for the three-stage pipeline:
// Input stage - Choose one or combine:
.fromPackage("hl7.fhir.r4.core", "4.0.1") // NPM registry
.fromPackageRef("https://example.com/package.tgz") // Remote TGZ
.localStructureDefinitions({...}) // Local files
.fromSchemas(array) // TypeSchema objects
// Processing & introspection stage - Optional:
.typeSchema({ // IR transformations
treeShake: {...}, // Filter types
promoteLogical: {...}, // Promote logical models
})
.introspection({ // Debug output (optional)
typeSchemas: "./schemas", // Type Schemas path/.ndjson
typeTree: "./tree.json" // Type Tree
})
// Output stage - Choose one:
.typescript({...}) // TypeScript
.python({...}) // Python
.csharp("Namespace", "./path") // C#
// Finalize:
.outputTo("./output") // Output directory
.cleanOutput(true) // Clean before generation
.generate() // Execute- Universal intermediate format for FHIR data
- Defined in
src/typeschema/types.ts - Contains: identifier, description, fields, dependencies, base type
- Fields include type, required flag, array flag, binding info
- Supports enums for constrained value sets
Located in src/typeschema/core/:
transformer.ts: Main conversion logic from FHIR to TypeSchema- Handles different FHIR element types
- Processes inheritance and choice types
- Manages field flattening and snapshot generation
Located in src/api/writer-generator/:
- Base
Writerclass: Handles I/O, indentation, formatting - Language writers: TypeScript, Python, C#, Mustache
- Each writer traverses TypeSchema index and generates code
- Maintains language-specific idioms and conventions
- Extend the transformer in
src/typeschema/core/transformer.tsto produce TypeSchema data - Add logic to the language writer in
src/api/writer-generator/[language].ts - Add tests in
test/unit/typeschema/andtest/unit/api/ - Document in design docs if it's a major feature
- Use
builder.introspection({ typeSchemas: "./debug-schemas" })to inspect intermediate output - Check
src/typeschema/types.tsfor TypeSchema structure - Review
src/typeschema/core/transformer.tsfor transformation logic - Enable verbose logging with
builder.setLogLevel("DEBUG")
- Use
builder.build()instead ofgenerate()to avoid file I/O - Tests are organized by component in
test/unit/ - Run
bun test:coverageto see coverage metrics - Use
bun test --watchfor development
- Configured via
builder.typeSchema({ treeShake: {...} }) - Specify which resources and fields to include
- Automatically resolves dependencies
- Reference format:
"hl7.fhir.r4.core#4.0.1"
src/index.ts- Main entry point and exportssrc/config.ts- Configuration type definitionssrc/api/builder.ts- APIBuilder implementationsrc/typeschema/types.ts- TypeSchema type definitionssrc/typeschema/generator.ts- TypeSchema generation orchestration
src/api/writer-generator/introspection.ts- TypeSchema introspection generationsrc/api/writer-generator/typescript.ts- TypeScript code generationsrc/api/writer-generator/python.ts- Python/Pydantic generationsrc/api/writer-generator/csharp.ts- C# generationsrc/api/writer-generator/base.ts- Common writer utilities
src/typeschema/register.ts- Package registration and canonical resolutionsrc/typeschema/core/transformer.ts- FHIR → TypeSchema conversionsrc/typeschema/core/field-builder.ts- Field extraction logicsrc/typeschema/core/binding.ts- Value set and binding handling
test/unit/typeschema/- TypeSchema processor teststest/unit/api/- Generator and builder teststest/assets/- Test fixtures and sample data
- R5 Support: Limited, still in development
- Profile Extensions: Basic parsing only, US Core in progress
- Choice Types: Supported but representation differs by language
- Circular References: Handled but may affect tree shaking
- Large Packages: May require increased Node.js memory (
--max-old-space-size)
- Use tree shaking to reduce schema count
- Enable caching in APIBuilder
- Process large packages in batches
- Use
build()instead ofgenerate()for testing - Run
bun run qualitybefore committing (combines typecheck, lint, test:unit)
This toolkit focuses on type generation and code generation:
- Current: TypeScript, Python, C# interface/class generation from FHIR R4
- In Progress: R5 support, profile/extension enhancements
- Planned: Rust, GraphQL, OpenAPI, validation functions, mock data generation