@haste-health/client 0.15.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 +228 -0
- package/lib/http/index.d.ts +29 -0
- package/lib/http/index.js +566 -0
- package/lib/http/index.js.map +1 -0
- package/lib/index.d.ts +39 -0
- package/lib/index.js +341 -0
- package/lib/index.js.map +1 -0
- package/lib/interface.d.ts +38 -0
- package/lib/interface.js +2 -0
- package/lib/interface.js.map +1 -0
- package/lib/middleware/index.d.ts +15 -0
- package/lib/middleware/index.js +34 -0
- package/lib/middleware/index.js.map +1 -0
- package/lib/types/index.d.ts +225 -0
- package/lib/types/index.js +2 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/r4.d.ts +257 -0
- package/lib/types/r4.js +2 -0
- package/lib/types/r4.js.map +1 -0
- package/lib/types/r4b.d.ts +257 -0
- package/lib/types/r4b.js +2 -0
- package/lib/types/r4b.js.map +1 -0
- package/lib/types/utilities.d.ts +40 -0
- package/lib/types/utilities.js +10 -0
- package/lib/types/utilities.js.map +1 -0
- package/lib/url.d.ts +51 -0
- package/lib/url.js +83 -0
- package/lib/url.js.map +1 -0
- package/package.json +45 -0
- package/src/http/index.ts +673 -0
- package/src/index.ts +555 -0
- package/src/interface.ts +203 -0
- package/src/middleware/index.test.ts +120 -0
- package/src/middleware/index.ts +95 -0
- package/src/types/index.ts +373 -0
- package/src/types/utilities.ts +62 -0
- package/src/url.test.ts +52 -0
- package/src/url.ts +141 -0
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# HasteHealth Client
|
|
2
|
+
|
|
3
|
+
HTTPClient and classes + middleware to define your own clients to conform to the FHIR specification.
|
|
4
|
+
|
|
5
|
+
## Client Types
|
|
6
|
+
|
|
7
|
+
Clients are built using middleware. To define a client you create a middleware handler and pass it in as an argument to either the SynchronousClient, AsynchronousClient constructor.
|
|
8
|
+
|
|
9
|
+
## Middleware
|
|
10
|
+
|
|
11
|
+
### Arguments
|
|
12
|
+
|
|
13
|
+
Middleware is built using an array of functions each function is passed the following arguments:
|
|
14
|
+
|
|
15
|
+
| Name | Description | type |
|
|
16
|
+
| ---------- | ------------------------------------------------------ | ----------- |
|
|
17
|
+
| request | A FHIRRequest object information varies based on type. | FHIRRequest |
|
|
18
|
+
| args | Object contains state and ctx | state,ctx |
|
|
19
|
+
| args.state | Internal state of the middleware. | unknown |
|
|
20
|
+
| args.ctx | Context of the call (will vary based on client). | unknown |
|
|
21
|
+
|
|
22
|
+
### Response
|
|
23
|
+
|
|
24
|
+
Middleware should return the following object:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
{
|
|
28
|
+
ctx: CTX;
|
|
29
|
+
state: State;
|
|
30
|
+
response: FHIRResponse;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Depending on whether client is asynchronous or syncrhonous will either be the object directly or a promise resolving to this object.
|
|
35
|
+
|
|
36
|
+
### Client
|
|
37
|
+
|
|
38
|
+
Constructor that allows you to create asynchronous FHIR clients.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import {
|
|
42
|
+
createMiddlewareAsync,
|
|
43
|
+
MiddlewareAsync,
|
|
44
|
+
} from "@haste-health/client/lib/middleware/index.js";
|
|
45
|
+
import { AsynchronousClient } from "@haste-health/client/lib/index.js";
|
|
46
|
+
|
|
47
|
+
return new AsynchronousClient<StateType, CTX>(
|
|
48
|
+
initialState,
|
|
49
|
+
createMiddlewareAsync<State, CTX>(middlewarefunctions)
|
|
50
|
+
);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## HTTPClient
|
|
54
|
+
|
|
55
|
+
We provide an HTTP client by default that allows you to call FHIR servers via API calls.
|
|
56
|
+
Supports all the method calls within the AsynchronousClient interface:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
export interface FHIRClientAsync<CTX> {
|
|
60
|
+
request(ctx: CTX, request: FHIRRequest): Promise<FHIRResponse>;
|
|
61
|
+
capabilities<FHIRVersion extends FHIR_VERSION>(
|
|
62
|
+
ctx: CTX,
|
|
63
|
+
fhirVersion: FHIRVersion
|
|
64
|
+
): Promise<Resource<FHIRVersion, "CapabilityStatement">>;
|
|
65
|
+
search_system<FHIRVersion extends FHIR_VERSION>(
|
|
66
|
+
ctx: CTX,
|
|
67
|
+
fhirVersion: FHIRVersion,
|
|
68
|
+
parameters: ParsedParameter<string | number>[] | string
|
|
69
|
+
): Promise<{
|
|
70
|
+
total?: number;
|
|
71
|
+
resources: Resource<FHIRVersion, AllResourceTypes>[];
|
|
72
|
+
}>;
|
|
73
|
+
search_type<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
74
|
+
ctx: CTX,
|
|
75
|
+
fhirVersion: FHIRVersion,
|
|
76
|
+
type: T,
|
|
77
|
+
parameters: ParsedParameter<string | number>[] | string
|
|
78
|
+
): Promise<{
|
|
79
|
+
total?: number;
|
|
80
|
+
resources: Resource<FHIRVersion, T>[];
|
|
81
|
+
}>;
|
|
82
|
+
create<
|
|
83
|
+
FHIRVersion extends FHIR_VERSION,
|
|
84
|
+
Value extends Resource<FHIRVersion, AllResourceTypes>
|
|
85
|
+
>(
|
|
86
|
+
ctx: CTX,
|
|
87
|
+
fhirVersion: FHIRVersion,
|
|
88
|
+
resource: Value
|
|
89
|
+
): Promise<Value>;
|
|
90
|
+
update<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
91
|
+
ctx: CTX,
|
|
92
|
+
fhirVersion: FHIRVersion,
|
|
93
|
+
resourceType: T,
|
|
94
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>,
|
|
95
|
+
resource: Resource<FHIRVersion, T>
|
|
96
|
+
): Promise<Resource<FHIRVersion, T>>;
|
|
97
|
+
patch<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
98
|
+
ctx: CTX,
|
|
99
|
+
fhirVersion: FHIRVersion,
|
|
100
|
+
resourceType: T,
|
|
101
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>,
|
|
102
|
+
patches: any
|
|
103
|
+
): Promise<Resource<FHIRVersion, T>>;
|
|
104
|
+
read<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
105
|
+
ctx: CTX,
|
|
106
|
+
fhirVersion: FHIRVersion,
|
|
107
|
+
resourceType: T,
|
|
108
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>
|
|
109
|
+
): Promise<Resource<FHIRVersion, T> | undefined>;
|
|
110
|
+
vread<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
111
|
+
ctx: CTX,
|
|
112
|
+
fhirVersion: FHIRVersion,
|
|
113
|
+
resourceType: T,
|
|
114
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>,
|
|
115
|
+
versionId: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>
|
|
116
|
+
): Promise<Resource<FHIRVersion, T> | undefined>;
|
|
117
|
+
delete<FHIRVersion extends FHIR_VERSION, T extends ResourceType<FHIRVersion>>(
|
|
118
|
+
ctx: CTX,
|
|
119
|
+
fhirVersion: FHIRVersion,
|
|
120
|
+
resourceType: T,
|
|
121
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>
|
|
122
|
+
): Promise<void>;
|
|
123
|
+
|
|
124
|
+
historySystem<FHIRVersion extends FHIR_VERSION>(
|
|
125
|
+
ctx: CTX,
|
|
126
|
+
fhirVersion: FHIRVersion,
|
|
127
|
+
parameters?: ParsedParameter<string | number>[] | string
|
|
128
|
+
): Promise<NonNullable<Resource<FHIRVersion, "Bundle">["entry"]>>;
|
|
129
|
+
historyType<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
130
|
+
ctx: CTX,
|
|
131
|
+
fhirVersion: FHIRVersion,
|
|
132
|
+
resourceType: T,
|
|
133
|
+
parameters?: ParsedParameter<string | number>[] | string
|
|
134
|
+
): Promise<NonNullable<Resource<FHIRVersion, "Bundle">["entry"]>>;
|
|
135
|
+
historyInstance<FHIRVersion extends FHIR_VERSION, T extends AllResourceTypes>(
|
|
136
|
+
ctx: CTX,
|
|
137
|
+
fhirVersion: FHIRVersion,
|
|
138
|
+
resourceType: T,
|
|
139
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>,
|
|
140
|
+
parameters?: ParsedParameter<string | number>[] | string
|
|
141
|
+
): Promise<NonNullable<Resource<FHIRVersion, "Bundle">["entry"]>>;
|
|
142
|
+
invoke_system<
|
|
143
|
+
FHIRVersion extends FHIR_VERSION,
|
|
144
|
+
Op extends IOperation<any, any>
|
|
145
|
+
>(
|
|
146
|
+
op: Op,
|
|
147
|
+
ctx: CTX,
|
|
148
|
+
fhirVersion: FHIRVersion,
|
|
149
|
+
input: OPMetadata<Op>["Input"]
|
|
150
|
+
): Promise<OPMetadata<Op>["Output"]>;
|
|
151
|
+
invoke_type<
|
|
152
|
+
FHIRVersion extends FHIR_VERSION,
|
|
153
|
+
Op extends IOperation<any, any>,
|
|
154
|
+
T extends AllResourceTypes
|
|
155
|
+
>(
|
|
156
|
+
op: Op,
|
|
157
|
+
ctx: CTX,
|
|
158
|
+
fhirVersion: FHIRVersion,
|
|
159
|
+
resourceType: T,
|
|
160
|
+
input: OPMetadata<Op>["Input"]
|
|
161
|
+
): Promise<OPMetadata<Op>["Output"]>;
|
|
162
|
+
invoke_instance<
|
|
163
|
+
FHIRVersion extends FHIR_VERSION,
|
|
164
|
+
Op extends IOperation<any, any>,
|
|
165
|
+
T extends AllResourceTypes
|
|
166
|
+
>(
|
|
167
|
+
op: Op,
|
|
168
|
+
ctx: CTX,
|
|
169
|
+
fhirVersion: FHIRVersion,
|
|
170
|
+
resourceType: T,
|
|
171
|
+
id: NonNullable<Resource<FHIRVersion, AllResourceTypes>["id"]>,
|
|
172
|
+
input: OPMetadata<Op>["Input"]
|
|
173
|
+
): Promise<OPMetadata<Op>["Output"]>;
|
|
174
|
+
transaction<FHIRVersion extends FHIR_VERSION>(
|
|
175
|
+
ctx: CTX,
|
|
176
|
+
fhirVersion: FHIRVersion,
|
|
177
|
+
bundle: Resource<FHIRVersion, "Bundle">
|
|
178
|
+
): Promise<Resource<FHIRVersion, "Bundle">>;
|
|
179
|
+
batch<FHIRVersion extends FHIR_VERSION>(
|
|
180
|
+
ctx: CTX,
|
|
181
|
+
fhirVersion: FHIRVersion,
|
|
182
|
+
bundle: Resource<FHIRVersion, "Bundle">
|
|
183
|
+
): Promise<Resource<FHIRVersion, "Bundle">>;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Usage
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { expect, test } from "@jest/globals";
|
|
191
|
+
|
|
192
|
+
import HTTPClient from "@haste-health/client/http";
|
|
193
|
+
import { OperationDefinition } from "@haste-health/fhir-types/r4/types";
|
|
194
|
+
import { R4 } from "@haste-health/fhir-types/version";
|
|
195
|
+
|
|
196
|
+
const client = HTTPClient({
|
|
197
|
+
url: "FHIR_API_ROOT_URL",
|
|
198
|
+
getAccessToken: async function () {
|
|
199
|
+
return "API TOKEN";
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const operationDefinition: OperationDefinition = {
|
|
204
|
+
resourceType: "OperationDefinition",
|
|
205
|
+
name: "test",
|
|
206
|
+
status: "draft",
|
|
207
|
+
kind: "operation",
|
|
208
|
+
code: "my-operation",
|
|
209
|
+
system: false,
|
|
210
|
+
instance: false,
|
|
211
|
+
type: false,
|
|
212
|
+
parameter: [],
|
|
213
|
+
};
|
|
214
|
+
const response = await client.create({}, R4, operationDefinition);
|
|
215
|
+
expect(response).toMatchObject({
|
|
216
|
+
resourceType: "OperationDefinition",
|
|
217
|
+
name: "test",
|
|
218
|
+
status: "draft",
|
|
219
|
+
kind: "operation",
|
|
220
|
+
code: "my-operation",
|
|
221
|
+
system: false,
|
|
222
|
+
instance: false,
|
|
223
|
+
type: false,
|
|
224
|
+
parameter: [],
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
await client.delete({}, R4, "OperationDefinition", response.id as string);
|
|
228
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FHIR_VERSION } from "@haste-health/fhir-types/versions";
|
|
2
|
+
import { AsynchronousClient } from "../index.js";
|
|
3
|
+
import { AllInteractions, FHIRErrorResponse, FHIRRequest } from "../types/index.js";
|
|
4
|
+
type DeriveFHIRURL = (fhirVersion: FHIR_VERSION) => string;
|
|
5
|
+
export type HTTPClientState = {
|
|
6
|
+
authenticate?: () => void;
|
|
7
|
+
getAccessToken?: () => Promise<string>;
|
|
8
|
+
url: string | DeriveFHIRURL;
|
|
9
|
+
};
|
|
10
|
+
export type HTTPContext = {
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Used as default and for display purposes in admin app.
|
|
15
|
+
* @param domain HasteHealth Domain
|
|
16
|
+
* @param fhirVersion FHIRVersion
|
|
17
|
+
* @returns HasteHealth VersionedURL.
|
|
18
|
+
*/
|
|
19
|
+
export declare const deriveHasteHealthVersionedURL: (domain: string, fhirVersion: FHIR_VERSION) => string;
|
|
20
|
+
export declare class ResponseError<Version extends FHIR_VERSION> extends Error {
|
|
21
|
+
private readonly _request;
|
|
22
|
+
private readonly _response;
|
|
23
|
+
constructor(request: FHIRRequest<Version, AllInteractions>, response: FHIRErrorResponse<Version>);
|
|
24
|
+
get response(): FHIRErrorResponse<Version>;
|
|
25
|
+
get request(): FHIRRequest<Version, AllInteractions>;
|
|
26
|
+
}
|
|
27
|
+
export declare function isResponseError(e: unknown): e is ResponseError<FHIR_VERSION>;
|
|
28
|
+
export default function createHTTPClient(initialState: HTTPClientState): AsynchronousClient<HTTPContext>;
|
|
29
|
+
export {};
|