@atomic-ehr/codegen 0.0.1-canary.20251007123452.75482df → 0.0.1-canary.20251007145120.703d8ab

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.js CHANGED
@@ -5430,8 +5430,8 @@ var Writer = class extends FileSystemWriter {
5430
5430
  }
5431
5431
  disclaimer() {
5432
5432
  return [
5433
- "WARNING: This file is autogenerated by FHIR Schema Codegen.",
5434
- "https://github.com/fhir-schema/fhir-schema-codegen",
5433
+ "WARNING: This file is autogenerated by @atomic-ehr/codegen.",
5434
+ "GitHub: https://github.com/orgs/atomic-ehr/repositories",
5435
5435
  "Any manual changes made to this file may be overwritten."
5436
5436
  ];
5437
5437
  }
@@ -5532,76 +5532,62 @@ var primitiveType2tsType = {
5532
5532
  id: "string",
5533
5533
  xhtml: "string"
5534
5534
  };
5535
+ var tsFhirPackageDir = (name) => {
5536
+ return kebabCase(name);
5537
+ };
5538
+ var tsModuleName = (id) => {
5539
+ return pascalCase(id.name);
5540
+ };
5541
+ var tsModuleFileName = (id) => {
5542
+ return `${tsModuleName(id)}.ts`;
5543
+ };
5535
5544
  var canonicalToName = (canonical, dropFragment = true) => {
5536
5545
  if (!canonical) return void 0;
5537
5546
  let localName = canonical.split("/").pop();
5538
- if (dropFragment && localName?.includes("#")) {
5547
+ if (!localName) return void 0;
5548
+ if (dropFragment && localName.includes("#")) {
5539
5549
  localName = localName.split("#")[0];
5540
5550
  }
5541
- if (/^\d/.test(localName ?? "")) {
5551
+ if (!localName) return void 0;
5552
+ if (/^\d/.test(localName)) {
5542
5553
  localName = `number_${localName}`;
5543
5554
  }
5544
- return localName?.replace(/[- ]/g, "_");
5545
- };
5546
- var tsBaseFileName = (id) => {
5547
- return pascalCase(id.name);
5555
+ return normalizeTsName(localName);
5548
5556
  };
5549
- var tsFileName = (id) => {
5550
- return `${tsBaseFileName(id)}.ts`;
5557
+ var tsResourceName = (id) => {
5558
+ if (id.kind === "nested") {
5559
+ const url = id.url;
5560
+ const path = canonicalToName(url, false);
5561
+ if (!path) return "";
5562
+ const [resourceName, fragment] = path.split("#");
5563
+ const name = uppercaseFirstLetterOfEach((fragment ?? "").split(".")).join("");
5564
+ return normalizeTsName([resourceName, name].join(""));
5565
+ }
5566
+ return normalizeTsName(id.name);
5551
5567
  };
5552
- var normalizeName = (n) => {
5553
- if (n === "extends") {
5554
- return "extends_";
5555
- }
5568
+ var tsFieldName = (n) => normalizeTsName(n);
5569
+ var normalizeTsName = (n) => {
5556
5570
  return n.replace(/[- ]/g, "_");
5557
5571
  };
5558
- var resourceName = (id) => {
5559
- return normalizeName(id.name);
5560
- };
5561
- var deriveNestedSchemaName = (url) => {
5562
- const path = canonicalToName(url, false);
5563
- if (!path) {
5564
- return "";
5565
- }
5566
- const [resourceName2, fragment] = path.split("#");
5567
- const name = uppercaseFirstLetterOfEach((fragment ?? "").split(".")).join("");
5568
- return [resourceName2, name].join("");
5569
- };
5570
5572
  var TypeScript = class extends Writer {
5571
5573
  resourceRelatives = [];
5572
- tsImport(tsPackageName, ...entities) {
5573
- this.lineSM(`import { ${entities.join(", ")} } from '${tsPackageName}'`);
5574
+ tsImportType(tsPackageName, ...entities) {
5575
+ this.lineSM(`import type { ${entities.join(", ")} } from '${tsPackageName}'`);
5574
5576
  }
5575
5577
  generateFhirPackageIndexFile(schemas) {
5576
5578
  this.cat("index.ts", () => {
5577
5579
  let exports = schemas.map((schema) => ({
5578
5580
  identifier: schema.identifier,
5579
- tsPackageName: tsBaseFileName(schema.identifier),
5580
- resourceName: resourceName(schema.identifier)
5581
+ tsPackageName: tsModuleName(schema.identifier),
5582
+ resourceName: tsResourceName(schema.identifier)
5581
5583
  })).sort((a, b) => a.resourceName.localeCompare(b.resourceName));
5582
5584
  exports = Array.from(new Map(exports.map((exp) => [exp.resourceName.toLowerCase(), exp])).values()).sort(
5583
5585
  (a, b) => a.resourceName.localeCompare(b.resourceName)
5584
5586
  );
5585
5587
  for (const exp of exports) {
5586
5588
  this.debugComment(exp.identifier);
5587
- this.tsImport(`./${exp.tsPackageName}`, exp.resourceName);
5588
- }
5589
- this.lineSM(`export { ${exports.map((e) => e.resourceName).join(", ")} }`);
5590
- this.line("");
5591
- this.curlyBlock(["export type ResourceTypeMap = "], () => {
5592
- this.lineSM("User: Record<string, any>");
5593
- exports.forEach((exp) => {
5594
- this.debugComment(exp.identifier);
5595
- this.lineSM(`${exp.resourceName}: ${exp.resourceName}`);
5596
- });
5597
- });
5598
- this.lineSM("export type ResourceType = keyof ResourceTypeMap");
5599
- this.squareBlock(["export const resourceList: readonly ResourceType[] = "], () => {
5600
- exports.forEach((exp) => {
5601
- this.debugComment(exp.identifier);
5602
- this.line(`'${exp.resourceName}', `);
5603
- });
5604
- });
5589
+ this.lineSM(`export type { ${exp.resourceName} } from './${exp.tsPackageName}'`);
5590
+ }
5605
5591
  });
5606
5592
  }
5607
5593
  generateDependenciesImports(schema) {
@@ -5613,17 +5599,30 @@ var TypeScript = class extends Writer {
5613
5599
  })),
5614
5600
  ...schema.dependencies.filter((dep) => ["nested"].includes(dep.kind)).map((dep) => ({
5615
5601
  tsPackage: `../${kebabCase(dep.package)}/${pascalCase(canonicalToName(dep.url) ?? "")}`,
5616
- name: deriveNestedSchemaName(dep.url)
5602
+ name: tsResourceName(dep)
5617
5603
  }))
5618
5604
  ].sort((a, b) => a.name.localeCompare(b.name));
5619
5605
  for (const dep of deps) {
5620
- this.tsImport(dep.tsPackage, dep.name);
5606
+ this.tsImportType(dep.tsPackage, dep.name);
5621
5607
  }
5608
+ this.line();
5609
+ }
5610
+ }
5611
+ generateComplexTypeReexports(schema) {
5612
+ const complexTypeDeps = schema.dependencies?.filter((dep) => ["complex-type"].includes(dep.kind)).map((dep) => ({
5613
+ tsPackage: `../${kebabCase(dep.package)}/${pascalCase(dep.name)}`,
5614
+ name: uppercaseFirstLetter(dep.name)
5615
+ }));
5616
+ if (complexTypeDeps && complexTypeDeps.length > 0) {
5617
+ for (const dep of complexTypeDeps) {
5618
+ this.lineSM(`export type { ${dep.name} }from '${dep.tsPackage}';`);
5619
+ }
5620
+ this.line();
5622
5621
  }
5623
5622
  }
5624
5623
  addFieldExtension(fieldName, field) {
5625
5624
  if (field.type.kind === "primitive-type") {
5626
- this.lineSM(`_${normalizeName(fieldName)}?: Element`);
5625
+ this.lineSM(`_${tsFieldName(fieldName)}?: Element`);
5627
5626
  }
5628
5627
  }
5629
5628
  generateType(schema) {
@@ -5631,17 +5630,15 @@ var TypeScript = class extends Writer {
5631
5630
  if (schema.identifier.name === "Reference") {
5632
5631
  name = "Reference<T extends string = string>";
5633
5632
  } else if (schema.identifier.kind === "nested") {
5634
- name = normalizeName(deriveNestedSchemaName(schema.identifier.url));
5633
+ name = tsResourceName(schema.identifier);
5635
5634
  } else {
5636
- name = normalizeName(schema.identifier.name);
5635
+ name = tsResourceName(schema.identifier);
5637
5636
  }
5638
5637
  const parent = canonicalToName(schema.base?.url);
5639
5638
  const extendsClause = parent && `extends ${parent}`;
5640
5639
  this.debugComment(schema.identifier);
5641
5640
  this.curlyBlock(["export", "interface", name, extendsClause], () => {
5642
- if (!schema.fields) {
5643
- return;
5644
- }
5641
+ if (!schema.fields) return;
5645
5642
  if (schema.identifier.kind === "resource") {
5646
5643
  const possibleResourceTypes = [schema.identifier];
5647
5644
  possibleResourceTypes.push(...resourceChildren(this.resourceRelatives, schema.identifier));
@@ -5653,7 +5650,7 @@ var TypeScript = class extends Writer {
5653
5650
  const field = notChoiceDeclaration(anyField);
5654
5651
  if (field === void 0) continue;
5655
5652
  this.debugComment(fieldName, ":", field);
5656
- const fieldNameFixed = normalizeName(fieldName);
5653
+ const fieldNameFixed = tsFieldName(fieldName);
5657
5654
  const optionalSymbol = field.required ? "" : "?";
5658
5655
  const arraySymbol = field.array ? "[]" : "";
5659
5656
  if (field.type === void 0) {
@@ -5661,7 +5658,7 @@ var TypeScript = class extends Writer {
5661
5658
  }
5662
5659
  let type = field.type.name;
5663
5660
  if (field.type.kind === "nested") {
5664
- type = deriveNestedSchemaName(field.type.url);
5661
+ type = tsResourceName(field.type);
5665
5662
  }
5666
5663
  if (field.type.kind === "primitive-type") {
5667
5664
  type = primitiveType2tsType[field.type.name] ?? "string";
@@ -5693,11 +5690,11 @@ var TypeScript = class extends Writer {
5693
5690
  }
5694
5691
  }
5695
5692
  generateResourceModule(schema) {
5696
- this.cat(`${tsFileName(schema.identifier)}`, () => {
5693
+ this.cat(`${tsModuleFileName(schema.identifier)}`, () => {
5697
5694
  this.generateDisclaimer();
5698
5695
  if (["complex-type", "resource", "logical", "nested"].includes(schema.identifier.kind)) {
5699
5696
  this.generateDependenciesImports(schema);
5700
- this.line();
5697
+ this.generateComplexTypeReexports(schema);
5701
5698
  this.generateNestedTypes(schema);
5702
5699
  this.generateType(schema);
5703
5700
  } else {
@@ -5706,18 +5703,18 @@ var TypeScript = class extends Writer {
5706
5703
  });
5707
5704
  }
5708
5705
  generate(schemas) {
5709
- this.resourceRelatives = resourceRelatives(schemas);
5710
5706
  const typesToGenerate = [
5711
5707
  ...collectComplexTypes(schemas),
5712
5708
  ...collectResources(schemas)
5713
5709
  // ...collectLogicalModels(typeSchemas),
5714
5710
  // ...collectProfiles(typeSchemas),
5715
5711
  ];
5712
+ this.resourceRelatives = resourceRelatives(typesToGenerate);
5716
5713
  const grouped = groupByPackages(typesToGenerate);
5717
5714
  this.cd("/", () => {
5718
5715
  for (const [packageName, packageSchemas] of Object.entries(grouped)) {
5719
- const tsPackageName = kebabCase(packageName);
5720
- this.cd(tsPackageName, () => {
5716
+ const tsPackageDir = tsFhirPackageDir(packageName);
5717
+ this.cd(tsPackageDir, () => {
5721
5718
  for (const schema of packageSchemas) {
5722
5719
  this.generateResourceModule(schema);
5723
5720
  }