@medplum/core 0.9.15 → 0.9.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1178,10 +1178,11 @@ function getPropertyDisplayName(property) {
1178
1178
 
1179
1179
  // PKCE auth ased on:
1180
1180
  // https://aws.amazon.com/blogs/security/how-to-add-authentication-single-page-web-application-with-amazon-cognito-oauth2-implementation/
1181
- var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_createPdf, _MedplumClient_storage, _MedplumClient_schema, _MedplumClient_requestCache, _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_addFetchOptionsDefaults, _MedplumClient_setRequestContentType, _MedplumClient_setRequestBody, _MedplumClient_handleUnauthenticated, _MedplumClient_startPkce, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
1181
+ var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_createPdf, _MedplumClient_storage, _MedplumClient_schema, _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_startPkce, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
1182
1182
  const DEFAULT_BASE_URL = 'https://api.medplum.com/';
1183
1183
  const DEFAULT_SCOPE = 'launch/patient openid fhirUser offline_access user/*.*';
1184
1184
  const DEFAULT_RESOURCE_CACHE_SIZE = 1000;
1185
+ const DEFAULT_CACHE_TIME = 10000; // 10 seconds
1185
1186
  const JSON_CONTENT_TYPE = 'application/json';
1186
1187
  const FHIR_CONTENT_TYPE = 'application/fhir+json';
1187
1188
  const PATCH_CONTENT_TYPE = 'application/json-patch+json';
@@ -1234,7 +1235,7 @@ const PATCH_CONTENT_TYPE = 'application/json-patch+json';
1234
1235
  */
1235
1236
  class MedplumClient extends EventTarget {
1236
1237
  constructor(options) {
1237
- var _a;
1238
+ var _a, _b;
1238
1239
  super();
1239
1240
  _MedplumClient_instances.add(this);
1240
1241
  _MedplumClient_fetch.set(this, void 0);
@@ -1242,6 +1243,7 @@ class MedplumClient extends EventTarget {
1242
1243
  _MedplumClient_storage.set(this, void 0);
1243
1244
  _MedplumClient_schema.set(this, void 0);
1244
1245
  _MedplumClient_requestCache.set(this, void 0);
1246
+ _MedplumClient_cacheTime.set(this, void 0);
1245
1247
  _MedplumClient_baseUrl.set(this, void 0);
1246
1248
  _MedplumClient_clientId.set(this, void 0);
1247
1249
  _MedplumClient_authorizeUrl.set(this, void 0);
@@ -1267,6 +1269,7 @@ class MedplumClient extends EventTarget {
1267
1269
  __classPrivateFieldSet(this, _MedplumClient_storage, new ClientStorage(), "f");
1268
1270
  __classPrivateFieldSet(this, _MedplumClient_schema, createSchema(), "f");
1269
1271
  __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");
1272
+ __classPrivateFieldSet(this, _MedplumClient_cacheTime, (_b = options === null || options === void 0 ? void 0 : options.cacheTime) !== null && _b !== void 0 ? _b : DEFAULT_CACHE_TIME, "f");
1270
1273
  __classPrivateFieldSet(this, _MedplumClient_baseUrl, (options === null || options === void 0 ? void 0 : options.baseUrl) || DEFAULT_BASE_URL, "f");
1271
1274
  __classPrivateFieldSet(this, _MedplumClient_clientId, (options === null || options === void 0 ? void 0 : options.clientId) || '', "f");
1272
1275
  __classPrivateFieldSet(this, _MedplumClient_authorizeUrl, (options === null || options === void 0 ? void 0 : options.authorizeUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/authorize', "f");
@@ -1335,14 +1338,12 @@ class MedplumClient extends EventTarget {
1335
1338
  */
1336
1339
  get(url, options = {}) {
1337
1340
  url = url.toString();
1338
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1339
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(url);
1340
- if (cached) {
1341
- return cached;
1342
- }
1341
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, url, options);
1342
+ if (cached) {
1343
+ return cached.value;
1343
1344
  }
1344
1345
  const promise = new ReadablePromise(__classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'GET', url, options));
1345
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(url, promise);
1346
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, url, promise);
1346
1347
  return promise;
1347
1348
  }
1348
1349
  /**
@@ -1582,14 +1583,12 @@ class MedplumClient extends EventTarget {
1582
1583
  url.searchParams.set('_count', '1');
1583
1584
  url.searchParams.sort();
1584
1585
  const cacheKey = url.toString() + '-searchOne';
1585
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1586
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1587
- if (cached) {
1588
- return cached;
1589
- }
1586
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, cacheKey, options);
1587
+ if (cached) {
1588
+ return cached.value;
1590
1589
  }
1591
1590
  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; }));
1592
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1591
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
1593
1592
  return promise;
1594
1593
  }
1595
1594
  /**
@@ -1614,14 +1613,12 @@ class MedplumClient extends EventTarget {
1614
1613
  searchResources(resourceType, query, options = {}) {
1615
1614
  const url = this.fhirSearchUrl(resourceType, query);
1616
1615
  const cacheKey = url.toString() + '-searchResources';
1617
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1618
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1619
- if (cached) {
1620
- return cached;
1621
- }
1616
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, cacheKey, options);
1617
+ if (cached) {
1618
+ return cached.value;
1622
1619
  }
1623
1620
  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 : []; }));
1624
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1621
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
1625
1622
  return promise;
1626
1623
  }
1627
1624
  /**
@@ -1644,7 +1641,8 @@ class MedplumClient extends EventTarget {
1644
1641
  * @returns The resource if it is available in the cache; undefined otherwise.
1645
1642
  */
1646
1643
  getCached(resourceType, id) {
1647
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString());
1644
+ var _a;
1645
+ const cached = (_a = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString())) === null || _a === void 0 ? void 0 : _a.value;
1648
1646
  return cached && !cached.isPending() ? cached.read() : undefined;
1649
1647
  }
1650
1648
  /**
@@ -2208,7 +2206,7 @@ class MedplumClient extends EventTarget {
2208
2206
  });
2209
2207
  }
2210
2208
  }
2211
- _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_schema = new WeakMap(), _MedplumClient_requestCache = 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) {
2209
+ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_schema = 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) {
2212
2210
  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); });
2213
2211
  logins.push(newLogin);
2214
2212
  __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('logins', logins);
@@ -2227,6 +2225,19 @@ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(),
2227
2225
  }), "f");
2228
2226
  return __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
2229
2227
  });
2228
+ }, _MedplumClient_getCacheEntry = function _MedplumClient_getCacheEntry(key, options) {
2229
+ 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') {
2230
+ return undefined;
2231
+ }
2232
+ const entry = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(key);
2233
+ if (!entry || entry.requestTime + __classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") < Date.now()) {
2234
+ return undefined;
2235
+ }
2236
+ return entry;
2237
+ }, _MedplumClient_setCacheEntry = function _MedplumClient_setCacheEntry(key, value) {
2238
+ if (__classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") > 0) {
2239
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(key, { requestTime: Date.now(), value });
2240
+ }
2230
2241
  }, _MedplumClient_request = function _MedplumClient_request(method, url, options = {}) {
2231
2242
  return __awaiter(this, void 0, void 0, function* () {
2232
2243
  if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {