@medplum/fhir-router 3.2.10 → 3.2.12

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.
@@ -1,4 +1,6 @@
1
1
  import { Bundle } from '@medplum/fhirtypes';
2
+ import { CapabilityStatementRestInteraction } from '@medplum/fhirtypes';
3
+ import { CapabilityStatementRestResourceInteraction } from '@medplum/fhirtypes';
2
4
  import { Event as Event_2 } from '@medplum/core';
3
5
  import { EventTarget as EventTarget_2 } from '@medplum/core';
4
6
  import type { IncomingHttpHeaders } from 'node:http';
@@ -9,41 +11,6 @@ import { Reference } from '@medplum/fhirtypes';
9
11
  import { Resource } from '@medplum/fhirtypes';
10
12
  import { SearchRequest } from '@medplum/core';
11
13
 
12
- export declare abstract class BaseRepository {
13
- /**
14
- * Searches for FHIR resources.
15
- *
16
- * See: https://www.hl7.org/fhir/http.html#search
17
- * @param searchRequest - The FHIR search request.
18
- * @returns The search results.
19
- */
20
- abstract search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
21
- /**
22
- * Searches for a single FHIR resource.
23
- *
24
- * This is a convenience method for `search()` that returns the first resource rather than a `Bundle`.
25
- *
26
- * The return value is the resource, if available; otherwise, undefined.
27
- *
28
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
29
- * @param searchRequest - The FHIR search request.
30
- * @returns Promise to the first search result or undefined.
31
- */
32
- searchOne<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T | undefined>;
33
- /**
34
- * Sends a FHIR search request for an array of resources.
35
- *
36
- * This is a convenience method for `search()` that returns the resources as an array rather than a `Bundle`.
37
- *
38
- * The return value is an array of resources.
39
- *
40
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
41
- * @param searchRequest - The FHIR search request.
42
- * @returns Promise to the array of search results.
43
- */
44
- searchResources<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T[]>;
45
- }
46
-
47
14
  export declare interface BatchEvent extends Event_2 {
48
15
  bundleType: Bundle['type'];
49
16
  count?: number;
@@ -51,19 +18,27 @@ export declare interface BatchEvent extends Event_2 {
51
18
  size?: number;
52
19
  }
53
20
 
21
+ export declare function createResourceImpl<T extends Resource>(resourceType: T['resourceType'], resource: T, repo: FhirRepository, options?: CreateResourceOptions): Promise<FhirResponse>;
22
+
23
+ export declare type CreateResourceOptions = {
24
+ assignedId?: boolean;
25
+ };
26
+
54
27
  export declare interface FhirOptions {
55
28
  introspectionEnabled?: boolean;
56
29
  }
57
30
 
58
31
  /**
59
- * The FhirRepository interface defines the methods that are required to implement a FHIR repository.
32
+ * The FhirRepository abstract class defines the methods that are required to implement a FHIR repository.
60
33
  * A FHIR repository is responsible for storing and retrieving FHIR resources.
61
34
  * It is used by the FHIR router to implement the FHIR REST API.
62
35
  * The primary implementations at this time are:
63
36
  * 1. MemoryRepository - A repository that stores resources in memory.
64
37
  * 2. Server Repository - A repository that stores resources in a relational database.
38
+ * Additionally, several convenience method implementations are provided to offer advanced functionality on top of the
39
+ * abstract basic operations.
65
40
  */
66
- export declare interface FhirRepository<TClient = unknown> {
41
+ export declare abstract class FhirRepository<TClient = unknown> {
67
42
  /**
68
43
  * Sets the repository mode.
69
44
  * In general, it is assumed that repositories will start in "reader" mode,
@@ -72,7 +47,7 @@ export declare interface FhirRepository<TClient = unknown> {
72
47
  * but after using "writer" once it should use "writer" exclusively.
73
48
  * @param mode - The repository mode.
74
49
  */
75
- setMode(mode: RepositoryMode): void;
50
+ abstract setMode(mode: RepositoryMode): void;
76
51
  /**
77
52
  * Creates a FHIR resource.
78
53
  *
@@ -80,7 +55,14 @@ export declare interface FhirRepository<TClient = unknown> {
80
55
  * @param resource - The FHIR resource to create.
81
56
  * @returns The created resource.
82
57
  */
83
- createResource<T extends Resource>(resource: T): Promise<T>;
58
+ abstract createResource<T extends Resource>(resource: T, options?: CreateResourceOptions): Promise<T>;
59
+ /**
60
+ * Generates a new unique ID for a resource.
61
+ *
62
+ * See: https://www.hl7.org/fhir/R4/resource.html#id
63
+ * @returns The ID string.
64
+ */
65
+ abstract generateId(): string;
84
66
  /**
85
67
  * Reads a FHIR resource by ID.
86
68
  *
@@ -89,7 +71,7 @@ export declare interface FhirRepository<TClient = unknown> {
89
71
  * @param id - The FHIR resource ID.
90
72
  * @returns The FHIR resource.
91
73
  */
92
- readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
74
+ abstract readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
93
75
  /**
94
76
  * Reads a FHIR resource by reference.
95
77
  *
@@ -97,7 +79,7 @@ export declare interface FhirRepository<TClient = unknown> {
97
79
  * @param reference - The FHIR reference.
98
80
  * @returns The FHIR resource.
99
81
  */
100
- readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
82
+ abstract readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
101
83
  /**
102
84
  * Reads a collection of FHIR resources by reference.
103
85
  *
@@ -105,7 +87,7 @@ export declare interface FhirRepository<TClient = unknown> {
105
87
  * @param references - The FHIR references.
106
88
  * @returns The FHIR resources.
107
89
  */
108
- readReferences(references: readonly Reference[]): Promise<(Resource | Error)[]>;
90
+ abstract readReferences(references: readonly Reference[]): Promise<(Resource | Error)[]>;
109
91
  /**
110
92
  * Returns resource history.
111
93
  *
@@ -116,7 +98,7 @@ export declare interface FhirRepository<TClient = unknown> {
116
98
  * @param id - The FHIR resource ID.
117
99
  * @returns Operation outcome and a history bundle.
118
100
  */
119
- readHistory<T extends Resource>(resourceType: string, id: string): Promise<Bundle<T>>;
101
+ abstract readHistory<T extends Resource>(resourceType: string, id: string): Promise<Bundle<T>>;
120
102
  /**
121
103
  * Reads a FHIR resource version.
122
104
  *
@@ -125,7 +107,7 @@ export declare interface FhirRepository<TClient = unknown> {
125
107
  * @param id - The FHIR resource ID.
126
108
  * @param vid - The FHIR resource version ID.
127
109
  */
128
- readVersion<T extends Resource>(resourceType: string, id: string, vid: string): Promise<T>;
110
+ abstract readVersion<T extends Resource>(resourceType: string, id: string, vid: string): Promise<T>;
129
111
  /**
130
112
  * Updates a FHIR resource.
131
113
  *
@@ -133,19 +115,7 @@ export declare interface FhirRepository<TClient = unknown> {
133
115
  * @param resource - The FHIR resource to update.
134
116
  * @returns The updated resource.
135
117
  */
136
- updateResource<T extends Resource>(resource: T, versionId?: string): Promise<T>;
137
- /**
138
- * Creates or updates a resource based on a search criteria.
139
- *
140
- * See: https://www.hl7.org/fhir/http.html#cond-update
141
- * @param resource - The FHIR resource to create or update.
142
- * @param search - The search criteria to find an existing resource to update.
143
- * @returns The updated resource.
144
- */
145
- conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest): Promise<{
146
- resource: T;
147
- outcome: OperationOutcome;
148
- }>;
118
+ abstract updateResource<T extends Resource>(resource: T, options?: UpdateResourceOptions): Promise<T>;
149
119
  /**
150
120
  * Deletes a FHIR resource.
151
121
  *
@@ -153,7 +123,7 @@ export declare interface FhirRepository<TClient = unknown> {
153
123
  * @param resourceType - The FHIR resource type.
154
124
  * @param id - The FHIR resource ID.
155
125
  */
156
- deleteResource(resourceType: string, id: string): Promise<void>;
126
+ abstract deleteResource(resourceType: string, id: string): Promise<void>;
157
127
  /**
158
128
  * Patches a FHIR resource.
159
129
  *
@@ -163,7 +133,7 @@ export declare interface FhirRepository<TClient = unknown> {
163
133
  * @param patch - The JSONPatch operations.
164
134
  * @returns The patched resource.
165
135
  */
166
- patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
136
+ abstract patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
167
137
  /**
168
138
  * Searches for FHIR resources.
169
139
  *
@@ -171,8 +141,16 @@ export declare interface FhirRepository<TClient = unknown> {
171
141
  * @param searchRequest - The FHIR search request.
172
142
  * @returns The search results.
173
143
  */
174
- search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
175
- searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
144
+ abstract search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
145
+ abstract searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
146
+ /**
147
+ * Runs a callback function within a transaction.
148
+ *
149
+ * @param callback - The callback function to be run within a transaction.
150
+ */
151
+ abstract withTransaction<TResult>(callback: (client: TClient) => Promise<TResult>, options?: {
152
+ serializable?: boolean;
153
+ }): Promise<TResult>;
176
154
  /**
177
155
  * Searches for a single FHIR resource.
178
156
  *
@@ -197,12 +175,15 @@ export declare interface FhirRepository<TClient = unknown> {
197
175
  * @returns Promise to the array of search results.
198
176
  */
199
177
  searchResources<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T[]>;
200
- /**
201
- * Runs a callback function within a transaction.
202
- *
203
- * @param callback - The callback function to be run within a transaction.
204
- */
205
- withTransaction<TResult>(callback: (client: TClient) => Promise<TResult>): Promise<TResult>;
178
+ conditionalCreate<T extends Resource>(resource: T, search: SearchRequest<T>, options?: CreateResourceOptions): Promise<{
179
+ resource: T;
180
+ outcome: OperationOutcome;
181
+ }>;
182
+ conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest, options?: CreateResourceOptions & UpdateResourceOptions): Promise<{
183
+ resource: T;
184
+ outcome: OperationOutcome;
185
+ }>;
186
+ conditionalDelete(search: SearchRequest): Promise<void>;
206
187
  }
207
188
 
208
189
  export declare type FhirRequest = {
@@ -220,17 +201,27 @@ export declare type FhirRequestConfig = {
220
201
  graphqlMaxDepth?: number;
221
202
  graphqlMaxPageSize?: number;
222
203
  graphqlMaxSearches?: number;
204
+ transactions?: boolean;
223
205
  };
224
206
 
225
207
  export declare type FhirResponse = [OperationOutcome] | [OperationOutcome, Resource];
226
208
 
227
- export declare type FhirRouteHandler = (req: FhirRequest, repo: FhirRepository, router: FhirRouter) => Promise<FhirResponse>;
209
+ export declare type FhirRouteHandler = (req: FhirRequest, repo: FhirRepository, router: FhirRouter, options?: FhirRouteOptions) => Promise<FhirResponse>;
210
+
211
+ export declare type FhirRouteMetadata = {
212
+ interaction: RestInteraction;
213
+ };
214
+
215
+ export declare type FhirRouteOptions = {
216
+ batch?: boolean;
217
+ };
228
218
 
229
219
  export declare class FhirRouter extends EventTarget_2 {
230
- readonly router: Router<FhirRouteHandler>;
220
+ readonly router: Router<FhirRouteHandler, FhirRouteMetadata>;
231
221
  readonly options: FhirOptions;
232
222
  constructor(options?: {});
233
- add(method: HttpMethod, path: string, handler: FhirRouteHandler): void;
223
+ add(method: HttpMethod, path: string, handler: FhirRouteHandler, interaction?: RestInteraction): void;
224
+ find(method: HttpMethod, path: string): RouteResult<FhirRouteHandler, FhirRouteMetadata> | undefined;
234
225
  handleRequest(req: FhirRequest, repo: FhirRepository): Promise<FhirResponse>;
235
226
  log(level: string, message: string, data?: Record<string, any>): void;
236
227
  }
@@ -242,18 +233,15 @@ export declare interface LogEvent extends Event_2 {
242
233
  data?: Record<string, any>;
243
234
  }
244
235
 
245
- export declare class MemoryRepository extends BaseRepository implements FhirRepository {
236
+ export declare class MemoryRepository extends FhirRepository<undefined> {
246
237
  private readonly resources;
247
238
  private readonly history;
248
239
  constructor();
249
240
  clear(): void;
250
241
  setMode(_mode: RepositoryMode): void;
251
242
  createResource<T extends Resource>(resource: T): Promise<T>;
252
- updateResource<T extends Resource>(resource: T, versionId?: string): Promise<T>;
253
- conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest): Promise<{
254
- resource: T;
255
- outcome: OperationOutcome;
256
- }>;
243
+ generateId(): string;
244
+ updateResource<T extends Resource>(resource: T, options?: UpdateResourceOptions): Promise<T>;
257
245
  patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
258
246
  readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
259
247
  readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
@@ -263,7 +251,7 @@ export declare class MemoryRepository extends BaseRepository implements FhirRepo
263
251
  search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
264
252
  searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
265
253
  deleteResource(resourceType: string, id: string): Promise<void>;
266
- withTransaction<TResult>(callback: (client: unknown) => Promise<TResult>): Promise<TResult>;
254
+ withTransaction<TResult>(callback: (client: undefined) => Promise<TResult>): Promise<TResult>;
267
255
  }
268
256
 
269
257
  export declare type PathSegment = {
@@ -276,21 +264,32 @@ export declare enum RepositoryMode {
276
264
  WRITER = "writer"
277
265
  }
278
266
 
279
- export declare type Route<T> = {
267
+ /** @see http://hl7.org/fhir/R4/codesystem-restful-interaction.html */
268
+ export declare type RestInteraction = CapabilityStatementRestInteraction['code'] | CapabilityStatementRestResourceInteraction['code'] | 'operation';
269
+
270
+ export declare type Route<Handler, Metadata> = {
280
271
  method: HttpMethod;
281
272
  path: PathSegment[];
282
- handler: T;
273
+ handler: Handler;
274
+ data?: Metadata;
283
275
  };
284
276
 
285
- export declare class Router<T> {
286
- readonly routes: Route<T>[];
287
- add(method: HttpMethod, pathStr: string, handler: T): void;
288
- find(method: HttpMethod, pathStr: string): RouteResult<T> | undefined;
277
+ export declare class Router<Handler, Metadata> {
278
+ readonly routes: Route<Handler, Metadata>[];
279
+ add(method: HttpMethod, pathStr: string, handler: Handler, data?: Metadata): void;
280
+ find(method: HttpMethod, pathStr: string): RouteResult<Handler, Metadata> | undefined;
289
281
  }
290
282
 
291
- export declare type RouteResult<T> = {
292
- handler: T;
283
+ export declare type RouteResult<Handler, Metadata> = {
284
+ handler: Handler;
293
285
  params: Record<string, string>;
286
+ data?: Metadata;
287
+ };
288
+
289
+ export declare function updateResourceImpl<T extends Resource>(resourceType: T['resourceType'], id: string, resource: T, repo: FhirRepository, options?: UpdateResourceOptions): Promise<FhirResponse>;
290
+
291
+ export declare type UpdateResourceOptions = {
292
+ ifMatch?: string;
294
293
  };
295
294
 
296
295
  export { }
@@ -1,4 +1,6 @@
1
1
  import { Bundle } from '@medplum/fhirtypes';
2
+ import { CapabilityStatementRestInteraction } from '@medplum/fhirtypes';
3
+ import { CapabilityStatementRestResourceInteraction } from '@medplum/fhirtypes';
2
4
  import { Event as Event_2 } from '@medplum/core';
3
5
  import { EventTarget as EventTarget_2 } from '@medplum/core';
4
6
  import type { IncomingHttpHeaders } from 'node:http';
@@ -9,41 +11,6 @@ import { Reference } from '@medplum/fhirtypes';
9
11
  import { Resource } from '@medplum/fhirtypes';
10
12
  import { SearchRequest } from '@medplum/core';
11
13
 
12
- export declare abstract class BaseRepository {
13
- /**
14
- * Searches for FHIR resources.
15
- *
16
- * See: https://www.hl7.org/fhir/http.html#search
17
- * @param searchRequest - The FHIR search request.
18
- * @returns The search results.
19
- */
20
- abstract search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
21
- /**
22
- * Searches for a single FHIR resource.
23
- *
24
- * This is a convenience method for `search()` that returns the first resource rather than a `Bundle`.
25
- *
26
- * The return value is the resource, if available; otherwise, undefined.
27
- *
28
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
29
- * @param searchRequest - The FHIR search request.
30
- * @returns Promise to the first search result or undefined.
31
- */
32
- searchOne<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T | undefined>;
33
- /**
34
- * Sends a FHIR search request for an array of resources.
35
- *
36
- * This is a convenience method for `search()` that returns the resources as an array rather than a `Bundle`.
37
- *
38
- * The return value is an array of resources.
39
- *
40
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
41
- * @param searchRequest - The FHIR search request.
42
- * @returns Promise to the array of search results.
43
- */
44
- searchResources<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T[]>;
45
- }
46
-
47
14
  export declare interface BatchEvent extends Event_2 {
48
15
  bundleType: Bundle['type'];
49
16
  count?: number;
@@ -51,19 +18,27 @@ export declare interface BatchEvent extends Event_2 {
51
18
  size?: number;
52
19
  }
53
20
 
21
+ export declare function createResourceImpl<T extends Resource>(resourceType: T['resourceType'], resource: T, repo: FhirRepository, options?: CreateResourceOptions): Promise<FhirResponse>;
22
+
23
+ export declare type CreateResourceOptions = {
24
+ assignedId?: boolean;
25
+ };
26
+
54
27
  export declare interface FhirOptions {
55
28
  introspectionEnabled?: boolean;
56
29
  }
57
30
 
58
31
  /**
59
- * The FhirRepository interface defines the methods that are required to implement a FHIR repository.
32
+ * The FhirRepository abstract class defines the methods that are required to implement a FHIR repository.
60
33
  * A FHIR repository is responsible for storing and retrieving FHIR resources.
61
34
  * It is used by the FHIR router to implement the FHIR REST API.
62
35
  * The primary implementations at this time are:
63
36
  * 1. MemoryRepository - A repository that stores resources in memory.
64
37
  * 2. Server Repository - A repository that stores resources in a relational database.
38
+ * Additionally, several convenience method implementations are provided to offer advanced functionality on top of the
39
+ * abstract basic operations.
65
40
  */
66
- export declare interface FhirRepository<TClient = unknown> {
41
+ export declare abstract class FhirRepository<TClient = unknown> {
67
42
  /**
68
43
  * Sets the repository mode.
69
44
  * In general, it is assumed that repositories will start in "reader" mode,
@@ -72,7 +47,7 @@ export declare interface FhirRepository<TClient = unknown> {
72
47
  * but after using "writer" once it should use "writer" exclusively.
73
48
  * @param mode - The repository mode.
74
49
  */
75
- setMode(mode: RepositoryMode): void;
50
+ abstract setMode(mode: RepositoryMode): void;
76
51
  /**
77
52
  * Creates a FHIR resource.
78
53
  *
@@ -80,7 +55,14 @@ export declare interface FhirRepository<TClient = unknown> {
80
55
  * @param resource - The FHIR resource to create.
81
56
  * @returns The created resource.
82
57
  */
83
- createResource<T extends Resource>(resource: T): Promise<T>;
58
+ abstract createResource<T extends Resource>(resource: T, options?: CreateResourceOptions): Promise<T>;
59
+ /**
60
+ * Generates a new unique ID for a resource.
61
+ *
62
+ * See: https://www.hl7.org/fhir/R4/resource.html#id
63
+ * @returns The ID string.
64
+ */
65
+ abstract generateId(): string;
84
66
  /**
85
67
  * Reads a FHIR resource by ID.
86
68
  *
@@ -89,7 +71,7 @@ export declare interface FhirRepository<TClient = unknown> {
89
71
  * @param id - The FHIR resource ID.
90
72
  * @returns The FHIR resource.
91
73
  */
92
- readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
74
+ abstract readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
93
75
  /**
94
76
  * Reads a FHIR resource by reference.
95
77
  *
@@ -97,7 +79,7 @@ export declare interface FhirRepository<TClient = unknown> {
97
79
  * @param reference - The FHIR reference.
98
80
  * @returns The FHIR resource.
99
81
  */
100
- readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
82
+ abstract readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
101
83
  /**
102
84
  * Reads a collection of FHIR resources by reference.
103
85
  *
@@ -105,7 +87,7 @@ export declare interface FhirRepository<TClient = unknown> {
105
87
  * @param references - The FHIR references.
106
88
  * @returns The FHIR resources.
107
89
  */
108
- readReferences(references: readonly Reference[]): Promise<(Resource | Error)[]>;
90
+ abstract readReferences(references: readonly Reference[]): Promise<(Resource | Error)[]>;
109
91
  /**
110
92
  * Returns resource history.
111
93
  *
@@ -116,7 +98,7 @@ export declare interface FhirRepository<TClient = unknown> {
116
98
  * @param id - The FHIR resource ID.
117
99
  * @returns Operation outcome and a history bundle.
118
100
  */
119
- readHistory<T extends Resource>(resourceType: string, id: string): Promise<Bundle<T>>;
101
+ abstract readHistory<T extends Resource>(resourceType: string, id: string): Promise<Bundle<T>>;
120
102
  /**
121
103
  * Reads a FHIR resource version.
122
104
  *
@@ -125,7 +107,7 @@ export declare interface FhirRepository<TClient = unknown> {
125
107
  * @param id - The FHIR resource ID.
126
108
  * @param vid - The FHIR resource version ID.
127
109
  */
128
- readVersion<T extends Resource>(resourceType: string, id: string, vid: string): Promise<T>;
110
+ abstract readVersion<T extends Resource>(resourceType: string, id: string, vid: string): Promise<T>;
129
111
  /**
130
112
  * Updates a FHIR resource.
131
113
  *
@@ -133,19 +115,7 @@ export declare interface FhirRepository<TClient = unknown> {
133
115
  * @param resource - The FHIR resource to update.
134
116
  * @returns The updated resource.
135
117
  */
136
- updateResource<T extends Resource>(resource: T, versionId?: string): Promise<T>;
137
- /**
138
- * Creates or updates a resource based on a search criteria.
139
- *
140
- * See: https://www.hl7.org/fhir/http.html#cond-update
141
- * @param resource - The FHIR resource to create or update.
142
- * @param search - The search criteria to find an existing resource to update.
143
- * @returns The updated resource.
144
- */
145
- conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest): Promise<{
146
- resource: T;
147
- outcome: OperationOutcome;
148
- }>;
118
+ abstract updateResource<T extends Resource>(resource: T, options?: UpdateResourceOptions): Promise<T>;
149
119
  /**
150
120
  * Deletes a FHIR resource.
151
121
  *
@@ -153,7 +123,7 @@ export declare interface FhirRepository<TClient = unknown> {
153
123
  * @param resourceType - The FHIR resource type.
154
124
  * @param id - The FHIR resource ID.
155
125
  */
156
- deleteResource(resourceType: string, id: string): Promise<void>;
126
+ abstract deleteResource(resourceType: string, id: string): Promise<void>;
157
127
  /**
158
128
  * Patches a FHIR resource.
159
129
  *
@@ -163,7 +133,7 @@ export declare interface FhirRepository<TClient = unknown> {
163
133
  * @param patch - The JSONPatch operations.
164
134
  * @returns The patched resource.
165
135
  */
166
- patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
136
+ abstract patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
167
137
  /**
168
138
  * Searches for FHIR resources.
169
139
  *
@@ -171,8 +141,16 @@ export declare interface FhirRepository<TClient = unknown> {
171
141
  * @param searchRequest - The FHIR search request.
172
142
  * @returns The search results.
173
143
  */
174
- search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
175
- searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
144
+ abstract search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
145
+ abstract searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
146
+ /**
147
+ * Runs a callback function within a transaction.
148
+ *
149
+ * @param callback - The callback function to be run within a transaction.
150
+ */
151
+ abstract withTransaction<TResult>(callback: (client: TClient) => Promise<TResult>, options?: {
152
+ serializable?: boolean;
153
+ }): Promise<TResult>;
176
154
  /**
177
155
  * Searches for a single FHIR resource.
178
156
  *
@@ -197,12 +175,15 @@ export declare interface FhirRepository<TClient = unknown> {
197
175
  * @returns Promise to the array of search results.
198
176
  */
199
177
  searchResources<T extends Resource>(searchRequest: SearchRequest<T>): Promise<T[]>;
200
- /**
201
- * Runs a callback function within a transaction.
202
- *
203
- * @param callback - The callback function to be run within a transaction.
204
- */
205
- withTransaction<TResult>(callback: (client: TClient) => Promise<TResult>): Promise<TResult>;
178
+ conditionalCreate<T extends Resource>(resource: T, search: SearchRequest<T>, options?: CreateResourceOptions): Promise<{
179
+ resource: T;
180
+ outcome: OperationOutcome;
181
+ }>;
182
+ conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest, options?: CreateResourceOptions & UpdateResourceOptions): Promise<{
183
+ resource: T;
184
+ outcome: OperationOutcome;
185
+ }>;
186
+ conditionalDelete(search: SearchRequest): Promise<void>;
206
187
  }
207
188
 
208
189
  export declare type FhirRequest = {
@@ -220,17 +201,27 @@ export declare type FhirRequestConfig = {
220
201
  graphqlMaxDepth?: number;
221
202
  graphqlMaxPageSize?: number;
222
203
  graphqlMaxSearches?: number;
204
+ transactions?: boolean;
223
205
  };
224
206
 
225
207
  export declare type FhirResponse = [OperationOutcome] | [OperationOutcome, Resource];
226
208
 
227
- export declare type FhirRouteHandler = (req: FhirRequest, repo: FhirRepository, router: FhirRouter) => Promise<FhirResponse>;
209
+ export declare type FhirRouteHandler = (req: FhirRequest, repo: FhirRepository, router: FhirRouter, options?: FhirRouteOptions) => Promise<FhirResponse>;
210
+
211
+ export declare type FhirRouteMetadata = {
212
+ interaction: RestInteraction;
213
+ };
214
+
215
+ export declare type FhirRouteOptions = {
216
+ batch?: boolean;
217
+ };
228
218
 
229
219
  export declare class FhirRouter extends EventTarget_2 {
230
- readonly router: Router<FhirRouteHandler>;
220
+ readonly router: Router<FhirRouteHandler, FhirRouteMetadata>;
231
221
  readonly options: FhirOptions;
232
222
  constructor(options?: {});
233
- add(method: HttpMethod, path: string, handler: FhirRouteHandler): void;
223
+ add(method: HttpMethod, path: string, handler: FhirRouteHandler, interaction?: RestInteraction): void;
224
+ find(method: HttpMethod, path: string): RouteResult<FhirRouteHandler, FhirRouteMetadata> | undefined;
234
225
  handleRequest(req: FhirRequest, repo: FhirRepository): Promise<FhirResponse>;
235
226
  log(level: string, message: string, data?: Record<string, any>): void;
236
227
  }
@@ -242,18 +233,15 @@ export declare interface LogEvent extends Event_2 {
242
233
  data?: Record<string, any>;
243
234
  }
244
235
 
245
- export declare class MemoryRepository extends BaseRepository implements FhirRepository {
236
+ export declare class MemoryRepository extends FhirRepository<undefined> {
246
237
  private readonly resources;
247
238
  private readonly history;
248
239
  constructor();
249
240
  clear(): void;
250
241
  setMode(_mode: RepositoryMode): void;
251
242
  createResource<T extends Resource>(resource: T): Promise<T>;
252
- updateResource<T extends Resource>(resource: T, versionId?: string): Promise<T>;
253
- conditionalUpdate<T extends Resource>(resource: T, search: SearchRequest): Promise<{
254
- resource: T;
255
- outcome: OperationOutcome;
256
- }>;
243
+ generateId(): string;
244
+ updateResource<T extends Resource>(resource: T, options?: UpdateResourceOptions): Promise<T>;
257
245
  patchResource(resourceType: string, id: string, patch: Operation[]): Promise<Resource>;
258
246
  readResource<T extends Resource>(resourceType: string, id: string): Promise<T>;
259
247
  readReference<T extends Resource>(reference: Reference<T>): Promise<T>;
@@ -263,7 +251,7 @@ export declare class MemoryRepository extends BaseRepository implements FhirRepo
263
251
  search<T extends Resource>(searchRequest: SearchRequest<T>): Promise<Bundle<T>>;
264
252
  searchByReference<T extends Resource>(searchRequest: SearchRequest<T>, referenceField: string, references: string[]): Promise<Record<string, T[]>>;
265
253
  deleteResource(resourceType: string, id: string): Promise<void>;
266
- withTransaction<TResult>(callback: (client: unknown) => Promise<TResult>): Promise<TResult>;
254
+ withTransaction<TResult>(callback: (client: undefined) => Promise<TResult>): Promise<TResult>;
267
255
  }
268
256
 
269
257
  export declare type PathSegment = {
@@ -276,21 +264,32 @@ export declare enum RepositoryMode {
276
264
  WRITER = "writer"
277
265
  }
278
266
 
279
- export declare type Route<T> = {
267
+ /** @see http://hl7.org/fhir/R4/codesystem-restful-interaction.html */
268
+ export declare type RestInteraction = CapabilityStatementRestInteraction['code'] | CapabilityStatementRestResourceInteraction['code'] | 'operation';
269
+
270
+ export declare type Route<Handler, Metadata> = {
280
271
  method: HttpMethod;
281
272
  path: PathSegment[];
282
- handler: T;
273
+ handler: Handler;
274
+ data?: Metadata;
283
275
  };
284
276
 
285
- export declare class Router<T> {
286
- readonly routes: Route<T>[];
287
- add(method: HttpMethod, pathStr: string, handler: T): void;
288
- find(method: HttpMethod, pathStr: string): RouteResult<T> | undefined;
277
+ export declare class Router<Handler, Metadata> {
278
+ readonly routes: Route<Handler, Metadata>[];
279
+ add(method: HttpMethod, pathStr: string, handler: Handler, data?: Metadata): void;
280
+ find(method: HttpMethod, pathStr: string): RouteResult<Handler, Metadata> | undefined;
289
281
  }
290
282
 
291
- export declare type RouteResult<T> = {
292
- handler: T;
283
+ export declare type RouteResult<Handler, Metadata> = {
284
+ handler: Handler;
293
285
  params: Record<string, string>;
286
+ data?: Metadata;
287
+ };
288
+
289
+ export declare function updateResourceImpl<T extends Resource>(resourceType: T['resourceType'], id: string, resource: T, repo: FhirRepository, options?: UpdateResourceOptions): Promise<FhirResponse>;
290
+
291
+ export declare type UpdateResourceOptions = {
292
+ ifMatch?: string;
294
293
  };
295
294
 
296
295
  export { }