@atomic-ehr/codegen 0.0.1-canary.20251009095714.f3c2978 → 0.0.1-canary.20251010120059.024c867

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/index.d.ts CHANGED
@@ -304,7 +304,7 @@ interface TypeschemaGeneratorOptions {
304
304
 
305
305
  type Register = {
306
306
  appendFs(fs: FHIRSchema): void;
307
- ensureCanonicalUrl(name: string | Name | CanonicalUrl): CanonicalUrl;
307
+ ensureSpecializationCanonicalUrl(name: string | Name | CanonicalUrl): CanonicalUrl;
308
308
  resolveSd(canonicalUrl: CanonicalUrl): StructureDefinition | undefined;
309
309
  resolveFs(canonicalUrl: CanonicalUrl): RichFHIRSchema | undefined;
310
310
  resolveFsGenealogy(canonicalUrl: CanonicalUrl): RichFHIRSchema[];
package/dist/index.js CHANGED
@@ -1452,23 +1452,24 @@ var registerFromManager = async (manager, logger) => {
1452
1452
  const indexByPackages = [];
1453
1453
  for (const pkg of packages) {
1454
1454
  const resources = await manager.search({ package: pkg });
1455
- const index = {};
1455
+ const perPackageIndex = {};
1456
1456
  for (const resource of resources) {
1457
1457
  const url = resource.url;
1458
1458
  if (!url) continue;
1459
- index[url] = resource;
1459
+ if (perPackageIndex[url]) throw new Error(`Duplicate resource URL: ${url}`);
1460
+ perPackageIndex[url] = resource;
1461
+ if (flatRawIndex[url]) throw new Error(`Duplicate resource URL: ${url}`);
1460
1462
  flatRawIndex[url] = resource;
1461
1463
  }
1462
1464
  indexByPackages.push({
1463
1465
  package_meta: pkg,
1464
- index
1466
+ index: perPackageIndex
1465
1467
  });
1466
1468
  }
1467
1469
  const sdIndex = {};
1468
1470
  const vsIndex = {};
1469
1471
  const fsIndex = {};
1470
- const nameToCanonical = {};
1471
- let [fsSuccess, fsFailed] = [0, 0];
1472
+ const specNameToCanonical = {};
1472
1473
  for (const resourcesByPackage of indexByPackages) {
1473
1474
  const packageMeta = resourcesByPackage.package_meta;
1474
1475
  for (const [surl, resource] of Object.entries(resourcesByPackage.index)) {
@@ -1476,16 +1477,19 @@ var registerFromManager = async (manager, logger) => {
1476
1477
  if (resource.resourceType === "StructureDefinition") {
1477
1478
  const sd = resource;
1478
1479
  sdIndex[url] = sd;
1479
- try {
1480
- const rfs = enrichFHIRSchema(fhirschema.translate(sd), packageMeta);
1481
- fsIndex[rfs.url] = rfs;
1482
- nameToCanonical[rfs.name] = rfs.url;
1483
- fsSuccess++;
1484
- } catch (error) {
1485
- logger?.warn(
1486
- `Failed to convert StructureDefinition ${sd.name || sd.id}: ${error instanceof Error ? error.message : String(error)}`
1487
- );
1488
- fsFailed++;
1480
+ const rfs = enrichFHIRSchema(fhirschema.translate(sd), packageMeta);
1481
+ fsIndex[rfs.url] = rfs;
1482
+ if (rfs.derivation === void 0 || rfs.derivation === "specialization") {
1483
+ if (specNameToCanonical[rfs.name]) {
1484
+ const info = {
1485
+ old: specNameToCanonical[rfs.name],
1486
+ oldDerivation: flatRawIndex[specNameToCanonical[rfs.name]].derivation,
1487
+ new: rfs.url,
1488
+ newDerivation: rfs.derivation
1489
+ };
1490
+ throw new Error(`Duplicate name ${rfs.name} ${JSON.stringify(info, void 0, 2)}`);
1491
+ }
1492
+ specNameToCanonical[rfs.name] = rfs.url;
1489
1493
  }
1490
1494
  }
1491
1495
  if (resource.resourceType === "ValueSet") {
@@ -1496,7 +1500,7 @@ var registerFromManager = async (manager, logger) => {
1496
1500
  }
1497
1501
  }
1498
1502
  logger?.success(
1499
- `FHIR Schema conversion for '${packageMetaToFhir(packageMeta)}' completed: ${fsSuccess} successful, ${fsFailed} failed`
1503
+ `FHIR Schema conversion for '${packageMetaToFhir(packageMeta)}' completed: ${Object.keys(fsIndex).length} successful`
1500
1504
  );
1501
1505
  }
1502
1506
  const complexTypes = {};
@@ -1510,7 +1514,7 @@ var registerFromManager = async (manager, logger) => {
1510
1514
  if (fs3 === void 0) throw new Error(`Failed to resolve FHIR Schema genealogy for '${canonicalUrl}'`);
1511
1515
  const genealogy = [fs3];
1512
1516
  while (fs3?.base) {
1513
- fs3 = fsIndex[fs3.base] || fsIndex[nameToCanonical[fs3.base]];
1517
+ fs3 = fsIndex[fs3.base] || fsIndex[specNameToCanonical[fs3.base]];
1514
1518
  genealogy.push(fs3);
1515
1519
  if (fs3 === void 0) throw new Error(`Failed to resolve FHIR Schema genealogy for '${canonicalUrl}'`);
1516
1520
  }
@@ -1524,12 +1528,12 @@ var registerFromManager = async (manager, logger) => {
1524
1528
  appendFs(fs3) {
1525
1529
  const rfs = enrichFHIRSchema(fs3);
1526
1530
  fsIndex[rfs.url] = rfs;
1527
- nameToCanonical[rfs.name] = rfs.url;
1531
+ specNameToCanonical[rfs.name] = rfs.url;
1528
1532
  },
1529
1533
  resolveFs: (canonicalUrl) => fsIndex[canonicalUrl],
1530
1534
  resolveFsGenealogy,
1531
1535
  resolveFsSpecializations,
1532
- ensureCanonicalUrl: (name) => nameToCanonical[name] || name,
1536
+ ensureSpecializationCanonicalUrl: (name) => specNameToCanonical[name] || name,
1533
1537
  allSd: () => Object.values(sdIndex),
1534
1538
  resolveSd: (canonicalUrl) => sdIndex[canonicalUrl],
1535
1539
  allFs: () => Object.values(fsIndex),
@@ -1685,7 +1689,7 @@ function mkNestedTypes(register, fhirSchema, logger) {
1685
1689
  package: fhirSchema.package_meta.name,
1686
1690
  version: fhirSchema.package_meta.version,
1687
1691
  name: baseName,
1688
- url: register.ensureCanonicalUrl(baseName)
1692
+ url: register.ensureSpecializationCanonicalUrl(baseName)
1689
1693
  };
1690
1694
  const fields = transformNestedElements(register, fhirSchema, path, element.elements, logger);
1691
1695
  const nestedType = {
@@ -1749,34 +1753,28 @@ function isExcluded(register, fhirSchema, path) {
1749
1753
  var buildReferences = (element, register, _packageInfo) => {
1750
1754
  if (!element.refers) return void 0;
1751
1755
  return element.refers.map((ref) => {
1752
- const curl = register.ensureCanonicalUrl(ref);
1756
+ const curl = register.ensureSpecializationCanonicalUrl(ref);
1753
1757
  const fs3 = register.resolveFs(curl);
1754
1758
  return mkIdentifier(fs3);
1755
1759
  });
1756
1760
  };
1757
1761
  function buildFieldType(register, fhirSchema, element, logger) {
1758
1762
  if (element.elementReference) {
1759
- const refPath = element.elementReference.filter((_, i) => i % 2 === 1).map((p) => p).filter((p) => p !== "elements");
1760
- if (refPath.length > 0) {
1761
- return mkNestedIdentifier(register, fhirSchema, refPath, logger);
1762
- }
1763
- }
1764
- if (element.type) {
1765
- const kind = element.type.match(/^[a-z]/) ? "primitive-type" : "complex-type";
1766
- const url = register.ensureCanonicalUrl(element.type);
1763
+ const refPath = element.elementReference.slice(1).filter((_, i) => i % 2 === 1);
1764
+ return mkNestedIdentifier(register, fhirSchema, refPath, logger);
1765
+ } else if (element.type) {
1766
+ const url = register.ensureSpecializationCanonicalUrl(element.type);
1767
1767
  const fieldFs = register.resolveFs(url);
1768
- if (!fieldFs) {
1769
- throw new Error(`Could not resolve field '${element.type}'`);
1770
- }
1771
- return {
1772
- kind,
1773
- package: fieldFs.package_meta.name,
1774
- version: fieldFs.package_meta.version,
1775
- name: element.type,
1776
- url
1777
- };
1778
- }
1779
- return void 0;
1768
+ if (!fieldFs) throw new Error(`Could not resolve field '${element.type}'`);
1769
+ return mkIdentifier(fieldFs);
1770
+ } else if (element.choices) {
1771
+ return void 0;
1772
+ } else if (fhirSchema.derivation === "constraint") {
1773
+ return void 0;
1774
+ } else
1775
+ throw new Error(
1776
+ `Can't recognize element type '${fhirSchema.url}' (${fhirSchema.derivation}): ${JSON.stringify(element, void 0, 2)}`
1777
+ );
1780
1778
  }
1781
1779
  var mkField = (register, fhirSchema, path, element, logger) => {
1782
1780
  let binding;
@@ -2094,7 +2092,7 @@ function transformFhirSchemaResource(register, fhirSchema, logger) {
2094
2092
  const identifier = mkIdentifier(fhirSchema);
2095
2093
  let base;
2096
2094
  if (fhirSchema.base && fhirSchema.type !== "Element") {
2097
- const baseFs = register.resolveFs(register.ensureCanonicalUrl(fhirSchema.base));
2095
+ const baseFs = register.resolveFs(register.ensureSpecializationCanonicalUrl(fhirSchema.base));
2098
2096
  if (!baseFs) {
2099
2097
  throw new Error(`Base resource not found '${fhirSchema.base}' for '${fhirSchema.url}'`);
2100
2098
  }
@@ -2993,10 +2991,14 @@ var mkTypeSchemaIndex = (schemas) => {
2993
2991
  const index = {};
2994
2992
  const append = (schema) => {
2995
2993
  const url = schema.identifier.url;
2996
- if (!index[url]) {
2997
- index[url] = {};
2998
- }
2999
- index[url][schema.identifier.package] = schema;
2994
+ const pkg = schema.identifier.package;
2995
+ if (!index[url]) index[url] = {};
2996
+ if (index[url][schema.identifier.package] && pkg !== "shared") {
2997
+ const r1 = JSON.stringify(schema.identifier, void 0, 2);
2998
+ const r2 = JSON.stringify(index[url][pkg]?.identifier, void 0, 2);
2999
+ throw new Error(`Duplicate schema: ${r1} and ${r2}`);
3000
+ }
3001
+ index[url][pkg] = schema;
3000
3002
  };
3001
3003
  for (const schema of schemas) {
3002
3004
  append(schema);
@@ -3067,7 +3069,6 @@ var mkTypeSchemaIndex = (schemas) => {
3067
3069
  };
3068
3070
  const isWithMetaField = (profile) => {
3069
3071
  return hierarchy(profile).filter(isSpecializationTypeSchema).some((schema) => {
3070
- console.log(schema.fields?.meta);
3071
3072
  return schema.fields?.meta !== void 0;
3072
3073
  });
3073
3074
  };
@@ -5444,6 +5445,7 @@ var TypeScript = class extends Writer {
5444
5445
  if (schema.nested) {
5445
5446
  for (const subtype of schema.nested) {
5446
5447
  this.generateType(tsIndex, subtype);
5448
+ this.line();
5447
5449
  }
5448
5450
  }
5449
5451
  }
@@ -5598,10 +5600,12 @@ var TypeScript = class extends Writer {
5598
5600
  this.generateDependenciesImports(schema);
5599
5601
  this.generateComplexTypeReexports(schema);
5600
5602
  this.generateNestedTypes(tsIndex, schema);
5603
+ this.comment("CanonicalURL:", schema.identifier.url);
5601
5604
  this.generateType(tsIndex, schema);
5602
5605
  } else if (isProfileTypeSchema(schema)) {
5603
5606
  const flatProfile = tsIndex.flatProfile(schema);
5604
5607
  this.generateDependenciesImports(flatProfile);
5608
+ this.comment("CanonicalURL:", schema.identifier.url);
5605
5609
  this.generateProfileType(tsIndex, flatProfile);
5606
5610
  this.generateAttachProfile(flatProfile);
5607
5611
  this.generateExtractProfile(tsIndex, flatProfile);