@atomic-ehr/codegen 0.0.1-canary.20251009083957.bf76104 → 0.0.1-canary.20251009095714.f3c2978

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
@@ -1409,15 +1409,15 @@ var enrichFHIRSchema = (schema, packageMeta) => {
1409
1409
  base: schema.base
1410
1410
  };
1411
1411
  };
1412
+ var isPrimitiveIdentifier = (id) => {
1413
+ return id?.kind === "primitive-type";
1414
+ };
1412
1415
  var isNestedIdentifier = (id) => {
1413
1416
  return id?.kind === "nested";
1414
1417
  };
1415
1418
  var isProfileIdentifier = (id) => {
1416
1419
  return id?.kind === "profile";
1417
1420
  };
1418
- var isFhirSchemaBased = (schema) => {
1419
- return schema?.identifier.kind !== "value-set";
1420
- };
1421
1421
  var isSpecializationTypeSchema = (schema) => {
1422
1422
  return schema?.identifier.kind === "resource" || schema?.identifier.kind === "complex-type" || schema?.identifier.kind === "logical";
1423
1423
  };
@@ -1440,6 +1440,10 @@ var isNotChoiceDeclarationField = (field) => {
1440
1440
  if (!field) return false;
1441
1441
  return field.choices === void 0;
1442
1442
  };
1443
+ var isChoiceDeclarationField = (field) => {
1444
+ if (!field) return false;
1445
+ return field.choices !== void 0;
1446
+ };
1443
1447
 
1444
1448
  // src/typeschema/register.ts
1445
1449
  var registerFromManager = async (manager, logger) => {
@@ -1619,7 +1623,7 @@ function mkBindingIdentifier(fhirSchema, path, bindingName) {
1619
1623
 
1620
1624
  // src/typeschema/core/nested-types.ts
1621
1625
  function mkNestedIdentifier(register, fhirSchema, path, logger) {
1622
- var nestedTypeOrigins = {};
1626
+ const nestedTypeOrigins = {};
1623
1627
  if (fhirSchema.derivation === "constraint") {
1624
1628
  const specializations = register.resolveFsSpecializations(fhirSchema.url);
1625
1629
  const nestedTypeGenealogy = specializations.map((fs3) => mkNestedTypes(register, fs3, logger)).filter((e) => e !== void 0).flat();
@@ -3019,14 +3023,17 @@ var mkTypeSchemaIndex = (schemas) => {
3019
3023
  }
3020
3024
  return res;
3021
3025
  };
3022
- const findLastSpecialization = (id) => {
3023
- const schema = resolve2(id);
3024
- if (!schema) return id;
3026
+ const findLastSpecialization = (schema) => {
3025
3027
  const nonConstraintSchema = hierarchy(schema).find((s) => s.identifier.kind !== "profile");
3026
3028
  if (!nonConstraintSchema) {
3027
- throw new Error(`No non-constraint schema found in hierarchy for ${id.name}`);
3029
+ throw new Error(`No non-constraint schema found in hierarchy for ${schema.identifier.name}`);
3028
3030
  }
3029
- return nonConstraintSchema.identifier;
3031
+ return nonConstraintSchema;
3032
+ };
3033
+ const findLastSpecializationByIdentifier = (id) => {
3034
+ const schema = resolve2(id);
3035
+ if (!schema) return id;
3036
+ return findLastSpecialization(schema).identifier;
3030
3037
  };
3031
3038
  const flatProfile = (schema) => {
3032
3039
  const hierarchySchemas = hierarchy(schema);
@@ -3075,6 +3082,7 @@ var mkTypeSchemaIndex = (schemas) => {
3075
3082
  resourceChildren,
3076
3083
  hierarchy,
3077
3084
  findLastSpecialization,
3085
+ findLastSpecializationByIdentifier,
3078
3086
  flatProfile,
3079
3087
  isWithMetaField
3080
3088
  };
@@ -5271,6 +5279,11 @@ var primitiveType2tsType = {
5271
5279
  id: "string",
5272
5280
  xhtml: "string"
5273
5281
  };
5282
+ var resolvePrimitiveType = (name) => {
5283
+ const tsType = primitiveType2tsType[name];
5284
+ if (tsType === void 0) throw new Error(`Unknown primitive type ${name}`);
5285
+ return tsType;
5286
+ };
5274
5287
  var tsFhirPackageDir = (name) => {
5275
5288
  return kebabCase(name);
5276
5289
  };
@@ -5378,7 +5391,7 @@ var TypeScript = class extends Writer {
5378
5391
  }
5379
5392
  }
5380
5393
  generateType(tsIndex, schema) {
5381
- var name;
5394
+ let name;
5382
5395
  if (schema.identifier.name === "Reference") {
5383
5396
  name = "Reference<T extends string = string>";
5384
5397
  } else if (schema.identifier.kind === "nested") {
@@ -5386,43 +5399,41 @@ var TypeScript = class extends Writer {
5386
5399
  } else {
5387
5400
  name = tsResourceName(schema.identifier);
5388
5401
  }
5389
- const parent = canonicalToName(schema.base?.url);
5390
- const extendsClause = parent && `extends ${parent}`;
5402
+ let extendsClause;
5403
+ if (schema.base) extendsClause = `extends ${canonicalToName(schema.base.url)}`;
5391
5404
  this.debugComment(schema.identifier);
5392
5405
  this.curlyBlock(["export", "interface", name, extendsClause], () => {
5393
- if (!schema.fields) return;
5394
- if (schema.identifier.kind === "resource") {
5406
+ if (isResourceTypeSchema(schema)) {
5395
5407
  const possibleResourceTypes = [schema.identifier];
5396
5408
  possibleResourceTypes.push(...tsIndex.resourceChildren(schema.identifier));
5397
5409
  this.lineSM(`resourceType: ${possibleResourceTypes.map((e) => `"${e.name}"`).join(" | ")}`);
5398
5410
  this.line();
5399
5411
  }
5412
+ if (!schema.fields) return;
5400
5413
  const fields = Object.entries(schema.fields).sort((a, b) => a[0].localeCompare(b[0]));
5401
5414
  for (const [fieldName, field] of fields) {
5402
- if (!isNotChoiceDeclarationField(field)) continue;
5415
+ if (isChoiceDeclarationField(field)) continue;
5416
+ if (field.type === void 0) continue;
5403
5417
  this.debugComment(fieldName, ":", field);
5404
5418
  const tsName = tsFieldName(fieldName);
5405
- const optionalSymbol = field.required ? "" : "?";
5406
- const arraySymbol = field.array ? "[]" : "";
5407
- if (field.type === void 0) continue;
5408
- let tsType = field.type.name;
5409
- if (field.type.kind === "nested") {
5410
- tsType = tsResourceName(field.type);
5411
- }
5412
- if (field.type.kind === "primitive-type") {
5413
- tsType = primitiveType2tsType[field.type.name] ?? "string";
5414
- }
5415
- if (schema.identifier.name === "Reference" && tsName === "reference") {
5419
+ let tsType;
5420
+ if (field.enum) {
5421
+ tsType = field.enum.map((e) => `"${e}"`).join(" | ");
5422
+ } else if (schema.identifier.name === "Reference" && tsName === "reference") {
5416
5423
  tsType = "`${T}/${string}`";
5417
- }
5418
- if (field.reference?.length) {
5424
+ } else if (field.reference && field.reference.length > 0) {
5419
5425
  const references = field.reference.map((ref) => `"${ref.name}"`).join(" | ");
5420
5426
  tsType = `Reference<${references}>`;
5427
+ } else if (isPrimitiveIdentifier(field.type)) {
5428
+ tsType = resolvePrimitiveType(field.type.name);
5429
+ } else if (isNestedIdentifier(field.type)) {
5430
+ tsType = tsResourceName(field.type);
5431
+ } else {
5432
+ tsType = field.type.name;
5421
5433
  }
5422
- if (field.enum) {
5423
- tsType = field.enum.map((e) => `"${e}"`).join(" | ");
5424
- }
5425
- this.lineSM(`${tsName}${optionalSymbol}:`, `${tsType}${arraySymbol}`);
5434
+ const optionalSymbol = field.required ? "" : "?";
5435
+ const arraySymbol = field.array ? "[]" : "";
5436
+ this.lineSM(`${tsName}${optionalSymbol}: ${tsType}${arraySymbol}`);
5426
5437
  if (["resource", "complex-type"].includes(schema.identifier.kind)) {
5427
5438
  this.addFieldExtension(fieldName, field);
5428
5439
  }
@@ -5436,34 +5447,30 @@ var TypeScript = class extends Writer {
5436
5447
  }
5437
5448
  }
5438
5449
  }
5439
- generateProfileType(tsIndex, schema) {
5440
- const name = tsResourceName(schema.identifier);
5441
- this.debugComment("identifier", schema.identifier);
5442
- this.debugComment("base", schema.base);
5443
- this.curlyBlock(["export", "interface", name], () => {
5444
- this.lineSM(`__profileUrl: "${schema.identifier.url}"`);
5450
+ generateProfileType(tsIndex, flatProfile) {
5451
+ const tsName = tsResourceName(flatProfile.identifier);
5452
+ this.debugComment("identifier", flatProfile.identifier);
5453
+ this.debugComment("base", flatProfile.base);
5454
+ this.curlyBlock(["export", "interface", tsName], () => {
5455
+ this.lineSM(`__profileUrl: "${flatProfile.identifier.url}"`);
5445
5456
  this.line();
5446
- if (!isFhirSchemaBased(schema)) return;
5447
- for (const [fieldName, field] of Object.entries(schema.fields ?? {})) {
5448
- if (!isNotChoiceDeclarationField(field)) continue;
5457
+ for (const [fieldName, field] of Object.entries(flatProfile.fields ?? {})) {
5458
+ if (isChoiceDeclarationField(field)) continue;
5449
5459
  this.debugComment(fieldName, field);
5450
- const tsName = tsFieldName(fieldName);
5460
+ const tsName2 = tsFieldName(fieldName);
5451
5461
  let tsType;
5452
- if (field.type.kind === "nested") {
5453
- tsType = tsResourceName(field.type);
5454
- } else if (field.enum) {
5462
+ if (field.enum) {
5455
5463
  tsType = field.enum.map((e) => `'${e}'`).join(" | ");
5456
5464
  } else if (field.reference && field.reference.length > 0) {
5457
- const specializationId = tsIndex.findLastSpecialization(schema.identifier);
5458
- const specialization = tsIndex.resolve(specializationId);
5465
+ const specialization = tsIndex.findLastSpecialization(flatProfile);
5459
5466
  if (!isSpecializationTypeSchema(specialization))
5460
- throw new Error(`Invalid specialization for ${schema.identifier}`);
5467
+ throw new Error(`Invalid specialization for ${flatProfile.identifier}`);
5461
5468
  const sField = specialization.fields?.[fieldName];
5462
- if (sField === void 0 || !isNotChoiceDeclarationField(sField))
5469
+ if (sField === void 0 || isChoiceDeclarationField(sField) || sField.reference === void 0)
5463
5470
  throw new Error(`Invalid field declaration for ${fieldName}`);
5464
- const sRefs = (sField.reference ?? []).map((e) => e.name);
5471
+ const sRefs = sField.reference.map((e) => e.name);
5465
5472
  const references = field.reference.map((ref) => {
5466
- const resRef = tsIndex.findLastSpecialization(ref);
5473
+ const resRef = tsIndex.findLastSpecializationByIdentifier(ref);
5467
5474
  if (resRef.name !== ref.name) {
5468
5475
  return `"${resRef.name}" /*${ref.name}*/`;
5469
5476
  }
@@ -5474,10 +5481,14 @@ var TypeScript = class extends Writer {
5474
5481
  } else {
5475
5482
  tsType = `Reference<${references}>`;
5476
5483
  }
5484
+ } else if (isNestedIdentifier(field.type)) {
5485
+ tsType = tsResourceName(field.type);
5486
+ } else if (isPrimitiveIdentifier(field.type)) {
5487
+ tsType = resolvePrimitiveType(field.type.name);
5477
5488
  } else {
5478
- tsType = primitiveType2tsType[field.type.name] ?? field.type.name;
5489
+ tsType = field.type.name;
5479
5490
  }
5480
- this.lineSM(`${tsName}${!field.required ? "?" : ""}: ${tsType}${field.array ? "[]" : ""}`);
5491
+ this.lineSM(`${tsName2}${!field.required ? "?" : ""}: ${tsType}${field.array ? "[]" : ""}`);
5481
5492
  }
5482
5493
  });
5483
5494
  this.line();
@@ -5514,7 +5525,7 @@ var TypeScript = class extends Writer {
5514
5525
  const profileFields = Object.entries(flatProfile.fields || {}).filter(([_fieldName, field]) => {
5515
5526
  return isNotChoiceDeclarationField(field) && field.type !== void 0;
5516
5527
  }).map(([fieldName]) => fieldName);
5517
- const specialization = tsIndex.resolve(tsIndex.findLastSpecialization(flatProfile.identifier));
5528
+ const specialization = tsIndex.findLastSpecialization(flatProfile);
5518
5529
  if (!isSpecializationTypeSchema(specialization))
5519
5530
  throw new Error(`Specialization not found for ${flatProfile.identifier.url}`);
5520
5531
  const shouldCast = {};
@@ -5876,6 +5887,7 @@ var APIBuilder = class {
5876
5887
  result.errors.push(
5877
5888
  `${type} generator failed: ${error instanceof Error ? error.message : String(error)}`
5878
5889
  );
5890
+ if (this.options.throwException) throw error;
5879
5891
  }
5880
5892
  }
5881
5893
  }