README.md

# fhir

## Type-safe FHIR resources and client

[![Package Version](https://img.shields.io/hexpm/v/fhir)](https://hex.pm/packages/fhir)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/fhir/)
![GitHub License](https://img.shields.io/github/license/potatoemr/gleam-fhir?color=%237A6276)

## Features
- Supports R4, R4B, R5
- Gleam types for FHIR data types and resources
- Primitive types to represent valid FHIR date, dateTime, instant, time
- JSON encoder and to_json functions
- Enums for valuesets with required binding
- Sans-io request/response for Create, Read, Update, Delete, Search
- Client layers over sans-io for easy use on Erlang target and Lustre apps
- Primitive extensions, optionally generated for each type
- Complex extensions, generically available on all types, and optionally generated to make specific extensions type safe

## Warning

!!! fhir@0.3.0 (current version) supports r4us ONLY; r4/r4b/r5 are not available, because of hex.pm package size limits. !!!

## Welcome

<div style="display: flex">
<img src="https://github.com/PotatoEMR/gleam-fhir/raw/refs/heads/main/docs/static/lucy.svg" title="Gleam Lucy" alt="Gleam Lucy" width="35%">
<img src="https://github.com/PotatoEMR/gleam-fhir/raw/refs/heads/main/docs/static/fhir.svg" title="HL7® FHIR® Flame" alt="HL7® FHIR Flame" width="35%">
</div>

[FHIR®](https://hl7.org/fhir/) (Fast Healthcare Interoperability Resources) is a standard for health care data exchange, published by HL7®. [Gleam](https://gleam.run/) is simple and type-safe, making FHIR resources features like cardinality and choice types easy to work with. This package provides FHIR resources with JSON serialization, and a FHIR REST client with standard interactions (Create, Read, etc.) and operations. Supports R4, R4B, R5. Supporting everything in FHIR would be nice but the main priority is making common things friendly.

Docs are friendly too! Click -> [Resources](https://hexdocs.pm/fhir/resources.html) & [Client](https://hexdocs.pm/fhir/clients.html) <-

## Quick Start

```sh
gleam new hello_fhir && cd hello_fhir && gleam add fhir
```
```gleam

//In hello_fhir.gleam
import fhir/r4
import fhir/r4_httpc
import fhir/r4_valuesets
import gleam/option.{Some}

pub fn main() {
  let joe =
    r4.Patient(
      ..r4.patient_new(),
      identifier: [
        r4.Identifier(
          ..r4.identifier_new(),
          system: Some("https://fhir.nhs.uk/Id/nhs-number"),
          value: Some("0123456789"),
        ),
      ],
      name: [
        r4.Humanname(
          ..r4.humanname_new(),
          given: ["Joe"],
          family: Some("Armstrong"),
        ),
      ],
      gender: Some(r4_valuesets.AdministrativegenderMale),
      marital_status: Some(
        r4.Codeableconcept(..r4.codeableconcept_new(), coding: [
          r4.Coding(
            ..r4.coding_new(),
            system: Some(
              "http://terminology.hl7.org/CodeSystem/v3-MaritalStatus",
            ),
            code: Some("M"),
            display: Some("Married"),
          ),
        ]),
      ),
    )

  echo joe

  let assert Ok(client) = r4_httpc.fhirclient_new("https://r4.smarthealthit.org/")

  let assert Ok(created) = r4_httpc.patient_create(joe, client)
  let assert Some(id) = created.id
  let assert Ok(read) = r4_httpc.patient_read(id, client)
  echo read
  let rip = r4.Patient(..read, deceased: Some(r4.PatientDeceasedBoolean(True)))
  let assert Ok(updated) = r4_httpc.patient_update(rip, client)
  echo updated
  let assert Ok(_) = r4_httpc.patient_delete(updated, client)
}
```
```sh
gleam run
```

## Modules

(for each of r4, r4b, r5)

- [r4_valuesets](https://hexdocs.pm/fhir/fhir/r4_valuesets.html) Enums for valuesets with required binding
  - [r4](https://hexdocs.pm/fhir/fhir/r4.html) Resources, data types, fns for new, decoder, to_json
    - [r4_sansio](https://hexdocs.pm/fhir/fhir/r4_sansio.html) Prepare http request and parse http response
      - Clients [r4_httpc](https://hexdocs.pm/fhir/fhir/r4_httpc.html) for httpc (Erlang) and [r4_rsvp](https://hexdocs.pm/fhir/fhir/r4_rsvp.html) for rsvp (Lustre apps)

## AI use

AI in parts of codegen (which creates Gleam code from FHIR data) and some tests. No AI in API design or documentation. 

## Other FHIR Docs

These pages provide only an intro to FHIR and the Gleam implementation. For comprehensive information, see the FHIR docs, e.g. for [R4 AllergyIntolerance](https://hl7.org/fhir/R4/allergyintolerance.html#resource).

Gleam FHIR is not a mature project. For an application with real users and regulations, you are probably better off with a mature FHIR SDK such as [https://docs.fire.ly/](https://docs.fire.ly/). These Gleam FHIR pages are modelled on their .NET SDK documentation.

[https://chat.fhir.org/](https://chat.fhir.org/) is a public chatroom with many FHIR implementers. If you have a question, there is a good chance someone has asked it there.