@medplum/core 1.0.4 → 1.0.6

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 (107) hide show
  1. package/README.md +9 -7
  2. package/dist/cjs/client.d.ts +1 -0
  3. package/dist/cjs/fhirlexer/parse.d.ts +18 -2
  4. package/dist/cjs/fhirmapper/parse.d.ts +2 -2
  5. package/dist/cjs/fhirpath/atoms.d.ts +28 -53
  6. package/dist/cjs/fhirpath/parse.d.ts +37 -0
  7. package/dist/cjs/format.d.ts +2 -1
  8. package/dist/cjs/{index.js → index.cjs} +999 -996
  9. package/dist/cjs/index.cjs.map +1 -0
  10. package/dist/cjs/index.min.cjs +1 -0
  11. package/dist/cjs/search.d.ts +2 -1
  12. package/dist/esm/{base-schema.json.js → base-schema.json.mjs} +1 -1
  13. package/dist/esm/{base-schema.json.js.map → base-schema.json.mjs.map} +1 -1
  14. package/dist/esm/{cache.js → cache.mjs} +2 -2
  15. package/dist/esm/cache.mjs.map +1 -0
  16. package/dist/esm/client.d.ts +1 -0
  17. package/dist/esm/{client.js → client.mjs} +264 -268
  18. package/dist/esm/client.mjs.map +1 -0
  19. package/dist/esm/{crypto.js → crypto.mjs} +4 -7
  20. package/dist/esm/crypto.mjs.map +1 -0
  21. package/dist/esm/{eventtarget.js → eventtarget.mjs} +2 -2
  22. package/dist/esm/eventtarget.mjs.map +1 -0
  23. package/dist/esm/fhirlexer/parse.d.ts +18 -2
  24. package/dist/esm/fhirlexer/{parse.js → parse.mjs} +35 -10
  25. package/dist/esm/fhirlexer/parse.mjs.map +1 -0
  26. package/dist/esm/fhirlexer/{tokenize.js → tokenize.mjs} +12 -12
  27. package/dist/esm/fhirlexer/tokenize.mjs.map +1 -0
  28. package/dist/esm/fhirmapper/parse.d.ts +2 -2
  29. package/dist/esm/fhirpath/atoms.d.ts +28 -53
  30. package/dist/esm/fhirpath/{atoms.js → atoms.mjs} +79 -62
  31. package/dist/esm/fhirpath/atoms.mjs.map +1 -0
  32. package/dist/esm/fhirpath/{date.js → date.mjs} +1 -1
  33. package/dist/esm/fhirpath/date.mjs.map +1 -0
  34. package/dist/esm/fhirpath/{functions.js → functions.mjs} +16 -22
  35. package/dist/esm/fhirpath/functions.mjs.map +1 -0
  36. package/dist/esm/fhirpath/parse.d.ts +37 -0
  37. package/dist/esm/fhirpath/{parse.js → parse.mjs} +38 -42
  38. package/dist/esm/fhirpath/parse.mjs.map +1 -0
  39. package/dist/esm/fhirpath/{tokenize.js → tokenize.mjs} +4 -4
  40. package/dist/esm/fhirpath/tokenize.mjs.map +1 -0
  41. package/dist/esm/fhirpath/{utils.js → utils.mjs} +6 -7
  42. package/dist/esm/fhirpath/utils.mjs.map +1 -0
  43. package/dist/esm/format.d.ts +2 -1
  44. package/dist/esm/{format.js → format.mjs} +29 -21
  45. package/dist/esm/format.mjs.map +1 -0
  46. package/dist/esm/{hl7.js → hl7.mjs} +8 -9
  47. package/dist/esm/hl7.mjs.map +1 -0
  48. package/dist/esm/index.min.mjs +1 -0
  49. package/dist/esm/{index.js → index.mjs} +17 -17
  50. package/dist/esm/index.mjs.map +1 -0
  51. package/dist/esm/{jwt.js → jwt.mjs} +1 -1
  52. package/dist/esm/jwt.mjs.map +1 -0
  53. package/dist/esm/{match.js → match.mjs} +12 -13
  54. package/dist/esm/match.mjs.map +1 -0
  55. package/dist/esm/node_modules/tslib/{tslib.es6.js → tslib.es6.mjs} +2 -12
  56. package/dist/esm/node_modules/tslib/tslib.es6.mjs.map +1 -0
  57. package/dist/esm/{outcomes.js → outcomes.mjs} +3 -5
  58. package/dist/esm/outcomes.mjs.map +1 -0
  59. package/dist/esm/{readablepromise.js → readablepromise.mjs} +2 -2
  60. package/dist/esm/readablepromise.mjs.map +1 -0
  61. package/dist/esm/search.d.ts +2 -1
  62. package/dist/esm/{search.js → search.mjs} +1 -1
  63. package/dist/esm/search.mjs.map +1 -0
  64. package/dist/esm/{searchparams.js → searchparams.mjs} +8 -9
  65. package/dist/esm/searchparams.mjs.map +1 -0
  66. package/dist/esm/{storage.js → storage.mjs} +4 -5
  67. package/dist/esm/storage.mjs.map +1 -0
  68. package/dist/esm/{types.js → types.mjs} +6 -9
  69. package/dist/esm/types.mjs.map +1 -0
  70. package/dist/esm/{utils.js → utils.mjs} +15 -26
  71. package/dist/esm/utils.mjs.map +1 -0
  72. package/package.json +5 -5
  73. package/rollup.config.mjs +6 -7
  74. package/dist/cjs/fhirmapper/atoms.d.ts +0 -67
  75. package/dist/cjs/fhirmapper/testall.d.ts +0 -1
  76. package/dist/cjs/index.js.map +0 -1
  77. package/dist/cjs/index.min.js +0 -2
  78. package/dist/cjs/index.min.js.map +0 -1
  79. package/dist/esm/cache.js.map +0 -1
  80. package/dist/esm/client.js.map +0 -1
  81. package/dist/esm/crypto.js.map +0 -1
  82. package/dist/esm/eventtarget.js.map +0 -1
  83. package/dist/esm/fhirlexer/parse.js.map +0 -1
  84. package/dist/esm/fhirlexer/tokenize.js.map +0 -1
  85. package/dist/esm/fhirmapper/atoms.d.ts +0 -67
  86. package/dist/esm/fhirmapper/testall.d.ts +0 -1
  87. package/dist/esm/fhirpath/atoms.js.map +0 -1
  88. package/dist/esm/fhirpath/date.js.map +0 -1
  89. package/dist/esm/fhirpath/functions.js.map +0 -1
  90. package/dist/esm/fhirpath/parse.js.map +0 -1
  91. package/dist/esm/fhirpath/tokenize.js.map +0 -1
  92. package/dist/esm/fhirpath/utils.js.map +0 -1
  93. package/dist/esm/format.js.map +0 -1
  94. package/dist/esm/hl7.js.map +0 -1
  95. package/dist/esm/index.js.map +0 -1
  96. package/dist/esm/index.min.js +0 -2
  97. package/dist/esm/index.min.js.map +0 -1
  98. package/dist/esm/jwt.js.map +0 -1
  99. package/dist/esm/match.js.map +0 -1
  100. package/dist/esm/node_modules/tslib/tslib.es6.js.map +0 -1
  101. package/dist/esm/outcomes.js.map +0 -1
  102. package/dist/esm/readablepromise.js.map +0 -1
  103. package/dist/esm/search.js.map +0 -1
  104. package/dist/esm/searchparams.js.map +0 -1
  105. package/dist/esm/storage.js.map +0 -1
  106. package/dist/esm/types.js.map +0 -1
  107. package/dist/esm/utils.js.map +0 -1
@@ -1,17 +1,17 @@
1
- import { __classPrivateFieldSet, __classPrivateFieldGet, __awaiter } from './node_modules/tslib/tslib.es6.js';
2
- import { LRUCache } from './cache.js';
3
- import { getRandomString, encryptSHA256 } from './crypto.js';
4
- import { EventTarget } from './eventtarget.js';
5
- import { parseJWTPayload } from './jwt.js';
6
- import { ReadablePromise } from './readablepromise.js';
7
- import { ClientStorage } from './storage.js';
8
- import { globalSchema, indexStructureDefinition, indexSearchParameter } from './types.js';
9
- import { createReference, arrayBufferToBase64 } from './utils.js';
1
+ import { __classPrivateFieldSet, __classPrivateFieldGet } from './node_modules/tslib/tslib.es6.mjs';
2
+ import { LRUCache } from './cache.mjs';
3
+ import { getRandomString, encryptSHA256 } from './crypto.mjs';
4
+ import { EventTarget } from './eventtarget.mjs';
5
+ import { parseJWTPayload } from './jwt.mjs';
6
+ import { ReadablePromise } from './readablepromise.mjs';
7
+ import { ClientStorage } from './storage.mjs';
8
+ import { globalSchema, indexStructureDefinition, indexSearchParameter } from './types.mjs';
9
+ import { createReference, arrayBufferToBase64 } from './utils.mjs';
10
10
 
11
11
  // PKCE auth based on:
12
12
  // https://aws.amazon.com/blogs/security/how-to-add-authentication-single-page-web-application-with-amazon-cognito-oauth2-implementation/
13
- var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_createPdf, _MedplumClient_storage, _MedplumClient_requestCache, _MedplumClient_cacheTime, _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_getCacheEntry, _MedplumClient_setCacheEntry, _MedplumClient_request, _MedplumClient_addFetchOptionsDefaults, _MedplumClient_setRequestContentType, _MedplumClient_setRequestBody, _MedplumClient_handleUnauthenticated, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
14
- const MEDPLUM_VERSION = "1.0.4-6dbdb7f6";
13
+ var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_createPdf, _MedplumClient_storage, _MedplumClient_requestCache, _MedplumClient_cacheTime, _MedplumClient_baseUrl, _MedplumClient_authorizeUrl, _MedplumClient_tokenUrl, _MedplumClient_logoutUrl, _MedplumClient_onUnauthenticated, _MedplumClient_clientId, _MedplumClient_clientSecret, _MedplumClient_accessToken, _MedplumClient_refreshToken, _MedplumClient_refreshPromise, _MedplumClient_profilePromise, _MedplumClient_profile, _MedplumClient_config, _MedplumClient_addLogin, _MedplumClient_refreshProfile, _MedplumClient_getCacheEntry, _MedplumClient_setCacheEntry, _MedplumClient_request, _MedplumClient_addFetchOptionsDefaults, _MedplumClient_setRequestContentType, _MedplumClient_setRequestBody, _MedplumClient_handleUnauthenticated, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
14
+ const MEDPLUM_VERSION = "1.0.6-5860113c";
15
15
  const DEFAULT_BASE_URL = 'https://api.medplum.com/';
16
16
  const DEFAULT_RESOURCE_CACHE_SIZE = 1000;
17
17
  const DEFAULT_CACHE_TIME = 60000; // 60 seconds
@@ -72,7 +72,6 @@ const PATCH_CONTENT_TYPE = 'application/json-patch+json';
72
72
  */
73
73
  class MedplumClient extends EventTarget {
74
74
  constructor(options) {
75
- var _a, _b;
76
75
  super();
77
76
  _MedplumClient_instances.add(this);
78
77
  _MedplumClient_fetch.set(this, void 0);
@@ -81,33 +80,34 @@ class MedplumClient extends EventTarget {
81
80
  _MedplumClient_requestCache.set(this, void 0);
82
81
  _MedplumClient_cacheTime.set(this, void 0);
83
82
  _MedplumClient_baseUrl.set(this, void 0);
84
- _MedplumClient_clientId.set(this, void 0);
85
83
  _MedplumClient_authorizeUrl.set(this, void 0);
86
84
  _MedplumClient_tokenUrl.set(this, void 0);
87
85
  _MedplumClient_logoutUrl.set(this, void 0);
88
86
  _MedplumClient_onUnauthenticated.set(this, void 0);
87
+ _MedplumClient_clientId.set(this, void 0);
88
+ _MedplumClient_clientSecret.set(this, void 0);
89
89
  _MedplumClient_accessToken.set(this, void 0);
90
90
  _MedplumClient_refreshToken.set(this, void 0);
91
91
  _MedplumClient_refreshPromise.set(this, void 0);
92
92
  _MedplumClient_profilePromise.set(this, void 0);
93
93
  _MedplumClient_profile.set(this, void 0);
94
94
  _MedplumClient_config.set(this, void 0);
95
- if (options === null || options === void 0 ? void 0 : options.baseUrl) {
95
+ if (options?.baseUrl) {
96
96
  if (!options.baseUrl.startsWith('http')) {
97
97
  throw new Error('Base URL must start with http or https');
98
98
  }
99
99
  }
100
- __classPrivateFieldSet(this, _MedplumClient_fetch, (options === null || options === void 0 ? void 0 : options.fetch) || window.fetch.bind(window), "f");
101
- __classPrivateFieldSet(this, _MedplumClient_createPdf, options === null || options === void 0 ? void 0 : options.createPdf, "f");
100
+ __classPrivateFieldSet(this, _MedplumClient_fetch, options?.fetch || window.fetch.bind(window), "f");
101
+ __classPrivateFieldSet(this, _MedplumClient_createPdf, options?.createPdf, "f");
102
102
  __classPrivateFieldSet(this, _MedplumClient_storage, new ClientStorage(), "f");
103
- __classPrivateFieldSet(this, _MedplumClient_requestCache, new LRUCache((_a = options === null || options === void 0 ? void 0 : options.resourceCacheSize) !== null && _a !== void 0 ? _a : DEFAULT_RESOURCE_CACHE_SIZE), "f");
104
- __classPrivateFieldSet(this, _MedplumClient_cacheTime, (_b = options === null || options === void 0 ? void 0 : options.cacheTime) !== null && _b !== void 0 ? _b : DEFAULT_CACHE_TIME, "f");
105
- __classPrivateFieldSet(this, _MedplumClient_baseUrl, ensureTrailingSlash(options === null || options === void 0 ? void 0 : options.baseUrl) || DEFAULT_BASE_URL, "f");
106
- __classPrivateFieldSet(this, _MedplumClient_clientId, (options === null || options === void 0 ? void 0 : options.clientId) || '', "f");
107
- __classPrivateFieldSet(this, _MedplumClient_authorizeUrl, (options === null || options === void 0 ? void 0 : options.authorizeUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/authorize', "f");
108
- __classPrivateFieldSet(this, _MedplumClient_tokenUrl, (options === null || options === void 0 ? void 0 : options.tokenUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/token', "f");
109
- __classPrivateFieldSet(this, _MedplumClient_logoutUrl, (options === null || options === void 0 ? void 0 : options.logoutUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/logout', "f");
110
- __classPrivateFieldSet(this, _MedplumClient_onUnauthenticated, options === null || options === void 0 ? void 0 : options.onUnauthenticated, "f");
103
+ __classPrivateFieldSet(this, _MedplumClient_requestCache, new LRUCache(options?.resourceCacheSize ?? DEFAULT_RESOURCE_CACHE_SIZE), "f");
104
+ __classPrivateFieldSet(this, _MedplumClient_cacheTime, options?.cacheTime ?? DEFAULT_CACHE_TIME, "f");
105
+ __classPrivateFieldSet(this, _MedplumClient_baseUrl, ensureTrailingSlash(options?.baseUrl) || DEFAULT_BASE_URL, "f");
106
+ __classPrivateFieldSet(this, _MedplumClient_clientId, options?.clientId || '', "f");
107
+ __classPrivateFieldSet(this, _MedplumClient_authorizeUrl, options?.authorizeUrl || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/authorize', "f");
108
+ __classPrivateFieldSet(this, _MedplumClient_tokenUrl, options?.tokenUrl || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/token', "f");
109
+ __classPrivateFieldSet(this, _MedplumClient_logoutUrl, options?.logoutUrl || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/logout', "f");
110
+ __classPrivateFieldSet(this, _MedplumClient_onUnauthenticated, options?.onUnauthenticated, "f");
111
111
  const activeLogin = this.getActiveLogin();
112
112
  if (activeLogin) {
113
113
  __classPrivateFieldSet(this, _MedplumClient_accessToken, activeLogin.accessToken, "f");
@@ -282,10 +282,12 @@ class MedplumClient extends EventTarget {
282
282
  * @param newUserRequest Register request including email and password.
283
283
  * @returns Promise to the authentication response.
284
284
  */
285
- startNewUser(newUserRequest) {
286
- return __awaiter(this, void 0, void 0, function* () {
287
- yield this.startPkce();
288
- return this.post('auth/newuser', Object.assign(Object.assign({}, newUserRequest), { codeChallengeMethod: 'S256', codeChallenge: sessionStorage.getItem('codeChallenge') }));
285
+ async startNewUser(newUserRequest) {
286
+ await this.startPkce();
287
+ return this.post('auth/newuser', {
288
+ ...newUserRequest,
289
+ codeChallengeMethod: 'S256',
290
+ codeChallenge: sessionStorage.getItem('codeChallenge'),
289
291
  });
290
292
  }
291
293
  /**
@@ -296,10 +298,8 @@ class MedplumClient extends EventTarget {
296
298
  * @param newProjectRequest Register request including email and password.
297
299
  * @returns Promise to the authentication response.
298
300
  */
299
- startNewProject(newProjectRequest) {
300
- return __awaiter(this, void 0, void 0, function* () {
301
- return this.post('auth/newproject', newProjectRequest);
302
- });
301
+ async startNewProject(newProjectRequest) {
302
+ return this.post('auth/newproject', newProjectRequest);
303
303
  }
304
304
  /**
305
305
  * Initiates a new patient flow.
@@ -309,10 +309,8 @@ class MedplumClient extends EventTarget {
309
309
  * @param newPatientRequest Register request including email and password.
310
310
  * @returns Promise to the authentication response.
311
311
  */
312
- startNewPatient(newPatientRequest) {
313
- return __awaiter(this, void 0, void 0, function* () {
314
- return this.post('auth/newpatient', newPatientRequest);
315
- });
312
+ async startNewPatient(newPatientRequest) {
313
+ return this.post('auth/newpatient', newPatientRequest);
316
314
  }
317
315
  /**
318
316
  * Initiates a user login flow.
@@ -320,12 +318,14 @@ class MedplumClient extends EventTarget {
320
318
  * @param loginRequest Login request including email and password.
321
319
  * @returns Promise to the authentication response.
322
320
  */
323
- startLogin(loginRequest) {
324
- var _a;
325
- return __awaiter(this, void 0, void 0, function* () {
326
- const { codeChallenge, codeChallengeMethod } = this.getCodeChallenge(loginRequest);
327
- return this.post('auth/login', Object.assign(Object.assign({}, loginRequest), { clientId: (_a = loginRequest.clientId) !== null && _a !== void 0 ? _a : __classPrivateFieldGet(this, _MedplumClient_clientId, "f"), scope: loginRequest.scope, codeChallengeMethod,
328
- codeChallenge }));
321
+ async startLogin(loginRequest) {
322
+ const { codeChallenge, codeChallengeMethod } = this.getCodeChallenge(loginRequest);
323
+ return this.post('auth/login', {
324
+ ...loginRequest,
325
+ clientId: loginRequest.clientId ?? __classPrivateFieldGet(this, _MedplumClient_clientId, "f"),
326
+ scope: loginRequest.scope,
327
+ codeChallengeMethod,
328
+ codeChallenge,
329
329
  });
330
330
  }
331
331
  /**
@@ -336,12 +336,14 @@ class MedplumClient extends EventTarget {
336
336
  * @param loginRequest Login request including Google credential response.
337
337
  * @returns Promise to the authentication response.
338
338
  */
339
- startGoogleLogin(loginRequest) {
340
- var _a;
341
- return __awaiter(this, void 0, void 0, function* () {
342
- const { codeChallenge, codeChallengeMethod } = this.getCodeChallenge(loginRequest);
343
- return this.post('auth/google', Object.assign(Object.assign({}, loginRequest), { clientId: (_a = loginRequest.clientId) !== null && _a !== void 0 ? _a : __classPrivateFieldGet(this, _MedplumClient_clientId, "f"), scope: loginRequest.scope, codeChallengeMethod,
344
- codeChallenge }));
339
+ async startGoogleLogin(loginRequest) {
340
+ const { codeChallenge, codeChallengeMethod } = this.getCodeChallenge(loginRequest);
341
+ return this.post('auth/google', {
342
+ ...loginRequest,
343
+ clientId: loginRequest.clientId ?? __classPrivateFieldGet(this, _MedplumClient_clientId, "f"),
344
+ scope: loginRequest.scope,
345
+ codeChallengeMethod,
346
+ codeChallenge,
345
347
  });
346
348
  }
347
349
  getCodeChallenge(loginRequest) {
@@ -374,18 +376,16 @@ class MedplumClient extends EventTarget {
374
376
  * This may result in navigating away to the sign in page.
375
377
  * @category Authentication
376
378
  */
377
- signInWithRedirect() {
378
- return __awaiter(this, void 0, void 0, function* () {
379
- const urlParams = new URLSearchParams(window.location.search);
380
- const code = urlParams.get('code');
381
- if (!code) {
382
- yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_requestAuthorization).call(this);
383
- return undefined;
384
- }
385
- else {
386
- return this.processCode(code);
387
- }
388
- });
379
+ async signInWithRedirect() {
380
+ const urlParams = new URLSearchParams(window.location.search);
381
+ const code = urlParams.get('code');
382
+ if (!code) {
383
+ await __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_requestAuthorization).call(this);
384
+ return undefined;
385
+ }
386
+ else {
387
+ return this.processCode(code);
388
+ }
389
389
  }
390
390
  /**
391
391
  * Tries to sign out the user.
@@ -501,7 +501,7 @@ class MedplumClient extends EventTarget {
501
501
  if (cached) {
502
502
  return cached.value;
503
503
  }
504
- const promise = new ReadablePromise(this.search(resourceType, url.searchParams, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
504
+ const promise = new ReadablePromise(this.search(resourceType, url.searchParams, options).then((b) => b.entry?.[0]?.resource));
505
505
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
506
506
  return promise;
507
507
  }
@@ -534,7 +534,7 @@ class MedplumClient extends EventTarget {
534
534
  if (cached) {
535
535
  return cached.value;
536
536
  }
537
- const promise = new ReadablePromise(this.search(resourceType, query, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a.map((e) => e.resource)) !== null && _b !== void 0 ? _b : []; }));
537
+ const promise = new ReadablePromise(this.search(resourceType, query, options).then((b) => b.entry?.map((e) => e.resource) ?? []));
538
538
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
539
539
  return promise;
540
540
  }
@@ -562,8 +562,7 @@ class MedplumClient extends EventTarget {
562
562
  * @returns The resource if it is available in the cache; undefined otherwise.
563
563
  */
564
564
  getCached(resourceType, id) {
565
- var _a;
566
- const cached = (_a = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString())) === null || _a === void 0 ? void 0 : _a.value;
565
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString())?.value;
567
566
  return cached && cached.isOk() ? cached.read() : undefined;
568
567
  }
569
568
  /**
@@ -626,7 +625,7 @@ class MedplumClient extends EventTarget {
626
625
  * @returns The resource if available; undefined otherwise.
627
626
  */
628
627
  readReference(reference, options = {}) {
629
- const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
628
+ const refString = reference?.reference;
630
629
  if (!refString) {
631
630
  return new ReadablePromise(Promise.reject(new Error('Missing reference')));
632
631
  }
@@ -654,12 +653,11 @@ class MedplumClient extends EventTarget {
654
653
  * @param resourceType The FHIR resource type.
655
654
  * @returns Promise to a schema with the requested resource type.
656
655
  */
657
- requestSchema(resourceType) {
658
- return __awaiter(this, void 0, void 0, function* () {
659
- if (resourceType in globalSchema.types) {
660
- return globalSchema;
661
- }
662
- const query = `{
656
+ async requestSchema(resourceType) {
657
+ if (resourceType in globalSchema.types) {
658
+ return globalSchema;
659
+ }
660
+ const query = `{
663
661
  StructureDefinitionList(name: "${resourceType}") {
664
662
  name,
665
663
  description,
@@ -688,15 +686,14 @@ class MedplumClient extends EventTarget {
688
686
  target
689
687
  }
690
688
  }`.replace(/\s+/g, ' ');
691
- const response = (yield this.graphql(query));
692
- for (const structureDefinition of response.data.StructureDefinitionList) {
693
- indexStructureDefinition(structureDefinition);
694
- }
695
- for (const searchParameter of response.data.SearchParameterList) {
696
- indexSearchParameter(searchParameter);
697
- }
698
- return globalSchema;
699
- });
689
+ const response = (await this.graphql(query));
690
+ for (const structureDefinition of response.data.StructureDefinitionList) {
691
+ indexStructureDefinition(structureDefinition);
692
+ }
693
+ for (const searchParameter of response.data.SearchParameterList) {
694
+ indexSearchParameter(searchParameter);
695
+ }
696
+ return globalSchema;
700
697
  }
701
698
  /**
702
699
  * Reads resource history by resource type and ID.
@@ -834,11 +831,8 @@ class MedplumClient extends EventTarget {
834
831
  * @param query The search query for an equivalent resource (should not include resource type or "?").
835
832
  * @returns The result of the create operation.
836
833
  */
837
- createResourceIfNoneExist(resource, query) {
838
- var _a;
839
- return __awaiter(this, void 0, void 0, function* () {
840
- return ((_a = (yield this.searchOne(resource.resourceType, query))) !== null && _a !== void 0 ? _a : this.createResource(resource));
841
- });
834
+ async createResourceIfNoneExist(resource, query) {
835
+ return ((await this.searchOne(resource.resourceType, query)) ?? this.createResource(resource));
842
836
  }
843
837
  /**
844
838
  * Creates a FHIR `Binary` resource with the provided data content.
@@ -925,14 +919,12 @@ class MedplumClient extends EventTarget {
925
919
  * @param docDefinition The PDF document definition.
926
920
  * @returns The result of the create operation.
927
921
  */
928
- createPdf(docDefinition, filename, tableLayouts, fonts) {
929
- return __awaiter(this, void 0, void 0, function* () {
930
- if (!__classPrivateFieldGet(this, _MedplumClient_createPdf, "f")) {
931
- throw new Error('PDF creation not enabled');
932
- }
933
- const blob = yield __classPrivateFieldGet(this, _MedplumClient_createPdf, "f").call(this, docDefinition, tableLayouts, fonts);
934
- return this.createBinary(blob, filename, 'application/pdf');
935
- });
922
+ async createPdf(docDefinition, filename, tableLayouts, fonts) {
923
+ if (!__classPrivateFieldGet(this, _MedplumClient_createPdf, "f")) {
924
+ throw new Error('PDF creation not enabled');
925
+ }
926
+ const blob = await __classPrivateFieldGet(this, _MedplumClient_createPdf, "f").call(this, docDefinition, tableLayouts, fonts);
927
+ return this.createBinary(blob, filename, 'application/pdf');
936
928
  }
937
929
  /**
938
930
  * Creates a FHIR `Communication` resource with the provided data content.
@@ -994,20 +986,18 @@ class MedplumClient extends EventTarget {
994
986
  * @param resource The FHIR resource to update.
995
987
  * @returns The result of the update operation.
996
988
  */
997
- updateResource(resource) {
998
- return __awaiter(this, void 0, void 0, function* () {
999
- if (!resource.resourceType) {
1000
- throw new Error('Missing resourceType');
1001
- }
1002
- if (!resource.id) {
1003
- throw new Error('Missing id');
1004
- }
1005
- this.invalidateSearches(resource.resourceType);
1006
- const result = yield this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
1007
- // On 304 not modified, result will be undefined
1008
- // Return the user input instead
1009
- return result !== null && result !== void 0 ? result : resource;
1010
- });
989
+ async updateResource(resource) {
990
+ if (!resource.resourceType) {
991
+ throw new Error('Missing resourceType');
992
+ }
993
+ if (!resource.id) {
994
+ throw new Error('Missing id');
995
+ }
996
+ this.invalidateSearches(resource.resourceType);
997
+ const result = await this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
998
+ // On 304 not modified, result will be undefined
999
+ // Return the user input instead
1000
+ return result ?? resource;
1011
1001
  }
1012
1002
  /**
1013
1003
  * Updates a FHIR resource using JSONPatch operations.
@@ -1242,18 +1232,16 @@ class MedplumClient extends EventTarget {
1242
1232
  /**
1243
1233
  * @category Authentication
1244
1234
  */
1245
- setActiveLogin(login) {
1246
- return __awaiter(this, void 0, void 0, function* () {
1247
- __classPrivateFieldSet(this, _MedplumClient_accessToken, login.accessToken, "f");
1248
- __classPrivateFieldSet(this, _MedplumClient_refreshToken, login.refreshToken, "f");
1249
- __classPrivateFieldSet(this, _MedplumClient_profile, undefined, "f");
1250
- __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1251
- __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('activeLogin', login);
1252
- __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addLogin).call(this, login);
1253
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").clear();
1254
- __classPrivateFieldSet(this, _MedplumClient_refreshPromise, undefined, "f");
1255
- yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refreshProfile).call(this);
1256
- });
1235
+ async setActiveLogin(login) {
1236
+ __classPrivateFieldSet(this, _MedplumClient_accessToken, login.accessToken, "f");
1237
+ __classPrivateFieldSet(this, _MedplumClient_refreshToken, login.refreshToken, "f");
1238
+ __classPrivateFieldSet(this, _MedplumClient_profile, undefined, "f");
1239
+ __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1240
+ __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('activeLogin', login);
1241
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addLogin).call(this, login);
1242
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").clear();
1243
+ __classPrivateFieldSet(this, _MedplumClient_refreshPromise, undefined, "f");
1244
+ await __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refreshProfile).call(this);
1257
1245
  }
1258
1246
  /**
1259
1247
  * @category Authentication
@@ -1274,8 +1262,7 @@ class MedplumClient extends EventTarget {
1274
1262
  * @category Authentication
1275
1263
  */
1276
1264
  getLogins() {
1277
- var _a;
1278
- return (_a = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getObject('logins')) !== null && _a !== void 0 ? _a : [];
1265
+ return __classPrivateFieldGet(this, _MedplumClient_storage, "f").getObject('logins') ?? [];
1279
1266
  }
1280
1267
  /**
1281
1268
  * @category Authentication
@@ -1292,13 +1279,11 @@ class MedplumClient extends EventTarget {
1292
1279
  /**
1293
1280
  * @category User Profile
1294
1281
  */
1295
- getProfileAsync() {
1296
- return __awaiter(this, void 0, void 0, function* () {
1297
- if (__classPrivateFieldGet(this, _MedplumClient_profilePromise, "f")) {
1298
- yield __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1299
- }
1300
- return this.getProfile();
1301
- });
1282
+ async getProfileAsync() {
1283
+ if (__classPrivateFieldGet(this, _MedplumClient_profilePromise, "f")) {
1284
+ await __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1285
+ }
1286
+ return this.getProfile();
1302
1287
  }
1303
1288
  /**
1304
1289
  * @category User Profile
@@ -1313,30 +1298,26 @@ class MedplumClient extends EventTarget {
1313
1298
  * @param url The URL to request.
1314
1299
  * @returns Promise to the response body as a blob.
1315
1300
  */
1316
- download(url, options = {}) {
1317
- return __awaiter(this, void 0, void 0, function* () {
1318
- if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1319
- yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1320
- }
1321
- __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addFetchOptionsDefaults).call(this, options);
1322
- const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url.toString(), options);
1323
- return response.blob();
1324
- });
1301
+ async download(url, options = {}) {
1302
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1303
+ await __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1304
+ }
1305
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addFetchOptionsDefaults).call(this, options);
1306
+ const response = await __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url.toString(), options);
1307
+ return response.blob();
1325
1308
  }
1326
1309
  /**
1327
1310
  * Starts a new PKCE flow.
1328
1311
  * These PKCE values are stateful, and must survive redirects and page refreshes.
1329
1312
  */
1330
- startPkce() {
1331
- return __awaiter(this, void 0, void 0, function* () {
1332
- const pkceState = getRandomString();
1333
- sessionStorage.setItem('pkceState', pkceState);
1334
- const codeVerifier = getRandomString();
1335
- sessionStorage.setItem('codeVerifier', codeVerifier);
1336
- const arrayHash = yield encryptSHA256(codeVerifier);
1337
- const codeChallenge = arrayBufferToBase64(arrayHash).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
1338
- sessionStorage.setItem('codeChallenge', codeChallenge);
1339
- });
1313
+ async startPkce() {
1314
+ const pkceState = getRandomString();
1315
+ sessionStorage.setItem('pkceState', pkceState);
1316
+ const codeVerifier = getRandomString();
1317
+ sessionStorage.setItem('codeVerifier', codeVerifier);
1318
+ const arrayHash = await encryptSHA256(codeVerifier);
1319
+ const codeChallenge = arrayBufferToBase64(arrayHash).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
1320
+ sessionStorage.setItem('codeChallenge', codeChallenge);
1340
1321
  }
1341
1322
  /**
1342
1323
  * Processes an OAuth authorization code.
@@ -1363,37 +1344,35 @@ class MedplumClient extends EventTarget {
1363
1344
  * @param clientSecret The client secret.
1364
1345
  * @returns Promise that resolves to the client profile.
1365
1346
  */
1366
- startClientLogin(clientId, clientSecret) {
1367
- return __awaiter(this, void 0, void 0, function* () {
1368
- const formBody = new URLSearchParams();
1369
- formBody.set('grant_type', 'client_credentials');
1370
- formBody.set('client_id', clientId);
1371
- formBody.set('client_secret', clientSecret);
1372
- return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody);
1373
- });
1347
+ async startClientLogin(clientId, clientSecret) {
1348
+ __classPrivateFieldSet(this, _MedplumClient_clientId, clientId, "f");
1349
+ __classPrivateFieldSet(this, _MedplumClient_clientSecret, clientSecret, "f");
1350
+ const formBody = new URLSearchParams();
1351
+ formBody.set('grant_type', 'client_credentials');
1352
+ formBody.set('client_id', clientId);
1353
+ formBody.set('client_secret', clientSecret);
1354
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody);
1374
1355
  }
1375
1356
  }
1376
- _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_requestCache = new WeakMap(), _MedplumClient_cacheTime = 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) {
1377
- 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); });
1357
+ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_requestCache = new WeakMap(), _MedplumClient_cacheTime = new WeakMap(), _MedplumClient_baseUrl = new WeakMap(), _MedplumClient_authorizeUrl = new WeakMap(), _MedplumClient_tokenUrl = new WeakMap(), _MedplumClient_logoutUrl = new WeakMap(), _MedplumClient_onUnauthenticated = new WeakMap(), _MedplumClient_clientId = new WeakMap(), _MedplumClient_clientSecret = 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) {
1358
+ const logins = this.getLogins().filter((login) => login.profile?.reference !== newLogin.profile?.reference);
1378
1359
  logins.push(newLogin);
1379
1360
  __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('logins', logins);
1380
- }, _MedplumClient_refreshProfile = function _MedplumClient_refreshProfile() {
1381
- return __awaiter(this, void 0, void 0, function* () {
1382
- __classPrivateFieldSet(this, _MedplumClient_profilePromise, new Promise((resolve, reject) => {
1383
- this.get('auth/me')
1384
- .then((result) => {
1385
- __classPrivateFieldSet(this, _MedplumClient_profilePromise, undefined, "f");
1386
- __classPrivateFieldSet(this, _MedplumClient_profile, result.profile, "f");
1387
- __classPrivateFieldSet(this, _MedplumClient_config, result.config, "f");
1388
- this.dispatchEvent({ type: 'change' });
1389
- resolve(__classPrivateFieldGet(this, _MedplumClient_profile, "f"));
1390
- })
1391
- .catch(reject);
1392
- }), "f");
1393
- return __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1394
- });
1361
+ }, _MedplumClient_refreshProfile = async function _MedplumClient_refreshProfile() {
1362
+ __classPrivateFieldSet(this, _MedplumClient_profilePromise, new Promise((resolve, reject) => {
1363
+ this.get('auth/me')
1364
+ .then((result) => {
1365
+ __classPrivateFieldSet(this, _MedplumClient_profilePromise, undefined, "f");
1366
+ __classPrivateFieldSet(this, _MedplumClient_profile, result.profile, "f");
1367
+ __classPrivateFieldSet(this, _MedplumClient_config, result.config, "f");
1368
+ this.dispatchEvent({ type: 'change' });
1369
+ resolve(__classPrivateFieldGet(this, _MedplumClient_profile, "f"));
1370
+ })
1371
+ .catch(reject);
1372
+ }), "f");
1373
+ return __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
1395
1374
  }, _MedplumClient_getCacheEntry = function _MedplumClient_getCacheEntry(key, options) {
1396
- if (__classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") <= 0 || (options === null || options === void 0 ? void 0 : options.cache) === 'no-cache' || (options === null || options === void 0 ? void 0 : options.cache) === 'reload') {
1375
+ if (__classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") <= 0 || options?.cache === 'no-cache' || options?.cache === 'reload') {
1397
1376
  return undefined;
1398
1377
  }
1399
1378
  const entry = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(key);
@@ -1405,31 +1384,37 @@ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(),
1405
1384
  if (__classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") > 0) {
1406
1385
  __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(key, { requestTime: Date.now(), value });
1407
1386
  }
1408
- }, _MedplumClient_request = function _MedplumClient_request(method, url, options = {}) {
1409
- return __awaiter(this, void 0, void 0, function* () {
1410
- if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1411
- yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1412
- }
1413
- if (!url.startsWith('http')) {
1414
- url = __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + url;
1415
- }
1416
- options.method = method;
1417
- __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addFetchOptionsDefaults).call(this, options);
1418
- const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url, options);
1419
- if (response.status === 401) {
1420
- // Refresh and try again
1421
- return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_handleUnauthenticated).call(this, method, url, options);
1422
- }
1423
- if (response.status === 204 || response.status === 304) {
1424
- // No content or change
1425
- return undefined;
1426
- }
1427
- const obj = yield response.json();
1428
- if (response.status >= 400) {
1429
- throw obj;
1430
- }
1431
- return obj;
1432
- });
1387
+ }, _MedplumClient_request =
1388
+ /**
1389
+ * Makes an HTTP request.
1390
+ * @param {string} method
1391
+ * @param {string} url
1392
+ * @param {string=} contentType
1393
+ * @param {Object=} body
1394
+ */
1395
+ async function _MedplumClient_request(method, url, options = {}) {
1396
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1397
+ await __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1398
+ }
1399
+ if (!url.startsWith('http')) {
1400
+ url = __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + url;
1401
+ }
1402
+ options.method = method;
1403
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addFetchOptionsDefaults).call(this, options);
1404
+ const response = await __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url, options);
1405
+ if (response.status === 401) {
1406
+ // Refresh and try again
1407
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_handleUnauthenticated).call(this, method, url, options);
1408
+ }
1409
+ if (response.status === 204 || response.status === 304) {
1410
+ // No content or change
1411
+ return undefined;
1412
+ }
1413
+ const obj = await response.json();
1414
+ if (response.status >= 400) {
1415
+ throw obj;
1416
+ }
1417
+ return obj;
1433
1418
  }, _MedplumClient_addFetchOptionsDefaults = function _MedplumClient_addFetchOptionsDefaults(options) {
1434
1419
  if (!options.headers) {
1435
1420
  options.headers = {};
@@ -1465,82 +1450,93 @@ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(),
1465
1450
  options.body = JSON.stringify(data);
1466
1451
  }
1467
1452
  }, _MedplumClient_handleUnauthenticated = function _MedplumClient_handleUnauthenticated(method, url, options) {
1468
- return __awaiter(this, void 0, void 0, function* () {
1469
- return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refresh).call(this)
1470
- .then(() => __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, method, url, options))
1471
- .catch((error) => {
1472
- this.clear();
1473
- if (__classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f")) {
1474
- __classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f").call(this);
1475
- }
1476
- return Promise.reject(error);
1477
- });
1478
- });
1479
- }, _MedplumClient_requestAuthorization = function _MedplumClient_requestAuthorization() {
1480
- return __awaiter(this, void 0, void 0, function* () {
1481
- yield this.startPkce();
1482
- const url = new URL(__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f"));
1483
- url.searchParams.set('response_type', 'code');
1484
- url.searchParams.set('state', sessionStorage.getItem('pkceState'));
1485
- url.searchParams.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
1486
- url.searchParams.set('redirect_uri', getBaseUrl());
1487
- url.searchParams.set('code_challenge_method', 'S256');
1488
- url.searchParams.set('code_challenge', sessionStorage.getItem('codeChallenge'));
1489
- window.location.assign(url.toString());
1490
- });
1453
+ if (__classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refresh).call(this)) {
1454
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, method, url, options);
1455
+ }
1456
+ this.clear();
1457
+ if (__classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f")) {
1458
+ __classPrivateFieldGet(this, _MedplumClient_onUnauthenticated, "f").call(this);
1459
+ }
1460
+ return Promise.reject(new Error('Unauthenticated'));
1461
+ }, _MedplumClient_requestAuthorization =
1462
+ /**
1463
+ * Redirects the user to the login screen for authorization.
1464
+ * Clears all auth state including local storage and session storage.
1465
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint
1466
+ */
1467
+ async function _MedplumClient_requestAuthorization() {
1468
+ await this.startPkce();
1469
+ const url = new URL(__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f"));
1470
+ url.searchParams.set('response_type', 'code');
1471
+ url.searchParams.set('state', sessionStorage.getItem('pkceState'));
1472
+ url.searchParams.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
1473
+ url.searchParams.set('redirect_uri', getBaseUrl());
1474
+ url.searchParams.set('code_challenge_method', 'S256');
1475
+ url.searchParams.set('code_challenge', sessionStorage.getItem('codeChallenge'));
1476
+ window.location.assign(url.toString());
1491
1477
  }, _MedplumClient_refresh = function _MedplumClient_refresh() {
1492
- return __awaiter(this, void 0, void 0, function* () {
1493
- if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1494
- return __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1495
- }
1496
- if (!__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f")) {
1497
- this.clear();
1498
- throw new Error('Invalid refresh token');
1499
- }
1478
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
1479
+ return __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1480
+ }
1481
+ if (__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f")) {
1500
1482
  const formBody = new URLSearchParams();
1501
1483
  formBody.set('grant_type', 'refresh_token');
1502
1484
  formBody.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
1503
1485
  formBody.set('refresh_token', __classPrivateFieldGet(this, _MedplumClient_refreshToken, "f"));
1504
1486
  __classPrivateFieldSet(this, _MedplumClient_refreshPromise, __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody), "f");
1505
- yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1506
- });
1507
- }, _MedplumClient_fetchTokens = function _MedplumClient_fetchTokens(formBody) {
1508
- return __awaiter(this, void 0, void 0, function* () {
1509
- return __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, __classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f"), {
1510
- method: 'POST',
1511
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1512
- body: formBody,
1513
- credentials: 'include',
1514
- })
1515
- .then((response) => {
1516
- if (!response.ok) {
1517
- throw new Error('Failed to fetch tokens');
1518
- }
1519
- return response.json();
1520
- })
1521
- .then((tokens) => __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_verifyTokens).call(this, tokens))
1522
- .then(() => this.getProfile());
1523
- });
1524
- }, _MedplumClient_verifyTokens = function _MedplumClient_verifyTokens(tokens) {
1525
- return __awaiter(this, void 0, void 0, function* () {
1526
- const token = tokens.access_token;
1527
- // Verify token has not expired
1528
- const tokenPayload = parseJWTPayload(token);
1529
- if (Date.now() >= tokenPayload.exp * 1000) {
1530
- this.clear();
1531
- throw new Error('Token expired');
1532
- }
1533
- // Verify app_client_id
1534
- if (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") && tokenPayload.client_id !== __classPrivateFieldGet(this, _MedplumClient_clientId, "f")) {
1535
- this.clear();
1536
- throw new Error('Token was not issued for this audience');
1487
+ return __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1488
+ }
1489
+ if (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") && __classPrivateFieldGet(this, _MedplumClient_clientSecret, "f")) {
1490
+ __classPrivateFieldSet(this, _MedplumClient_refreshPromise, this.startClientLogin(__classPrivateFieldGet(this, _MedplumClient_clientId, "f"), __classPrivateFieldGet(this, _MedplumClient_clientSecret, "f")), "f");
1491
+ return __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
1492
+ }
1493
+ return undefined;
1494
+ }, _MedplumClient_fetchTokens =
1495
+ /**
1496
+ * Makes a POST request to the tokens endpoint.
1497
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
1498
+ * @param formBody Token parameters in URL encoded format.
1499
+ */
1500
+ async function _MedplumClient_fetchTokens(formBody) {
1501
+ return __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, __classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f"), {
1502
+ method: 'POST',
1503
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1504
+ body: formBody,
1505
+ credentials: 'include',
1506
+ })
1507
+ .then((response) => {
1508
+ if (!response.ok) {
1509
+ throw new Error('Failed to fetch tokens');
1537
1510
  }
1538
- yield this.setActiveLogin({
1539
- accessToken: token,
1540
- refreshToken: tokens.refresh_token,
1541
- project: tokens.project,
1542
- profile: tokens.profile,
1543
- });
1511
+ return response.json();
1512
+ })
1513
+ .then((tokens) => __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_verifyTokens).call(this, tokens))
1514
+ .then(() => this.getProfile());
1515
+ }, _MedplumClient_verifyTokens =
1516
+ /**
1517
+ * Verifies the tokens received from the auth server.
1518
+ * Validates the JWT against the JWKS.
1519
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
1520
+ * @param tokens
1521
+ */
1522
+ async function _MedplumClient_verifyTokens(tokens) {
1523
+ const token = tokens.access_token;
1524
+ // Verify token has not expired
1525
+ const tokenPayload = parseJWTPayload(token);
1526
+ if (Date.now() >= tokenPayload.exp * 1000) {
1527
+ this.clear();
1528
+ throw new Error('Token expired');
1529
+ }
1530
+ // Verify app_client_id
1531
+ if (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") && tokenPayload.client_id !== __classPrivateFieldGet(this, _MedplumClient_clientId, "f")) {
1532
+ this.clear();
1533
+ throw new Error('Token was not issued for this audience');
1534
+ }
1535
+ await this.setActiveLogin({
1536
+ accessToken: token,
1537
+ refreshToken: tokens.refresh_token,
1538
+ project: tokens.project,
1539
+ profile: tokens.profile,
1544
1540
  });
1545
1541
  }, _MedplumClient_setupStorageListener = function _MedplumClient_setupStorageListener() {
1546
1542
  try {
@@ -1572,4 +1568,4 @@ function ensureTrailingSlash(url) {
1572
1568
  }
1573
1569
 
1574
1570
  export { MEDPLUM_VERSION, MedplumClient };
1575
- //# sourceMappingURL=client.js.map
1571
+ //# sourceMappingURL=client.mjs.map