@hipnation-truth/sdk 0.25.3 → 0.26.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.
package/dist/index.mjs CHANGED
@@ -287,6 +287,31 @@ var AttachmentsResource = class {
287
287
  }
288
288
  };
289
289
 
290
+ // src/resources/rest-auth.ts
291
+ function buildAuthHeaders(auth, options) {
292
+ return __async(this, null, function* () {
293
+ const headers = {
294
+ "X-API-Key": auth.apiKey
295
+ };
296
+ if ((options == null ? void 0 : options.accept) !== false) {
297
+ headers.Accept = "application/json";
298
+ }
299
+ if (options == null ? void 0 : options.json) {
300
+ headers["Content-Type"] = "application/json";
301
+ }
302
+ if (auth.getAuthToken) {
303
+ try {
304
+ const token = yield auth.getAuthToken();
305
+ if (token) {
306
+ headers.Authorization = `Bearer ${token}`;
307
+ }
308
+ } catch (e) {
309
+ }
310
+ }
311
+ return headers;
312
+ });
313
+ }
314
+
290
315
  // src/resources/conversations.ts
291
316
  var ConversationsError = class extends Error {
292
317
  constructor(operation, status, message) {
@@ -399,9 +424,10 @@ var ConversationMessagesSubresource = class {
399
424
  }
400
425
  };
401
426
  var _ConversationsResource = class _ConversationsResource {
402
- constructor(apiBaseUrl, apiKey, convex) {
427
+ constructor(apiBaseUrl, apiKey, convex, getAuthToken) {
403
428
  this.baseUrl = apiBaseUrl;
404
429
  this.apiKey = apiKey;
430
+ this.auth = { apiKey, getAuthToken };
405
431
  this.convex = convex != null ? convex : null;
406
432
  const post = (path, body) => this.postRequest(path, body);
407
433
  const patch = (path, body) => this.patchRequest(path, body);
@@ -484,11 +510,7 @@ var _ConversationsResource = class _ConversationsResource {
484
510
  try {
485
511
  res = yield fetch(`${this.baseUrl}/api${path}`, {
486
512
  method: "POST",
487
- headers: {
488
- "Content-Type": "application/json",
489
- Accept: "application/json",
490
- "X-API-Key": this.apiKey
491
- },
513
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
492
514
  body: JSON.stringify(body),
493
515
  signal: controller.signal
494
516
  });
@@ -530,11 +552,7 @@ var _ConversationsResource = class _ConversationsResource {
530
552
  try {
531
553
  res = yield fetch(`${this.baseUrl}/api${path}`, {
532
554
  method: "PATCH",
533
- headers: {
534
- "Content-Type": "application/json",
535
- Accept: "application/json",
536
- "X-API-Key": this.apiKey
537
- },
555
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
538
556
  body: JSON.stringify(body),
539
557
  signal: controller.signal
540
558
  });
@@ -868,20 +886,10 @@ var DialpadProxyError = class extends Error {
868
886
 
869
887
  // src/resources/ehr.ts
870
888
  var EhrProviderProxy = class {
871
- constructor(apiBaseUrl, provider, apiKey) {
889
+ constructor(apiBaseUrl, provider, apiKey, getAuthToken) {
872
890
  this.baseUrl = apiBaseUrl;
873
891
  this.provider = provider;
874
- this.apiKey = apiKey;
875
- }
876
- headers(withBody) {
877
- const headers = {
878
- Accept: "application/json",
879
- "X-API-Key": this.apiKey
880
- };
881
- if (withBody) {
882
- headers["Content-Type"] = "application/json";
883
- }
884
- return headers;
892
+ this.auth = { apiKey, getAuthToken };
885
893
  }
886
894
  request(method, path, body, params) {
887
895
  return __async(this, null, function* () {
@@ -895,7 +903,7 @@ var EhrProviderProxy = class {
895
903
  }
896
904
  const response = yield fetch(url.toString(), {
897
905
  method,
898
- headers: this.headers(body !== void 0),
906
+ headers: yield buildAuthHeaders(this.auth, { json: body !== void 0 }),
899
907
  body: body !== void 0 ? JSON.stringify(body) : void 0
900
908
  });
901
909
  if (!response.ok) {
@@ -940,9 +948,14 @@ var EhrProviderProxy = class {
940
948
  }
941
949
  };
942
950
  var EhrResource = class {
943
- constructor(apiBaseUrl, apiKey) {
944
- this.elation = new EhrProviderProxy(apiBaseUrl, "elation", apiKey);
945
- this.hint = new EhrProviderProxy(apiBaseUrl, "hint", apiKey);
951
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
952
+ this.elation = new EhrProviderProxy(
953
+ apiBaseUrl,
954
+ "elation",
955
+ apiKey,
956
+ getAuthToken
957
+ );
958
+ this.hint = new EhrProviderProxy(apiBaseUrl, "hint", apiKey, getAuthToken);
946
959
  }
947
960
  };
948
961
  var EhrProxyError = class extends Error {
@@ -1907,7 +1920,7 @@ var TruthClient = class {
1907
1920
  const apiUrl = this.tracker.apiUrl;
1908
1921
  this.patients = new PatientResource(this.convex);
1909
1922
  this.appointments = new AppointmentResource(this.convex);
1910
- this.ehr = new EhrResource(apiUrl, config.apiKey);
1923
+ this.ehr = new EhrResource(apiUrl, config.apiKey, config.getAuthToken);
1911
1924
  this.messages = new MessagesResource(apiUrl, config.apiKey);
1912
1925
  this.reminders = new RemindersResource(this.convex);
1913
1926
  this.translation = new TranslationResource(apiUrl, config.apiKey);
@@ -1924,7 +1937,8 @@ var TruthClient = class {
1924
1937
  this.conversations = new ConversationsResource(
1925
1938
  apiUrl,
1926
1939
  config.apiKey,
1927
- this.convex
1940
+ this.convex,
1941
+ config.getAuthToken
1928
1942
  );
1929
1943
  this.userSettings = new UserSettingsResource(this.convex);
1930
1944
  this._serviceWorkerPath = (_h = config.serviceWorkerPath) != null ? _h : "/truth-sw.js";