Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Created July 9, 2021 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdjkelly/55f2bedd34a86ec4d7afcc179d26cdaf to your computer and use it in GitHub Desktop.
Save jdjkelly/55f2bedd34a86ec4d7afcc179d26cdaf to your computer and use it in GitHub Desktop.
FHIR Client
/**
* @module client
* @remarks Almost no-dependency TS FHIR client (we use cross fetch to give us a fetch instance)
*/
import fetch from "cross-fetch";
type Summary = "true" | "false" | "text" | "count" | "data";
export default function(baseUrl: string, init: RequestInit) {
async function read(type: string, id: string, summary?: Summary) {
return fetch(`${baseUrl}/type/id`, {
method: "GET",
...init
});
}
async function vread(type: string, id: string, vid: string) {
return fetch(`${baseUrl}/${type}/${id}/_history/${vid}`, {
method: "GET",
...init
});
}
async function update(type: string, id: string, resource: fhir3.FhirResource | fhir4.FhirResource) {
return fetch(`${baseUrl}/${type}/${id}`, {
method: "PUT",
body: JSON.stringify(resource),
...init
});
}
async function patch(type: string, id: string, resource: fhir3.FhirResource | fhir4.FhirResource) {
return fetch(`${baseUrl}/${type}/${id}`, {
method: "PATCH",
body: JSON.stringify(resource),
...init
});
}
async function destroy(type: string, id: string) {
return fetch(`${baseUrl}/${type}/${id}`, {
method: "DELETE",
...init
});
}
async function search() {
throw new Error("Not Implemented");
}
async function create(type: string, id: string) {
return fetch(`${baseUrl}/${type}`, {
method: "POST",
...init
});
}
async function history() {
throw new Error("Not Implemented");
}
async function capabilities() {
return fetch(`${baseUrl}/metadata`, {
method: "GET",
...init
});
}
async function batch(bundle: fhir3.Bundle | fhir4.Bundle) {
return fetch(`${baseUrl}/`, {
method: "POST",
body: JSON.stringify(bundle),
...init
});
}
async function transaction(bundle: fhir3.Bundle | fhir4.Bundle) {
return fetch(`${baseUrl}/`, {
method: "POST",
body: JSON.stringify(bundle),
...init
});
}
return {
read,
vread,
update,
patch,
destroy,
search,
create,
history,
capabilities,
batch,
transaction
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment