@medplum/core 0.9.4 → 0.9.7

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,713 +1,854 @@
1
- import { Binary, Bundle, Project, ProjectMembership, Reference, Resource, UserConfiguration, ValueSet } from '@medplum/fhirtypes';
2
- import type { Operation } from 'fast-json-patch';
3
- import { EventTarget } from './eventtarget';
4
- import { Hl7Message } from './hl7';
5
- import { ReadablePromise } from './readablepromise';
6
- import { SearchRequest } from './search';
7
- import { IndexedStructureDefinition } from './types';
8
- import { ProfileResource } from './utils';
9
- export interface MedplumClientOptions {
10
- /**
11
- * The client ID.
12
- * Optional. Default is to defer to the server to use the default client.
13
- * Use this to use a specific client for SMART-on-FHIR.
14
- */
15
- clientId?: string;
16
- /**
17
- * Base server URL.
18
- * Optional. Default value is "https://api.medplum.com/".
19
- * Use this to point to a custom Medplum deployment.
20
- */
21
- baseUrl?: string;
22
- /**
23
- * OAuth2 authorize URL.
24
- * Optional. Default value is baseUrl + "/oauth2/authorize".
25
- * Use this if you want to use a separate OAuth server.
26
- */
27
- authorizeUrl?: string;
28
- /**
29
- * OAuth2 token URL.
30
- * Optional. Default value is baseUrl + "/oauth2/token".
31
- * Use this if you want to use a separate OAuth server.
32
- */
33
- tokenUrl?: string;
34
- /**
35
- * OAuth2 logout URL.
36
- * Optional. Default value is baseUrl + "/oauth2/logout".
37
- * Use this if you want to use a separate OAuth server.
38
- */
39
- logoutUrl?: string;
40
- /**
41
- * Number of resources to store in the cache.
42
- * Optional. Default value is 1000.
43
- * Consider using this for performance of displaying Patient or Practitioner resources.
44
- */
45
- resourceCacheSize?: number;
46
- /**
47
- * Optional fetch implementation.
48
- * Optional. Default is window.fetch.
49
- * For nodejs applications, consider the 'node-fetch' package.
50
- */
51
- fetch?: FetchLike;
52
- /**
53
- * Optional callback for when the client is unauthenticated.
54
- * Default is do nothing.
55
- * For client side applications, consider redirecting to a sign in page.
56
- */
57
- onUnauthenticated?: () => void;
58
- }
59
- export interface FetchLike {
60
- (url: string, options?: any): Promise<any>;
61
- }
62
- export interface RegisterRequest {
63
- readonly firstName: string;
64
- readonly lastName: string;
65
- readonly projectName: string;
66
- readonly email: string;
67
- readonly password: string;
68
- readonly remember?: boolean;
69
- readonly recaptchaToken: string;
70
- }
71
- export interface GoogleCredentialResponse {
72
- readonly clientId: string;
73
- readonly credential: string;
74
- }
75
- export interface LoginAuthenticationResponse {
76
- readonly login: string;
77
- readonly code?: string;
78
- readonly memberships?: ProjectMembership[];
79
- }
80
- export interface LoginProfileResponse {
81
- readonly login: string;
82
- readonly scope: string;
83
- }
84
- export interface LoginScopeResponse {
85
- readonly login: string;
86
- readonly code: string;
87
- }
88
- export interface LoginState {
89
- readonly project: Reference<Project>;
90
- readonly profile: Reference<ProfileResource>;
91
- readonly accessToken: string;
92
- readonly refreshToken: string;
93
- }
94
- export interface TokenResponse {
95
- readonly token_type: string;
96
- readonly id_token: string;
97
- readonly access_token: string;
98
- readonly refresh_token: string;
99
- readonly expires_in: number;
100
- readonly project: Reference<Project>;
101
- readonly profile: Reference<ProfileResource>;
102
- }
103
- export interface BotEvent {
104
- readonly contentType: string;
105
- readonly input: Resource | Hl7Message | string;
106
- }
107
- /**
108
- * The MedplumClient class provides a client for the Medplum FHIR server.
109
- *
110
- * The client can be used in the browser, in a NodeJS application, or in a Medplum Bot.
111
- *
112
- * The client provides helpful methods for common operations such as:
113
- * 1) Authenticating
114
- * 2) Creating resources
115
- * 2) Reading resources
116
- * 3) Updating resources
117
- * 5) Deleting resources
118
- * 6) Searching
119
- * 7) Making GraphQL queries
120
- *
121
- * Here is a quick example of how to use the client:
122
- *
123
- * ```typescript
124
- * import { MedplumClient } from '@medplum/core';
125
- * const medplum = new MedplumClient();
126
- * ```
127
- *
128
- * Create a `Patient`:
129
- *
130
- * ```typescript
131
- * const patient = await medplum.createResource({
132
- * resourceType: 'Patient',
133
- * name: [{
134
- * given: ['Alice'],
135
- * family: 'Smith'
136
- * }]
137
- * });
138
- * ```
139
- *
140
- * Read a `Patient` by ID:
141
- *
142
- * ```typescript
143
- * const patient = await medplum.readResource('Patient', '123');
144
- * console.log(patient.name[0].given[0]);
145
- * ```
146
- *
147
- * Search for a `Patient` by name:
148
- *
149
- * ```typescript
150
- * const bundle = await medplum.search('Patient?name=Alice');
151
- * console.log(bundle.total);
152
- * ```
153
- *
154
- */
155
- export declare class MedplumClient extends EventTarget {
156
- #private;
157
- constructor(options?: MedplumClientOptions);
158
- /**
159
- * Clears all auth state including local storage and session storage.
160
- */
161
- clear(): void;
162
- /**
163
- * Makes an HTTP GET request to the specified URL.
164
- *
165
- * This is a lower level method for custom requests.
166
- * For common operations, we recommend using higher level methods
167
- * such as `readResource()`, `search()`, etc.
168
- *
169
- * @param url The target URL.
170
- * @param options Optional fetch options.
171
- * @returns Promise to the response content.
172
- */
173
- get<T = any>(url: string, options?: RequestInit): ReadablePromise<T>;
174
- /**
175
- * Makes an HTTP POST request to the specified URL.
176
- *
177
- * This is a lower level method for custom requests.
178
- * For common operations, we recommend using higher level methods
179
- * such as `createResource()`.
180
- *
181
- * @param url The target URL.
182
- * @param body The content body. Strings and `File` objects are passed directly. Other objects are converted to JSON.
183
- * @param contentType The content type to be included in the "Content-Type" header.
184
- * @param options Optional fetch options.
185
- * @returns Promise to the response content.
186
- */
187
- post(url: string, body: any, contentType?: string, options?: RequestInit): Promise<any>;
188
- /**
189
- * Makes an HTTP PUT request to the specified URL.
190
- *
191
- * This is a lower level method for custom requests.
192
- * For common operations, we recommend using higher level methods
193
- * such as `updateResource()`.
194
- *
195
- * @param url The target URL.
196
- * @param body The content body. Strings and `File` objects are passed directly. Other objects are converted to JSON.
197
- * @param contentType The content type to be included in the "Content-Type" header.
198
- * @param options Optional fetch options.
199
- * @returns Promise to the response content.
200
- */
201
- put(url: string, body: any, contentType?: string, options?: RequestInit): Promise<any>;
202
- /**
203
- * Makes an HTTP PATCH request to the specified URL.
204
- *
205
- * This is a lower level method for custom requests.
206
- * For common operations, we recommend using higher level methods
207
- * such as `patchResource()`.
208
- *
209
- * @param url The target URL.
210
- * @param operations Array of JSONPatch operations.
211
- * @param options Optional fetch options.
212
- * @returns Promise to the response content.
213
- */
214
- patch(url: string, operations: Operation[], options?: RequestInit): Promise<any>;
215
- /**
216
- * Makes an HTTP DELETE request to the specified URL.
217
- *
218
- * This is a lower level method for custom requests.
219
- * For common operations, we recommend using higher level methods
220
- * such as `deleteResource()`.
221
- *
222
- * @param url The target URL.
223
- * @param options Optional fetch options.
224
- * @returns Promise to the response content.
225
- */
226
- delete(url: string, options?: RequestInit): Promise<any>;
227
- /**
228
- * Tries to register a new user.
229
- * @param request The registration request.
230
- * @returns Promise to the authentication response.
231
- */
232
- register(request: RegisterRequest): Promise<void>;
233
- /**
234
- * Initiates a user login flow.
235
- * @param email The email address of the user.
236
- * @param password The password of the user.
237
- * @param remember Optional flag to remember the user.
238
- * @returns Promise to the authentication response.
239
- */
240
- startLogin(email: string, password: string, remember?: boolean): Promise<LoginAuthenticationResponse>;
241
- /**
242
- * Tries to sign in with Google authentication.
243
- * The response parameter is the result of a Google authentication.
244
- * See: https://developers.google.com/identity/gsi/web/guides/handle-credential-responses-js-functions
245
- * @param googleResponse The Google credential response.
246
- * @returns Promise to the authentication response.
247
- */
248
- startGoogleLogin(googleResponse: GoogleCredentialResponse): Promise<LoginAuthenticationResponse>;
249
- /**
250
- * Signs out locally.
251
- * Does not invalidate tokens with the server.
252
- */
253
- signOut(): Promise<void>;
254
- /**
255
- * Tries to sign in the user.
256
- * Returns true if the user is signed in.
257
- * This may result in navigating away to the sign in page.
258
- */
259
- signInWithRedirect(): Promise<ProfileResource | void> | undefined;
260
- /**
261
- * Tries to sign out the user.
262
- * See: https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html
263
- */
264
- signOutWithRedirect(): void;
265
- /**
266
- * Builds a FHIR URL from a collection of URL path components.
267
- * For example, `buildUrl('/Patient', '123')` returns `fhir/R4/Patient/123`.
268
- * @param path The path component of the URL.
269
- * @returns The well-formed FHIR URL.
270
- */
271
- fhirUrl(...path: string[]): string;
272
- /**
273
- * Sends a FHIR search request.
274
- *
275
- * Example using a FHIR search string:
276
- *
277
- * ```typescript
278
- * const bundle = await client.search('Patient?name=Alice');
279
- * console.log(bundle);
280
- * ```
281
- *
282
- * Example using a structured search:
283
- *
284
- * ```typescript
285
- * const bundle = await client.search({
286
- * resourceType: 'Patient',
287
- * filters: [{
288
- * code: 'name',
289
- * operator: 'eq',
290
- * value: 'Alice',
291
- * }]
292
- * });
293
- * console.log(bundle);
294
- * ```
295
- *
296
- * The return value is a FHIR bundle:
297
- *
298
- * ```json
299
- * {
300
- * "resourceType": "Bundle",
301
- * "type": "searchest",
302
- * "total": 1,
303
- * "entry": [
304
- * {
305
- * "resource": {
306
- * "resourceType": "Patient",
307
- * "name": [
308
- * {
309
- * "given": [
310
- * "George"
311
- * ],
312
- * "family": "Washington"
313
- * }
314
- * ],
315
- * }
316
- * }
317
- * ]
318
- * }
319
- * ```
320
- *
321
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
322
- *
323
- * @param query The search query as either a string or a structured search object.
324
- * @returns Promise to the search result bundle.
325
- */
326
- search<T extends Resource>(query: string | SearchRequest, options?: RequestInit): Promise<Bundle<T>>;
327
- /**
328
- * Sends a FHIR search request for a single resource.
329
- *
330
- * This is a convenience method for `search()` that returns the first resource rather than a `Bundle`.
331
- *
332
- * Example using a FHIR search string:
333
- *
334
- * ```typescript
335
- * const patient = await client.searchOne('Patient?identifier=123');
336
- * console.log(patient);
337
- * ```
338
- *
339
- * The return value is the resource, if available; otherwise, undefined.
340
- *
341
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
342
- *
343
- * @param query The search query as either a string or a structured search object.
344
- * @returns Promise to the search result bundle.
345
- */
346
- searchOne<T extends Resource>(query: string | SearchRequest, options?: RequestInit): Promise<T | undefined>;
347
- /**
348
- * Sends a FHIR search request for an array of resources.
349
- *
350
- * This is a convenience method for `search()` that returns the resources as an array rather than a `Bundle`.
351
- *
352
- * Example using a FHIR search string:
353
- *
354
- * ```typescript
355
- * const patients = await client.searchResources('Patient?name=Alice');
356
- * console.log(patients);
357
- * ```
358
- *
359
- * The return value is an array of resources.
360
- *
361
- * See FHIR search for full details: https://www.hl7.org/fhir/search.html
362
- *
363
- * @param query The search query as either a string or a structured search object.
364
- * @returns Promise to the search result bundle.
365
- */
366
- searchResources<T extends Resource>(query: string | SearchRequest, options?: RequestInit): Promise<T[]>;
367
- /**
368
- * Searches a ValueSet resource using the "expand" operation.
369
- * See: https://www.hl7.org/fhir/operation-valueset-expand.html
370
- * @param system The ValueSet system url.
371
- * @param filter The search string.
372
- * @returns Promise to expanded ValueSet.
373
- */
374
- searchValueSet(system: string, filter: string, options?: RequestInit): Promise<ValueSet>;
375
- /**
376
- * Returns a cached resource if it is available.
377
- * @param resourceType The FHIR resource type.
378
- * @param id The FHIR resource ID.
379
- * @returns The resource if it is available in the cache; undefined otherwise.
380
- */
381
- getCached<T extends Resource>(resourceType: string, id: string): T | undefined;
382
- /**
383
- * Returns a cached resource if it is available.
384
- * @param resourceType The FHIR resource type.
385
- * @param id The FHIR resource ID.
386
- * @returns The resource if it is available in the cache; undefined otherwise.
387
- */
388
- getCachedReference<T extends Resource>(reference: Reference<T>): T | undefined;
389
- /**
390
- * Reads a resource by resource type and ID.
391
- *
392
- * Example:
393
- *
394
- * ```typescript
395
- * const patient = await medplum.readResource('Patient', '123');
396
- * console.log(patient);
397
- * ```
398
- *
399
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
400
- *
401
- * @param resourceType The FHIR resource type.
402
- * @param id The resource ID.
403
- * @returns The resource if available; undefined otherwise.
404
- */
405
- readResource<T extends Resource>(resourceType: string, id: string): ReadablePromise<T>;
406
- /**
407
- * Reads a resource by resource type and ID using the in-memory resource cache.
408
- *
409
- * If the resource is not available in the cache, it will be read from the server.
410
- *
411
- * Example:
412
- *
413
- * ```typescript
414
- * const patient = await medplum.readCached('Patient', '123');
415
- * console.log(patient);
416
- * ```
417
- *
418
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
419
- *
420
- * @param resourceType The FHIR resource type.
421
- * @param id The resource ID.
422
- * @returns The resource if available; undefined otherwise.
423
- */
424
- readCached<T extends Resource>(resourceType: string, id: string): ReadablePromise<T>;
425
- /**
426
- * Reads a resource by `Reference`.
427
- *
428
- * This is a convenience method for `readResource()` that accepts a `Reference` object.
429
- *
430
- * Example:
431
- *
432
- * ```typescript
433
- * const serviceRequest = await medplum.readResource('ServiceRequest', '123');
434
- * const patient = await medplum.readReference(serviceRequest.subject);
435
- * console.log(patient);
436
- * ```
437
- *
438
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
439
- *
440
- * @param reference The FHIR reference object.
441
- * @returns The resource if available; undefined otherwise.
442
- */
443
- readReference<T extends Resource>(reference: Reference<T>): ReadablePromise<T>;
444
- /**
445
- * Reads a resource by `Reference` using the in-memory resource cache.
446
- *
447
- * This is a convenience method for `readResource()` that accepts a `Reference` object.
448
- *
449
- * If the resource is not available in the cache, it will be read from the server.
450
- *
451
- * Example:
452
- *
453
- * ```typescript
454
- * const serviceRequest = await medplum.readResource('ServiceRequest', '123');
455
- * const patient = await medplum.readCachedReference(serviceRequest.subject);
456
- * console.log(patient);
457
- * ```
458
- *
459
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
460
- *
461
- * @param reference The FHIR reference object.
462
- * @returns The resource if available; undefined otherwise.
463
- */
464
- readCachedReference<T extends Resource>(reference: Reference<T>): ReadablePromise<T>;
465
- /**
466
- * Returns a cached schema for a resource type.
467
- * If the schema is not cached, returns undefined.
468
- * It is assumed that a client will call requestSchema before using this method.
469
- * @param resourceType The FHIR resource type.
470
- * @returns The schema if immediately available, undefined otherwise.
471
- */
472
- getSchema(): IndexedStructureDefinition;
473
- /**
474
- * Requests the schema for a resource type.
475
- * If the schema is already cached, the promise is resolved immediately.
476
- * @param resourceType The FHIR resource type.
477
- * @returns Promise to a schema with the requested resource type.
478
- */
479
- requestSchema(resourceType: string): Promise<IndexedStructureDefinition>;
480
- /**
481
- * Reads resource history by resource type and ID.
482
- *
483
- * The return value is a bundle of all versions of the resource.
484
- *
485
- * Example:
486
- *
487
- * ```typescript
488
- * const history = await medplum.readHistory('Patient', '123');
489
- * console.log(history);
490
- * ```
491
- *
492
- * See the FHIR "history" operation for full details: https://www.hl7.org/fhir/http.html#history
493
- *
494
- * @param resourceType The FHIR resource type.
495
- * @param id The resource ID.
496
- * @returns The resource if available; undefined otherwise.
497
- */
498
- readHistory<T extends Resource>(resourceType: string, id: string): Promise<Bundle<T>>;
499
- /**
500
- * Reads a specific version of a resource by resource type, ID, and version ID.
501
- *
502
- * Example:
503
- *
504
- * ```typescript
505
- * const version = await medplum.readVersion('Patient', '123', '456');
506
- * console.log(version);
507
- * ```
508
- *
509
- * See the FHIR "vread" operation for full details: https://www.hl7.org/fhir/http.html#vread
510
- *
511
- * @param resourceType The FHIR resource type.
512
- * @param id The resource ID.
513
- * @returns The resource if available; undefined otherwise.
514
- */
515
- readVersion<T extends Resource>(resourceType: string, id: string, vid: string): Promise<T>;
516
- readPatientEverything(id: string): Promise<Bundle>;
517
- /**
518
- * Creates a new FHIR resource.
519
- *
520
- * The return value is the newly created resource, including the ID and meta.
521
- *
522
- * Example:
523
- *
524
- * ```typescript
525
- * const result = await medplum.createResource({
526
- * resourceType: 'Patient',
527
- * name: [{
528
- * family: 'Smith',
529
- * given: ['John']
530
- * }]
531
- * });
532
- * console.log(result.id);
533
- * ```
534
- *
535
- * See the FHIR "create" operation for full details: https://www.hl7.org/fhir/http.html#create
536
- *
537
- * @param resource The FHIR resource to create.
538
- * @returns The result of the create operation.
539
- */
540
- createResource<T extends Resource>(resource: T): Promise<T>;
541
- /**
542
- * Conditionally create a new FHIR resource only if some equivalent resource does not already exist on the server.
543
- *
544
- * The return value is the existing resource or the newly created resource, including the ID and meta.
545
- *
546
- * Example:
547
- *
548
- * ```typescript
549
- * const result = await medplum.createResourceIfNoneExist(
550
- * 'Patient?identifier=123',
551
- * {
552
- * resourceType: 'Patient',
553
- * identifier: [{
554
- * system: 'http://example.com/mrn',
555
- * value: '123'
556
- * }]
557
- * name: [{
558
- * family: 'Smith',
559
- * given: ['John']
560
- * }]
561
- * });
562
- * console.log(result.id);
563
- * ```
564
- *
565
- * This method is syntactic sugar for:
566
- *
567
- * ```typescript
568
- * return searchOne(query) ?? createResource(resource);
569
- * ```
570
- *
571
- * The query parameter only contains the search parameters (what would be in the URL following the "?").
572
- *
573
- * See the FHIR "conditional create" operation for full details: https://www.hl7.org/fhir/http.html#ccreate
574
- *
575
- * @param resource The FHIR resource to create.
576
- * @param query The search query for an equivalent resource.
577
- * @returns The result of the create operation.
578
- */
579
- createResourceIfNoneExist<T extends Resource>(resource: T, query: string): Promise<T>;
580
- /**
581
- * Creates a FHIR `Binary` resource with the provided data content.
582
- *
583
- * The return value is the newly created resource, including the ID and meta.
584
- *
585
- * The `data` parameter can be a string or a `File` object.
586
- *
587
- * A `File` object often comes from a `<input type="file">` element.
588
- *
589
- * Example:
590
- *
591
- * ```typescript
592
- * const result = await medplum.createBinary(myFile, 'test.jpg', 'image/jpeg');
593
- * console.log(result.id);
594
- * ```
595
- *
596
- * See the FHIR "create" operation for full details: https://www.hl7.org/fhir/http.html#create
597
- *
598
- * @param data The binary data to upload.
599
- * @param filename Optional filename for the binary.
600
- * @param contentType Content type for the binary.
601
- * @returns The result of the create operation.
602
- */
603
- createBinary(data: string | File, filename: string | undefined, contentType: string): Promise<Binary>;
604
- /**
605
- * Creates a PDF as a FHIR `Binary` resource based on pdfmake document definition.
606
- *
607
- * The return value is the newly created resource, including the ID and meta.
608
- *
609
- * The `docDefinition` parameter is a pdfmake document definition.
610
- *
611
- * Example:
612
- *
613
- * ```typescript
614
- * const result = await medplum.createPdf({
615
- * content: ['Hello world']
616
- * });
617
- * console.log(result.id);
618
- * ```
619
- *
620
- * See the pdfmake document definition for full details: https://pdfmake.github.io/docs/0.1/document-definition-object/
621
- *
622
- * @param docDefinition The FHIR resource to create.
623
- * @returns The result of the create operation.
624
- */
625
- createPdf(docDefinition: Record<string, unknown>, filename?: string): Promise<Binary>;
626
- /**
627
- * Updates a FHIR resource.
628
- *
629
- * The return value is the updated resource, including the ID and meta.
630
- *
631
- * Example:
632
- *
633
- * ```typescript
634
- * const result = await medplum.updateResource({
635
- * resourceType: 'Patient',
636
- * id: '123',
637
- * name: [{
638
- * family: 'Smith',
639
- * given: ['John']
640
- * }]
641
- * });
642
- * console.log(result.meta.versionId);
643
- * ```
644
- *
645
- * See the FHIR "update" operation for full details: https://www.hl7.org/fhir/http.html#update
646
- *
647
- * @param resource The FHIR resource to update.
648
- * @returns The result of the update operation.
649
- */
650
- updateResource<T extends Resource>(resource: T): Promise<T>;
651
- /**
652
- * Updates a FHIR resource using JSONPatch operations.
653
- *
654
- * The return value is the updated resource, including the ID and meta.
655
- *
656
- * Example:
657
- *
658
- * ```typescript
659
- * const result = await medplum.patchResource('Patient', '123', [
660
- * {op: 'replace', path: '/name/0/family', value: 'Smith'},
661
- * ]);
662
- * console.log(result.meta.versionId);
663
- * ```
664
- *
665
- * See the FHIR "update" operation for full details: https://www.hl7.org/fhir/http.html#patch
666
- *
667
- * See the JSONPatch specification for full details: https://tools.ietf.org/html/rfc6902
668
- *
669
- * @param resourceType The FHIR resource type.
670
- * @param id The resource ID.
671
- * @param operations The JSONPatch operations.
672
- * @returns The result of the patch operations.
673
- */
674
- patchResource<T extends Resource>(resourceType: string, id: string, operations: Operation[]): Promise<T>;
675
- /**
676
- * Deletes a FHIR resource by resource type and ID.
677
- *
678
- * Example:
679
- *
680
- * ```typescript
681
- * await medplum.deleteResource('Patient', '123');
682
- * ```
683
- *
684
- * See the FHIR "delete" operation for full details: https://www.hl7.org/fhir/http.html#delete
685
- *
686
- * @param resourceType The FHIR resource type.
687
- * @param id The resource ID.
688
- * @returns The result of the delete operation.
689
- */
690
- deleteResource(resourceType: string, id: string): Promise<any>;
691
- graphql(query: string, options?: RequestInit): Promise<any>;
692
- getActiveLogin(): LoginState | undefined;
693
- setActiveLogin(login: LoginState): Promise<void>;
694
- setAccessToken(accessToken: string): void;
695
- getLogins(): LoginState[];
696
- isLoading(): boolean;
697
- getProfile(): ProfileResource | undefined;
698
- getProfileAsync(): Promise<ProfileResource | undefined>;
699
- getUserConfiguration(): UserConfiguration | undefined;
700
- /**
701
- * Downloads the URL as a blob.
702
- * @param url The URL to request.
703
- * @returns Promise to the response body as a blob.
704
- */
705
- download(url: string, options?: RequestInit): Promise<Blob>;
706
- /**
707
- * Processes an OAuth authorization code.
708
- * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenRequest
709
- * @param code The authorization code received by URL parameter.
710
- */
711
- processCode(code: string): Promise<ProfileResource>;
712
- clientCredentials(clientId: string, clientSecret: string): Promise<ProfileResource>;
713
- }
1
+ import { Binary, Bundle, Communication, OperationOutcome, Project, ProjectMembership, Reference, Resource, UserConfiguration, ValueSet } from '@medplum/fhirtypes';
2
+ import { EventTarget } from './eventtarget';
3
+ import { Hl7Message } from './hl7';
4
+ import { ReadablePromise } from './readablepromise';
5
+ import { SearchRequest } from './search';
6
+ import { IndexedStructureDefinition } from './types';
7
+ import { ProfileResource } from './utils';
8
+ /**
9
+ * The MedplumClientOptions interface defines configuration options for MedplumClient.
10
+ *
11
+ * All configuration settings are optional.
12
+ */
13
+ export interface MedplumClientOptions {
14
+ /**
15
+ * Base server URL.
16
+ *
17
+ * Default value is "https://api.medplum.com/".
18
+ *
19
+ * Use this to point to a custom Medplum deployment.
20
+ */
21
+ baseUrl?: string;
22
+ /**
23
+ * OAuth2 authorize URL.
24
+ *
25
+ * Default value is baseUrl + "/oauth2/authorize".
26
+ *
27
+ * Use this if you want to use a separate OAuth server.
28
+ */
29
+ authorizeUrl?: string;
30
+ /**
31
+ * OAuth2 token URL.
32
+ *
33
+ * Default value is baseUrl + "/oauth2/token".
34
+ *
35
+ * Use this if you want to use a separate OAuth server.
36
+ */
37
+ tokenUrl?: string;
38
+ /**
39
+ * OAuth2 logout URL.
40
+ *
41
+ * Default value is baseUrl + "/oauth2/logout".
42
+ *
43
+ * Use this if you want to use a separate OAuth server.
44
+ */
45
+ logoutUrl?: string;
46
+ /**
47
+ * The client ID.
48
+ *
49
+ * Client ID can be used for SMART-on-FHIR customization.
50
+ */
51
+ clientId?: string;
52
+ /**
53
+ * Number of resources to store in the cache.
54
+ *
55
+ * Default value is 1000.
56
+ *
57
+ * Consider using this for performance of displaying Patient or Practitioner resources.
58
+ */
59
+ resourceCacheSize?: number;
60
+ /**
61
+ * Fetch implementation.
62
+ *
63
+ * Default is window.fetch (if available).
64
+ *
65
+ * For nodejs applications, consider the 'node-fetch' package.
66
+ */
67
+ fetch?: FetchLike;
68
+ /**
69
+ * Callback for when the client is unauthenticated.
70
+ *
71
+ * Default is do nothing.
72
+ *
73
+ * For client side applications, consider redirecting to a sign in page.
74
+ */
75
+ onUnauthenticated?: () => void;
76
+ }
77
+ export interface FetchLike {
78
+ (url: string, options?: any): Promise<any>;
79
+ }
80
+ export interface LoginRequest {
81
+ readonly email: string;
82
+ readonly password: string;
83
+ readonly remember?: boolean;
84
+ readonly projectId?: string;
85
+ }
86
+ export interface RegisterRequest {
87
+ readonly firstName: string;
88
+ readonly lastName: string;
89
+ readonly projectName: string;
90
+ readonly email: string;
91
+ readonly password: string;
92
+ readonly remember?: boolean;
93
+ readonly recaptchaToken: string;
94
+ }
95
+ export interface GoogleCredentialResponse {
96
+ readonly clientId: string;
97
+ readonly credential: string;
98
+ }
99
+ export interface LoginAuthenticationResponse {
100
+ readonly login: string;
101
+ readonly code?: string;
102
+ readonly memberships?: ProjectMembership[];
103
+ }
104
+ export interface LoginProfileResponse {
105
+ readonly login: string;
106
+ readonly scope: string;
107
+ }
108
+ export interface LoginScopeResponse {
109
+ readonly login: string;
110
+ readonly code: string;
111
+ }
112
+ export interface LoginState {
113
+ readonly project: Reference<Project>;
114
+ readonly profile: Reference<ProfileResource>;
115
+ readonly accessToken: string;
116
+ readonly refreshToken: string;
117
+ }
118
+ export interface TokenResponse {
119
+ readonly token_type: string;
120
+ readonly id_token: string;
121
+ readonly access_token: string;
122
+ readonly refresh_token: string;
123
+ readonly expires_in: number;
124
+ readonly project: Reference<Project>;
125
+ readonly profile: Reference<ProfileResource>;
126
+ }
127
+ export interface BotEvent {
128
+ readonly contentType: string;
129
+ readonly input: Resource | Hl7Message | string;
130
+ }
131
+ /**
132
+ * JSONPatch patch operation.
133
+ * Compatible with fast-json-patch Operation.
134
+ */
135
+ export interface PatchOperation {
136
+ readonly op: string;
137
+ readonly path: string;
138
+ readonly value?: any;
139
+ }
140
+ /**
141
+ * Email address definition.
142
+ * Compatible with nodemailer Mail.Address.
143
+ */
144
+ export interface MailAddress {
145
+ readonly name: string;
146
+ readonly address: string;
147
+ }
148
+ /**
149
+ * Email attachment definition.
150
+ * Compatible with nodemailer Mail.Options.
151
+ */
152
+ export interface MailAttachment {
153
+ /** String, Buffer or a Stream contents for the attachmentent */
154
+ readonly content?: string;
155
+ /** path to a file or an URL (data uris are allowed as well) if you want to stream the file instead of including it (better for larger attachments) */
156
+ readonly path?: string;
157
+ /** filename to be reported as the name of the attached file, use of unicode is allowed. If you do not want to use a filename, set this value as false, otherwise a filename is generated automatically */
158
+ readonly filename?: string | false;
159
+ /** optional content type for the attachment, if not set will be derived from the filename property */
160
+ readonly contentType?: string;
161
+ }
162
+ /**
163
+ * Email message definition.
164
+ * Compatible with nodemailer Mail.Options.
165
+ */
166
+ export interface MailOptions {
167
+ /** The e-mail address of the sender. All e-mail addresses can be plain 'sender@server.com' or formatted 'Sender Name <sender@server.com>' */
168
+ readonly from?: string | MailAddress;
169
+ /** An e-mail address that will appear on the Sender: field */
170
+ readonly sender?: string | MailAddress;
171
+ /** Comma separated list or an array of recipients e-mail addresses that will appear on the To: field */
172
+ readonly to?: string | MailAddress | string[] | MailAddress[];
173
+ /** Comma separated list or an array of recipients e-mail addresses that will appear on the Cc: field */
174
+ readonly cc?: string | MailAddress | string[] | MailAddress[];
175
+ /** Comma separated list or an array of recipients e-mail addresses that will appear on the Bcc: field */
176
+ readonly bcc?: string | MailAddress | string[] | MailAddress[];
177
+ /** An e-mail address that will appear on the Reply-To: field */
178
+ readonly replyTo?: string | MailAddress;
179
+ /** The subject of the e-mail */
180
+ readonly subject?: string;
181
+ /** The plaintext version of the message */
182
+ readonly text?: string;
183
+ /** The HTML version of the message */
184
+ readonly html?: string;
185
+ /** An array of attachment objects */
186
+ readonly attachments?: MailAttachment[];
187
+ }
188
+ /**
189
+ * The MedplumClient class provides a client for the Medplum FHIR server.
190
+ *
191
+ * The client can be used in the browser, in a NodeJS application, or in a Medplum Bot.
192
+ *
193
+ * The client provides helpful methods for common operations such as:
194
+ * 1) Authenticating
195
+ * 2) Creating resources
196
+ * 2) Reading resources
197
+ * 3) Updating resources
198
+ * 5) Deleting resources
199
+ * 6) Searching
200
+ * 7) Making GraphQL queries
201
+ *
202
+ * Here is a quick example of how to use the client:
203
+ *
204
+ * ```typescript
205
+ * import { MedplumClient } from '@medplum/core';
206
+ * const medplum = new MedplumClient();
207
+ * ```
208
+ *
209
+ * Create a `Patient`:
210
+ *
211
+ * ```typescript
212
+ * const patient = await medplum.createResource({
213
+ * resourceType: 'Patient',
214
+ * name: [{
215
+ * given: ['Alice'],
216
+ * family: 'Smith'
217
+ * }]
218
+ * });
219
+ * ```
220
+ *
221
+ * Read a `Patient` by ID:
222
+ *
223
+ * ```typescript
224
+ * const patient = await medplum.readResource('Patient', '123');
225
+ * console.log(patient.name[0].given[0]);
226
+ * ```
227
+ *
228
+ * Search for a `Patient` by name:
229
+ *
230
+ * ```typescript
231
+ * const bundle = await medplum.search('Patient?name=Alice');
232
+ * console.log(bundle.total);
233
+ * ```
234
+ */
235
+ export declare class MedplumClient extends EventTarget {
236
+ #private;
237
+ constructor(options?: MedplumClientOptions);
238
+ /**
239
+ * Returns the current base URL for all API requests.
240
+ * By default, this is set to `https://api.medplum.com/`.
241
+ * This can be overridden by setting the `baseUrl` option when creating the client.
242
+ * @returns The current base URL for all API requests.
243
+ */
244
+ getBaseUrl(): string;
245
+ /**
246
+ * Clears all auth state including local storage and session storage.
247
+ */
248
+ clear(): void;
249
+ /**
250
+ * Makes an HTTP GET request to the specified URL.
251
+ *
252
+ * This is a lower level method for custom requests.
253
+ * For common operations, we recommend using higher level methods
254
+ * such as `readResource()`, `search()`, etc.
255
+ *
256
+ * @param url The target URL.
257
+ * @param options Optional fetch options.
258
+ * @returns Promise to the response content.
259
+ */
260
+ get<T = any>(url: URL | string, options?: RequestInit): ReadablePromise<T>;
261
+ /**
262
+ * Makes an HTTP POST request to the specified URL.
263
+ *
264
+ * This is a lower level method for custom requests.
265
+ * For common operations, we recommend using higher level methods
266
+ * such as `createResource()`.
267
+ *
268
+ * @param url The target URL.
269
+ * @param body The content body. Strings and `File` objects are passed directly. Other objects are converted to JSON.
270
+ * @param contentType The content type to be included in the "Content-Type" header.
271
+ * @param options Optional fetch options.
272
+ * @returns Promise to the response content.
273
+ */
274
+ post(url: URL | string, body: any, contentType?: string, options?: RequestInit): Promise<any>;
275
+ /**
276
+ * Makes an HTTP PUT request to the specified URL.
277
+ *
278
+ * This is a lower level method for custom requests.
279
+ * For common operations, we recommend using higher level methods
280
+ * such as `updateResource()`.
281
+ *
282
+ * @param url The target URL.
283
+ * @param body The content body. Strings and `File` objects are passed directly. Other objects are converted to JSON.
284
+ * @param contentType The content type to be included in the "Content-Type" header.
285
+ * @param options Optional fetch options.
286
+ * @returns Promise to the response content.
287
+ */
288
+ put(url: URL | string, body: any, contentType?: string, options?: RequestInit): Promise<any>;
289
+ /**
290
+ * Makes an HTTP PATCH request to the specified URL.
291
+ *
292
+ * This is a lower level method for custom requests.
293
+ * For common operations, we recommend using higher level methods
294
+ * such as `patchResource()`.
295
+ *
296
+ * @param url The target URL.
297
+ * @param operations Array of JSONPatch operations.
298
+ * @param options Optional fetch options.
299
+ * @returns Promise to the response content.
300
+ */
301
+ patch(url: URL | string, operations: PatchOperation[], options?: RequestInit): Promise<any>;
302
+ /**
303
+ * Makes an HTTP DELETE request to the specified URL.
304
+ *
305
+ * This is a lower level method for custom requests.
306
+ * For common operations, we recommend using higher level methods
307
+ * such as `deleteResource()`.
308
+ *
309
+ * @param url The target URL.
310
+ * @param options Optional fetch options.
311
+ * @returns Promise to the response content.
312
+ */
313
+ delete(url: URL | string, options?: RequestInit): Promise<any>;
314
+ /**
315
+ * Tries to register a new user.
316
+ * @param request The registration request.
317
+ * @returns Promise to the authentication response.
318
+ */
319
+ register(request: RegisterRequest): Promise<void>;
320
+ /**
321
+ * Initiates a user login flow.
322
+ * @param loginRequest Login request including email and password.
323
+ * @returns Promise to the authentication response.
324
+ */
325
+ startLogin(loginRequest: LoginRequest): Promise<LoginAuthenticationResponse>;
326
+ /**
327
+ * Tries to sign in with Google authentication.
328
+ * The response parameter is the result of a Google authentication.
329
+ * See: https://developers.google.com/identity/gsi/web/guides/handle-credential-responses-js-functions
330
+ * @param googleResponse The Google credential response.
331
+ * @returns Promise to the authentication response.
332
+ */
333
+ startGoogleLogin(googleResponse: GoogleCredentialResponse): Promise<LoginAuthenticationResponse>;
334
+ /**
335
+ * Signs out locally.
336
+ * Does not invalidate tokens with the server.
337
+ */
338
+ signOut(): Promise<void>;
339
+ /**
340
+ * Tries to sign in the user.
341
+ * Returns true if the user is signed in.
342
+ * This may result in navigating away to the sign in page.
343
+ */
344
+ signInWithRedirect(): Promise<ProfileResource | void> | undefined;
345
+ /**
346
+ * Tries to sign out the user.
347
+ * See: https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html
348
+ */
349
+ signOutWithRedirect(): void;
350
+ /**
351
+ * Builds a FHIR URL from a collection of URL path components.
352
+ * For example, `buildUrl('/Patient', '123')` returns `fhir/R4/Patient/123`.
353
+ * @param path The path component of the URL.
354
+ * @returns The well-formed FHIR URL.
355
+ */
356
+ fhirUrl(...path: string[]): URL;
357
+ /**
358
+ * Sends a FHIR search request.
359
+ *
360
+ * Example using a FHIR search string:
361
+ *
362
+ * ```typescript
363
+ * const bundle = await client.search('Patient?name=Alice');
364
+ * console.log(bundle);
365
+ * ```
366
+ *
367
+ * Example using a structured search:
368
+ *
369
+ * ```typescript
370
+ * const bundle = await client.search({
371
+ * resourceType: 'Patient',
372
+ * filters: [{
373
+ * code: 'name',
374
+ * operator: 'eq',
375
+ * value: 'Alice',
376
+ * }]
377
+ * });
378
+ * console.log(bundle);
379
+ * ```
380
+ *
381
+ * The return value is a FHIR bundle:
382
+ *
383
+ * ```json
384
+ * {
385
+ * "resourceType": "Bundle",
386
+ * "type": "searchest",
387
+ * "total": 1,
388
+ * "entry": [
389
+ * {
390
+ * "resource": {
391
+ * "resourceType": "Patient",
392
+ * "name": [
393
+ * {
394
+ * "given": [
395
+ * "George"
396
+ * ],
397
+ * "family": "Washington"
398
+ * }
399
+ * ],
400
+ * }
401
+ * }
402
+ * ]
403
+ * }
404
+ * ```
405
+ *
406
+ * See FHIR search for full details: https://www.hl7.org/fhir/search.html
407
+ *
408
+ * @param query The search query as either a string or a structured search object.
409
+ * @returns Promise to the search result bundle.
410
+ */
411
+ search<T extends Resource>(query: string | SearchRequest, options?: RequestInit): ReadablePromise<Bundle<T>>;
412
+ /**
413
+ * Sends a FHIR search request for a single resource.
414
+ *
415
+ * This is a convenience method for `search()` that returns the first resource rather than a `Bundle`.
416
+ *
417
+ * Example using a FHIR search string:
418
+ *
419
+ * ```typescript
420
+ * const patient = await client.searchOne('Patient?identifier=123');
421
+ * console.log(patient);
422
+ * ```
423
+ *
424
+ * The return value is the resource, if available; otherwise, undefined.
425
+ *
426
+ * See FHIR search for full details: https://www.hl7.org/fhir/search.html
427
+ *
428
+ * @param query The search query as either a string or a structured search object.
429
+ * @returns Promise to the search result bundle.
430
+ */
431
+ searchOne<T extends Resource>(query: string | SearchRequest, options?: RequestInit): ReadablePromise<T | undefined>;
432
+ /**
433
+ * Sends a FHIR search request for an array of resources.
434
+ *
435
+ * This is a convenience method for `search()` that returns the resources as an array rather than a `Bundle`.
436
+ *
437
+ * Example using a FHIR search string:
438
+ *
439
+ * ```typescript
440
+ * const patients = await client.searchResources('Patient?name=Alice');
441
+ * console.log(patients);
442
+ * ```
443
+ *
444
+ * The return value is an array of resources.
445
+ *
446
+ * See FHIR search for full details: https://www.hl7.org/fhir/search.html
447
+ *
448
+ * @param query The search query as either a string or a structured search object.
449
+ * @returns Promise to the search result bundle.
450
+ */
451
+ searchResources<T extends Resource>(query: string | SearchRequest, options?: RequestInit): ReadablePromise<T[]>;
452
+ /**
453
+ * Searches a ValueSet resource using the "expand" operation.
454
+ * See: https://www.hl7.org/fhir/operation-valueset-expand.html
455
+ * @param system The ValueSet system url.
456
+ * @param filter The search string.
457
+ * @returns Promise to expanded ValueSet.
458
+ */
459
+ searchValueSet(system: string, filter: string, options?: RequestInit): ReadablePromise<ValueSet>;
460
+ /**
461
+ * Returns a cached resource if it is available.
462
+ * @param resourceType The FHIR resource type.
463
+ * @param id The FHIR resource ID.
464
+ * @returns The resource if it is available in the cache; undefined otherwise.
465
+ */
466
+ getCached<T extends Resource>(resourceType: string, id: string): T | undefined;
467
+ /**
468
+ * Returns a cached resource if it is available.
469
+ * @param resourceType The FHIR resource type.
470
+ * @param id The FHIR resource ID.
471
+ * @returns The resource if it is available in the cache; undefined otherwise.
472
+ */
473
+ getCachedReference<T extends Resource>(reference: Reference<T>): T | undefined;
474
+ /**
475
+ * Reads a resource by resource type and ID.
476
+ *
477
+ * Example:
478
+ *
479
+ * ```typescript
480
+ * const patient = await medplum.readResource('Patient', '123');
481
+ * console.log(patient);
482
+ * ```
483
+ *
484
+ * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
485
+ *
486
+ * @param resourceType The FHIR resource type.
487
+ * @param id The resource ID.
488
+ * @returns The resource if available; undefined otherwise.
489
+ */
490
+ readResource<T extends Resource>(resourceType: string, id: string): ReadablePromise<T>;
491
+ /**
492
+ * Reads a resource by resource type and ID using the in-memory resource cache.
493
+ *
494
+ * If the resource is not available in the cache, it will be read from the server.
495
+ *
496
+ * Example:
497
+ *
498
+ * ```typescript
499
+ * const patient = await medplum.readCached('Patient', '123');
500
+ * console.log(patient);
501
+ * ```
502
+ *
503
+ * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
504
+ *
505
+ * @param resourceType The FHIR resource type.
506
+ * @param id The resource ID.
507
+ * @returns The resource if available; undefined otherwise.
508
+ */
509
+ readCached<T extends Resource>(resourceType: string, id: string): ReadablePromise<T>;
510
+ /**
511
+ * Reads a resource by `Reference`.
512
+ *
513
+ * This is a convenience method for `readResource()` that accepts a `Reference` object.
514
+ *
515
+ * Example:
516
+ *
517
+ * ```typescript
518
+ * const serviceRequest = await medplum.readResource('ServiceRequest', '123');
519
+ * const patient = await medplum.readReference(serviceRequest.subject);
520
+ * console.log(patient);
521
+ * ```
522
+ *
523
+ * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
524
+ *
525
+ * @param reference The FHIR reference object.
526
+ * @returns The resource if available; undefined otherwise.
527
+ */
528
+ readReference<T extends Resource>(reference: Reference<T>): ReadablePromise<T>;
529
+ /**
530
+ * Reads a resource by `Reference` using the in-memory resource cache.
531
+ *
532
+ * This is a convenience method for `readResource()` that accepts a `Reference` object.
533
+ *
534
+ * If the resource is not available in the cache, it will be read from the server.
535
+ *
536
+ * Example:
537
+ *
538
+ * ```typescript
539
+ * const serviceRequest = await medplum.readResource('ServiceRequest', '123');
540
+ * const patient = await medplum.readCachedReference(serviceRequest.subject);
541
+ * console.log(patient);
542
+ * ```
543
+ *
544
+ * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
545
+ *
546
+ * @param reference The FHIR reference object.
547
+ * @returns The resource if available; undefined otherwise.
548
+ */
549
+ readCachedReference<T extends Resource>(reference: Reference<T>): ReadablePromise<T>;
550
+ /**
551
+ * Returns a cached schema for a resource type.
552
+ * If the schema is not cached, returns undefined.
553
+ * It is assumed that a client will call requestSchema before using this method.
554
+ * @param resourceType The FHIR resource type.
555
+ * @returns The schema if immediately available, undefined otherwise.
556
+ */
557
+ getSchema(): IndexedStructureDefinition;
558
+ /**
559
+ * Requests the schema for a resource type.
560
+ * If the schema is already cached, the promise is resolved immediately.
561
+ * @param resourceType The FHIR resource type.
562
+ * @returns Promise to a schema with the requested resource type.
563
+ */
564
+ requestSchema(resourceType: string): Promise<IndexedStructureDefinition>;
565
+ /**
566
+ * Reads resource history by resource type and ID.
567
+ *
568
+ * The return value is a bundle of all versions of the resource.
569
+ *
570
+ * Example:
571
+ *
572
+ * ```typescript
573
+ * const history = await medplum.readHistory('Patient', '123');
574
+ * console.log(history);
575
+ * ```
576
+ *
577
+ * See the FHIR "history" operation for full details: https://www.hl7.org/fhir/http.html#history
578
+ *
579
+ * @param resourceType The FHIR resource type.
580
+ * @param id The resource ID.
581
+ * @returns Promise to the resource history.
582
+ */
583
+ readHistory<T extends Resource>(resourceType: string, id: string): ReadablePromise<Bundle<T>>;
584
+ /**
585
+ * Reads a specific version of a resource by resource type, ID, and version ID.
586
+ *
587
+ * Example:
588
+ *
589
+ * ```typescript
590
+ * const version = await medplum.readVersion('Patient', '123', '456');
591
+ * console.log(version);
592
+ * ```
593
+ *
594
+ * See the FHIR "vread" operation for full details: https://www.hl7.org/fhir/http.html#vread
595
+ *
596
+ * @param resourceType The FHIR resource type.
597
+ * @param id The resource ID.
598
+ * @returns The resource if available; undefined otherwise.
599
+ */
600
+ readVersion<T extends Resource>(resourceType: string, id: string, vid: string): ReadablePromise<T>;
601
+ readPatientEverything(id: string): ReadablePromise<Bundle>;
602
+ /**
603
+ * Creates a new FHIR resource.
604
+ *
605
+ * The return value is the newly created resource, including the ID and meta.
606
+ *
607
+ * Example:
608
+ *
609
+ * ```typescript
610
+ * const result = await medplum.createResource({
611
+ * resourceType: 'Patient',
612
+ * name: [{
613
+ * family: 'Smith',
614
+ * given: ['John']
615
+ * }]
616
+ * });
617
+ * console.log(result.id);
618
+ * ```
619
+ *
620
+ * See the FHIR "create" operation for full details: https://www.hl7.org/fhir/http.html#create
621
+ *
622
+ * @param resource The FHIR resource to create.
623
+ * @returns The result of the create operation.
624
+ */
625
+ createResource<T extends Resource>(resource: T): Promise<T>;
626
+ /**
627
+ * Conditionally create a new FHIR resource only if some equivalent resource does not already exist on the server.
628
+ *
629
+ * The return value is the existing resource or the newly created resource, including the ID and meta.
630
+ *
631
+ * Example:
632
+ *
633
+ * ```typescript
634
+ * const result = await medplum.createResourceIfNoneExist(
635
+ * 'Patient?identifier=123',
636
+ * {
637
+ * resourceType: 'Patient',
638
+ * identifier: [{
639
+ * system: 'http://example.com/mrn',
640
+ * value: '123'
641
+ * }]
642
+ * name: [{
643
+ * family: 'Smith',
644
+ * given: ['John']
645
+ * }]
646
+ * });
647
+ * console.log(result.id);
648
+ * ```
649
+ *
650
+ * This method is syntactic sugar for:
651
+ *
652
+ * ```typescript
653
+ * return searchOne(query) ?? createResource(resource);
654
+ * ```
655
+ *
656
+ * The query parameter only contains the search parameters (what would be in the URL following the "?").
657
+ *
658
+ * See the FHIR "conditional create" operation for full details: https://www.hl7.org/fhir/http.html#ccreate
659
+ *
660
+ * @param resource The FHIR resource to create.
661
+ * @param query The search query for an equivalent resource.
662
+ * @returns The result of the create operation.
663
+ */
664
+ createResourceIfNoneExist<T extends Resource>(resource: T, query: string): Promise<T>;
665
+ /**
666
+ * Creates a FHIR `Binary` resource with the provided data content.
667
+ *
668
+ * The return value is the newly created resource, including the ID and meta.
669
+ *
670
+ * The `data` parameter can be a string or a `File` object.
671
+ *
672
+ * A `File` object often comes from a `<input type="file">` element.
673
+ *
674
+ * Example:
675
+ *
676
+ * ```typescript
677
+ * const result = await medplum.createBinary(myFile, 'test.jpg', 'image/jpeg');
678
+ * console.log(result.id);
679
+ * ```
680
+ *
681
+ * See the FHIR "create" operation for full details: https://www.hl7.org/fhir/http.html#create
682
+ *
683
+ * @param data The binary data to upload.
684
+ * @param filename Optional filename for the binary.
685
+ * @param contentType Content type for the binary.
686
+ * @returns The result of the create operation.
687
+ */
688
+ createBinary(data: string | File, filename: string | undefined, contentType: string): Promise<Binary>;
689
+ /**
690
+ * Creates a PDF as a FHIR `Binary` resource based on pdfmake document definition.
691
+ *
692
+ * The return value is the newly created resource, including the ID and meta.
693
+ *
694
+ * The `docDefinition` parameter is a pdfmake document definition.
695
+ *
696
+ * Example:
697
+ *
698
+ * ```typescript
699
+ * const result = await medplum.createPdf({
700
+ * content: ['Hello world']
701
+ * });
702
+ * console.log(result.id);
703
+ * ```
704
+ *
705
+ * See the pdfmake document definition for full details: https://pdfmake.github.io/docs/0.1/document-definition-object/
706
+ *
707
+ * @param docDefinition The FHIR resource to create.
708
+ * @returns The result of the create operation.
709
+ */
710
+ createPdf(docDefinition: Record<string, unknown>, filename?: string): Promise<Binary>;
711
+ /**
712
+ * Creates a FHIR `Communication` resource with the provided data content.
713
+ *
714
+ * This is a convenience method to handle commmon cases where a `Communication` resource is created with a `payload`.
715
+ *
716
+ * @param resource The FHIR resource to comment on.
717
+ * @param text The text of the comment.
718
+ * @returns The result of the create operation.
719
+ */
720
+ createComment(resource: Resource, text: string): Promise<Communication>;
721
+ /**
722
+ * Updates a FHIR resource.
723
+ *
724
+ * The return value is the updated resource, including the ID and meta.
725
+ *
726
+ * Example:
727
+ *
728
+ * ```typescript
729
+ * const result = await medplum.updateResource({
730
+ * resourceType: 'Patient',
731
+ * id: '123',
732
+ * name: [{
733
+ * family: 'Smith',
734
+ * given: ['John']
735
+ * }]
736
+ * });
737
+ * console.log(result.meta.versionId);
738
+ * ```
739
+ *
740
+ * See the FHIR "update" operation for full details: https://www.hl7.org/fhir/http.html#update
741
+ *
742
+ * @param resource The FHIR resource to update.
743
+ * @returns The result of the update operation.
744
+ */
745
+ updateResource<T extends Resource>(resource: T): Promise<T>;
746
+ /**
747
+ * Updates a FHIR resource using JSONPatch operations.
748
+ *
749
+ * The return value is the updated resource, including the ID and meta.
750
+ *
751
+ * Example:
752
+ *
753
+ * ```typescript
754
+ * const result = await medplum.patchResource('Patient', '123', [
755
+ * {op: 'replace', path: '/name/0/family', value: 'Smith'},
756
+ * ]);
757
+ * console.log(result.meta.versionId);
758
+ * ```
759
+ *
760
+ * See the FHIR "update" operation for full details: https://www.hl7.org/fhir/http.html#patch
761
+ *
762
+ * See the JSONPatch specification for full details: https://tools.ietf.org/html/rfc6902
763
+ *
764
+ * @param resourceType The FHIR resource type.
765
+ * @param id The resource ID.
766
+ * @param operations The JSONPatch operations.
767
+ * @returns The result of the patch operations.
768
+ */
769
+ patchResource<T extends Resource>(resourceType: string, id: string, operations: PatchOperation[]): Promise<T>;
770
+ /**
771
+ * Deletes a FHIR resource by resource type and ID.
772
+ *
773
+ * Example:
774
+ *
775
+ * ```typescript
776
+ * await medplum.deleteResource('Patient', '123');
777
+ * ```
778
+ *
779
+ * See the FHIR "delete" operation for full details: https://www.hl7.org/fhir/http.html#delete
780
+ *
781
+ * @param resourceType The FHIR resource type.
782
+ * @param id The resource ID.
783
+ * @returns The result of the delete operation.
784
+ */
785
+ deleteResource(resourceType: string, id: string): Promise<any>;
786
+ /**
787
+ * Sends an email using the Medplum Email API.
788
+ *
789
+ * Builds the email using nodemailer MailComposer.
790
+ *
791
+ * Examples:
792
+ *
793
+ * Send a simple text email:
794
+ *
795
+ * ```typescript
796
+ * await medplum.sendEmail({
797
+ * to: 'alice@example.com',
798
+ * cc: 'bob@example.com',
799
+ * subject: 'Hello',
800
+ * text: 'Hello Alice',
801
+ * });
802
+ * ```
803
+ *
804
+ * Send an email with a `Binary` attachment:
805
+ *
806
+ * ```typescript
807
+ * await medplum.sendEmail({
808
+ * to: 'alice@example.com',
809
+ * subject: 'Email with attachment',
810
+ * text: 'See the attached report',
811
+ * attachments: [{
812
+ * filename: 'report.pdf',
813
+ * path: "Binary/" + binary.id
814
+ * }]
815
+ * });
816
+ * ```
817
+ *
818
+ * See options here: https://nodemailer.com/extras/mailcomposer/
819
+ *
820
+ * @param options The MailComposer options.
821
+ * @returns Promise to the operation outcome.
822
+ */
823
+ sendEmail(email: MailOptions): Promise<OperationOutcome>;
824
+ graphql(query: string, options?: RequestInit): Promise<any>;
825
+ getActiveLogin(): LoginState | undefined;
826
+ setActiveLogin(login: LoginState): Promise<void>;
827
+ getAccessToken(): string | undefined;
828
+ setAccessToken(accessToken: string): void;
829
+ getLogins(): LoginState[];
830
+ isLoading(): boolean;
831
+ getProfile(): ProfileResource | undefined;
832
+ getProfileAsync(): Promise<ProfileResource | undefined>;
833
+ getUserConfiguration(): UserConfiguration | undefined;
834
+ /**
835
+ * Downloads the URL as a blob.
836
+ * @param url The URL to request.
837
+ * @returns Promise to the response body as a blob.
838
+ */
839
+ download(url: URL | string, options?: RequestInit): Promise<Blob>;
840
+ /**
841
+ * Processes an OAuth authorization code.
842
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenRequest
843
+ * @param code The authorization code received by URL parameter.
844
+ */
845
+ processCode(code: string): Promise<ProfileResource>;
846
+ /**
847
+ * Starts a new OAuth2 client credentials flow.
848
+ * See: https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
849
+ * @param clientId The client ID.
850
+ * @param clientSecret The client secret.
851
+ * @returns Promise that resolves to the client profile.
852
+ */
853
+ startClientLogin(clientId: string, clientSecret: string): Promise<ProfileResource>;
854
+ }