@atomic-ehr/codegen 0.0.1-canary.20250923152114.2852906 → 0.0.1-canary.20250924082247.588e04a
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.
|
@@ -15,8 +15,4 @@ export declare function transformElements(fhirSchema: FHIRSchema, parentPath: st
|
|
|
15
15
|
* Transform a single FHIRSchema to TypeSchema(s) with enhanced categorization
|
|
16
16
|
* Returns the main schema plus any binding schemas
|
|
17
17
|
*/
|
|
18
|
-
export declare function transformFHIRSchema(
|
|
19
|
-
/**
|
|
20
|
-
* Transform multiple FHIRSchemas
|
|
21
|
-
*/
|
|
22
|
-
export declare function transformFHIRSchemas(fhirSchemas: FHIRSchema[], manager: ReturnType<typeof CanonicalManager>, packageInfo?: PackageInfo): Promise<TypeSchema[]>;
|
|
18
|
+
export declare function transformFHIRSchema(manager: ReturnType<typeof CanonicalManager>, fhirSchema: FHIRSchema, packageInfo: PackageInfo): Promise<TypeSchema[]>;
|
|
@@ -191,7 +191,7 @@ async function transformExtension(fhirSchema, manager, packageInfo) {
|
|
|
191
191
|
* Transform a single FHIRSchema to TypeSchema(s) with enhanced categorization
|
|
192
192
|
* Returns the main schema plus any binding schemas
|
|
193
193
|
*/
|
|
194
|
-
export async function transformFHIRSchema(
|
|
194
|
+
export async function transformFHIRSchema(manager, fhirSchema, packageInfo) {
|
|
195
195
|
const results = [];
|
|
196
196
|
// Extract package info from schema if not provided
|
|
197
197
|
if (!packageInfo && (fhirSchema.package_name || fhirSchema.package_id)) {
|
|
@@ -332,14 +332,3 @@ export async function transformFHIRSchema(fhirSchema, manager, packageInfo) {
|
|
|
332
332
|
results.push(...bindingSchemas);
|
|
333
333
|
return results;
|
|
334
334
|
}
|
|
335
|
-
/**
|
|
336
|
-
* Transform multiple FHIRSchemas
|
|
337
|
-
*/
|
|
338
|
-
export async function transformFHIRSchemas(fhirSchemas, manager, packageInfo) {
|
|
339
|
-
const allResults = [];
|
|
340
|
-
for (const fhirSchema of fhirSchemas) {
|
|
341
|
-
const results = await transformFHIRSchema(fhirSchema, manager, packageInfo);
|
|
342
|
-
allResults.push(...results);
|
|
343
|
-
}
|
|
344
|
-
return allResults;
|
|
345
|
-
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Generates TypeSchema documents from FHIR packages using fhrischema.
|
|
5
5
|
* Provides high-level API for converting FHIR Structure Definitions to TypeSchema format.
|
|
6
6
|
*/
|
|
7
|
-
import { type FHIRSchema } from "@atomic-ehr/fhirschema";
|
|
7
|
+
import { type FHIRSchema, type StructureDefinition } from "@atomic-ehr/fhirschema";
|
|
8
8
|
import type { TypeSchemaConfig } from "../config.js";
|
|
9
9
|
import type { TypeSchema } from "./type-schema.types.js";
|
|
10
10
|
import type { PackageInfo, TypeschemaGeneratorOptions } from "./types.js";
|
|
@@ -22,17 +22,14 @@ export declare class TypeSchemaGenerator {
|
|
|
22
22
|
private logger;
|
|
23
23
|
constructor(options?: TypeschemaGeneratorOptions, cacheConfig?: TypeSchemaConfig);
|
|
24
24
|
private initializeCache;
|
|
25
|
+
fetchPackage(packageName: string, packageVersion?: string): Promise<{
|
|
26
|
+
structureDefinitions: StructureDefinition[];
|
|
27
|
+
valueSets: any[];
|
|
28
|
+
}>;
|
|
29
|
+
generateFhirSchemas(structureDefinitions: StructureDefinition[]): FHIRSchema[];
|
|
30
|
+
generateValueSetSchemas(valueSets: any[], packageInfo: PackageInfo): Promise<TypeSchema[]>;
|
|
25
31
|
generateFromPackage(packageName: string, packageVersion?: string): Promise<TypeSchema[]>;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Generate TypeSchema from multiple FHIR schemas with FHIR-specific enhancements
|
|
29
|
-
*/
|
|
30
|
-
generateFromSchemas(fhirSchemas: FHIRSchema[], packageInfo?: PackageInfo): Promise<TypeSchema[]>;
|
|
31
|
-
private groupTypeSchemas;
|
|
32
|
-
private enhanceProfiles;
|
|
33
|
-
private enhanceExtensions;
|
|
34
|
-
private enhanceValueSets;
|
|
35
|
-
private enhanceCodeSystems;
|
|
32
|
+
generateFromSchemas(packageInfo: PackageInfo, fhirSchemas: FHIRSchema[]): Promise<TypeSchema[]>;
|
|
36
33
|
/**
|
|
37
34
|
* Apply treeshaking to StructureDefinitions before FHIR schema transformation
|
|
38
35
|
* This is more efficient and includes smart reference handling
|
|
@@ -48,15 +45,3 @@ export declare class TypeSchemaGenerator {
|
|
|
48
45
|
*/
|
|
49
46
|
private extractResourceNameFromUrl;
|
|
50
47
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Convenience function to generate TypeSchema from a package
|
|
53
|
-
*/
|
|
54
|
-
export declare function generateTypeSchemaFromPackage(packageName: string, options?: TypeschemaGeneratorOptions): Promise<TypeSchema[]>;
|
|
55
|
-
/**
|
|
56
|
-
* Convenience function to generate TypeSchema from FHIR schemas
|
|
57
|
-
*/
|
|
58
|
-
export declare function generateTypeSchemaFromSchemas(fhirSchemas: FHIRSchema[], packageInfo?: PackageInfo, options?: TypeschemaGeneratorOptions): Promise<TypeSchema[]>;
|
|
59
|
-
/**
|
|
60
|
-
* Convenience function to generate TypeSchema from a single FHIR schema
|
|
61
|
-
*/
|
|
62
|
-
export declare function generateTypeSchemaFromSchema(fhirSchema: FHIRSchema, packageInfo?: PackageInfo, options?: TypeschemaGeneratorOptions): Promise<TypeSchema[]>;
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* Provides high-level API for converting FHIR Structure Definitions to TypeSchema format.
|
|
6
6
|
*/
|
|
7
7
|
import { CanonicalManager } from "@atomic-ehr/fhir-canonical-manager";
|
|
8
|
-
import
|
|
8
|
+
import * as fhirschema from "@atomic-ehr/fhirschema";
|
|
9
9
|
import { createLogger } from "../utils/codegen-logger.js";
|
|
10
10
|
import { TypeSchemaCache } from "./cache.js";
|
|
11
|
-
import { transformFHIRSchema
|
|
11
|
+
import { transformFHIRSchema } from "./core/transformer.js";
|
|
12
12
|
import { transformValueSet } from "./value-set/processor.js";
|
|
13
13
|
/**
|
|
14
14
|
* TypeSchema Generator class
|
|
@@ -39,16 +39,7 @@ export class TypeSchemaGenerator {
|
|
|
39
39
|
await this.cache.initialize();
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
async
|
|
43
|
-
await this.initializeCache();
|
|
44
|
-
const forceRegenerate = this.cacheConfig?.forceRegenerate ?? false;
|
|
45
|
-
if (this.cache && !forceRegenerate) {
|
|
46
|
-
const cachedSchemas = this.cache.getByPackage(packageName);
|
|
47
|
-
if (cachedSchemas.length > 0) {
|
|
48
|
-
this.logger.info(`Using cached TypeSchemas for package: ${packageName} (${cachedSchemas.length} schemas)`);
|
|
49
|
-
return cachedSchemas;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
42
|
+
async fetchPackage(packageName, packageVersion) {
|
|
52
43
|
this.logger.step(`Loading FHIR package: ${packageName}${packageVersion ? `@${packageVersion}` : ""}`);
|
|
53
44
|
this.manager = CanonicalManager({
|
|
54
45
|
packages: [`${packageName}${packageVersion ? `@${packageVersion}` : ""}`],
|
|
@@ -59,14 +50,18 @@ export class TypeSchemaGenerator {
|
|
|
59
50
|
const structureDefinitions = allResources.filter((resource) => resource.resourceType === "StructureDefinition");
|
|
60
51
|
const valueSets = allResources.filter((resource) => resource.resourceType === "ValueSet");
|
|
61
52
|
this.logger.info(`Found ${structureDefinitions.length} StructureDefinitions and ${valueSets.length} ValueSets in package`);
|
|
53
|
+
return { structureDefinitions, valueSets };
|
|
54
|
+
}
|
|
55
|
+
generateFhirSchemas(structureDefinitions) {
|
|
62
56
|
this.logger.progress(`Converting ${structureDefinitions.length} StructureDefinitions to FHIRSchemas`);
|
|
57
|
+
// TODO: do it on the TypeSchema
|
|
63
58
|
const filteredStructureDefinitions = this.applyStructureDefinitionTreeshaking(structureDefinitions);
|
|
64
59
|
const fhirSchemas = [];
|
|
65
60
|
let convertedCount = 0;
|
|
66
61
|
let failedCount = 0;
|
|
67
62
|
for (const sd of filteredStructureDefinitions) {
|
|
68
63
|
try {
|
|
69
|
-
const fhirSchema = translate(sd);
|
|
64
|
+
const fhirSchema = fhirschema.translate(sd);
|
|
70
65
|
fhirSchemas.push(fhirSchema);
|
|
71
66
|
convertedCount++;
|
|
72
67
|
this.logger.debug(`Converted StructureDefinition: ${sd.name || sd.id} (${sd.resourceType})`);
|
|
@@ -76,14 +71,13 @@ export class TypeSchemaGenerator {
|
|
|
76
71
|
this.logger.warn(`Failed to convert StructureDefinition ${sd.name || sd.id}: ${error instanceof Error ? error.message : String(error)}`);
|
|
77
72
|
}
|
|
78
73
|
}
|
|
79
|
-
this.logger.success(`Schema conversion completed: ${convertedCount}/${filteredStructureDefinitions.length} successful, ${failedCount} failed`);
|
|
74
|
+
this.logger.success(`FHIR Schema conversion completed: ${convertedCount}/${filteredStructureDefinitions.length} successful, ${failedCount} failed`);
|
|
75
|
+
return fhirSchemas;
|
|
76
|
+
}
|
|
77
|
+
async generateValueSetSchemas(valueSets, packageInfo) {
|
|
80
78
|
if (valueSets.length > 0) {
|
|
81
79
|
this.logger.debug(`${valueSets.length} ValueSets available for enum extraction`);
|
|
82
80
|
}
|
|
83
|
-
const packageInfo = {
|
|
84
|
-
name: packageName,
|
|
85
|
-
version: packageVersion || "latest",
|
|
86
|
-
};
|
|
87
81
|
// Process ValueSets separately to add to TypeSchema output
|
|
88
82
|
const valueSetSchemas = [];
|
|
89
83
|
if (valueSets.length > 0) {
|
|
@@ -106,111 +100,40 @@ export class TypeSchemaGenerator {
|
|
|
106
100
|
}
|
|
107
101
|
this.logger.success(`ValueSet conversion completed: ${valueSetConvertedCount}/${valueSets.length} successful, ${valueSetFailedCount} failed`);
|
|
108
102
|
}
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
return valueSetSchemas;
|
|
104
|
+
}
|
|
105
|
+
async generateFromPackage(packageName, packageVersion) {
|
|
106
|
+
await this.initializeCache();
|
|
107
|
+
if (this.cache && !(this.cacheConfig?.forceRegenerate ?? false)) {
|
|
108
|
+
const cachedSchemas = this.cache.getByPackage(packageName);
|
|
109
|
+
if (cachedSchemas.length > 0) {
|
|
110
|
+
this.logger.info(`Using cached TypeSchemas for package: ${packageName} (${cachedSchemas.length} schemas)`);
|
|
111
|
+
return cachedSchemas;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const packageInfo = {
|
|
115
|
+
name: packageName,
|
|
116
|
+
version: packageVersion || "latest",
|
|
117
|
+
};
|
|
118
|
+
const { valueSets, structureDefinitions } = await this.fetchPackage(packageName, packageVersion);
|
|
119
|
+
const fhirSchemas = this.generateFhirSchemas(structureDefinitions);
|
|
120
|
+
const valueSetSchemas = await this.generateValueSetSchemas(valueSets, packageInfo);
|
|
121
|
+
const schemas = await this.generateFromSchemas(packageInfo, fhirSchemas);
|
|
111
122
|
const allSchemas = [...schemas, ...valueSetSchemas];
|
|
112
|
-
if (this.cache
|
|
113
|
-
this.logger.info(`Caching ${allSchemas.length} generated schemas for package: ${packageName}`);
|
|
123
|
+
if (this.cache) {
|
|
114
124
|
for (const schema of allSchemas) {
|
|
115
125
|
await this.cache.set(schema);
|
|
116
126
|
}
|
|
117
|
-
this.logger.success(`Cached ${allSchemas.length} TypeSchemas for package: ${packageName}`);
|
|
118
127
|
}
|
|
119
128
|
return allSchemas;
|
|
120
129
|
}
|
|
121
|
-
async
|
|
122
|
-
this.logger.info(
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
* Generate TypeSchema from multiple FHIR schemas with FHIR-specific enhancements
|
|
127
|
-
*/
|
|
128
|
-
async generateFromSchemas(fhirSchemas, packageInfo) {
|
|
129
|
-
this.logger.info(`Transforming ${fhirSchemas.length} FHIR schemas to TypeSchema`);
|
|
130
|
-
const baseSchemas = await transformFHIRSchemas(fhirSchemas, this.manager, packageInfo);
|
|
131
|
-
const results = [];
|
|
132
|
-
const groupedSchemas = this.groupTypeSchemas(baseSchemas);
|
|
133
|
-
results.push(...groupedSchemas.resources);
|
|
134
|
-
results.push(...groupedSchemas.complexTypes);
|
|
135
|
-
results.push(...groupedSchemas.primitives);
|
|
136
|
-
if (groupedSchemas.profiles.length > 0) {
|
|
137
|
-
this.logger.info(`Enhancing ${groupedSchemas.profiles.length} profiles`);
|
|
138
|
-
const profileResults = await this.enhanceProfiles(groupedSchemas.profiles);
|
|
139
|
-
results.push(...profileResults);
|
|
140
|
-
}
|
|
141
|
-
if (groupedSchemas.extensions.length > 0) {
|
|
142
|
-
this.logger.info(`Enhancing ${groupedSchemas.extensions.length} extensions`);
|
|
143
|
-
const extensionResults = await this.enhanceExtensions(groupedSchemas.extensions);
|
|
144
|
-
results.push(...extensionResults);
|
|
145
|
-
}
|
|
146
|
-
if (groupedSchemas.valueSets.length > 0) {
|
|
147
|
-
this.logger.info(`Enhancing ${groupedSchemas.valueSets.length} value sets`);
|
|
148
|
-
const valueSetResults = await this.enhanceValueSets(groupedSchemas.valueSets);
|
|
149
|
-
results.push(...valueSetResults);
|
|
150
|
-
}
|
|
151
|
-
if (groupedSchemas.codeSystems.length > 0) {
|
|
152
|
-
this.logger.info(`Enhancing ${groupedSchemas.codeSystems.length} code systems`);
|
|
153
|
-
const codeSystemResults = await this.enhanceCodeSystems(groupedSchemas.codeSystems);
|
|
154
|
-
results.push(...codeSystemResults);
|
|
155
|
-
}
|
|
156
|
-
this.logger.success(`Generated ${results.length} enhanced FHIR type schemas: ${groupedSchemas.resources.length} resources, ${groupedSchemas.complexTypes.length} complex types, ${groupedSchemas.primitives.length} primitives`);
|
|
157
|
-
return results;
|
|
158
|
-
}
|
|
159
|
-
groupTypeSchemas(schemas) {
|
|
160
|
-
const groups = {
|
|
161
|
-
resources: [],
|
|
162
|
-
complexTypes: [],
|
|
163
|
-
primitives: [],
|
|
164
|
-
profiles: [],
|
|
165
|
-
extensions: [],
|
|
166
|
-
valueSets: [],
|
|
167
|
-
codeSystems: [],
|
|
168
|
-
};
|
|
169
|
-
for (const schema of schemas) {
|
|
170
|
-
switch (schema.identifier.kind) {
|
|
171
|
-
case "resource":
|
|
172
|
-
groups.resources.push(schema);
|
|
173
|
-
break;
|
|
174
|
-
case "complex-type":
|
|
175
|
-
groups.complexTypes.push(schema);
|
|
176
|
-
break;
|
|
177
|
-
case "primitive-type":
|
|
178
|
-
groups.primitives.push(schema);
|
|
179
|
-
break;
|
|
180
|
-
case "binding":
|
|
181
|
-
if ("metadata" in schema && schema.metadata?.isExtension) {
|
|
182
|
-
groups.extensions.push(schema);
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
groups.complexTypes.push(schema);
|
|
186
|
-
}
|
|
187
|
-
break;
|
|
188
|
-
case "value-set":
|
|
189
|
-
groups.valueSets.push(schema);
|
|
190
|
-
break;
|
|
191
|
-
default:
|
|
192
|
-
if ("metadata" in schema && schema.metadata?.isCodeSystem) {
|
|
193
|
-
groups.codeSystems.push(schema);
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
groups.complexTypes.push(schema);
|
|
197
|
-
}
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
130
|
+
async generateFromSchemas(packageInfo, fhirSchemas) {
|
|
131
|
+
this.logger.info(`Transforming ${fhirSchemas.length} FHIR schemas to Type Schema`);
|
|
132
|
+
const typeSchemas = [];
|
|
133
|
+
for (const fhirSchema of fhirSchemas) {
|
|
134
|
+
typeSchemas.push(...(await transformFHIRSchema(this.manager, fhirSchema, packageInfo)));
|
|
200
135
|
}
|
|
201
|
-
return
|
|
202
|
-
}
|
|
203
|
-
async enhanceProfiles(schemas) {
|
|
204
|
-
return schemas;
|
|
205
|
-
}
|
|
206
|
-
async enhanceExtensions(schemas) {
|
|
207
|
-
return schemas;
|
|
208
|
-
}
|
|
209
|
-
async enhanceValueSets(schemas) {
|
|
210
|
-
return schemas;
|
|
211
|
-
}
|
|
212
|
-
async enhanceCodeSystems(schemas) {
|
|
213
|
-
return schemas;
|
|
136
|
+
return typeSchemas;
|
|
214
137
|
}
|
|
215
138
|
/**
|
|
216
139
|
* Apply treeshaking to StructureDefinitions before FHIR schema transformation
|
|
@@ -340,24 +263,3 @@ export class TypeSchemaGenerator {
|
|
|
340
263
|
return match ? (match[1] ?? null) : null;
|
|
341
264
|
}
|
|
342
265
|
}
|
|
343
|
-
/**
|
|
344
|
-
* Convenience function to generate TypeSchema from a package
|
|
345
|
-
*/
|
|
346
|
-
export async function generateTypeSchemaFromPackage(packageName, options = {}) {
|
|
347
|
-
const generator = new TypeSchemaGenerator(options);
|
|
348
|
-
return await generator.generateFromPackage(packageName);
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Convenience function to generate TypeSchema from FHIR schemas
|
|
352
|
-
*/
|
|
353
|
-
export async function generateTypeSchemaFromSchemas(fhirSchemas, packageInfo, options = {}) {
|
|
354
|
-
const generator = new TypeSchemaGenerator(options);
|
|
355
|
-
return await generator.generateFromSchemas(fhirSchemas, packageInfo);
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Convenience function to generate TypeSchema from a single FHIR schema
|
|
359
|
-
*/
|
|
360
|
-
export async function generateTypeSchemaFromSchema(fhirSchema, packageInfo, options = {}) {
|
|
361
|
-
const generator = new TypeSchemaGenerator(options);
|
|
362
|
-
return await generator.generateFromFhirSchema(fhirSchema, packageInfo);
|
|
363
|
-
}
|
package/package.json
CHANGED