@health-samurai/aidbox-client 0.0.0-alpha.1

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.
Files changed (71) hide show
  1. package/README.md +137 -0
  2. package/dist/src/client.d.ts +25 -0
  3. package/dist/src/client.d.ts.map +1 -0
  4. package/dist/src/client.js +981 -0
  5. package/dist/src/fhir-http.d.ts +72 -0
  6. package/dist/src/fhir-http.d.ts.map +1 -0
  7. package/dist/src/fhir-http.js +1 -0
  8. package/dist/src/fhir-types/hl7-fhir-r4-core/BackboneElement.d.ts +5 -0
  9. package/dist/src/fhir-types/hl7-fhir-r4-core/BackboneElement.d.ts.map +1 -0
  10. package/dist/src/fhir-types/hl7-fhir-r4-core/BackboneElement.js +5 -0
  11. package/dist/src/fhir-types/hl7-fhir-r4-core/Bundle.d.ts +53 -0
  12. package/dist/src/fhir-types/hl7-fhir-r4-core/Bundle.d.ts.map +1 -0
  13. package/dist/src/fhir-types/hl7-fhir-r4-core/Bundle.js +10 -0
  14. package/dist/src/fhir-types/hl7-fhir-r4-core/CodeableConcept.d.ts +10 -0
  15. package/dist/src/fhir-types/hl7-fhir-r4-core/CodeableConcept.d.ts.map +1 -0
  16. package/dist/src/fhir-types/hl7-fhir-r4-core/CodeableConcept.js +5 -0
  17. package/dist/src/fhir-types/hl7-fhir-r4-core/Coding.d.ts +15 -0
  18. package/dist/src/fhir-types/hl7-fhir-r4-core/Coding.d.ts.map +1 -0
  19. package/dist/src/fhir-types/hl7-fhir-r4-core/Coding.js +5 -0
  20. package/dist/src/fhir-types/hl7-fhir-r4-core/DomainResource.d.ts +10 -0
  21. package/dist/src/fhir-types/hl7-fhir-r4-core/DomainResource.d.ts.map +1 -0
  22. package/dist/src/fhir-types/hl7-fhir-r4-core/DomainResource.js +10 -0
  23. package/dist/src/fhir-types/hl7-fhir-r4-core/Element.d.ts +5 -0
  24. package/dist/src/fhir-types/hl7-fhir-r4-core/Element.d.ts.map +1 -0
  25. package/dist/src/fhir-types/hl7-fhir-r4-core/Element.js +5 -0
  26. package/dist/src/fhir-types/hl7-fhir-r4-core/Identifier.d.ts +20 -0
  27. package/dist/src/fhir-types/hl7-fhir-r4-core/Identifier.d.ts.map +1 -0
  28. package/dist/src/fhir-types/hl7-fhir-r4-core/Identifier.js +5 -0
  29. package/dist/src/fhir-types/hl7-fhir-r4-core/Meta.d.ts +17 -0
  30. package/dist/src/fhir-types/hl7-fhir-r4-core/Meta.d.ts.map +1 -0
  31. package/dist/src/fhir-types/hl7-fhir-r4-core/Meta.js +5 -0
  32. package/dist/src/fhir-types/hl7-fhir-r4-core/Narrative.d.ts +9 -0
  33. package/dist/src/fhir-types/hl7-fhir-r4-core/Narrative.d.ts.map +1 -0
  34. package/dist/src/fhir-types/hl7-fhir-r4-core/Narrative.js +5 -0
  35. package/dist/src/fhir-types/hl7-fhir-r4-core/OperationOutcome.d.ts +19 -0
  36. package/dist/src/fhir-types/hl7-fhir-r4-core/OperationOutcome.d.ts.map +1 -0
  37. package/dist/src/fhir-types/hl7-fhir-r4-core/OperationOutcome.js +10 -0
  38. package/dist/src/fhir-types/hl7-fhir-r4-core/Period.d.ts +9 -0
  39. package/dist/src/fhir-types/hl7-fhir-r4-core/Period.d.ts.map +1 -0
  40. package/dist/src/fhir-types/hl7-fhir-r4-core/Period.js +5 -0
  41. package/dist/src/fhir-types/hl7-fhir-r4-core/Reference.d.ts +14 -0
  42. package/dist/src/fhir-types/hl7-fhir-r4-core/Reference.d.ts.map +1 -0
  43. package/dist/src/fhir-types/hl7-fhir-r4-core/Reference.js +5 -0
  44. package/dist/src/fhir-types/hl7-fhir-r4-core/Resource.d.ts +14 -0
  45. package/dist/src/fhir-types/hl7-fhir-r4-core/Resource.d.ts.map +1 -0
  46. package/dist/src/fhir-types/hl7-fhir-r4-core/Resource.js +10 -0
  47. package/dist/src/fhir-types/hl7-fhir-r4-core/Signature.d.ts +20 -0
  48. package/dist/src/fhir-types/hl7-fhir-r4-core/Signature.d.ts.map +1 -0
  49. package/dist/src/fhir-types/hl7-fhir-r4-core/Signature.js +5 -0
  50. package/dist/src/fhir-types/hl7-fhir-r4-core/index.d.ts +19 -0
  51. package/dist/src/fhir-types/hl7-fhir-r4-core/index.d.ts.map +1 -0
  52. package/dist/src/fhir-types/hl7-fhir-r4-core/index.js +4 -0
  53. package/dist/src/index.d.ts +8 -0
  54. package/dist/src/index.d.ts.map +1 -0
  55. package/dist/src/index.js +4 -0
  56. package/dist/src/result.d.ts +21 -0
  57. package/dist/src/result.d.ts.map +1 -0
  58. package/dist/src/result.js +36 -0
  59. package/dist/src/types.d.ts +100 -0
  60. package/dist/src/types.d.ts.map +1 -0
  61. package/dist/src/types.js +155 -0
  62. package/dist/src/utils.d.ts +3 -0
  63. package/dist/src/utils.d.ts.map +1 -0
  64. package/dist/src/utils.js +215 -0
  65. package/dist/test/client.test.d.ts +2 -0
  66. package/dist/test/client.test.d.ts.map +1 -0
  67. package/dist/test/fhir-http.test.d.ts +2 -0
  68. package/dist/test/fhir-http.test.d.ts.map +1 -0
  69. package/dist/test/result.test.d.ts +2 -0
  70. package/dist/test/result.test.d.ts.map +1 -0
  71. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Typescript FHIR client
2
+
3
+ A typescript client for interacting with a FHIR server.
4
+
5
+ ## Usage
6
+
7
+ The client is created with the `makeClient` function:
8
+
9
+ ```typescript
10
+ const client = makeClient({ baseurl: "https://fhir-server.address" });
11
+ ```
12
+
13
+ This constructor accepts an additional `onResponse` option, which is a side-effect-only function, that recieves a copy of the Response from the server as is:
14
+
15
+ ```typescript
16
+ const client = makeClient({
17
+ baseurl: "https://fhir-server.address"
18
+ onResponse: (response: Response) => {
19
+ /* analyze Response, use throw to interrupt request early */
20
+ }
21
+ });
22
+ ```
23
+
24
+ ## Methods
25
+
26
+ The client provides two basic methods of interaction:
27
+
28
+ - `rawRequest` - send request to the FHIR server and recieve response in a raw format
29
+ - `request<T>` - send request to the FHIR server and recieve response with its body parsed to the specified type `T`
30
+
31
+ In successful case, the `rawRequest` returns an object with JavaScript Repsonse, and additional meta information.
32
+ When server responds with an error code, this function throws an error:
33
+
34
+ ```typescript
35
+ const result = await client.rawRequest({
36
+ method: "GET",
37
+ url: "/fhir/Patient/patient-id",
38
+ headers: {Accept: "application/json"},
39
+ params: [["some" "parameters"], ["if", "needed"]],
40
+ }).then((result) => {
41
+ const patient: Patient = await result.response.json();
42
+ // ...
43
+ }).catch ((error) => {
44
+ if (error instanceof ErrorResponse) {
45
+ const outcome = await error.responseWithMeta.response.json
46
+ // ...
47
+ }
48
+ });
49
+ ```
50
+
51
+ Alternatively, a `request` method can be used.
52
+ It returns a `Result<T, OperationOutcome>`, which contains an already parsed result, coerced to the specified type `T`.
53
+
54
+ ```typescript
55
+ const result: Result<Patient, OperationOutcome> = client.request<Patient>({
56
+ method: "GET",
57
+ url: "/fhir/Patient/patient-id",
58
+ headers: {Accept: "application/json"},
59
+ params: [["some" "parameters"], ["if", "needed"]],
60
+ });
61
+
62
+ if (result.isOk()) {
63
+ const patient: Patient = result.value.resource;
64
+ // work with patient
65
+ }
66
+
67
+ if (result.isErr()) {
68
+ const outcome: OperationOutcome = result.error.resource;
69
+ // process OperationOutcome
70
+ }
71
+ ```
72
+
73
+ Both methods can throw `RequestError` class, if the error happened before the request was actually made.
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:
113
+
114
+ ```typescript
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 })
118
+
119
+ const { resource: patient } = result.value;
120
+
121
+ // work with patient.
122
+ ```
123
+
124
+ Unwrapping is not required to modify the data in the result:
125
+
126
+ ```typescript
127
+ const result = await client.read<Patient>({type: 'Patient', id: 'patient-id'});
128
+
129
+ return result
130
+ .map(({resource}: {resource: Patient}): Patient => {
131
+ /* work with Patient resource */
132
+ })
133
+ .mapErr(({resource}: {resource: OperationOutcome}): OperationOutcome => {
134
+ /* work with OperationOutcome resource */
135
+ });
136
+ // result is still Result<Patient, OperationOutcome>
137
+ ```
@@ -0,0 +1,25 @@
1
+ import type { ClientParams, FhirServerClient } from "./types";
2
+ /**
3
+ * Create a client to the FHIR server.
4
+ *
5
+ * ```typescript
6
+ * const client = makeClient({ baseurl: "https://fhir-server.address" })
7
+ *
8
+ * // alternatively, specify different FHIR types:
9
+ * import type { Bundle, OperationOutcome } from "hl7-fhir-r5-core";
10
+ * const client = makeClient<Bundle, OperationOutcome>({ baseurl: "https://fhir-server.address" })
11
+ * ```
12
+ *
13
+ * Main client functions are `request` for typed interactions, and `rawRequest` for manual response processing.
14
+ *
15
+ * This client also provides a set of convenience methods for accessing FHIR operations:
16
+ *
17
+ * - `read`
18
+ * - `search`
19
+ * - `create`
20
+ * - `operation`
21
+ * - `validate`
22
+ *
23
+ */
24
+ export declare function makeClient<TBundle, TOperationOutcome, TUser>({ baseurl, onResponse, }: ClientParams): FhirServerClient<TBundle, TOperationOutcome, TUser>;
25
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EACX,YAAY,EACZ,gBAAgB,EAIhB,MAAM,SAAS,CAAC;AAgBjB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,EAC7D,OAAO,EACP,UAAsB,GACtB,EAAE,YAAY,GAAG,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,CAAC,CA+dpE"}