|
1 | | -import type { PreprocessContext } from "@atomic-ehr/fhir-canonical-manager"; |
| 1 | +import { ensureDependency, inResource, renamePackage, replaceText } from "@atomic-ehr/fhir-canonical-manager/patch"; |
2 | 2 | import { APIBuilder, prettyReport } from "../../../src/api/builder"; |
3 | 3 |
|
4 | | -// Fix known package name typos (in-memory transformation) |
5 | | -const packageNameFixes: Record<string, string> = { |
6 | | - "simplifier.core.r4.rResources": "simplifier.core.r4.resources", |
7 | | -}; |
8 | | - |
9 | | -// Packages that need hl7.fhir.r4.core dependency injected |
10 | | -const needsCoreDependency = (name: string): boolean => { |
11 | | - return ( |
12 | | - name.startsWith("simplifier.core.r4.") || |
13 | | - name === "simplifier.core.r4" || |
14 | | - name.startsWith("hl7.fhir.no.") || |
15 | | - name.startsWith("ehelse.fhir.no.") || |
16 | | - name.startsWith("nhn.fhir.no.") || |
17 | | - name.startsWith("sfm.") |
18 | | - ); |
19 | | -}; |
20 | | - |
21 | | -const preprocessPackage = (ctx: PreprocessContext): PreprocessContext => { |
22 | | - // GdRelatedPerson widens patient reference to include Person, but the |
23 | | - // base R4 RelatedPerson.patient only allows Patient. Drop the Person targets. |
24 | | - if (ctx.kind === "resource") { |
25 | | - const res = ctx.resource as { url?: string }; |
26 | | - if (res.url === "http://ehelse.no/fhir/StructureDefinition/gd-RelatedPerson") { |
27 | | - let str = JSON.stringify(ctx.resource); |
28 | | - str = str.replaceAll( |
29 | | - "http://hl7.org/fhir/StructureDefinition/Person", |
30 | | - "http://hl7.org/fhir/StructureDefinition/Patient", |
31 | | - ); |
32 | | - str = str.replaceAll( |
33 | | - "http://hl7.no/fhir/StructureDefinition/no-basis-Person", |
34 | | - "http://hl7.org/fhir/StructureDefinition/Patient", |
35 | | - ); |
36 | | - str = str.replaceAll( |
37 | | - "http://ehelse.no/fhir/StructureDefinition/gd-Person", |
38 | | - "http://hl7.org/fhir/StructureDefinition/Patient", |
39 | | - ); |
40 | | - return { ...ctx, resource: JSON.parse(str) }; |
41 | | - } |
42 | | - return ctx; |
43 | | - } |
44 | | - let json = ctx.packageJson; |
45 | | - const name = json.name as string; |
46 | | - |
47 | | - // Fix package name typos |
48 | | - const fixedName = packageNameFixes[name]; |
49 | | - if (fixedName) { |
50 | | - console.log(`Fixed package name: ${name} -> ${fixedName}`); |
51 | | - json = { ...json, name: fixedName }; |
52 | | - } |
53 | | - |
54 | | - // Add missing core dependency to packages that don't properly declare it |
55 | | - if (needsCoreDependency(name)) { |
56 | | - const deps = (json.dependencies as Record<string, string>) || {}; |
57 | | - if (!deps["hl7.fhir.r4.core"]) { |
58 | | - console.log(`Injecting hl7.fhir.r4.core dependency into ${name}`); |
59 | | - json = { |
60 | | - ...json, |
61 | | - dependencies: { ...deps, "hl7.fhir.r4.core": "4.0.1" }, |
62 | | - }; |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - return { ...ctx, kind: "package", packageJson: json }; |
67 | | -}; |
68 | | - |
69 | 4 | if (require.main === module) { |
70 | 5 | console.log("Generating Norge R4 types..."); |
71 | 6 |
|
72 | 7 | const builder = new APIBuilder({ |
73 | | - preprocessPackage, |
74 | | - registry: "https://packages.simplifier.net", |
| 8 | + canonicalManager: { |
| 9 | + registry: "https://packages.simplifier.net", |
| 10 | + patches: { |
| 11 | + packageJson: [ |
| 12 | + // Many Norge packages reference core types without declaring the dependency; |
| 13 | + // make every package (except core itself) depend on core. |
| 14 | + ensureDependency({ "hl7.fhir.r4.core": "4.0.1" }), |
| 15 | + // Fix known package name typo. |
| 16 | + renamePackage("simplifier.core.r4.rResources", "simplifier.core.r4.resources"), |
| 17 | + ], |
| 18 | + // gd-RelatedPerson widens patient to include Person, but base R4 RelatedPerson.patient |
| 19 | + // only allows Patient — narrow the Person targets back to Patient. |
| 20 | + fhirResource: [ |
| 21 | + inResource("http://ehelse.no/fhir/StructureDefinition/gd-RelatedPerson", [ |
| 22 | + replaceText( |
| 23 | + "http://hl7.org/fhir/StructureDefinition/Person", |
| 24 | + "http://hl7.org/fhir/StructureDefinition/Patient", |
| 25 | + ), |
| 26 | + replaceText( |
| 27 | + "http://hl7.no/fhir/StructureDefinition/no-basis-Person", |
| 28 | + "http://hl7.org/fhir/StructureDefinition/Patient", |
| 29 | + ), |
| 30 | + replaceText( |
| 31 | + "http://ehelse.no/fhir/StructureDefinition/gd-Person", |
| 32 | + "http://hl7.org/fhir/StructureDefinition/Patient", |
| 33 | + ), |
| 34 | + ]), |
| 35 | + ], |
| 36 | + }, |
| 37 | + }, |
75 | 38 | }) |
76 | 39 | .fromPackage("hl7.fhir.r4.core", "4.0.1") |
77 | 40 | .fromPackage("ehelse.fhir.no.grunndata", "2.3.5") |
|
0 commit comments