@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/cjs/index.js CHANGED
@@ -1184,10 +1184,11 @@
1184
1184
 
1185
1185
  // PKCE auth ased on:
1186
1186
  // https://aws.amazon.com/blogs/security/how-to-add-authentication-single-page-web-application-with-amazon-cognito-oauth2-implementation/
1187
- 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;
1187
+ 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;
1188
1188
  const DEFAULT_BASE_URL = 'https://api.medplum.com/';
1189
1189
  const DEFAULT_SCOPE = 'launch/patient openid fhirUser offline_access user/*.*';
1190
1190
  const DEFAULT_RESOURCE_CACHE_SIZE = 1000;
1191
+ const DEFAULT_CACHE_TIME = 10000; // 10 seconds
1191
1192
  const JSON_CONTENT_TYPE = 'application/json';
1192
1193
  const FHIR_CONTENT_TYPE = 'application/fhir+json';
1193
1194
  const PATCH_CONTENT_TYPE = 'application/json-patch+json';
@@ -1240,7 +1241,7 @@
1240
1241
  */
1241
1242
  class MedplumClient extends EventTarget {
1242
1243
  constructor(options) {
1243
- var _a;
1244
+ var _a, _b;
1244
1245
  super();
1245
1246
  _MedplumClient_instances.add(this);
1246
1247
  _MedplumClient_fetch.set(this, void 0);
@@ -1248,6 +1249,7 @@
1248
1249
  _MedplumClient_storage.set(this, void 0);
1249
1250
  _MedplumClient_schema.set(this, void 0);
1250
1251
  _MedplumClient_requestCache.set(this, void 0);
1252
+ _MedplumClient_cacheTime.set(this, void 0);
1251
1253
  _MedplumClient_baseUrl.set(this, void 0);
1252
1254
  _MedplumClient_clientId.set(this, void 0);
1253
1255
  _MedplumClient_authorizeUrl.set(this, void 0);
@@ -1273,6 +1275,7 @@
1273
1275
  __classPrivateFieldSet(this, _MedplumClient_storage, new ClientStorage(), "f");
1274
1276
  __classPrivateFieldSet(this, _MedplumClient_schema, createSchema(), "f");
1275
1277
  __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");
1278
+ __classPrivateFieldSet(this, _MedplumClient_cacheTime, (_b = options === null || options === void 0 ? void 0 : options.cacheTime) !== null && _b !== void 0 ? _b : DEFAULT_CACHE_TIME, "f");
1276
1279
  __classPrivateFieldSet(this, _MedplumClient_baseUrl, (options === null || options === void 0 ? void 0 : options.baseUrl) || DEFAULT_BASE_URL, "f");
1277
1280
  __classPrivateFieldSet(this, _MedplumClient_clientId, (options === null || options === void 0 ? void 0 : options.clientId) || '', "f");
1278
1281
  __classPrivateFieldSet(this, _MedplumClient_authorizeUrl, (options === null || options === void 0 ? void 0 : options.authorizeUrl) || __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'oauth2/authorize', "f");
@@ -1341,14 +1344,12 @@
1341
1344
  */
1342
1345
  get(url, options = {}) {
1343
1346
  url = url.toString();
1344
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1345
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(url);
1346
- if (cached) {
1347
- return cached;
1348
- }
1347
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, url, options);
1348
+ if (cached) {
1349
+ return cached.value;
1349
1350
  }
1350
1351
  const promise = new ReadablePromise(__classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'GET', url, options));
1351
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(url, promise);
1352
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, url, promise);
1352
1353
  return promise;
1353
1354
  }
1354
1355
  /**
@@ -1588,14 +1589,12 @@
1588
1589
  url.searchParams.set('_count', '1');
1589
1590
  url.searchParams.sort();
1590
1591
  const cacheKey = url.toString() + '-searchOne';
1591
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1592
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1593
- if (cached) {
1594
- return cached;
1595
- }
1592
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, cacheKey, options);
1593
+ if (cached) {
1594
+ return cached.value;
1596
1595
  }
1597
1596
  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; }));
1598
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1597
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
1599
1598
  return promise;
1600
1599
  }
1601
1600
  /**
@@ -1620,14 +1619,12 @@
1620
1619
  searchResources(resourceType, query, options = {}) {
1621
1620
  const url = this.fhirSearchUrl(resourceType, query);
1622
1621
  const cacheKey = url.toString() + '-searchResources';
1623
- if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1624
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1625
- if (cached) {
1626
- return cached;
1627
- }
1622
+ const cached = __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_getCacheEntry).call(this, cacheKey, options);
1623
+ if (cached) {
1624
+ return cached.value;
1628
1625
  }
1629
1626
  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 : []; }));
1630
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1627
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setCacheEntry).call(this, cacheKey, promise);
1631
1628
  return promise;
1632
1629
  }
1633
1630
  /**
@@ -1650,7 +1647,8 @@
1650
1647
  * @returns The resource if it is available in the cache; undefined otherwise.
1651
1648
  */
1652
1649
  getCached(resourceType, id) {
1653
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString());
1650
+ var _a;
1651
+ const cached = (_a = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString())) === null || _a === void 0 ? void 0 : _a.value;
1654
1652
  return cached && !cached.isPending() ? cached.read() : undefined;
1655
1653
  }
1656
1654
  /**
@@ -2214,7 +2212,7 @@
2214
2212
  });
2215
2213
  }
2216
2214
  }
2217
- _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) {
2215
+ _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) {
2218
2216
  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); });
2219
2217
  logins.push(newLogin);
2220
2218
  __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('logins', logins);
@@ -2233,6 +2231,19 @@
2233
2231
  }), "f");
2234
2232
  return __classPrivateFieldGet(this, _MedplumClient_profilePromise, "f");
2235
2233
  });
2234
+ }, _MedplumClient_getCacheEntry = function _MedplumClient_getCacheEntry(key, options) {
2235
+ 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') {
2236
+ return undefined;
2237
+ }
2238
+ const entry = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(key);
2239
+ if (!entry || entry.requestTime + __classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") < Date.now()) {
2240
+ return undefined;
2241
+ }
2242
+ return entry;
2243
+ }, _MedplumClient_setCacheEntry = function _MedplumClient_setCacheEntry(key, value) {
2244
+ if (__classPrivateFieldGet(this, _MedplumClient_cacheTime, "f") > 0) {
2245
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(key, { requestTime: Date.now(), value });
2246
+ }
2236
2247
  }, _MedplumClient_request = function _MedplumClient_request(method, url, options = {}) {
2237
2248
  return __awaiter(this, void 0, void 0, function* () {
2238
2249
  if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {