@medplum/core 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +5 -3
  2. package/dist/cjs/index.js +1842 -0
  3. package/dist/cjs/index.js.map +1 -0
  4. package/dist/cjs/index.min.js +16 -0
  5. package/dist/cjs/index.min.js.map +1 -0
  6. package/dist/esm/index.js +1792 -0
  7. package/dist/esm/index.js.map +1 -0
  8. package/dist/esm/index.min.js +16 -0
  9. package/dist/esm/index.min.js.map +1 -0
  10. package/dist/{cache.d.ts → types/cache.d.ts} +1 -3
  11. package/dist/{client.d.ts → types/client.d.ts} +54 -69
  12. package/dist/{crypto.d.ts → types/crypto.d.ts} +0 -0
  13. package/dist/{eventtarget.d.ts → types/eventtarget.d.ts} +3 -3
  14. package/dist/{format.d.ts → types/format.d.ts} +0 -0
  15. package/dist/{index.d.ts → types/index.d.ts} +0 -0
  16. package/dist/types/jwt.d.ts +5 -0
  17. package/dist/{outcomes.d.ts → types/outcomes.d.ts} +6 -1
  18. package/dist/{search.d.ts → types/search.d.ts} +4 -0
  19. package/dist/{searchparams.d.ts → types/searchparams.d.ts} +2 -1
  20. package/dist/{storage.d.ts → types/storage.d.ts} +2 -2
  21. package/dist/{types.d.ts → types/types.d.ts} +18 -3
  22. package/dist/{utils.d.ts → types/utils.d.ts} +6 -0
  23. package/package.json +11 -5
  24. package/dist/cache.js +0 -39
  25. package/dist/cache.js.map +0 -1
  26. package/dist/client.js +0 -572
  27. package/dist/client.js.map +0 -1
  28. package/dist/crypto.js +0 -33
  29. package/dist/crypto.js.map +0 -1
  30. package/dist/eventtarget.js +0 -38
  31. package/dist/eventtarget.js.map +0 -1
  32. package/dist/format.js +0 -56
  33. package/dist/format.js.map +0 -1
  34. package/dist/index.js +0 -20
  35. package/dist/index.js.map +0 -1
  36. package/dist/jwt.d.ts +0 -5
  37. package/dist/jwt.js +0 -28
  38. package/dist/jwt.js.map +0 -1
  39. package/dist/outcomes.js +0 -154
  40. package/dist/outcomes.js.map +0 -1
  41. package/dist/search.js +0 -120
  42. package/dist/search.js.map +0 -1
  43. package/dist/searchparams.js +0 -128
  44. package/dist/searchparams.js.map +0 -1
  45. package/dist/storage.js +0 -90
  46. package/dist/storage.js.map +0 -1
  47. package/dist/types.js +0 -171
  48. package/dist/types.js.map +0 -1
  49. package/dist/utils.js +0 -239
  50. package/dist/utils.js.map +0 -1
@@ -0,0 +1,1792 @@
1
+ /*! *****************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+
16
+ function __awaiter(thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ }
25
+
26
+ function __classPrivateFieldGet(receiver, state, kind, f) {
27
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
28
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
29
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
30
+ }
31
+
32
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
33
+ if (kind === "m") throw new TypeError("Private method is not writable");
34
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
35
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
36
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
37
+ }
38
+
39
+ var _LRUCache_instances, _LRUCache_max, _LRUCache_cache, _LRUCache_first;
40
+ /**
41
+ * LRU cache (least recently used)
42
+ * Source: https://stackoverflow.com/a/46432113
43
+ */
44
+ class LRUCache {
45
+ constructor(max = 10) {
46
+ _LRUCache_instances.add(this);
47
+ _LRUCache_max.set(this, void 0);
48
+ _LRUCache_cache.set(this, void 0);
49
+ __classPrivateFieldSet(this, _LRUCache_max, max, "f");
50
+ __classPrivateFieldSet(this, _LRUCache_cache, new Map(), "f");
51
+ }
52
+ clear() {
53
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").clear();
54
+ }
55
+ get(key) {
56
+ const item = __classPrivateFieldGet(this, _LRUCache_cache, "f").get(key);
57
+ if (item) {
58
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(key);
59
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").set(key, item);
60
+ }
61
+ return item;
62
+ }
63
+ set(key, val) {
64
+ if (__classPrivateFieldGet(this, _LRUCache_cache, "f").has(key)) {
65
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(key);
66
+ }
67
+ else if (__classPrivateFieldGet(this, _LRUCache_cache, "f").size >= __classPrivateFieldGet(this, _LRUCache_max, "f")) {
68
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(__classPrivateFieldGet(this, _LRUCache_instances, "m", _LRUCache_first).call(this));
69
+ }
70
+ __classPrivateFieldGet(this, _LRUCache_cache, "f").set(key, val);
71
+ }
72
+ }
73
+ _LRUCache_max = new WeakMap(), _LRUCache_cache = new WeakMap(), _LRUCache_instances = new WeakSet(), _LRUCache_first = function _LRUCache_first() {
74
+ // This works because the Map class maintains ordered keys.
75
+ return __classPrivateFieldGet(this, _LRUCache_cache, "f").keys().next().value;
76
+ };
77
+
78
+ function formatAddress(address, options) {
79
+ const builder = [];
80
+ if (address.line) {
81
+ builder.push(...address.line);
82
+ }
83
+ if (address.city) {
84
+ builder.push(address.city);
85
+ }
86
+ if (address.state) {
87
+ builder.push(address.state);
88
+ }
89
+ if (address.postalCode) {
90
+ builder.push(address.postalCode);
91
+ }
92
+ if (address.use && ((options === null || options === void 0 ? void 0 : options.all) || (options === null || options === void 0 ? void 0 : options.use))) {
93
+ builder.push('[' + address.use + ']');
94
+ }
95
+ return builder.join(', ').trim();
96
+ }
97
+ function formatHumanName(name, options) {
98
+ const builder = [];
99
+ if (name.prefix && ((options === null || options === void 0 ? void 0 : options.all) || (options === null || options === void 0 ? void 0 : options.prefix))) {
100
+ builder.push(...name.prefix);
101
+ }
102
+ if (name.given) {
103
+ builder.push(...name.given);
104
+ }
105
+ if (name.family) {
106
+ builder.push(name.family);
107
+ }
108
+ if (name.suffix && ((options === null || options === void 0 ? void 0 : options.all) || (options === null || options === void 0 ? void 0 : options.suffix))) {
109
+ builder.push(...name.suffix);
110
+ }
111
+ if (name.use && ((options === null || options === void 0 ? void 0 : options.all) || (options === null || options === void 0 ? void 0 : options.use))) {
112
+ builder.push('[' + name.use + ']');
113
+ }
114
+ return builder.join(' ').trim();
115
+ }
116
+ function formatGivenName(name) {
117
+ const builder = [];
118
+ if (name.given) {
119
+ builder.push(...name.given);
120
+ }
121
+ return builder.join(' ').trim();
122
+ }
123
+ function formatFamilyName(name) {
124
+ return name.family || '';
125
+ }
126
+
127
+ /**
128
+ * Creates a reference resource.
129
+ * @param resource The FHIR reesource.
130
+ * @returns A reference resource.
131
+ */
132
+ function createReference(resource) {
133
+ const reference = getReferenceString(resource);
134
+ const display = getDisplayString(resource);
135
+ return display === reference ? { reference } : { reference, display };
136
+ }
137
+ /**
138
+ * Returns a reference string for a resource.
139
+ * @param resource The FHIR resource.
140
+ * @returns A reference string of the form resourceType/id.
141
+ */
142
+ function getReferenceString(resource) {
143
+ return resource.resourceType + '/' + resource.id;
144
+ }
145
+ /**
146
+ * Returns the ID portion of a reference.
147
+ * @param reference A FHIR reference.
148
+ * @returns The ID portion of a reference.
149
+ */
150
+ function resolveId(reference) {
151
+ var _a;
152
+ return (_a = reference === null || reference === void 0 ? void 0 : reference.reference) === null || _a === void 0 ? void 0 : _a.split('/')[1];
153
+ }
154
+ /**
155
+ * Returns true if the resource is a "ProfileResource".
156
+ * @param resource The FHIR resource.
157
+ * @returns True if the resource is a "ProfileResource".
158
+ */
159
+ function isProfileResource(resource) {
160
+ return (resource.resourceType === 'Patient' ||
161
+ resource.resourceType === 'Practitioner' ||
162
+ resource.resourceType === 'RelatedPerson');
163
+ }
164
+ /**
165
+ * Returns a display string for the resource.
166
+ * @param resource The input resource.
167
+ * @return Human friendly display string.
168
+ */
169
+ function getDisplayString(resource) {
170
+ if (isProfileResource(resource)) {
171
+ const profileName = getProfileResourceDisplayString(resource);
172
+ if (profileName) {
173
+ return profileName;
174
+ }
175
+ }
176
+ if (resource.resourceType === 'Device') {
177
+ const deviceName = getDeviceDisplayString(resource);
178
+ if (deviceName) {
179
+ return deviceName;
180
+ }
181
+ }
182
+ if (resource.resourceType === 'User') {
183
+ if (resource.email) {
184
+ return resource.email;
185
+ }
186
+ }
187
+ if ('name' in resource && resource.name && typeof resource.name === 'string') {
188
+ return resource.name;
189
+ }
190
+ return getReferenceString(resource);
191
+ }
192
+ /**
193
+ * Returns a display string for a profile resource if one is found.
194
+ * @param resource The profile resource.
195
+ * @returns The display name if one is found.
196
+ */
197
+ function getProfileResourceDisplayString(resource) {
198
+ const names = resource.name;
199
+ if (names && names.length > 0) {
200
+ return formatHumanName(names[0]);
201
+ }
202
+ return undefined;
203
+ }
204
+ /**
205
+ * Returns a display string for a device resource if one is found.
206
+ * @param device The device resource.
207
+ * @returns The display name if one is found.
208
+ */
209
+ function getDeviceDisplayString(device) {
210
+ const names = device.deviceName;
211
+ if (names && names.length > 0) {
212
+ return names[0].name;
213
+ }
214
+ return undefined;
215
+ }
216
+ /**
217
+ * Returns an image URL for the resource, if one is available.
218
+ * @param resource The input resource.
219
+ * @returns The image URL for the resource or undefined.
220
+ */
221
+ function getImageSrc(resource) {
222
+ if (isProfileResource(resource)) {
223
+ const photos = resource.photo;
224
+ if (photos) {
225
+ for (const photo of photos) {
226
+ if (photo.url && photo.contentType && photo.contentType.startsWith('image/')) {
227
+ return photo.url;
228
+ }
229
+ }
230
+ }
231
+ }
232
+ return undefined;
233
+ }
234
+ /**
235
+ * Returns a Date property as a Date.
236
+ * When working with JSON objects, Dates are often serialized as ISO-8601 strings.
237
+ * When that happens, we need to safely convert to a proper Date object.
238
+ * @param date The date property value, which could be a string or a Date object.
239
+ * @returns A Date object.
240
+ */
241
+ function getDateProperty(date) {
242
+ return date ? new Date(date) : undefined;
243
+ }
244
+ /**
245
+ * FHIR JSON stringify.
246
+ * Removes properties with empty string values.
247
+ * Removes objects with zero properties.
248
+ * See: https://www.hl7.org/fhir/json.html
249
+ * @param value The input value.
250
+ * @param pretty Optional flag to pretty-print the JSON.
251
+ * @returns The resulting JSON string.
252
+ */
253
+ function stringify(value, pretty) {
254
+ return JSON.stringify(value, stringifyReplacer, pretty ? 2 : undefined);
255
+ }
256
+ /**
257
+ * Evaluates JSON key/value pairs for FHIR JSON stringify.
258
+ * Removes properties with empty string values.
259
+ * Removes objects with zero properties.
260
+ * @param {string} k Property key.
261
+ * @param {*} v Property value.
262
+ */
263
+ function stringifyReplacer(k, v) {
264
+ return isEmpty(v) ? undefined : v;
265
+ }
266
+ /**
267
+ * Returns true if the value is empty (null, undefined, empty string, or empty object).
268
+ * @param v Any value.
269
+ * @returns True if the value is an empty string or an empty object.
270
+ */
271
+ function isEmpty(v) {
272
+ if (v === null || v === undefined) {
273
+ return true;
274
+ }
275
+ const t = typeof v;
276
+ return (t === 'string' && v === '') || (t === 'object' && Object.keys(v).length === 0);
277
+ }
278
+ /**
279
+ * Resource equality.
280
+ * Ignores meta.versionId and meta.lastUpdated.
281
+ * See: https://dmitripavlutin.com/how-to-compare-objects-in-javascript/#4-deep-equality
282
+ * @param object1 The first object.
283
+ * @param object2 The second object.
284
+ * @returns True if the objects are equal.
285
+ */
286
+ function deepEquals(object1, object2, path) {
287
+ let keys1 = Object.keys(object1);
288
+ let keys2 = Object.keys(object2);
289
+ if (path === 'meta') {
290
+ keys1 = keys1.filter((k) => k !== 'versionId' && k !== 'lastUpdated' && k !== 'author');
291
+ keys2 = keys2.filter((k) => k !== 'versionId' && k !== 'lastUpdated' && k !== 'author');
292
+ }
293
+ if (keys1.length !== keys2.length) {
294
+ return false;
295
+ }
296
+ for (const key of keys1) {
297
+ const val1 = object1[key];
298
+ const val2 = object2[key];
299
+ if (isObject(val1) && isObject(val2)) {
300
+ if (!deepEquals(val1, val2, key)) {
301
+ return false;
302
+ }
303
+ }
304
+ else {
305
+ if (val1 !== val2) {
306
+ return false;
307
+ }
308
+ }
309
+ }
310
+ return true;
311
+ }
312
+ function isObject(object) {
313
+ return object !== null && typeof object === 'object';
314
+ }
315
+ // Precompute hex octets
316
+ // See: https://stackoverflow.com/a/55200387
317
+ const byteToHex = [];
318
+ for (let n = 0; n < 256; n++) {
319
+ byteToHex.push(n.toString(16).padStart(2, '0'));
320
+ }
321
+ /**
322
+ * Converts an ArrayBuffer to hex string.
323
+ * See: https://stackoverflow.com/a/55200387
324
+ * @param arrayBuffer The input array buffer.
325
+ * @returns The resulting hex string.
326
+ */
327
+ function arrayBufferToHex(arrayBuffer) {
328
+ const bytes = new Uint8Array(arrayBuffer);
329
+ const result = new Array(bytes.length);
330
+ for (let i = 0; i < bytes.length; i++) {
331
+ result[i] = byteToHex[bytes[i]];
332
+ }
333
+ return result.join('');
334
+ }
335
+ /**
336
+ * Converts an ArrayBuffer to a base-64 encoded string.
337
+ * @param arrayBuffer The input array buffer.
338
+ * @returns The base-64 encoded string.
339
+ */
340
+ function arrayBufferToBase64(arrayBuffer) {
341
+ const bytes = new Uint8Array(arrayBuffer);
342
+ const result = [];
343
+ for (let i = 0; i < bytes.length; i++) {
344
+ result[i] = String.fromCharCode(bytes[i]);
345
+ }
346
+ return window.btoa(result.join(''));
347
+ }
348
+ function capitalize(word) {
349
+ return word.charAt(0).toUpperCase() + word.substr(1);
350
+ }
351
+ function isLowerCase(c) {
352
+ return c === c.toLowerCase();
353
+ }
354
+
355
+ /**
356
+ * Returns a cryptographically secure random string.
357
+ */
358
+ function getRandomString() {
359
+ const randomItems = new Uint32Array(28);
360
+ crypto.getRandomValues(randomItems);
361
+ return arrayBufferToHex(randomItems.buffer);
362
+ }
363
+ /**
364
+ * Encrypts a string with SHA256 encryption.
365
+ * @param str
366
+ */
367
+ function encryptSHA256(str) {
368
+ return __awaiter(this, void 0, void 0, function* () {
369
+ return crypto.subtle.digest('SHA-256', new TextEncoder().encode(str));
370
+ });
371
+ }
372
+
373
+ /*
374
+ * Based on: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
375
+ */
376
+ var _EventTarget_listeners;
377
+ class EventTarget {
378
+ constructor() {
379
+ _EventTarget_listeners.set(this, void 0);
380
+ __classPrivateFieldSet(this, _EventTarget_listeners, {}, "f");
381
+ }
382
+ addEventListener(type, callback) {
383
+ if (!__classPrivateFieldGet(this, _EventTarget_listeners, "f")[type]) {
384
+ __classPrivateFieldGet(this, _EventTarget_listeners, "f")[type] = [];
385
+ }
386
+ __classPrivateFieldGet(this, _EventTarget_listeners, "f")[type].push(callback);
387
+ }
388
+ removeEventListeneer(type, callback) {
389
+ const array = __classPrivateFieldGet(this, _EventTarget_listeners, "f")[type];
390
+ if (!array) {
391
+ return;
392
+ }
393
+ for (let i = 0; i < array.length; i++) {
394
+ if (array[i] === callback) {
395
+ array.splice(i, 1);
396
+ return;
397
+ }
398
+ }
399
+ }
400
+ dispatchEvent(event) {
401
+ const array = __classPrivateFieldGet(this, _EventTarget_listeners, "f")[event.type];
402
+ if (array) {
403
+ array.forEach((listener) => listener.call(this, event));
404
+ }
405
+ return !event.defaultPrevented;
406
+ }
407
+ }
408
+ _EventTarget_listeners = new WeakMap();
409
+
410
+ /**
411
+ * Decodes a section of a JWT.
412
+ * See: https://tools.ietf.org/html/rfc7519
413
+ * @param payload
414
+ */
415
+ function decodePayload(payload) {
416
+ const cleanedPayload = payload.replace(/-/g, '+').replace(/_/g, '/');
417
+ const decodedPayload = window.atob(cleanedPayload);
418
+ const uriEncodedPayload = Array.from(decodedPayload).reduce((acc, char) => {
419
+ const uriEncodedChar = ('00' + char.charCodeAt(0).toString(16)).slice(-2);
420
+ return `${acc}%${uriEncodedChar}`;
421
+ }, '');
422
+ const jsonPayload = decodeURIComponent(uriEncodedPayload);
423
+ return JSON.parse(jsonPayload);
424
+ }
425
+ /**
426
+ * Parses the JWT payload.
427
+ * @param token JWT token
428
+ */
429
+ function parseJWTPayload(token) {
430
+ const [_header, payload, _signature] = token.split('.');
431
+ return decodePayload(payload);
432
+ }
433
+
434
+ const OK_ID = 'ok';
435
+ const CREATED_ID = 'created';
436
+ const GONE_ID = 'gone';
437
+ const NOT_MODIFIED_ID = 'not-modified';
438
+ const NOT_FOUND_ID = 'not-found';
439
+ const ACCESS_DENIED = 'access-denied';
440
+ const allOk = {
441
+ resourceType: 'OperationOutcome',
442
+ id: OK_ID,
443
+ issue: [
444
+ {
445
+ severity: 'information',
446
+ code: 'information',
447
+ details: {
448
+ text: 'All OK',
449
+ },
450
+ },
451
+ ],
452
+ };
453
+ const created = {
454
+ resourceType: 'OperationOutcome',
455
+ id: CREATED_ID,
456
+ issue: [
457
+ {
458
+ severity: 'information',
459
+ code: 'information',
460
+ details: {
461
+ text: 'Created',
462
+ },
463
+ },
464
+ ],
465
+ };
466
+ const notModified = {
467
+ resourceType: 'OperationOutcome',
468
+ id: NOT_MODIFIED_ID,
469
+ issue: [
470
+ {
471
+ severity: 'information',
472
+ code: 'information',
473
+ details: {
474
+ text: 'Not Modified',
475
+ },
476
+ },
477
+ ],
478
+ };
479
+ const notFound = {
480
+ resourceType: 'OperationOutcome',
481
+ id: NOT_FOUND_ID,
482
+ issue: [
483
+ {
484
+ severity: 'error',
485
+ code: 'not-found',
486
+ details: {
487
+ text: 'Not found',
488
+ },
489
+ },
490
+ ],
491
+ };
492
+ const gone = {
493
+ resourceType: 'OperationOutcome',
494
+ id: GONE_ID,
495
+ issue: [
496
+ {
497
+ severity: 'error',
498
+ code: 'gone',
499
+ details: {
500
+ text: 'Gone',
501
+ },
502
+ },
503
+ ],
504
+ };
505
+ const accessDenied = {
506
+ resourceType: 'OperationOutcome',
507
+ id: ACCESS_DENIED,
508
+ issue: [
509
+ {
510
+ severity: 'error',
511
+ code: 'access-denied',
512
+ details: {
513
+ text: 'Access Denied',
514
+ },
515
+ },
516
+ ],
517
+ };
518
+ function badRequest(details, expression) {
519
+ return {
520
+ resourceType: 'OperationOutcome',
521
+ issue: [
522
+ {
523
+ severity: 'error',
524
+ code: 'invalid',
525
+ details: {
526
+ text: details,
527
+ },
528
+ expression: expression ? [expression] : undefined,
529
+ },
530
+ ],
531
+ };
532
+ }
533
+ function isOk(outcome) {
534
+ return outcome.id === OK_ID || outcome.id === CREATED_ID || outcome.id === NOT_MODIFIED_ID;
535
+ }
536
+ function isNotFound(outcome) {
537
+ return outcome.id === NOT_FOUND_ID;
538
+ }
539
+ function isGone(outcome) {
540
+ return outcome.id === GONE_ID;
541
+ }
542
+ function getStatus(outcome) {
543
+ if (outcome.id === OK_ID) {
544
+ return 200;
545
+ }
546
+ else if (outcome.id === CREATED_ID) {
547
+ return 201;
548
+ }
549
+ else if (outcome.id === NOT_MODIFIED_ID) {
550
+ return 304;
551
+ }
552
+ else if (outcome.id === ACCESS_DENIED) {
553
+ return 403;
554
+ }
555
+ else if (outcome.id === NOT_FOUND_ID) {
556
+ return 404;
557
+ }
558
+ else if (outcome.id === GONE_ID) {
559
+ return 410;
560
+ }
561
+ else {
562
+ return 400;
563
+ }
564
+ }
565
+ /**
566
+ * Asserts that the operation completed successfully and that the resource is defined.
567
+ * @param outcome The operation outcome.
568
+ * @param resource The resource that may or may not have been returned.
569
+ */
570
+ function assertOk(outcome, resource) {
571
+ if (!isOk(outcome) || resource === undefined) {
572
+ throw new OperationOutcomeError(outcome);
573
+ }
574
+ }
575
+ class OperationOutcomeError extends Error {
576
+ constructor(outcome) {
577
+ var _a, _b;
578
+ super((_b = (_a = outcome === null || outcome === void 0 ? void 0 : outcome.issue) === null || _a === void 0 ? void 0 : _a[0].details) === null || _b === void 0 ? void 0 : _b.text);
579
+ this.outcome = outcome;
580
+ }
581
+ }
582
+
583
+ /**
584
+ * Search operators.
585
+ * These operators represent "modifiers" and "prefixes" in FHIR search.
586
+ * See: https://www.hl7.org/fhir/search.html
587
+ */
588
+ var Operator;
589
+ (function (Operator) {
590
+ Operator["EQUALS"] = "eq";
591
+ Operator["NOT_EQUALS"] = "ne";
592
+ // Numbers
593
+ Operator["GREATER_THAN"] = "gt";
594
+ Operator["LESS_THAN"] = "lt";
595
+ Operator["GREATER_THAN_OR_EQUALS"] = "ge";
596
+ Operator["LESS_THAN_OR_EQUALS"] = "le";
597
+ // Dates
598
+ Operator["STARTS_AFTER"] = "sa";
599
+ Operator["ENDS_BEFORE"] = "eb";
600
+ Operator["APPROXIMATELY"] = "ap";
601
+ // String
602
+ Operator["CONTAINS"] = "contains";
603
+ Operator["EXACT"] = "exact";
604
+ // Token
605
+ Operator["TEXT"] = "text";
606
+ Operator["ABOVE"] = "above";
607
+ Operator["BELOW"] = "below";
608
+ Operator["IN"] = "in";
609
+ Operator["NOT_IN"] = "not-in";
610
+ Operator["OF_TYPE"] = "of-type";
611
+ })(Operator || (Operator = {}));
612
+ const MODIFIER_OPERATORS = [
613
+ Operator.CONTAINS,
614
+ Operator.EXACT,
615
+ Operator.TEXT,
616
+ Operator.ABOVE,
617
+ Operator.BELOW,
618
+ Operator.IN,
619
+ Operator.NOT_IN,
620
+ Operator.OF_TYPE,
621
+ ];
622
+ const PREFIX_OPERATORS = [
623
+ Operator.NOT_EQUALS,
624
+ Operator.GREATER_THAN,
625
+ Operator.LESS_THAN,
626
+ Operator.GREATER_THAN_OR_EQUALS,
627
+ Operator.LESS_THAN_OR_EQUALS,
628
+ Operator.STARTS_AFTER,
629
+ Operator.ENDS_BEFORE,
630
+ Operator.APPROXIMATELY,
631
+ ];
632
+ /**
633
+ * Parses a URL into a SearchRequest.
634
+ *
635
+ * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
636
+ *
637
+ * @param location The URL to parse.
638
+ * @returns Parsed search definition.
639
+ */
640
+ function parseSearchDefinition(location) {
641
+ const resourceType = location.pathname
642
+ .replace(/(^\/)|(\/$)/g, '') // Remove leading and trailing slashes
643
+ .split('/')
644
+ .pop();
645
+ const params = new URLSearchParams(location.search);
646
+ let filters = undefined;
647
+ let sortRules = undefined;
648
+ let fields = undefined;
649
+ let page = undefined;
650
+ let count = undefined;
651
+ let total = undefined;
652
+ params.forEach((value, key) => {
653
+ if (key === '_fields') {
654
+ fields = value.split(',');
655
+ }
656
+ else if (key === '_page') {
657
+ page = parseInt(value);
658
+ }
659
+ else if (key === '_count') {
660
+ count = parseInt(value);
661
+ }
662
+ else if (key === '_total') {
663
+ total = value;
664
+ }
665
+ else if (key === '_sort') {
666
+ sortRules = sortRules || [];
667
+ sortRules.push(parseSortRule(value));
668
+ }
669
+ else {
670
+ filters = filters || [];
671
+ filters.push(parseSearchFilter(key, value));
672
+ }
673
+ });
674
+ return {
675
+ resourceType,
676
+ filters,
677
+ fields,
678
+ page,
679
+ count,
680
+ total,
681
+ sortRules,
682
+ };
683
+ }
684
+ /**
685
+ * Parses a URL query parameter into a sort rule.
686
+ *
687
+ * By default, the sort rule is the field name.
688
+ *
689
+ * Sort rules can be reversed into descending order by prefixing the field name with a minus sign.
690
+ *
691
+ * See sorting: http://hl7.org/fhir/r4/search.html#_sort
692
+ *
693
+ * @param value The URL parameter value.
694
+ * @returns The parsed sort rule.
695
+ */
696
+ function parseSortRule(value) {
697
+ if (value.startsWith('-')) {
698
+ return { code: value.substring(1), descending: true };
699
+ }
700
+ else {
701
+ return { code: value };
702
+ }
703
+ }
704
+ /**
705
+ * Parses a URL query parameter into a search filter.
706
+ *
707
+ * FHIR search filters can be specified as modifiers or prefixes.
708
+ *
709
+ * For string properties, modifiers are appended to the key, e.g. "name:contains=eve".
710
+ *
711
+ * For date and numeric properties, prefixes are prepended to the value, e.g. "birthdate=gt2000".
712
+ *
713
+ * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
714
+ *
715
+ * @param key The URL parameter key.
716
+ * @param value The URL parameter value.
717
+ * @returns The parsed search filter.
718
+ */
719
+ function parseSearchFilter(key, value) {
720
+ let code = key;
721
+ let operator = Operator.EQUALS;
722
+ for (const modifier of MODIFIER_OPERATORS) {
723
+ const modifierIndex = code.indexOf(':' + modifier);
724
+ if (modifierIndex !== -1) {
725
+ operator = modifier;
726
+ code = code.substring(0, modifierIndex);
727
+ }
728
+ }
729
+ for (const prefix of PREFIX_OPERATORS) {
730
+ if (value.match(new RegExp('^' + prefix + '\\d'))) {
731
+ operator = prefix;
732
+ value = value.substring(prefix.length);
733
+ }
734
+ }
735
+ return { code, operator, value };
736
+ }
737
+ /**
738
+ * Formats a search definition object into a query string.
739
+ * Note: The return value does not include the resource type.
740
+ * @param {!SearchRequest} definition The search definition.
741
+ * @returns Formatted URL.
742
+ */
743
+ function formatSearchQuery(definition) {
744
+ const params = [];
745
+ if (definition.fields) {
746
+ params.push('_fields=' + definition.fields.join(','));
747
+ }
748
+ if (definition.filters) {
749
+ definition.filters.forEach((filter) => params.push(formatFilter(filter)));
750
+ }
751
+ if (definition.sortRules && definition.sortRules.length > 0) {
752
+ params.push(formatSortRules(definition.sortRules));
753
+ }
754
+ if (definition.page && definition.page > 0) {
755
+ params.push('_page=' + definition.page);
756
+ }
757
+ if (definition.count && definition.count > 0) {
758
+ params.push('_count=' + definition.count);
759
+ }
760
+ if (definition.total) {
761
+ params.push('_total=' + encodeURIComponent(definition.total));
762
+ }
763
+ if (params.length === 0) {
764
+ return '';
765
+ }
766
+ params.sort();
767
+ return '?' + params.join('&');
768
+ }
769
+ function formatFilter(filter) {
770
+ const modifier = MODIFIER_OPERATORS.includes(filter.operator) ? ':' + filter.operator : '';
771
+ const prefix = PREFIX_OPERATORS.includes(filter.operator) ? filter.operator : '';
772
+ return `${filter.code}${modifier}=${prefix}${encodeURIComponent(filter.value)}`;
773
+ }
774
+ function formatSortRules(sortRules) {
775
+ if (!sortRules || sortRules.length === 0) {
776
+ return '';
777
+ }
778
+ return '_sort=' + sortRules.map((sr) => (sr.descending ? '-' + sr.code : sr.code)).join(',');
779
+ }
780
+
781
+ var _ClientStorage_storage, _MemoryStorage_data;
782
+ /**
783
+ * The ClientStorage class is a utility class for storing strings and objects.
784
+ *
785
+ * When using MedplumClient in the browser, it will be backed by browser localStorage.
786
+ *
787
+ * When Using MedplumClient in the server, it will be backed by the MemoryStorage class.
788
+ */
789
+ class ClientStorage {
790
+ constructor() {
791
+ _ClientStorage_storage.set(this, void 0);
792
+ __classPrivateFieldSet(this, _ClientStorage_storage, typeof localStorage !== 'undefined' ? localStorage : new MemoryStorage(), "f");
793
+ }
794
+ clear() {
795
+ __classPrivateFieldGet(this, _ClientStorage_storage, "f").clear();
796
+ }
797
+ getString(key) {
798
+ return __classPrivateFieldGet(this, _ClientStorage_storage, "f").getItem(key) || undefined;
799
+ }
800
+ setString(key, value) {
801
+ if (value) {
802
+ __classPrivateFieldGet(this, _ClientStorage_storage, "f").setItem(key, value);
803
+ }
804
+ else {
805
+ __classPrivateFieldGet(this, _ClientStorage_storage, "f").removeItem(key);
806
+ }
807
+ }
808
+ getObject(key) {
809
+ const str = this.getString(key);
810
+ return str ? JSON.parse(str) : undefined;
811
+ }
812
+ setObject(key, value) {
813
+ this.setString(key, value ? stringify(value) : undefined);
814
+ }
815
+ }
816
+ _ClientStorage_storage = new WeakMap();
817
+ /**
818
+ * The MemoryStorage class is a minimal in-memory implementation of the Storage interface.
819
+ */
820
+ class MemoryStorage {
821
+ constructor() {
822
+ _MemoryStorage_data.set(this, void 0);
823
+ __classPrivateFieldSet(this, _MemoryStorage_data, new Map(), "f");
824
+ }
825
+ /**
826
+ * Returns the number of key/value pairs.
827
+ */
828
+ get length() {
829
+ return __classPrivateFieldGet(this, _MemoryStorage_data, "f").size;
830
+ }
831
+ /**
832
+ * Removes all key/value pairs, if there are any.
833
+ */
834
+ clear() {
835
+ __classPrivateFieldGet(this, _MemoryStorage_data, "f").clear();
836
+ }
837
+ /**
838
+ * Returns the current value associated with the given key, or null if the given key does not exist.
839
+ */
840
+ getItem(key) {
841
+ var _a;
842
+ return (_a = __classPrivateFieldGet(this, _MemoryStorage_data, "f").get(key)) !== null && _a !== void 0 ? _a : null;
843
+ }
844
+ /**
845
+ * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
846
+ */
847
+ setItem(key, value) {
848
+ if (value) {
849
+ __classPrivateFieldGet(this, _MemoryStorage_data, "f").set(key, value);
850
+ }
851
+ else {
852
+ __classPrivateFieldGet(this, _MemoryStorage_data, "f").delete(key);
853
+ }
854
+ }
855
+ /**
856
+ * Removes the key/value pair with the given key, if a key/value pair with the given key exists.
857
+ */
858
+ removeItem(key) {
859
+ __classPrivateFieldGet(this, _MemoryStorage_data, "f").delete(key);
860
+ }
861
+ /**
862
+ * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.
863
+ */
864
+ key(index) {
865
+ return Array.from(__classPrivateFieldGet(this, _MemoryStorage_data, "f").keys())[index];
866
+ }
867
+ }
868
+ _MemoryStorage_data = new WeakMap();
869
+
870
+ /**
871
+ * List of property types.
872
+ * http://www.hl7.org/fhir/valueset-defined-types.html
873
+ * The list here includes additions found from StructureDefinition resources.
874
+ */
875
+ var PropertyType;
876
+ (function (PropertyType) {
877
+ PropertyType["Address"] = "Address";
878
+ PropertyType["Age"] = "Age";
879
+ PropertyType["Annotation"] = "Annotation";
880
+ PropertyType["Attachment"] = "Attachment";
881
+ PropertyType["BackboneElement"] = "BackboneElement";
882
+ PropertyType["CodeableConcept"] = "CodeableConcept";
883
+ PropertyType["Coding"] = "Coding";
884
+ PropertyType["ContactDetail"] = "ContactDetail";
885
+ PropertyType["ContactPoint"] = "ContactPoint";
886
+ PropertyType["Contributor"] = "Contributor";
887
+ PropertyType["Count"] = "Count";
888
+ PropertyType["DataRequirement"] = "DataRequirement";
889
+ PropertyType["Distance"] = "Distance";
890
+ PropertyType["Dosage"] = "Dosage";
891
+ PropertyType["Duration"] = "Duration";
892
+ PropertyType["Expression"] = "Expression";
893
+ PropertyType["Extension"] = "Extension";
894
+ PropertyType["HumanName"] = "HumanName";
895
+ PropertyType["Identifier"] = "Identifier";
896
+ PropertyType["MarketingStatus"] = "MarketingStatus";
897
+ PropertyType["Meta"] = "Meta";
898
+ PropertyType["Money"] = "Money";
899
+ PropertyType["Narrative"] = "Narrative";
900
+ PropertyType["ParameterDefinition"] = "ParameterDefinition";
901
+ PropertyType["Period"] = "Period";
902
+ PropertyType["Population"] = "Population";
903
+ PropertyType["ProdCharacteristic"] = "ProdCharacteristic";
904
+ PropertyType["ProductShelfLife"] = "ProductShelfLife";
905
+ PropertyType["Quantity"] = "Quantity";
906
+ PropertyType["Range"] = "Range";
907
+ PropertyType["Ratio"] = "Ratio";
908
+ PropertyType["Reference"] = "Reference";
909
+ PropertyType["RelatedArtifact"] = "RelatedArtifact";
910
+ PropertyType["Resource"] = "Resource";
911
+ PropertyType["SampledData"] = "SampledData";
912
+ PropertyType["Signature"] = "Signature";
913
+ PropertyType["SubstanceAmount"] = "SubstanceAmount";
914
+ PropertyType["SystemString"] = "http://hl7.org/fhirpath/System.String";
915
+ PropertyType["Timing"] = "Timing";
916
+ PropertyType["TriggerDefinition"] = "TriggerDefinition";
917
+ PropertyType["UsageContext"] = "UsageContext";
918
+ PropertyType["base64Binary"] = "base64Binary";
919
+ PropertyType["boolean"] = "boolean";
920
+ PropertyType["canonical"] = "canonical";
921
+ PropertyType["code"] = "code";
922
+ PropertyType["date"] = "date";
923
+ PropertyType["dateTime"] = "dateTime";
924
+ PropertyType["decimal"] = "decimal";
925
+ PropertyType["id"] = "id";
926
+ PropertyType["instant"] = "instant";
927
+ PropertyType["integer"] = "integer";
928
+ PropertyType["markdown"] = "markdown";
929
+ PropertyType["oid"] = "oid";
930
+ PropertyType["positiveInt"] = "positiveInt";
931
+ PropertyType["string"] = "string";
932
+ PropertyType["time"] = "time";
933
+ PropertyType["unsignedInt"] = "unsignedInt";
934
+ PropertyType["uri"] = "uri";
935
+ PropertyType["url"] = "url";
936
+ PropertyType["uuid"] = "uuid";
937
+ })(PropertyType || (PropertyType = {}));
938
+ /**
939
+ * Creates a new empty IndexedStructureDefinition.
940
+ * @returns The empty IndexedStructureDefinition.
941
+ */
942
+ function createSchema() {
943
+ return { types: {} };
944
+ }
945
+ function createTypeSchema(typeName, description) {
946
+ return {
947
+ display: typeName,
948
+ description,
949
+ properties: {},
950
+ searchParams: {
951
+ _lastUpdated: {
952
+ base: [typeName],
953
+ code: '_lastUpdated',
954
+ type: 'date',
955
+ expression: typeName + '.meta.lastUpdated',
956
+ },
957
+ },
958
+ };
959
+ }
960
+ /**
961
+ * Indexes a StructureDefinition for fast lookup.
962
+ * See comments on IndexedStructureDefinition for more details.
963
+ * @param schema The output IndexedStructureDefinition.
964
+ * @param structureDefinition The original StructureDefinition.
965
+ */
966
+ function indexStructureDefinition(schema, structureDefinition) {
967
+ var _a;
968
+ const typeName = structureDefinition.name;
969
+ if (!typeName) {
970
+ return;
971
+ }
972
+ schema.types[typeName] = createTypeSchema(typeName, structureDefinition.description);
973
+ const elements = (_a = structureDefinition.snapshot) === null || _a === void 0 ? void 0 : _a.element;
974
+ if (elements) {
975
+ // Filter out any elements missing path or type
976
+ const filtered = elements.filter((e) => e.path !== typeName && e.path);
977
+ // First pass, build types
978
+ filtered.forEach((element) => indexType(schema, element));
979
+ // Second pass, build properties
980
+ filtered.forEach((element) => indexProperty(schema, element));
981
+ }
982
+ }
983
+ /**
984
+ * Indexes TypeSchema from an ElementDefinition.
985
+ * In the common case, there will be many ElementDefinition instances per TypeSchema.
986
+ * Only the first occurrence is saved.
987
+ * @param schema The output IndexedStructureDefinition.
988
+ * @param element The input ElementDefinition.
989
+ */
990
+ function indexType(schema, element) {
991
+ var _a, _b;
992
+ const path = element.path;
993
+ const typeCode = (_b = (_a = element.type) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.code;
994
+ if (typeCode !== 'Element' && typeCode !== 'BackboneElement') {
995
+ return;
996
+ }
997
+ const parts = path.split('.');
998
+ const typeName = buildTypeName(parts);
999
+ if (!(typeName in schema.types)) {
1000
+ schema.types[typeName] = createTypeSchema(typeName, element.definition);
1001
+ schema.types[typeName].parentType = buildTypeName(parts.slice(0, parts.length - 1));
1002
+ }
1003
+ }
1004
+ /**
1005
+ * Indexes PropertySchema from an ElementDefinition.
1006
+ * @param schema The output IndexedStructureDefinition.
1007
+ * @param element The input ElementDefinition.
1008
+ */
1009
+ function indexProperty(schema, element) {
1010
+ const path = element.path;
1011
+ const parts = path.split('.');
1012
+ if (parts.length === 1) {
1013
+ return;
1014
+ }
1015
+ const typeName = buildTypeName(parts.slice(0, parts.length - 1));
1016
+ const typeSchema = schema.types[typeName];
1017
+ const key = parts[parts.length - 1];
1018
+ typeSchema.properties[key] = element;
1019
+ }
1020
+ /**
1021
+ * Indexes a SearchParameter resource for fast lookup.
1022
+ * Indexes by SearchParameter.code, which is the query string parameter name.
1023
+ * @param schema The output IndexedStructureDefinition.
1024
+ * @param searchParam The SearchParameter resource.
1025
+ */
1026
+ function indexSearchParameter(schema, searchParam) {
1027
+ if (!searchParam.base) {
1028
+ return;
1029
+ }
1030
+ for (const resourceType of searchParam.base) {
1031
+ const typeSchema = schema.types[resourceType];
1032
+ if (!typeSchema) {
1033
+ continue;
1034
+ }
1035
+ if (!typeSchema.searchParams) {
1036
+ typeSchema.searchParams = {};
1037
+ }
1038
+ typeSchema.searchParams[searchParam.code] = searchParam;
1039
+ }
1040
+ }
1041
+ function buildTypeName(components) {
1042
+ return components.map(capitalize).join('');
1043
+ }
1044
+ function getPropertyDisplayName(property) {
1045
+ // Get the property name, which is the remainder after the last period
1046
+ // For example, for path "Patient.birthDate"
1047
+ // the property name is "birthDate"
1048
+ const propertyName = property.path.replaceAll('[x]', '').split('.').pop();
1049
+ // Special case for ID
1050
+ if (propertyName === 'id') {
1051
+ return 'ID';
1052
+ }
1053
+ // Split by capital letters
1054
+ // Capitalize the first letter of each word
1055
+ // Join together with spaces in between
1056
+ // Then normalize whitespace to single space character
1057
+ // For example, for property name "birthDate",
1058
+ // the display name is "Birth Date".
1059
+ return propertyName
1060
+ .split(/(?=[A-Z])/)
1061
+ .map(capitalize)
1062
+ .join(' ')
1063
+ .replace('_', ' ')
1064
+ .replace(/\s+/g, ' ');
1065
+ }
1066
+
1067
+ // PKCE auth ased on:
1068
+ // https://aws.amazon.com/blogs/security/how-to-add-authentication-single-page-web-application-with-amazon-cognito-oauth2-implementation/
1069
+ var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_storage, _MedplumClient_schema, _MedplumClient_resourceCache, _MedplumClient_baseUrl, _MedplumClient_clientId, _MedplumClient_authorizeUrl, _MedplumClient_tokenUrl, _MedplumClient_logoutUrl, _MedplumClient_onUnauthenticated, _MedplumClient_accessToken, _MedplumClient_refreshToken, _MedplumClient_refreshPromise, _MedplumClient_profilePromise, _MedplumClient_profile, _MedplumClient_config, _MedplumClient_addLogin, _MedplumClient_refreshProfile, _MedplumClient_request, _MedplumClient_buildFetchOptions, _MedplumClient_handleUnauthenticated, _MedplumClient_startPkce, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
1070
+ const DEFAULT_BASE_URL = 'https://api.medplum.com/';
1071
+ const DEFAULT_SCOPE = 'launch/patient openid fhirUser offline_access user/*.*';
1072
+ const DEFAULT_RESOURCE_CACHE_SIZE = 1000;
1073
+ const JSON_CONTENT_TYPE = 'application/json';
1074
+ const FHIR_CONTENT_TYPE = 'application/fhir+json';
1075
+ const PATCH_CONTENT_TYPE = 'application/json-patch+json';
1076
+ class MedplumClient extends EventTarget {
1077
+ constructor(options) {
1078
+ var _a;
1079
+ super();
1080
+ _MedplumClient_instances.add(this);
1081
+ _MedplumClient_fetch.set(this, void 0);
1082
+ _MedplumClient_storage.set(this, void 0);
1083
+ _MedplumClient_schema.set(this, void 0);
1084
+ _MedplumClient_resourceCache.set(this, void 0);
1085
+ _MedplumClient_baseUrl.set(this, void 0);
1086
+ _MedplumClient_clientId.set(this, void 0);
1087
+ _MedplumClient_authorizeUrl.set(this, void 0);
1088
+ _MedplumClient_tokenUrl.set(this, void 0);
1089
+ _MedplumClient_logoutUrl.set(this, void 0);
1090
+ _MedplumClient_onUnauthenticated.set(this, void 0);
1091
+ _MedplumClient_accessToken.set(this, void 0);
1092
+ _MedplumClient_refreshToken.set(this, void 0);
1093
+ _MedplumClient_refreshPromise.set(this, void 0);
1094
+ _MedplumClient_profilePromise.set(this, void 0);
1095
+ _MedplumClient_profile.set(this, void 0);
1096
+ _MedplumClient_config.set(this, void 0);
1097
+ if (options === null || options === void 0 ? void 0 : options.baseUrl) {
1098
+ if (!options.baseUrl.startsWith('http')) {
1099
+ throw new Error('Base URL must start with http or https');
1100
+ }
1101
+ if (!options.baseUrl.endsWith('/')) {
1102
+ throw new Error('Base URL must end with a trailing slash');
1103
+ }
1104
+ }
1105
+ __classPrivateFieldSet(this, _MedplumClient_fetch, (options === null || options === void 0 ? void 0 : options.fetch) || window.fetch.bind(window), "f");
1106
+ __classPrivateFieldSet(this, _MedplumClient_storage, new ClientStorage(), "f");
1107
+ __classPrivateFieldSet(this, _MedplumClient_schema, createSchema(), "f");
1108
+ __classPrivateFieldSet(this, _MedplumClient_resourceCache, new LRUCache((_a = options === null || options === void 0 ? void 0 : options.resourceCacheSize) !== null && _a !== void 0 ? _a : DEFAULT_RESOURCE_CACHE_SIZE), "f");
1109
+ __classPrivateFieldSet(this, _MedplumClient_baseUrl, (options === null || options === void 0 ? void 0 : options.baseUrl) || DEFAULT_BASE_URL, "f");
1110
+ __classPrivateFieldSet(this, _MedplumClient_clientId, (options === null || options === void 0 ? void 0 : options.clientId) || '', "f");
1111
+ __classPrivateFieldSet(this, _MedplumClient_authorizeUrl, (options === null || options === void 0 ? void 0 : options.authorizeUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/authorize', "f");
1112
+ __classPrivateFieldSet(this, _MedplumClient_tokenUrl, (options === null || options === void 0 ? void 0 : options.tokenUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/token', "f");
1113
+ __classPrivateFieldSet(this, _MedplumClient_logoutUrl, (options === null || options === void 0 ? void 0 : options.logoutUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/logout', "f");
1114
+ __classPrivateFieldSet(this, _MedplumClient_onUnauthenticated, options === null || options === void 0 ? void 0 : options.onUnauthenticated, "f");
1115
+ const activeLogin = this.getActiveLogin();
1116
+ if (activeLogin) {
1117
+ __classPrivateFieldSet(this, _MedplumClient_accessToken, activeLogin.accessToken, "f");
1118
+ __classPrivateFieldSet(this, _MedplumClient_refreshToken, activeLogin.refreshToken, "f");
1119
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refreshProfile).call(this).catch(console.log);
1120
+ }
1121
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setupStorageListener).call(this);
1122
+ }
1123
+ /**
1124
+ * Clears all auth state including local storage and session storage.
1125
+ */
1126
+ clear() {
1127
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").clear();
1128
+ __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").clear();
1129
+ __classPrivateFieldSet(this, _MedplumClient_accessToken, undefined, "f");
1130
+ __classPrivateFieldSet(this, _MedplumClient_refreshToken, undefined, "f");
1131
+ __classPrivateFieldSet(this, _MedplumClient_profile, undefined, "f");
1132
+ __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1133
+ this.dispatchEvent({ type: 'change' });
1134
+ }
1135
+ get(url) {
1136
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'GET', url);
1137
+ }
1138
+ post(url, body, contentType) {
1139
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'POST', url, contentType, body);
1140
+ }
1141
+ put(url, body, contentType) {
1142
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PUT', url, contentType, body);
1143
+ }
1144
+ delete(url) {
1145
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'DELETE', url);
1146
+ }
1147
+ /**
1148
+ * Tries to register a new user.
1149
+ * @param request The registration request.
1150
+ * @returns Promise to the authentication response.
1151
+ */
1152
+ register(request) {
1153
+ return __awaiter(this, void 0, void 0, function* () {
1154
+ const response = yield this.post('auth/register', request);
1155
+ yield this.setActiveLogin(response);
1156
+ });
1157
+ }
1158
+ /**
1159
+ * Initiates a user login flow.
1160
+ * @param email The email address of the user.
1161
+ * @param password The password of the user.
1162
+ * @param remember Optional flag to remember the user.
1163
+ * @returns Promise to the authentication response.
1164
+ */
1165
+ startLogin(email, password, remember) {
1166
+ return __awaiter(this, void 0, void 0, function* () {
1167
+ yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_startPkce).call(this);
1168
+ return this.post('auth/login', {
1169
+ clientId: __classPrivateFieldGet(this, _MedplumClient_clientId, "f"),
1170
+ scope: DEFAULT_SCOPE,
1171
+ codeChallengeMethod: 'S256',
1172
+ codeChallenge: __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeChallenge'),
1173
+ email,
1174
+ password,
1175
+ remember: !!remember,
1176
+ });
1177
+ });
1178
+ }
1179
+ /**
1180
+ * Tries to sign in with Google authentication.
1181
+ * The response parameter is the result of a Google authentication.
1182
+ * See: https://developers.google.com/identity/gsi/web/guides/handle-credential-responses-js-functions
1183
+ * @param googleResponse The Google credential response.
1184
+ * @returns Promise to the authentication response.
1185
+ */
1186
+ startGoogleLogin(googleResponse) {
1187
+ return __awaiter(this, void 0, void 0, function* () {
1188
+ yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_startPkce).call(this);
1189
+ return this.post('auth/google', googleResponse);
1190
+ });
1191
+ }
1192
+ /**
1193
+ * Signs out locally.
1194
+ * Does not invalidate tokens with the server.
1195
+ */
1196
+ signOut() {
1197
+ this.clear();
1198
+ return Promise.resolve();
1199
+ }
1200
+ /**
1201
+ * Tries to sign in the user.
1202
+ * Returns true if the user is signed in.
1203
+ * This may result in navigating away to the sign in page.
1204
+ */
1205
+ signInWithRedirect() {
1206
+ const urlParams = new URLSearchParams(window.location.search);
1207
+ const code = urlParams.get('code');
1208
+ if (!code) {
1209
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_requestAuthorization).call(this);
1210
+ return undefined;
1211
+ }
1212
+ else {
1213
+ return this.processCode(code);
1214
+ }
1215
+ }
1216
+ /**
1217
+ * Tries to sign out the user.
1218
+ * See: https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html
1219
+ */
1220
+ signOutWithRedirect() {
1221
+ window.location.assign(__classPrivateFieldGet(this, _MedplumClient_logoutUrl, "f"));
1222
+ }
1223
+ /**
1224
+ * Builds a FHIR URL from a collection of URL path components.
1225
+ * For example, `buildUrl('/Patient', '123')` returns `fhir/R4/Patient/123`.
1226
+ * @param path The path component of the URL.
1227
+ * @returns The well-formed FHIR URL.
1228
+ */
1229
+ fhirUrl(...path) {
1230
+ const builder = [__classPrivateFieldGet(this, _MedplumClient_baseUrl, "f"), 'fhir/R4'];
1231
+ path.forEach((p) => builder.push('/', encodeURIComponent(p)));
1232
+ return builder.join('');
1233
+ }
1234
+ /**
1235
+ * Sends a FHIR search request.
1236
+ * @param search The search query.
1237
+ * @returns Promise to the search result bundle.
1238
+ */
1239
+ search(search) {
1240
+ return this.get(this.fhirUrl(search.resourceType) + formatSearchQuery(search));
1241
+ }
1242
+ /**
1243
+ * Searches a ValueSet resource using the "expand" operation.
1244
+ * See: https://www.hl7.org/fhir/operation-valueset-expand.html
1245
+ * @param system The ValueSet system url.
1246
+ * @param filter The search string.
1247
+ * @returns Promise to expanded ValueSet.
1248
+ */
1249
+ searchValueSet(system, filter) {
1250
+ return this.get(this.fhirUrl('ValueSet', '$expand') +
1251
+ `?url=${encodeURIComponent(system)}` +
1252
+ `&filter=${encodeURIComponent(filter)}`);
1253
+ }
1254
+ /**
1255
+ * Returns a cached resource if it is available.
1256
+ * @param resourceType The FHIR resource type.
1257
+ * @param id The FHIR resource ID.
1258
+ * @returns The resource if it is available in the cache; undefined otherwise.
1259
+ */
1260
+ getCached(resourceType, id) {
1261
+ const cached = __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").get(resourceType + '/' + id);
1262
+ if (cached && !('then' in cached)) {
1263
+ return cached;
1264
+ }
1265
+ return undefined;
1266
+ }
1267
+ /**
1268
+ * Returns a cached resource if it is available.
1269
+ * @param resourceType The FHIR resource type.
1270
+ * @param id The FHIR resource ID.
1271
+ * @returns The resource if it is available in the cache; undefined otherwise.
1272
+ */
1273
+ getCachedReference(reference) {
1274
+ const cached = __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").get(reference.reference);
1275
+ if (cached && !('then' in cached)) {
1276
+ return cached;
1277
+ }
1278
+ return undefined;
1279
+ }
1280
+ read(resourceType, id) {
1281
+ const cacheKey = resourceType + '/' + id;
1282
+ const promise = this.get(this.fhirUrl(resourceType, id)).then((resource) => {
1283
+ __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").set(cacheKey, resource);
1284
+ return resource;
1285
+ });
1286
+ __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").set(cacheKey, promise);
1287
+ return promise;
1288
+ }
1289
+ readCached(resourceType, id) {
1290
+ const cached = __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").get(resourceType + '/' + id);
1291
+ return cached ? Promise.resolve(cached) : this.read(resourceType, id);
1292
+ }
1293
+ readReference(reference) {
1294
+ const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
1295
+ if (!refString) {
1296
+ return Promise.reject('Missing reference');
1297
+ }
1298
+ const [resourceType, id] = refString.split('/');
1299
+ return this.read(resourceType, id);
1300
+ }
1301
+ readCachedReference(reference) {
1302
+ const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
1303
+ if (!refString) {
1304
+ return Promise.reject('Missing reference');
1305
+ }
1306
+ const [resourceType, id] = refString.split('/');
1307
+ return this.readCached(resourceType, id);
1308
+ }
1309
+ /**
1310
+ * Returns a cached schema for a resource type.
1311
+ * If the schema is not cached, returns undefined.
1312
+ * It is assumed that a client will call requestSchema before using this method.
1313
+ * @param resourceType The FHIR resource type.
1314
+ * @returns The schema if immediately available, undefined otherwise.
1315
+ */
1316
+ getSchema() {
1317
+ return __classPrivateFieldGet(this, _MedplumClient_schema, "f");
1318
+ }
1319
+ /**
1320
+ * Requests the schema for a resource type.
1321
+ * If the schema is already cached, the promise is resolved immediately.
1322
+ * @param resourceType The FHIR resource type.
1323
+ * @returns Promise to a schema with the requested resource type.
1324
+ */
1325
+ requestSchema(resourceType) {
1326
+ return __awaiter(this, void 0, void 0, function* () {
1327
+ if (resourceType in __classPrivateFieldGet(this, _MedplumClient_schema, "f").types) {
1328
+ return Promise.resolve(__classPrivateFieldGet(this, _MedplumClient_schema, "f"));
1329
+ }
1330
+ const query = `{
1331
+ StructureDefinitionList(name: "${encodeURIComponent(resourceType)}") {
1332
+ name,
1333
+ description,
1334
+ snapshot {
1335
+ element {
1336
+ id,
1337
+ path,
1338
+ min,
1339
+ max,
1340
+ type {
1341
+ code,
1342
+ targetProfile
1343
+ },
1344
+ binding {
1345
+ valueSet
1346
+ },
1347
+ definition
1348
+ }
1349
+ }
1350
+ }
1351
+ SearchParameterList(base: "${encodeURIComponent(resourceType)}") {
1352
+ base,
1353
+ code,
1354
+ type,
1355
+ expression,
1356
+ target
1357
+ }
1358
+ }`.replace(/\s+/g, ' ');
1359
+ const response = (yield this.graphql(query));
1360
+ for (const structureDefinition of response.data.StructureDefinitionList) {
1361
+ indexStructureDefinition(__classPrivateFieldGet(this, _MedplumClient_schema, "f"), structureDefinition);
1362
+ }
1363
+ for (const searchParameter of response.data.SearchParameterList) {
1364
+ indexSearchParameter(__classPrivateFieldGet(this, _MedplumClient_schema, "f"), searchParameter);
1365
+ }
1366
+ return __classPrivateFieldGet(this, _MedplumClient_schema, "f");
1367
+ });
1368
+ }
1369
+ readHistory(resourceType, id) {
1370
+ return this.get(this.fhirUrl(resourceType, id, '_history'));
1371
+ }
1372
+ readPatientEverything(id) {
1373
+ return this.get(this.fhirUrl('Patient', id, '$everything'));
1374
+ }
1375
+ create(resource) {
1376
+ if (!resource.resourceType) {
1377
+ throw new Error('Missing resourceType');
1378
+ }
1379
+ return this.post(this.fhirUrl(resource.resourceType), resource);
1380
+ }
1381
+ createBinary(data, filename, contentType) {
1382
+ return this.post(this.fhirUrl('Binary') + '?_filename=' + encodeURIComponent(filename), data, contentType);
1383
+ }
1384
+ update(resource) {
1385
+ if (!resource.resourceType) {
1386
+ throw new Error('Missing resourceType');
1387
+ }
1388
+ if (!resource.id) {
1389
+ throw new Error('Missing id');
1390
+ }
1391
+ return this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
1392
+ }
1393
+ patch(resourceType, id, operations) {
1394
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PATCH', this.fhirUrl(resourceType, id), PATCH_CONTENT_TYPE, operations);
1395
+ }
1396
+ deleteResource(resourceType, id) {
1397
+ return this.delete(this.fhirUrl(resourceType, id));
1398
+ }
1399
+ graphql(query) {
1400
+ return this.post(this.fhirUrl('$graphql'), { query }, JSON_CONTENT_TYPE);
1401
+ }
1402
+ getActiveLogin() {
1403
+ return __classPrivateFieldGet(this, _MedplumClient_storage, "f").getObject('activeLogin');
1404
+ }
1405
+ setActiveLogin(login) {
1406
+ return __awaiter(this, void 0, void 0, function* () {
1407
+ __classPrivateFieldSet(this, _MedplumClient_accessToken, login.accessToken, "f");
1408
+ __classPrivateFieldSet(this, _MedplumClient_refreshToken, login.refreshToken, "f");
1409
+ __classPrivateFieldSet(this, _MedplumClient_profile, undefined, "f");
1410
+ __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1411
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('activeLogin', login);
1412
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addLogin).call(this, login);
1413
+ __classPrivateFieldGet(this, _MedplumClient_resourceCache, "f").clear();
1414
+ __classPrivateFieldSet(this, _MedplumClient_refreshPromise, undefined, "f");
1415
+ yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refreshProfile).call(this);
1416
+ });
1417
+ }
1418
+ getLogins() {
1419
+ var _a;
1420
+ return (_a = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getObject('logins')) !== null && _a !== void 0 ? _a : [];
1421
+ }
1422
+ isLoading() {
1423
+ return !!__classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1424
+ }
1425
+ getProfile() {
1426
+ return __classPrivateFieldGet(this, _MedplumClient_profile, "f");
1427
+ }
1428
+ getProfileAsync() {
1429
+ return __awaiter(this, void 0, void 0, function* () {
1430
+ if (__classPrivateFieldGet(this, _MedplumClient_profilePromise, "f")) {
1431
+ yield __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1432
+ }
1433
+ return this.getProfile();
1434
+ });
1435
+ }
1436
+ getUserConfiguration() {
1437
+ return __classPrivateFieldGet(this, _MedplumClient_config, "f");
1438
+ }
1439
+ /**
1440
+ * Downloads the URL as a blob.
1441
+ * @param url The URL to request.
1442
+ * @returns Promise to the response body as a blob.
1443
+ */
1444
+ download(url) {
1445
+ return __awaiter(this, void 0, void 0, function* () {
1446
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1447
+ yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1448
+ }
1449
+ const options = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_buildFetchOptions).call(this, 'GET');
1450
+ const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url, options);
1451
+ return response.blob();
1452
+ });
1453
+ }
1454
+ /**
1455
+ * Processes an OAuth authorization code.
1456
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenRequest
1457
+ * @param code The authorization code received by URL parameter.
1458
+ */
1459
+ processCode(code) {
1460
+ const pkceState = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('pkceState');
1461
+ if (!pkceState) {
1462
+ this.clear();
1463
+ throw new Error('Invalid PCKE state');
1464
+ }
1465
+ const codeVerifier = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeVerifier');
1466
+ if (!codeVerifier) {
1467
+ this.clear();
1468
+ throw new Error('Invalid PCKE code verifier');
1469
+ }
1470
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, 'grant_type=authorization_code' +
1471
+ (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") ? '&client_id=' + encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) : '') +
1472
+ '&code_verifier=' +
1473
+ encodeURIComponent(codeVerifier) +
1474
+ '&redirect_uri=' +
1475
+ encodeURIComponent(getBaseUrl()) +
1476
+ '&code=' +
1477
+ encodeURIComponent(code));
1478
+ }
1479
+ }
1480
+ _MedplumClient_fetch = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_schema = new WeakMap(), _MedplumClient_resourceCache = new WeakMap(), _MedplumClient_baseUrl = new WeakMap(), _MedplumClient_clientId = new WeakMap(), _MedplumClient_authorizeUrl = new WeakMap(), _MedplumClient_tokenUrl = new WeakMap(), _MedplumClient_logoutUrl = new WeakMap(), _MedplumClient_onUnauthenticated = new WeakMap(), _MedplumClient_accessToken = new WeakMap(), _MedplumClient_refreshToken = new WeakMap(), _MedplumClient_refreshPromise = new WeakMap(), _MedplumClient_profilePromise = new WeakMap(), _MedplumClient_profile = new WeakMap(), _MedplumClient_config = new WeakMap(), _MedplumClient_instances = new WeakSet(), _MedplumClient_addLogin = function _MedplumClient_addLogin(newLogin) {
1481
+ const logins = this.getLogins().filter((login) => { var _a, _b; return ((_a = login.profile) === null || _a === void 0 ? void 0 : _a.reference) !== ((_b = newLogin.profile) === null || _b === void 0 ? void 0 : _b.reference); });
1482
+ logins.push(newLogin);
1483
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('logins', logins);
1484
+ }, _MedplumClient_refreshProfile = function _MedplumClient_refreshProfile() {
1485
+ return __awaiter(this, void 0, void 0, function* () {
1486
+ __classPrivateFieldSet(this, _MedplumClient_profilePromise, new Promise((resolve, reject) => {
1487
+ this.get('auth/me')
1488
+ .then((result) => {
1489
+ __classPrivateFieldSet(this, _MedplumClient_profilePromise, undefined, "f");
1490
+ __classPrivateFieldSet(this, _MedplumClient_profile, result.profile, "f");
1491
+ __classPrivateFieldSet(this, _MedplumClient_config, result.config, "f");
1492
+ this.dispatchEvent({ type: 'change' });
1493
+ resolve(__classPrivateFieldGet(this, _MedplumClient_profile, "f"));
1494
+ })
1495
+ .catch(reject);
1496
+ }), "f");
1497
+ return __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1498
+ });
1499
+ }, _MedplumClient_request = function _MedplumClient_request(method, url, contentType, body) {
1500
+ return __awaiter(this, void 0, void 0, function* () {
1501
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1502
+ yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1503
+ }
1504
+ if (!url.startsWith('http')) {
1505
+ url = __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + url;
1506
+ }
1507
+ const options = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_buildFetchOptions).call(this, method, contentType, body);
1508
+ const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url, options);
1509
+ if (response.status === 401) {
1510
+ // Refresh and try again
1511
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_handleUnauthenticated).call(this, method, url, contentType, body);
1512
+ }
1513
+ if (response.status === 204 || response.status === 304) {
1514
+ // No content or change
1515
+ return undefined;
1516
+ }
1517
+ const obj = yield response.json();
1518
+ if (obj.resourceType === 'OperationOutcome' && !isOk(obj)) {
1519
+ return Promise.reject(obj);
1520
+ }
1521
+ return obj;
1522
+ });
1523
+ }, _MedplumClient_buildFetchOptions = function _MedplumClient_buildFetchOptions(method, contentType, body) {
1524
+ const headers = {
1525
+ 'Content-Type': contentType || FHIR_CONTENT_TYPE,
1526
+ };
1527
+ if (__classPrivateFieldGet(this, _MedplumClient_accessToken, "f")) {
1528
+ headers['Authorization'] = 'Bearer ' + __classPrivateFieldGet(this, _MedplumClient_accessToken, "f");
1529
+ }
1530
+ const options = {
1531
+ method: method,
1532
+ cache: 'no-cache',
1533
+ credentials: 'include',
1534
+ headers,
1535
+ };
1536
+ if (body) {
1537
+ if (typeof body === 'string' || (typeof File !== 'undefined' && body instanceof File)) {
1538
+ options.body = body;
1539
+ }
1540
+ else {
1541
+ options.body = stringify(body);
1542
+ }
1543
+ }
1544
+ return options;
1545
+ }, _MedplumClient_handleUnauthenticated = function _MedplumClient_handleUnauthenticated(method, url, contentType, body) {
1546
+ return __awaiter(this, void 0, void 0, function* () {
1547
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refresh).call(this)
1548
+ .then(() => __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, method, url, contentType, body))
1549
+ .catch((error) => {
1550
+ this.clear();
1551
+ if (__classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f")) {
1552
+ __classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f").call(this);
1553
+ }
1554
+ return Promise.reject(error);
1555
+ });
1556
+ });
1557
+ }, _MedplumClient_startPkce = function _MedplumClient_startPkce() {
1558
+ return __awaiter(this, void 0, void 0, function* () {
1559
+ const pkceState = getRandomString();
1560
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setString('pkceState', pkceState);
1561
+ const codeVerifier = getRandomString();
1562
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setString('codeVerifier', codeVerifier);
1563
+ const arrayHash = yield encryptSHA256(codeVerifier);
1564
+ const codeChallenge = arrayBufferToBase64(arrayHash).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
1565
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setString('codeChallenge', codeChallenge);
1566
+ });
1567
+ }, _MedplumClient_requestAuthorization = function _MedplumClient_requestAuthorization() {
1568
+ return __awaiter(this, void 0, void 0, function* () {
1569
+ if (!__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f")) {
1570
+ throw new Error('Missing authorize URL');
1571
+ }
1572
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_startPkce).call(this);
1573
+ window.location.assign(__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f") +
1574
+ '?response_type=code' +
1575
+ '&state=' +
1576
+ encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('pkceState')) +
1577
+ '&client_id=' +
1578
+ encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) +
1579
+ '&redirect_uri=' +
1580
+ encodeURIComponent(getBaseUrl()) +
1581
+ '&scope=' +
1582
+ encodeURIComponent(DEFAULT_SCOPE) +
1583
+ '&code_challenge_method=S256' +
1584
+ '&code_challenge=' +
1585
+ encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeChallenge')));
1586
+ });
1587
+ }, _MedplumClient_refresh = function _MedplumClient_refresh() {
1588
+ return __awaiter(this, void 0, void 0, function* () {
1589
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1590
+ return __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1591
+ }
1592
+ if (!__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f")) {
1593
+ this.clear();
1594
+ return Promise.reject('Invalid refresh token');
1595
+ }
1596
+ __classPrivateFieldSet(this, _MedplumClient_refreshPromise, __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, 'grant_type=refresh_token' +
1597
+ '&client_id=' +
1598
+ encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) +
1599
+ '&refresh_token=' +
1600
+ encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f"))), "f");
1601
+ yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1602
+ });
1603
+ }, _MedplumClient_fetchTokens = function _MedplumClient_fetchTokens(formBody) {
1604
+ return __awaiter(this, void 0, void 0, function* () {
1605
+ if (!__classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f")) {
1606
+ return Promise.reject('Missing token URL');
1607
+ }
1608
+ return __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, __classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f"), {
1609
+ method: 'POST',
1610
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1611
+ body: formBody,
1612
+ })
1613
+ .then((response) => {
1614
+ if (!response.ok) {
1615
+ return Promise.reject('Failed to fetch tokens');
1616
+ }
1617
+ return response.json();
1618
+ })
1619
+ .then((tokens) => __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_verifyTokens).call(this, tokens))
1620
+ .then(() => this.getProfile());
1621
+ });
1622
+ }, _MedplumClient_verifyTokens = function _MedplumClient_verifyTokens(tokens) {
1623
+ return __awaiter(this, void 0, void 0, function* () {
1624
+ const token = tokens.access_token;
1625
+ // Verify token has not expired
1626
+ const tokenPayload = parseJWTPayload(token);
1627
+ if (Date.now() >= tokenPayload.exp * 1000) {
1628
+ this.clear();
1629
+ return Promise.reject('Token expired');
1630
+ }
1631
+ // Verify app_client_id
1632
+ if (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") && tokenPayload.client_id !== __classPrivateFieldGet(this, _MedplumClient_clientId, "f")) {
1633
+ this.clear();
1634
+ return Promise.reject('Token was not issued for this audience');
1635
+ }
1636
+ yield this.setActiveLogin({
1637
+ accessToken: token,
1638
+ refreshToken: tokens.refresh_token,
1639
+ project: tokens.project,
1640
+ profile: tokens.profile,
1641
+ });
1642
+ });
1643
+ }, _MedplumClient_setupStorageListener = function _MedplumClient_setupStorageListener() {
1644
+ try {
1645
+ window.addEventListener('storage', (e) => {
1646
+ if (e.key === null || e.key === 'activeLogin') {
1647
+ // Storage events fire when different tabs make changes.
1648
+ // On storage clear (key === null) or activeLogin change (key === 'activeLogin')
1649
+ // Refresh the page to ensure the active login is up to date.
1650
+ window.location.reload();
1651
+ }
1652
+ });
1653
+ }
1654
+ catch (err) {
1655
+ // Silently ignore if this environment does not support storage events
1656
+ }
1657
+ };
1658
+ /**
1659
+ * Returns the base URL for the current page.
1660
+ */
1661
+ function getBaseUrl() {
1662
+ return window.location.protocol + '//' + window.location.host + '/';
1663
+ }
1664
+
1665
+ var SearchParameterType;
1666
+ (function (SearchParameterType) {
1667
+ SearchParameterType["BOOLEAN"] = "BOOLEAN";
1668
+ SearchParameterType["NUMBER"] = "NUMBER";
1669
+ SearchParameterType["QUANTITY"] = "QUANTITY";
1670
+ SearchParameterType["TEXT"] = "TEXT";
1671
+ SearchParameterType["REFERENCE"] = "REFERENCE";
1672
+ SearchParameterType["DATE"] = "DATE";
1673
+ SearchParameterType["DATETIME"] = "DATETIME";
1674
+ SearchParameterType["PERIOD"] = "PERIOD";
1675
+ })(SearchParameterType || (SearchParameterType = {}));
1676
+ /**
1677
+ * Returns the type details of a SearchParameter.
1678
+ *
1679
+ * The SearchParameter resource has a "type" parameter, but that is missing some critical information.
1680
+ *
1681
+ * For example:
1682
+ * 1) The "date" type includes "date", "datetime", and "period".
1683
+ * 2) The "token" type includes enums and booleans.
1684
+ * 3) Arrays/multiple values are not reflected at all.
1685
+ *
1686
+ * @param structureDefinitions Collection of StructureDefinition resources indexed by name.
1687
+ * @param resourceType The root resource type.
1688
+ * @param searchParam The search parameter.
1689
+ * @returns The search parameter type details.
1690
+ */
1691
+ function getSearchParameterDetails(structureDefinitions, resourceType, searchParam) {
1692
+ var _a, _b, _c, _d;
1693
+ if (searchParam.code === '_lastUpdated') {
1694
+ return { columnName: 'lastUpdated', type: SearchParameterType.DATETIME };
1695
+ }
1696
+ const columnName = convertCodeToColumnName(searchParam.code);
1697
+ const expression = (_a = getExpressionForResourceType(resourceType, searchParam.expression)) === null || _a === void 0 ? void 0 : _a.split('.');
1698
+ if (!expression) {
1699
+ // This happens on compound types
1700
+ // In the future, explore returning multiple column definitions
1701
+ return { columnName, type: SearchParameterType.TEXT };
1702
+ }
1703
+ let baseType = resourceType;
1704
+ let elementDefinition = undefined;
1705
+ let propertyType = undefined;
1706
+ let array = false;
1707
+ for (let i = 1; i < expression.length; i++) {
1708
+ const propertyName = expression[i];
1709
+ elementDefinition = (_c = (_b = structureDefinitions.types[baseType]) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c[propertyName];
1710
+ if (!elementDefinition) {
1711
+ // This happens on complex properties such as "collected[x]"/"collectedDateTime"/"collectedPeriod"
1712
+ // In the future, explore returning multiple column definitions
1713
+ return { columnName, type: SearchParameterType.TEXT, array };
1714
+ }
1715
+ if (elementDefinition.max === '*') {
1716
+ array = true;
1717
+ }
1718
+ propertyType = (_d = elementDefinition.type) === null || _d === void 0 ? void 0 : _d[0].code;
1719
+ if (!propertyType) {
1720
+ // This happens when one of parent properties uses contentReference
1721
+ // In the future, explore following the reference
1722
+ return { columnName, type: SearchParameterType.TEXT, array };
1723
+ }
1724
+ if (i < expression.length - 1) {
1725
+ if (propertyType === 'Element' || propertyType === 'BackboneElement') {
1726
+ baseType = baseType + capitalize(propertyName);
1727
+ }
1728
+ else {
1729
+ baseType = propertyType;
1730
+ }
1731
+ }
1732
+ }
1733
+ const type = getSearchParameterType(searchParam, propertyType);
1734
+ return { columnName, type, elementDefinition, array };
1735
+ }
1736
+ /**
1737
+ * Converts a hyphen-delimited code to camelCase string.
1738
+ * @param code The search parameter code.
1739
+ * @returns The SQL column name.
1740
+ */
1741
+ function convertCodeToColumnName(code) {
1742
+ return code.split('-').reduce((result, word, index) => result + (index ? capitalize(word) : word), '');
1743
+ }
1744
+ function getSearchParameterType(searchParam, propertyType) {
1745
+ let type = SearchParameterType.TEXT;
1746
+ switch (searchParam.type) {
1747
+ case 'date':
1748
+ type = SearchParameterType.DATE;
1749
+ break;
1750
+ case 'number':
1751
+ type = SearchParameterType.NUMBER;
1752
+ break;
1753
+ case 'quantity':
1754
+ type = SearchParameterType.QUANTITY;
1755
+ break;
1756
+ case 'reference':
1757
+ type = SearchParameterType.REFERENCE;
1758
+ break;
1759
+ case 'token':
1760
+ if (propertyType === 'boolean') {
1761
+ type = SearchParameterType.BOOLEAN;
1762
+ }
1763
+ break;
1764
+ }
1765
+ return type;
1766
+ }
1767
+ function getExpressionForResourceType(resourceType, expression) {
1768
+ const expressions = expression.split(' | ');
1769
+ for (const e of expressions) {
1770
+ const simplified = simplifyExpression(e);
1771
+ if (simplified.startsWith(resourceType + '.')) {
1772
+ return simplified;
1773
+ }
1774
+ }
1775
+ return undefined;
1776
+ }
1777
+ function simplifyExpression(input) {
1778
+ let result = input.trim();
1779
+ if (result.startsWith('(') && result.endsWith(')')) {
1780
+ result = result.substring(1, result.length - 1);
1781
+ }
1782
+ if (result.includes(' as ')) {
1783
+ result = result.substring(0, result.indexOf(' as '));
1784
+ }
1785
+ if (result.includes('.where(')) {
1786
+ result = result.substring(0, result.indexOf('.where('));
1787
+ }
1788
+ return result;
1789
+ }
1790
+
1791
+ export { MedplumClient, OperationOutcomeError, Operator, PropertyType, SearchParameterType, accessDenied, allOk, arrayBufferToBase64, arrayBufferToHex, assertOk, badRequest, buildTypeName, capitalize, createReference, createSchema, createTypeSchema, created, deepEquals, formatAddress, formatFamilyName, formatGivenName, formatHumanName, formatSearchQuery, getDateProperty, getDisplayString, getExpressionForResourceType, getImageSrc, getPropertyDisplayName, getReferenceString, getSearchParameterDetails, getStatus, gone, indexSearchParameter, indexStructureDefinition, isGone, isLowerCase, isNotFound, isOk, isProfileResource, notFound, notModified, parseSearchDefinition, resolveId, stringify };
1792
+ //# sourceMappingURL=index.js.map