fhirbuilder
v1.6.0
Published
Another FHIR Tool to build FHIR Resources
Downloads
377
Maintainers
Readme
Available Resources
Resources available in this package
| Resource | Resource | Resource | | ------------------------------------------------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------------- | | EpisodeOfCare | Organization | Patient | | Observation | Coverage | ServiceRequest | | AllergyIntolerance | Procedure | Bundle |
Install
npm i fhirbuilder
# for fhirtypes
npm i fhirtypes
Use
Create context
import { contextR4 } from 'fhirbuilder';
const r4Context = contextR4();
From r4Context
Object you can access to all FHIR Datatypes, Resources models and Validators
Using models
In this example we will use and Address model datatype
import { contextR4 } from 'fhirbuilder';
const { Address } = contextR4();
//you can create a new Instance
const address = new Address({
use: 'home',
type: 'postal',
text: 'test',
line: ['line1', 'line2'],
});
console.log(address);
Output
{
"use": "home",
"type": "postal",
"text": "test",
"line": ["line1", "line2"]
}
Using builders
For complex object you can use a builders
import { contextR4 } from 'fhirbuilder';
import { AdministrativeGenderEnum } from 'fhirtypes/dist/r4/enums';
const { Patient, HumanName, Address, PatientBuilder } = contextR4();
const patient = new PatientBuilder()
.setId('123')
.setActive(true)
.addName(
new HumanName({
family: 'Doe',
given: ['John'],
}),
)
.setGender(AdministrativeGenderEnum.MALE)
.addAddress(
new Address({
use: 'home',
line: ['123 Main St'],
city: 'Somewhere',
state: 'CA',
postalCode: '12345',
country: 'USA',
}),
)
.build();
Output
{
"id": "123",
"resourceType": "Patient",
"active": true,
"name": [
{
"family": "Doe",
"given": ["John"]
}
],
"gender": "male",
"address": [
{
"use": "home",
"line": ["123 Main St"],
"city": "Somewhere",
"state": "CA",
"postalCode": "12345",
"country": "USA"
}
]
}
Using Validators
const { Patient } = contextR4();
const identifier = new Identifier({
assigner: {
reference: 'Acme Healthcare', // ❌ invalid reference
},
});
const patient = new Patient({
id: '123',
active: true,
identifier: [identifier],
});
const result = patient.validate();
console.log(result);
Output error
{
"isValid": false,
"operationOutcome": {
"issue": [
{
"code": "invalid",
"details": {
"text": "Path: Patient.identifier[0].assigner.reference. Value: Acme Healthcare"
},
"diagnostics": "Invalid reference format. Reference must be in the format 'ResourceType/ResourceId'.",
"severity": "error"
}
]
}
}