@atomic-ehr/codegen 0.0.2-canary.20251119144634.930498f → 0.0.2-canary.20251120135224.f98eeb8
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/dist/cli/index.js +11 -14
- package/dist/index.d.ts +4 -6
- package/dist/index.js +0 -344
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1456,7 +1456,6 @@ var mkTypeSchemaIndex = (schemas, logger) => {
|
|
|
1456
1456
|
logical: {}
|
|
1457
1457
|
};
|
|
1458
1458
|
for (const schema of shemas) {
|
|
1459
|
-
schema.identifier;
|
|
1460
1459
|
tree[pkgId][schema.identifier.kind][schema.identifier.url] = {};
|
|
1461
1460
|
}
|
|
1462
1461
|
}
|
|
@@ -1606,327 +1605,6 @@ function createLogger(options = {}) {
|
|
|
1606
1605
|
return new CodegenLogger(options);
|
|
1607
1606
|
}
|
|
1608
1607
|
|
|
1609
|
-
// src/typeschema/generator.ts
|
|
1610
|
-
var TypeSchemaGenerator = class {
|
|
1611
|
-
manager;
|
|
1612
|
-
options;
|
|
1613
|
-
logger;
|
|
1614
|
-
constructor(options = {}) {
|
|
1615
|
-
this.options = { verbose: false, ...options };
|
|
1616
|
-
this.manager = options.manager || CanonicalManager({ packages: [], workingDir: "tmp/fhir" });
|
|
1617
|
-
this.logger = options.logger || createLogger({
|
|
1618
|
-
verbose: this.options.verbose,
|
|
1619
|
-
prefix: "TypeSchema"
|
|
1620
|
-
});
|
|
1621
|
-
}
|
|
1622
|
-
async registerFromPackageMetas(packageMetas) {
|
|
1623
|
-
const packageNames = packageMetas.map(packageMetaToFhir);
|
|
1624
|
-
this.logger?.step(`Loading FHIR packages: ${packageNames.join(", ")}`);
|
|
1625
|
-
await this.manager.init();
|
|
1626
|
-
return registerFromManager(this.manager, { focusedPackages: packageMetas });
|
|
1627
|
-
}
|
|
1628
|
-
generateFhirSchemas(structureDefinitions) {
|
|
1629
|
-
this.logger?.progress(`Converting ${structureDefinitions.length} StructureDefinitions to FHIRSchemas`);
|
|
1630
|
-
const fhirSchemas = [];
|
|
1631
|
-
let convertedCount = 0;
|
|
1632
|
-
let failedCount = 0;
|
|
1633
|
-
for (const sd of structureDefinitions) {
|
|
1634
|
-
try {
|
|
1635
|
-
const fhirSchema = fhirschema.translate(sd);
|
|
1636
|
-
fhirSchemas.push(fhirSchema);
|
|
1637
|
-
convertedCount++;
|
|
1638
|
-
this.logger?.debug(`Converted StructureDefinition: ${sd.name || sd.id} (${sd.resourceType})`);
|
|
1639
|
-
} catch (error) {
|
|
1640
|
-
failedCount++;
|
|
1641
|
-
this.logger?.warn(
|
|
1642
|
-
`Failed to convert StructureDefinition ${sd.name || sd.id}: ${error instanceof Error ? error.message : String(error)}`
|
|
1643
|
-
);
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1646
|
-
this.logger?.success(
|
|
1647
|
-
`FHIR Schema conversion completed: ${convertedCount}/${structureDefinitions.length} successful, ${failedCount} failed`
|
|
1648
|
-
);
|
|
1649
|
-
return fhirSchemas;
|
|
1650
|
-
}
|
|
1651
|
-
async generateValueSetSchemas(valueSets, logger) {
|
|
1652
|
-
if (valueSets.length > 0) {
|
|
1653
|
-
this.logger?.debug(`${valueSets.length} ValueSets available for enum extraction`);
|
|
1654
|
-
}
|
|
1655
|
-
const register = await registerFromManager(this.manager, { logger: this.logger });
|
|
1656
|
-
const valueSetSchemas = [];
|
|
1657
|
-
if (valueSets.length > 0) {
|
|
1658
|
-
this.logger?.progress(`Converting ${valueSets.length} ValueSets to TypeSchema`);
|
|
1659
|
-
let valueSetConvertedCount = 0;
|
|
1660
|
-
let valueSetFailedCount = 0;
|
|
1661
|
-
for (const vs of valueSets) {
|
|
1662
|
-
try {
|
|
1663
|
-
const valueSetSchema = await transformValueSet(register, vs, logger);
|
|
1664
|
-
if (valueSetSchema) {
|
|
1665
|
-
valueSetSchemas.push(valueSetSchema);
|
|
1666
|
-
valueSetConvertedCount++;
|
|
1667
|
-
this.logger?.debug(`Converted ValueSet: ${vs.name || vs.id}`);
|
|
1668
|
-
}
|
|
1669
|
-
} catch (error) {
|
|
1670
|
-
valueSetFailedCount++;
|
|
1671
|
-
this.logger?.warn(
|
|
1672
|
-
`Failed to convert ValueSet ${vs.name || vs.id}: ${error instanceof Error ? error.message : String(error)}`
|
|
1673
|
-
);
|
|
1674
|
-
}
|
|
1675
|
-
}
|
|
1676
|
-
this.logger?.success(
|
|
1677
|
-
`ValueSet conversion completed: ${valueSetConvertedCount}/${valueSets.length} successful, ${valueSetFailedCount} failed`
|
|
1678
|
-
);
|
|
1679
|
-
}
|
|
1680
|
-
return valueSetSchemas;
|
|
1681
|
-
}
|
|
1682
|
-
async generateFromPackage(packageName, packageVersion, logger) {
|
|
1683
|
-
const packageInfo = {
|
|
1684
|
-
name: packageName,
|
|
1685
|
-
version: packageVersion || "latest"
|
|
1686
|
-
};
|
|
1687
|
-
const register = await this.registerFromPackageMetas([packageInfo]);
|
|
1688
|
-
const valueSets = await this.generateValueSetSchemas(register.allVs(), logger);
|
|
1689
|
-
const fhirSchemas = (await Promise.all(register.allFs().map(async (fs4) => await transformFhirSchema(register, fs4, logger)))).flat();
|
|
1690
|
-
const allSchemas = [...fhirSchemas, ...valueSets];
|
|
1691
|
-
return allSchemas;
|
|
1692
|
-
}
|
|
1693
|
-
};
|
|
1694
|
-
var TypeSchemaParser = class {
|
|
1695
|
-
options;
|
|
1696
|
-
constructor(options = {}) {
|
|
1697
|
-
this.options = {
|
|
1698
|
-
format: "auto",
|
|
1699
|
-
validate: true,
|
|
1700
|
-
strict: false,
|
|
1701
|
-
...options
|
|
1702
|
-
};
|
|
1703
|
-
}
|
|
1704
|
-
/**
|
|
1705
|
-
* Parse TypeSchema from file
|
|
1706
|
-
*/
|
|
1707
|
-
async parseFromFile(filePath) {
|
|
1708
|
-
const content = await readFile(filePath, "utf-8");
|
|
1709
|
-
const format = this.options.format === "auto" ? this.detectFormat(content, filePath) : this.options.format;
|
|
1710
|
-
return this.parseFromString(content, format);
|
|
1711
|
-
}
|
|
1712
|
-
/**
|
|
1713
|
-
* Parse TypeSchema from string content
|
|
1714
|
-
*/
|
|
1715
|
-
async parseFromString(content, format) {
|
|
1716
|
-
const actualFormat = format || this.detectFormat(content);
|
|
1717
|
-
let schemas;
|
|
1718
|
-
if (actualFormat === "ndjson") {
|
|
1719
|
-
schemas = this.parseNDJSON(content);
|
|
1720
|
-
} else {
|
|
1721
|
-
schemas = this.parseJSON(content);
|
|
1722
|
-
}
|
|
1723
|
-
if (this.options.validate) {
|
|
1724
|
-
this.validateSchemas(schemas);
|
|
1725
|
-
}
|
|
1726
|
-
return schemas;
|
|
1727
|
-
}
|
|
1728
|
-
/**
|
|
1729
|
-
* Parse multiple TypeSchema files
|
|
1730
|
-
*/
|
|
1731
|
-
async parseFromFiles(filePaths) {
|
|
1732
|
-
const allSchemas = [];
|
|
1733
|
-
for (const filePath of filePaths) {
|
|
1734
|
-
const schemas = await this.parseFromFile(filePath);
|
|
1735
|
-
allSchemas.push(...schemas);
|
|
1736
|
-
}
|
|
1737
|
-
return allSchemas;
|
|
1738
|
-
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Parse a single TypeSchema object
|
|
1741
|
-
*/
|
|
1742
|
-
parseSchema(schemaData) {
|
|
1743
|
-
if (!schemaData.identifier) {
|
|
1744
|
-
throw new Error("TypeSchema must have an identifier");
|
|
1745
|
-
}
|
|
1746
|
-
if (!this.isValidIdentifier(schemaData.identifier)) {
|
|
1747
|
-
throw new Error("TypeSchema identifier is invalid");
|
|
1748
|
-
}
|
|
1749
|
-
return schemaData;
|
|
1750
|
-
}
|
|
1751
|
-
/**
|
|
1752
|
-
* Find schemas by identifier
|
|
1753
|
-
*/
|
|
1754
|
-
findByIdentifier(schemas, identifier) {
|
|
1755
|
-
return schemas.filter((schema) => this.matchesIdentifier(schema.identifier, identifier));
|
|
1756
|
-
}
|
|
1757
|
-
/**
|
|
1758
|
-
* Find schema by URL
|
|
1759
|
-
*/
|
|
1760
|
-
findByUrl(schemas, url) {
|
|
1761
|
-
return schemas.find((schema) => schema.identifier.url === url);
|
|
1762
|
-
}
|
|
1763
|
-
/**
|
|
1764
|
-
* Find schemas by kind
|
|
1765
|
-
*/
|
|
1766
|
-
findByKind(schemas, kind) {
|
|
1767
|
-
return schemas.filter((schema) => schema.identifier.kind === kind);
|
|
1768
|
-
}
|
|
1769
|
-
/**
|
|
1770
|
-
* Find schemas by package
|
|
1771
|
-
*/
|
|
1772
|
-
findByPackage(schemas, packageName) {
|
|
1773
|
-
return schemas.filter((schema) => schema.identifier.package === packageName);
|
|
1774
|
-
}
|
|
1775
|
-
/**
|
|
1776
|
-
* Get all dependencies from a schema
|
|
1777
|
-
*/
|
|
1778
|
-
// getDependencies(schema: TypeSchema): Identifier[] {
|
|
1779
|
-
// const dependencies: Identifier[] = [];
|
|
1780
|
-
// // Add base dependency
|
|
1781
|
-
// if ("base" in schema && schema.base) {
|
|
1782
|
-
// dependencies.push(schema.base);
|
|
1783
|
-
// }
|
|
1784
|
-
// // Add explicit dependencies
|
|
1785
|
-
// if ("dependencies" in schema && schema.dependencies) {
|
|
1786
|
-
// dependencies.push(...schema.dependencies);
|
|
1787
|
-
// }
|
|
1788
|
-
// // Add field type dependencies
|
|
1789
|
-
// if ("fields" in schema && schema.fields) {
|
|
1790
|
-
// for (const field of Object.values(schema.fields)) {
|
|
1791
|
-
// if ("type" in field && field.type) {
|
|
1792
|
-
// dependencies.push(field.type);
|
|
1793
|
-
// }
|
|
1794
|
-
// if ("binding" in field && field.binding) {
|
|
1795
|
-
// dependencies.push(field.binding);
|
|
1796
|
-
// }
|
|
1797
|
-
// if ("reference" in field && field.reference) {
|
|
1798
|
-
// dependencies.push(...field.reference);
|
|
1799
|
-
// }
|
|
1800
|
-
// }
|
|
1801
|
-
// }
|
|
1802
|
-
// if ("nested" in schema && schema.nested) {
|
|
1803
|
-
// for (const nested of schema.nested) {
|
|
1804
|
-
// dependencies.push(nested.identifier);
|
|
1805
|
-
// dependencies.push(nested.base);
|
|
1806
|
-
// for (const field of Object.values(nested.fields)) {
|
|
1807
|
-
// if ("type" in field && field.type) {
|
|
1808
|
-
// dependencies.push(field.type);
|
|
1809
|
-
// }
|
|
1810
|
-
// if ("binding" in field && field.binding) {
|
|
1811
|
-
// dependencies.push(field.binding);
|
|
1812
|
-
// }
|
|
1813
|
-
// if ("reference" in field && field.reference) {
|
|
1814
|
-
// dependencies.push(...field.reference);
|
|
1815
|
-
// }
|
|
1816
|
-
// }
|
|
1817
|
-
// }
|
|
1818
|
-
// }
|
|
1819
|
-
// // Add binding dependencies
|
|
1820
|
-
// if ("valueset" in schema) {
|
|
1821
|
-
// const bindingSchema = schema as any;
|
|
1822
|
-
// dependencies.push(bindingSchema.valueset);
|
|
1823
|
-
// if (bindingSchema.type) {
|
|
1824
|
-
// dependencies.push(bindingSchema.type);
|
|
1825
|
-
// }
|
|
1826
|
-
// }
|
|
1827
|
-
// // Remove duplicates
|
|
1828
|
-
// return this.deduplicateDependencies(dependencies);
|
|
1829
|
-
// }
|
|
1830
|
-
/**
|
|
1831
|
-
* Resolve schema dependencies
|
|
1832
|
-
*/
|
|
1833
|
-
// resolveDependencies(
|
|
1834
|
-
// schemas: TypeSchema[],
|
|
1835
|
-
// targetSchema: TypeSchema,
|
|
1836
|
-
// ): TypeSchema[] {
|
|
1837
|
-
// const dependencies = this.getDependencies(targetSchema);
|
|
1838
|
-
// const resolved: TypeSchema[] = [];
|
|
1839
|
-
// for (const dep of dependencies) {
|
|
1840
|
-
// const depSchema = this.findByUrl(schemas, dep.url);
|
|
1841
|
-
// if (depSchema) {
|
|
1842
|
-
// resolved.push(depSchema);
|
|
1843
|
-
// }
|
|
1844
|
-
// }
|
|
1845
|
-
// return resolved;
|
|
1846
|
-
// }
|
|
1847
|
-
/**
|
|
1848
|
-
* Detect format from content or filename
|
|
1849
|
-
*/
|
|
1850
|
-
detectFormat(content, filename) {
|
|
1851
|
-
if (filename) {
|
|
1852
|
-
if (filename.endsWith(".ndjson")) return "ndjson";
|
|
1853
|
-
if (filename.endsWith(".json")) return "json";
|
|
1854
|
-
}
|
|
1855
|
-
const trimmed = content.trim();
|
|
1856
|
-
if (trimmed.includes("\n")) {
|
|
1857
|
-
const lines = trimmed.split("\n").filter((line) => line.trim());
|
|
1858
|
-
if (lines.length > 1) {
|
|
1859
|
-
try {
|
|
1860
|
-
if (lines[0]) {
|
|
1861
|
-
JSON.parse(lines[0]);
|
|
1862
|
-
}
|
|
1863
|
-
return "ndjson";
|
|
1864
|
-
} catch {
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
return "json";
|
|
1869
|
-
}
|
|
1870
|
-
/**
|
|
1871
|
-
* Parse NDJSON format
|
|
1872
|
-
*/
|
|
1873
|
-
parseNDJSON(content) {
|
|
1874
|
-
const schemas = [];
|
|
1875
|
-
const lines = content.split("\n").filter((line) => line.trim());
|
|
1876
|
-
for (const line of lines) {
|
|
1877
|
-
try {
|
|
1878
|
-
const parsed = JSON.parse(line);
|
|
1879
|
-
schemas.push(this.parseSchema(parsed));
|
|
1880
|
-
} catch (error) {
|
|
1881
|
-
if (this.options.strict) {
|
|
1882
|
-
throw new Error(`Failed to parse NDJSON line: ${error}`);
|
|
1883
|
-
}
|
|
1884
|
-
}
|
|
1885
|
-
}
|
|
1886
|
-
return schemas;
|
|
1887
|
-
}
|
|
1888
|
-
/**
|
|
1889
|
-
* Parse JSON format
|
|
1890
|
-
*/
|
|
1891
|
-
parseJSON(content) {
|
|
1892
|
-
try {
|
|
1893
|
-
const parsed = JSON.parse(content);
|
|
1894
|
-
if (Array.isArray(parsed)) {
|
|
1895
|
-
return parsed.map((item) => this.parseSchema(item));
|
|
1896
|
-
} else {
|
|
1897
|
-
return [this.parseSchema(parsed)];
|
|
1898
|
-
}
|
|
1899
|
-
} catch (error) {
|
|
1900
|
-
throw new Error(`Failed to parse JSON: ${error}`);
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
/**
|
|
1904
|
-
* Validate schemas
|
|
1905
|
-
*/
|
|
1906
|
-
validateSchemas(schemas) {
|
|
1907
|
-
for (const schema of schemas) {
|
|
1908
|
-
if (!schema.identifier) {
|
|
1909
|
-
throw new Error("Schema missing identifier");
|
|
1910
|
-
}
|
|
1911
|
-
if (!this.isValidIdentifier(schema.identifier)) {
|
|
1912
|
-
throw new Error(`Invalid identifier in schema: ${JSON.stringify(schema.identifier)}`);
|
|
1913
|
-
}
|
|
1914
|
-
}
|
|
1915
|
-
}
|
|
1916
|
-
/**
|
|
1917
|
-
* Validate identifier structure
|
|
1918
|
-
*/
|
|
1919
|
-
isValidIdentifier(identifier) {
|
|
1920
|
-
return typeof identifier === "object" && identifier !== null && typeof identifier.kind === "string" && typeof identifier.package === "string" && typeof identifier.version === "string" && typeof identifier.name === "string" && typeof identifier.url === "string";
|
|
1921
|
-
}
|
|
1922
|
-
/**
|
|
1923
|
-
* Check if identifier matches criteria
|
|
1924
|
-
*/
|
|
1925
|
-
matchesIdentifier(identifier, criteria) {
|
|
1926
|
-
return (!criteria.kind || identifier.kind === criteria.kind) && (!criteria.package || identifier.package === criteria.package) && (!criteria.version || identifier.version === criteria.version) && (!criteria.name || identifier.name === criteria.name) && (!criteria.url || identifier.url === criteria.url);
|
|
1927
|
-
}
|
|
1928
|
-
};
|
|
1929
|
-
|
|
1930
1608
|
// src/typeschema/index.ts
|
|
1931
1609
|
var codeableReferenceInR4 = "Use CodeableReference which is not provided by FHIR R4.";
|
|
1932
1610
|
var availabilityInR4 = "Use Availability which is not provided by FHIR R4.";
|
|
@@ -2484,8 +2162,6 @@ var APIBuilder = class {
|
|
|
2484
2162
|
schemas = [];
|
|
2485
2163
|
options;
|
|
2486
2164
|
generators = /* @__PURE__ */ new Map();
|
|
2487
|
-
pendingOperations = [];
|
|
2488
|
-
typeSchemaGenerator;
|
|
2489
2165
|
logger;
|
|
2490
2166
|
packages = [];
|
|
2491
2167
|
progressCallback;
|
|
@@ -2519,12 +2195,6 @@ var APIBuilder = class {
|
|
|
2519
2195
|
this.packages.push(packageRef);
|
|
2520
2196
|
return this;
|
|
2521
2197
|
}
|
|
2522
|
-
fromFiles(...filePaths) {
|
|
2523
|
-
this.logger.debug(`Loading from ${filePaths.length} TypeSchema files`);
|
|
2524
|
-
const operation = this.loadFromFiles(filePaths);
|
|
2525
|
-
this.pendingOperations.push(operation);
|
|
2526
|
-
return this;
|
|
2527
|
-
}
|
|
2528
2198
|
fromSchemas(schemas) {
|
|
2529
2199
|
this.logger.debug(`Adding ${schemas.length} TypeSchemas to generation`);
|
|
2530
2200
|
this.schemas = [...this.schemas, ...schemas];
|
|
@@ -2696,20 +2366,6 @@ var APIBuilder = class {
|
|
|
2696
2366
|
getGenerators() {
|
|
2697
2367
|
return Array.from(this.generators.keys());
|
|
2698
2368
|
}
|
|
2699
|
-
async loadFromFiles(filePaths) {
|
|
2700
|
-
if (!this.typeSchemaGenerator) {
|
|
2701
|
-
this.typeSchemaGenerator = new TypeSchemaGenerator({
|
|
2702
|
-
verbose: this.options.verbose,
|
|
2703
|
-
logger: this.logger.child("Schema"),
|
|
2704
|
-
treeshake: this.typeSchemaConfig?.treeshake
|
|
2705
|
-
});
|
|
2706
|
-
}
|
|
2707
|
-
const parser = new TypeSchemaParser({
|
|
2708
|
-
format: "auto"
|
|
2709
|
-
});
|
|
2710
|
-
const schemas = await parser.parseFromFiles(filePaths);
|
|
2711
|
-
this.schemas = [...this.schemas, ...schemas];
|
|
2712
|
-
}
|
|
2713
2369
|
async executeGenerators(result, input) {
|
|
2714
2370
|
for (const [type, generator] of this.generators.entries()) {
|
|
2715
2371
|
this.logger.info(`Generating ${type}...`);
|