Typescript FHIR R4 Server

GEOFFREY BAUDIN
3 min readFeb 19, 2020

Why Typescript ?

You’re right, it already exists a javascript FHIR Server, it’s developped by Asymmetrik. Project is really well done, but a lot of people that want to work with types like I do, may encounter difficulties. Plus, as I already published a typings library for FHIR, see here, I thought it would be a great idea to use it in a real project.

Instead of creating a library with few values from the original once but typings, I decided to have a more modern approach. Thus, I took the same path that Angular uses.

How things work

If you are familiar with Angular framework, you will understand quickly how thing works.

1. Implement the server

A base class is present to help you to quickly define your own server. I mean:

  • Add custom middleware
  • Add additional routes to FHIR server
  • Parametrize stuff

But in the most basic use, you have to define a server object as following

2. Implement FHIR resources class

Now we can create classes to handle a FHIR requests against specific resource

The search Handler is there to allow you searching every Patient resources in your data sources. But it exists actions for each requirements from FHIR standard: search / create / update / remove / patch / history

3. Add decorators

Now, to connect things together, let’s add some decorators on our resource class to tell the library how to use it.

In the following example I will add Capability decorator to my object to:

  • define a search capability in server’s metadata
  • set kind of FHIR resource this object will handle
  • define search parameters that should be used with this resource

We also decorate our server to let it know about resources we would like to manage

4. Bootstrap the server

Finally we call a bootstrapping function that will set up the Asymmetrik server for us. It will dynamically create our server metadata and make routes available for managed resources.

That’s all, we now have a Typescript server ready to work. Navigate to server’s metadata (http://localhost:3001/4_0_0/metadata) and you should see something like:

{
"resourceType": "CapabilityStatement",
"status": "active",
"date": "2020-02-19T17:46:33-05:00",
"publisher": "Not provided",
"kind": "instance",
"software": {
"name": "FHIR Server",
"version": "1.4.0"
},
"implementation": {
"description": "FHIR Test Server (R4)"
},
"fhirVersion": "4.0.0",
"format": [
"application/fhir+json"
],
"rest": [
{
"mode": "server",
"resource": [
{
"type": "patient",
"profile": {
"reference": "http://hl7.org/fhir/patient.html"
},
"interaction": [
{
"code": "search-type"
}
],
"searchParam": [
{
"name": "identifier",
"definition": "http://hl7.org/fhir/SearchParameter/Patient-identifier",
"type": "token"
},
{
"name": "name",
"definition": "http://hl7.org/fhir/SearchParameter/Patient-name",
"type": "string"
}
]
}
]
}
]
}

You can find the complete sample code in repository.

Please tests it from npmjs !

--

--

GEOFFREY BAUDIN

Working at ‘Hopital du Valais’ in Switzerland, we are trying to improve our Information System using GraphQL, Typescript, Docker, MicroServices, FHIR …