This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compile Implementation Guides StructureDefinitions (#59)
- Loading branch information
Showing
12 changed files
with
292 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { StructureDefinitionImplementationGuides } from './index'; | ||
|
||
describe('StructureDefinitionImplementationGuides', () => { | ||
describe(`compile`, async () => { | ||
test(`valid StructureDefinition`, async () => { | ||
const compiled = new StructureDefinitionImplementationGuides().compile([ | ||
{ | ||
resourceType: 'StructureDefinition', | ||
id: 'CARIN-BB-Organization', | ||
url: 'http://hl7.org/fhir/us/carin/StructureDefinition/carin-bb-organization', | ||
version: '0.1.0', | ||
name: 'CARINBBOrganization', | ||
title: 'CARIN Blue Button Organization Profile', | ||
status: 'active', | ||
date: '2019-12-23T19:40:59+00:00', | ||
publisher: 'CARIN Alliance', | ||
description: | ||
'This profile builds on the USCoreOrganization Profile. It includes additional constraints relevant for the use cases addressed by this IG.', | ||
fhirVersion: '4.0.1', | ||
kind: 'resource', | ||
abstract: false, | ||
type: 'Organization', | ||
baseDefinition: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-organization', | ||
derivation: 'constraint', | ||
}, | ||
]); | ||
|
||
await expect(compiled).resolves.toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"baseDefinition": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-organization", | ||
"description": "This profile builds on the USCoreOrganization Profile. It includes additional constraints relevant for the use cases addressed by this IG.", | ||
"name": "CARINBBOrganization", | ||
"resourceType": "StructureDefinition", | ||
"type": "Organization", | ||
"url": "http://hl7.org/fhir/us/carin/StructureDefinition/carin-bb-organization", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test(`invalid StructureDefinition`, async () => { | ||
const compiled = new StructureDefinitionImplementationGuides().compile([ | ||
{ | ||
foo: 'bar', | ||
}, | ||
]); | ||
await expect(compiled).rejects.toThrowError(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ImplementationGuides } from 'fhir-works-on-aws-interface'; | ||
|
||
/** | ||
* Based on the FHIR StructuredDefinition. This type only includes the fields that are required for the compile process. | ||
* See: http://www.hl7.org/fhir/structuredefinition.html | ||
*/ | ||
export type FhirStructureDefinition = { | ||
resourceType: 'StructureDefinition'; | ||
url: string; | ||
name: string; | ||
description: string; | ||
baseDefinition: string; | ||
type: string; | ||
}; | ||
|
||
/** | ||
* This class compiles StructuredDefinitions from IG packages | ||
*/ | ||
export class StructureDefinitionImplementationGuides implements ImplementationGuides { | ||
/** | ||
* Compiles the contents of an Implementation Guide into an internal representation used to build the Capability Statement | ||
* | ||
* @param resources - an array of FHIR resources. See: https://www.hl7.org/fhir/profiling.html | ||
*/ | ||
// eslint-disable-next-line class-methods-use-this | ||
async compile(resources: any[]): Promise<any> { | ||
const validStructureDefinitions: FhirStructureDefinition[] = []; | ||
resources.forEach(s => { | ||
if (StructureDefinitionImplementationGuides.isFhirStructureDefinition(s)) { | ||
validStructureDefinitions.push(s); | ||
} else { | ||
throw new Error(`The following input is not a StructureDefinition: ${s.type} ${s.name}`); | ||
} | ||
}); | ||
|
||
return validStructureDefinitions.map((structureDefinition: any) => ({ | ||
name: structureDefinition.name, | ||
url: structureDefinition.url, | ||
type: structureDefinition.type, | ||
resourceType: structureDefinition.resourceType, | ||
description: structureDefinition.description, | ||
baseDefinition: structureDefinition.baseDefinition, | ||
})); | ||
} | ||
|
||
private static isFhirStructureDefinition(x: any): x is FhirStructureDefinition { | ||
return ( | ||
typeof x === 'object' && | ||
x && | ||
x.resourceType === 'StructureDefinition' && | ||
typeof x.url === 'string' && | ||
typeof x.name === 'string' && | ||
typeof x.description === 'string' && | ||
typeof x.baseDefinition === 'string' && | ||
typeof x.type === 'string' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export interface ResourceCapability { | ||
type: string; | ||
supportedProfile: string[]; | ||
} | ||
|
||
export interface ResourceCapabilityStatement { | ||
[resourceType: string]: ResourceCapability; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { FHIRStructureDefinitionRegistry } from './index'; | ||
|
||
const IGCompiledStructureDefinition = [ | ||
{ | ||
name: 'C4BBPatient', | ||
url: 'http://hl7.org/fhir/us/carin-bb/StructureDefinition/C4BB-Patient', | ||
type: 'Patient', | ||
resourceType: 'StructureDefinition', | ||
description: | ||
'This profile builds upon the US Core Patient profile. It is used to convey information about the patient who received the services described on the claim.', | ||
baseDefinition: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient', | ||
}, | ||
{ | ||
name: 'C4BBCoverage', | ||
url: 'http://hl7.org/fhir/us/carin-bb/StructureDefinition/C4BB-Coverage', | ||
type: 'Coverage', | ||
resourceType: 'StructureDefinition', | ||
description: | ||
'Data that reflect a payer’s coverage that was effective as of the date of service or the date of admission of the claim.', | ||
baseDefinition: 'http://hl7.org/fhir/StructureDefinition/Coverage', | ||
}, | ||
]; | ||
|
||
describe('FHIRStructureDefinitionRegistry', () => { | ||
test('getCapabilities snapshot', () => { | ||
expect(new FHIRStructureDefinitionRegistry(IGCompiledStructureDefinition).getCapabilities()) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"Coverage": Object { | ||
"supportedProfile": Array [ | ||
"http://hl7.org/fhir/us/carin-bb/StructureDefinition/C4BB-Coverage", | ||
], | ||
"type": "Coverage", | ||
}, | ||
"Patient": Object { | ||
"supportedProfile": Array [ | ||
"http://hl7.org/fhir/us/carin-bb/StructureDefinition/C4BB-Patient", | ||
], | ||
"type": "Patient", | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('getProfiles snapshot', () => { | ||
expect(new FHIRStructureDefinitionRegistry(IGCompiledStructureDefinition).getProfiles('Patient')) | ||
.toMatchInlineSnapshot(` | ||
Array [ | ||
"http://hl7.org/fhir/us/carin-bb/StructureDefinition/C4BB-Patient", | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { FhirStructureDefinition } from '../implementationGuides'; | ||
import { ResourceCapabilityStatement } from './ResourceCapabilityInterface'; | ||
|
||
/** | ||
* This class is the single authority over the supported FHIR StructuredDefinition and their definitions | ||
*/ | ||
export class FHIRStructureDefinitionRegistry { | ||
private readonly capabilityStatement: ResourceCapabilityStatement; | ||
|
||
constructor(compiledImplementationGuides?: any[]) { | ||
let compiledStructureDefinitions: FhirStructureDefinition[] = []; | ||
|
||
if (compiledImplementationGuides !== undefined) { | ||
compiledStructureDefinitions = [...compiledImplementationGuides]; | ||
} | ||
|
||
this.capabilityStatement = {}; | ||
|
||
compiledStructureDefinitions.forEach(compiledStructureDefinition => { | ||
const structuredDefinition = this.capabilityStatement[compiledStructureDefinition.type]; | ||
|
||
if (structuredDefinition) { | ||
this.capabilityStatement[compiledStructureDefinition.type].supportedProfile.push( | ||
compiledStructureDefinition.url, | ||
); | ||
} else { | ||
this.capabilityStatement[compiledStructureDefinition.type] = { | ||
type: compiledStructureDefinition.type, | ||
supportedProfile: [compiledStructureDefinition.url], | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Retrieve the profiles for a given resource type. Returns undefined if the parameter is not found on the registry. | ||
* @param resourceType FHIR resource type | ||
* @return a list of profiles | ||
*/ | ||
getProfiles(resourceType: string): string[] { | ||
return this.capabilityStatement[resourceType]?.supportedProfile ?? []; | ||
} | ||
|
||
/** | ||
* Retrieve a subset of the CapabilityStatement with the resource definitions | ||
* See https://www.hl7.org/fhir/capabilitystatement.html | ||
*/ | ||
getCapabilities(): ResourceCapabilityStatement { | ||
return this.capabilityStatement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.