@health-samurai/aidbox-client 0.0.0-alpha.1 → 0.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +233 -74
- package/dist/src/auth-providers.d.ts +23 -0
- package/dist/src/auth-providers.d.ts.map +1 -0
- package/dist/src/auth-providers.js +322 -0
- package/dist/src/client.d.ts +508 -11
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +1473 -759
- package/dist/src/fhir-types/hl7-fhir-r4-core/Address.d.ts +26 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Address.d.ts.map +1 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Address.js +5 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/ContactPoint.d.ts +16 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/ContactPoint.d.ts.map +1 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/ContactPoint.js +5 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/DomainResource.d.ts +1 -1
- package/dist/src/fhir-types/hl7-fhir-r4-core/DomainResource.d.ts.map +1 -1
- package/dist/src/fhir-types/hl7-fhir-r4-core/HumanName.d.ts +20 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/HumanName.d.ts.map +1 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/HumanName.js +5 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Patient.d.ts +58 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Patient.d.ts.map +1 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Patient.js +10 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/Resource.d.ts +1 -1
- package/dist/src/fhir-types/hl7-fhir-r4-core/Resource.d.ts.map +1 -1
- package/dist/src/fhir-types/hl7-fhir-r4-core/index.d.ts +5 -0
- package/dist/src/fhir-types/hl7-fhir-r4-core/index.d.ts.map +1 -1
- package/dist/src/fhir-types/hl7-fhir-r4-core/index.js +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/result.d.ts +114 -3
- package/dist/src/result.d.ts.map +1 -1
- package/dist/src/result.js +6 -2
- package/dist/src/types.d.ts +110 -66
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +10 -2
- package/package.json +48 -46
- package/dist/src/fhir-http.d.ts +0 -72
- package/dist/src/fhir-http.d.ts.map +0 -1
- package/dist/src/fhir-http.js +0 -1
package/README.md
CHANGED
|
@@ -1,35 +1,214 @@
|
|
|
1
1
|
# Typescript FHIR client
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A TypeScript client for interacting with a FHIR server.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
7
|
The client is created with the `makeClient` function:
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
|
-
const
|
|
10
|
+
const baseUrl = "https://fhir-server.address";
|
|
11
|
+
const client = new AidboxClient(
|
|
12
|
+
baseUrl,
|
|
13
|
+
new BrowserAuthProvider(baseUrl),
|
|
14
|
+
);
|
|
11
15
|
```
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
## Documentation
|
|
18
|
+
|
|
19
|
+
Documentation is generated automatically, and can be found [here](https://healthsamurai.github.io/aidbox-ts-sdk/aidbox-client/).
|
|
20
|
+
|
|
21
|
+
## Type Generator
|
|
22
|
+
|
|
23
|
+
This project is designed around the type generator that provides FHIR types based on the specified package.
|
|
24
|
+
However, not all types are provided in the client itself, only the necessary ones, like `Bundle`, and `OperationOutcome`.
|
|
25
|
+
If your application requires more types, use [atomic-ehr/codegen](https://github.com/atomic-ehr/codegen) to generate more types.
|
|
26
|
+
|
|
27
|
+
For example, using `atomic-ehr/codegen`, we can generate and import an `Observation` type, and ensure that all fields are provided when creating a resource:
|
|
14
28
|
|
|
15
29
|
```typescript
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
import type { Observation } from "hl7-fhir-r4-core";
|
|
31
|
+
|
|
32
|
+
client.create<Observation>({
|
|
33
|
+
resourceType: "Observation",
|
|
34
|
+
status: "final",
|
|
35
|
+
code: {
|
|
36
|
+
coding: [{
|
|
37
|
+
system: "http://loinc.org",
|
|
38
|
+
code: "59408-5",
|
|
39
|
+
display: "Blood pressure systolic & diastolic"
|
|
40
|
+
}],
|
|
41
|
+
text: "Blood pressure"
|
|
42
|
+
},
|
|
43
|
+
subject: {
|
|
44
|
+
reference: "Patient/pt-1"
|
|
45
|
+
},
|
|
46
|
+
effectiveDateTime: "2025-12-05T00:00:00Z",
|
|
47
|
+
valueString: "minimal"
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The default set of types in the client is based on FHIR R4 Core.
|
|
52
|
+
If your application requires a different set of types, it is possible to override that through type parameters when creating a client:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import type * as R5 from "hl7-fhir-r5-core";
|
|
56
|
+
import type { User } from "@health-samurai/aidbox-client";
|
|
57
|
+
|
|
58
|
+
const baseUrl = "https://fhir-server.address";
|
|
59
|
+
|
|
60
|
+
const client = new AidboxClient<R5.Bundle, R5.OperationOutcome, User> (
|
|
61
|
+
baseUrl,
|
|
62
|
+
new BrowserAuthProvider(baseUrl),
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## [FHIR Interactions](https://hl7.org/fhir/http.html)
|
|
67
|
+
|
|
68
|
+
This client provides a set of methods to work with a FHIR server in a more convenient way:
|
|
69
|
+
|
|
70
|
+
- Instance Level Interaction
|
|
71
|
+
- `read` - Read the current state of the resource
|
|
72
|
+
- `vread` - Read the state of a specific version of the resource
|
|
73
|
+
- `update` - Update an existing resource by its id (or create it if it is new)
|
|
74
|
+
- `conditionalUpdate` - Update an existing resource based on some identification criteria (or create it if it is new).
|
|
75
|
+
- `patch` - Update an existing resource by posting a set of changes to it.
|
|
76
|
+
- `conditionalPatch` - Update an existing resource, based on some identification criteria, by posting a set of changes to it.
|
|
77
|
+
- `delete` - Delete a resource.
|
|
78
|
+
- `deleteHistory` - Delete all historical versions of a resource.
|
|
79
|
+
- `deleteHistoryVersion` - Delete a specific version of a resource.
|
|
80
|
+
- `history` - Retrieve the change history for a particular resource.
|
|
81
|
+
- Type Level Interaction
|
|
82
|
+
- `create` - Create a new resource with a server assigned id
|
|
83
|
+
- `conditionalCreate` - Create a new resource with a server assigned id if an equivalent resource does not already exist.
|
|
84
|
+
- `search` - Search the resource type based on some filter criteria.
|
|
85
|
+
- `conditionalDelete` - Conditional delete a single or multiple resources based on some identification criteria.
|
|
86
|
+
- `history` - Retrieve the change history for a particular resource type.
|
|
87
|
+
- Whole System Interaction
|
|
88
|
+
- `capabilities` - Get a capability statement for the system.
|
|
89
|
+
- `batch`/`transaction` - Perform multiple interactions (e.g., create, read, update, delete, patch, and/or [extended operations]) in a single interaction.
|
|
90
|
+
- `delete` - Conditional Delete across all resource types based on some filter criteria.
|
|
91
|
+
- `history` - Retrieve the change history for all resources.
|
|
92
|
+
- `search` - Search across all resource types based on some filter criteria.
|
|
93
|
+
- Compartment Interaction
|
|
94
|
+
- `search` - Search resources associated with a specific compartment instance (see [Search Contexts](https://build.fhir.org/search.html#searchcontexts) and [Compartments](https://build.fhir.org/compartmentdefinition.html))
|
|
95
|
+
- Operations Framework
|
|
96
|
+
- `operation` - Perform an operation as defined by an `OperationDefinition`.
|
|
97
|
+
- `validate` - Perform the Validate Operation.
|
|
98
|
+
|
|
99
|
+
### Patient CRUD Example
|
|
100
|
+
|
|
101
|
+
Here's an example of
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { AidboxClient, BrowserAuthProvider } from "@health-samurai/aidbox-client";
|
|
105
|
+
import type { Patient } from "hl7-fhir-r4-core";
|
|
106
|
+
import { formatOperationOutcome } from "utils";
|
|
107
|
+
|
|
108
|
+
const client = new AidboxClient(
|
|
109
|
+
"http://localhost:8080",
|
|
110
|
+
new BrowserAuthProvider("http://localhost:8080"),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// Create a new Patient resource
|
|
114
|
+
const result = await client.create<Patient>({
|
|
115
|
+
type: "Patient",
|
|
116
|
+
resource: {
|
|
117
|
+
gender: "female",
|
|
118
|
+
resourceType: "Patient",
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Check if interaction was successful
|
|
123
|
+
if (result.isErr())
|
|
124
|
+
throw Error(formatOperationOutcome(result.value.resource), {
|
|
125
|
+
cause: result.value.resource,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const patient = result.value.resource;
|
|
129
|
+
|
|
130
|
+
if (!patient.id)
|
|
131
|
+
throw Error(
|
|
132
|
+
"id is optional in FHIR, so we check it to satisfy the type checker",
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
// Updating the patient
|
|
136
|
+
|
|
137
|
+
patient.name = [
|
|
138
|
+
{
|
|
139
|
+
given: ["Jane"],
|
|
140
|
+
family: "Doe",
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
const updateResult = await client.update<Patient>({
|
|
145
|
+
id: patient.id,
|
|
146
|
+
type: "Patient",
|
|
147
|
+
resource: patient,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (updateResult.isErr())
|
|
151
|
+
throw Error(formatOperationOutcome(updateResult.value.resource), {
|
|
152
|
+
cause: updateResult.value.resource,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Deleting the patient
|
|
156
|
+
|
|
157
|
+
const deleteResult = await client.delete<Patient>({
|
|
158
|
+
id: patient.id,
|
|
159
|
+
type: "Patient",
|
|
21
160
|
});
|
|
161
|
+
|
|
162
|
+
if (deleteResult.isErr())
|
|
163
|
+
throw Error(formatOperationOutcome(deleteResult.value.resource), {
|
|
164
|
+
cause: deleteResult.value.resource,
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Return data format
|
|
169
|
+
|
|
170
|
+
As seen in the example above, most methods return a `Result<T, E>` object.
|
|
171
|
+
This object represents a successful or erroneous state of the response.
|
|
172
|
+
|
|
173
|
+
A general usage pattern is as follows:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const result = await client.read<Patient>({ type: 'Patient', id: 'patient-id' });
|
|
177
|
+
|
|
178
|
+
if (result.isErr())
|
|
179
|
+
throw new Error("error reading Patient", { cause: result.value.resource })
|
|
180
|
+
|
|
181
|
+
const patient = result.value.resource;
|
|
182
|
+
|
|
183
|
+
// work with patient.
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
It is also possible to work with resources without unwrapping the `Result` object:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const result = await client.read<Patient>({ type: 'Patient', id: 'patient-id' });
|
|
190
|
+
|
|
191
|
+
return result
|
|
192
|
+
.map(({resource}: {resource: Patient}): Patient => {
|
|
193
|
+
/* work with Patient resource */
|
|
194
|
+
})
|
|
195
|
+
.mapErr(({resource}: {resource: OperationOutcome}): OperationOutcome => {
|
|
196
|
+
/* work with OperationOutcome resource */
|
|
197
|
+
});
|
|
198
|
+
// result is still Result<Patient, OperationOutcome>
|
|
22
199
|
```
|
|
23
200
|
|
|
24
|
-
|
|
201
|
+
See the [documentation](https://healthsamurai.github.io/aidbox-ts-sdk/aidbox-client/) for more info.
|
|
25
202
|
|
|
26
|
-
|
|
203
|
+
## Low-level methods
|
|
27
204
|
|
|
28
|
-
|
|
29
|
-
- `request<T>` - send request to the FHIR server and recieve response with its body parsed to the specified type `T`
|
|
205
|
+
The client provides two basic methods for writing custom interactions:
|
|
30
206
|
|
|
31
|
-
|
|
32
|
-
|
|
207
|
+
- `rawRequest` - send request to the FHIR server and receive response in a raw format
|
|
208
|
+
- `request<T>` - send request to the FHIR server and receive response with its body parsed to the specified type `T`
|
|
209
|
+
|
|
210
|
+
In a successful case, the `rawRequest` returns an object with JavaScript Response and additional meta information.
|
|
211
|
+
When the server responds with an error code, this function throws an error:
|
|
33
212
|
|
|
34
213
|
```typescript
|
|
35
214
|
const result = await client.rawRequest({
|
|
@@ -40,7 +219,7 @@ const result = await client.rawRequest({
|
|
|
40
219
|
}).then((result) => {
|
|
41
220
|
const patient: Patient = await result.response.json();
|
|
42
221
|
// ...
|
|
43
|
-
}).catch
|
|
222
|
+
}).catch((error) => {
|
|
44
223
|
if (error instanceof ErrorResponse) {
|
|
45
224
|
const outcome = await error.responseWithMeta.response.json
|
|
46
225
|
// ...
|
|
@@ -48,7 +227,7 @@ const result = await client.rawRequest({
|
|
|
48
227
|
});
|
|
49
228
|
```
|
|
50
229
|
|
|
51
|
-
Alternatively,
|
|
230
|
+
Alternatively, the `request` method can be used.
|
|
52
231
|
It returns a `Result<T, OperationOutcome>`, which contains an already parsed result, coerced to the specified type `T`.
|
|
53
232
|
|
|
54
233
|
```typescript
|
|
@@ -65,73 +244,53 @@ if (result.isOk()) {
|
|
|
65
244
|
}
|
|
66
245
|
|
|
67
246
|
if (result.isErr()) {
|
|
68
|
-
const outcome: OperationOutcome = result.
|
|
247
|
+
const outcome: OperationOutcome = result.value.resource;
|
|
69
248
|
// process OperationOutcome
|
|
70
249
|
}
|
|
71
250
|
```
|
|
72
251
|
|
|
73
|
-
Both methods can throw `RequestError` class
|
|
74
|
-
|
|
75
|
-
### [FHIR HTTP](https://hl7.org/fhir/http.html) methods:
|
|
76
|
-
|
|
77
|
-
Additional set of methods is provided to work with FHIR server in a more convinient way:
|
|
78
|
-
|
|
79
|
-
- [x] Instance Level Interaction
|
|
80
|
-
- [x] `read` - Read the current state of the resource
|
|
81
|
-
- [x] `vread` - Read the state of a specific version of the resource
|
|
82
|
-
- [x] `update` - Update an existing resource by its id (or create it if it is new)
|
|
83
|
-
- [x] `conditionalUpdate` - Update an existing resource based on some identification criteria (or create it if it is new)
|
|
84
|
-
- [x] `patch` - Update an existing resource by posting a set of changes to it
|
|
85
|
-
- [x] `conditionalPatch` - Update an existing resource, based on some identification criteria, by posting a set of changes to it
|
|
86
|
-
- [x] `delete` - Delete a resource
|
|
87
|
-
- [x] `deleteHistory` - Delete all historical versions of a resource
|
|
88
|
-
- [x] `deleteHistoryVersion` - Delete a specific version of a resource
|
|
89
|
-
- [x] `history` - Retrieve the change history for a particular resource
|
|
90
|
-
- [x] Type Level Interaction
|
|
91
|
-
- [x] `create` - Create a new resource with a server assigned id
|
|
92
|
-
- [x] `conditionalCreate` - Create a new resource with a server assigned id if an equivalent resource does not already exist
|
|
93
|
-
- [x] `search` - Search the resource type based on some filter criteria
|
|
94
|
-
- [x] `conditionalDelete` - Conditional delete a single or multiple resources based on some identification criteria
|
|
95
|
-
- [x] `history` - Retrieve the change history for a particular resource type
|
|
96
|
-
- [x] Whole System Interaction
|
|
97
|
-
- [x] `capabilities` - Get a capability statement for the system
|
|
98
|
-
- [x] `batch`/`transaction` - Perform multiple interactions (e.g., create, read, update, delete, patch, and/or [extended operations]) in a single interaction
|
|
99
|
-
- [x] `delete` - Conditional Delete across all resource types based on some filter criteria
|
|
100
|
-
- [x] `history` - Retrieve the change history for all resources
|
|
101
|
-
- [x] `search` - Search across all resource types based on some filter criteria
|
|
102
|
-
- [x] Compartment Interaction
|
|
103
|
-
- [x] `search` - Search resources associated with a specific compartment instance (see [Search Contexts](https://build.fhir.org/search.html#searchcontexts) and [Compartments](https://build.fhir.org/compartmentdefinition.html))
|
|
104
|
-
|
|
105
|
-
<!--
|
|
106
|
-
TODO: Operations
|
|
107
|
-
https://build.fhir.org/operations.html
|
|
108
|
-
-->
|
|
109
|
-
|
|
110
|
-
## Return data format
|
|
111
|
-
|
|
112
|
-
Most client methods return a `Result<T, E>` object, with methods to check if the request was successful:
|
|
252
|
+
Both methods can throw the `RequestError` class if the error happened before the request was actually made.
|
|
113
253
|
|
|
114
|
-
|
|
115
|
-
const result = await client.read<Patient>({type: 'Patient', id: 'patient-id'});
|
|
116
|
-
if (result.isErr())
|
|
117
|
-
throw new Error("error reading Patient", { cause: result.error })
|
|
254
|
+
## Authentication Providers
|
|
118
255
|
|
|
119
|
-
|
|
256
|
+
Authentication is managed via the `AuthProvider` interface.
|
|
120
257
|
|
|
121
|
-
|
|
122
|
-
|
|
258
|
+
Currently, the client only provides a `BrowserAuthProvider` class.
|
|
259
|
+
It is suitable for usage in browsers, but other environments may require a different method.
|
|
123
260
|
|
|
124
|
-
|
|
261
|
+
Thus, an application can describe its own Auth Provider by implementing a class that implements `AuthProvider`:
|
|
125
262
|
|
|
126
263
|
```typescript
|
|
127
|
-
|
|
264
|
+
import type { AuthProvider } from "@health-samurai/aidbox-client";
|
|
128
265
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
266
|
+
export class CustomAuthProvider implements AuthProvider {
|
|
267
|
+
public baseUrl: string;
|
|
268
|
+
|
|
269
|
+
constructor(baseUrl: string) {
|
|
270
|
+
this.baseUrl = baseUrl;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
public async establishSession() {
|
|
274
|
+
/* code to establish a session */
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
public async revokeSession() {
|
|
278
|
+
/* code to revoke the session */
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
public async fetch(
|
|
282
|
+
input: RequestInfo | URL,
|
|
283
|
+
init?: RequestInit,
|
|
284
|
+
): Promise<Response> {
|
|
285
|
+
/**
|
|
286
|
+
* A wrapper around the `fetch` function, that does all the
|
|
287
|
+
* necessary preparations and argument patching required for the
|
|
288
|
+
* request to go through.
|
|
289
|
+
*
|
|
290
|
+
* Optionally, security checks can be implemented, like verifying
|
|
291
|
+
* that the request indeed goes to the `baseUrl`, and not
|
|
292
|
+
* somewhere else.
|
|
293
|
+
*/
|
|
294
|
+
}
|
|
295
|
+
}
|
|
137
296
|
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AuthProvider } from "./types";
|
|
2
|
+
export declare class BrowserAuthProvider implements AuthProvider {
|
|
3
|
+
#private;
|
|
4
|
+
/** @ignore */
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
constructor(baseUrl: string);
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the session is already authenticated, and if not, redirects to the login page.
|
|
9
|
+
*/
|
|
10
|
+
establishSession(): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Sends a POST request to `baseurl/auth/logout`.
|
|
13
|
+
*/
|
|
14
|
+
revokeSession(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* A thin wrapper around `fetch` function.
|
|
17
|
+
* Checks if the client is authorized to perform a request, and redirects to a login page if not.
|
|
18
|
+
*
|
|
19
|
+
* Accepts the same arguments as `fetch`.
|
|
20
|
+
*/
|
|
21
|
+
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=auth-providers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-providers.d.ts","sourceRoot":"","sources":["../../src/auth-providers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,qBAAa,mBAAoB,YAAW,YAAY;;IACvD,cAAc;IACP,OAAO,EAAE,MAAM,CAAC;gBAEX,OAAO,EAAE,MAAM;IAgB3B;;OAEG;IACU,gBAAgB;IAQ7B;;OAEG;IACU,aAAa;IAW1B;;;;;OAKG;IACU,KAAK,CACjB,KAAK,EAAE,WAAW,GAAG,GAAG,EACxB,IAAI,CAAC,EAAE,WAAW,GAChB,OAAO,CAAC,QAAQ,CAAC;CAsBpB"}
|