@hipnation-truth/sdk 0.25.3 → 0.26.1

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
@@ -134,6 +134,31 @@ var AppointmentResource = class {
134
134
  }
135
135
  };
136
136
 
137
+ // src/resources/rest-auth.ts
138
+ function buildAuthHeaders(auth, options) {
139
+ return __async(this, null, function* () {
140
+ const headers = {
141
+ "X-API-Key": auth.apiKey
142
+ };
143
+ if ((options == null ? void 0 : options.accept) !== false) {
144
+ headers.Accept = "application/json";
145
+ }
146
+ if (options == null ? void 0 : options.json) {
147
+ headers["Content-Type"] = "application/json";
148
+ }
149
+ if (auth.getAuthToken) {
150
+ try {
151
+ const token = yield auth.getAuthToken();
152
+ if (token) {
153
+ headers.Authorization = `Bearer ${token}`;
154
+ }
155
+ } catch (e) {
156
+ }
157
+ }
158
+ return headers;
159
+ });
160
+ }
161
+
137
162
  // src/resources/attachments.ts
138
163
  var AttachmentsError = class extends Error {
139
164
  constructor(operation, status, message) {
@@ -143,20 +168,17 @@ var AttachmentsError = class extends Error {
143
168
  }
144
169
  };
145
170
  var AttachmentsResource = class {
146
- constructor(apiBaseUrl, apiKey, convexClient) {
171
+ constructor(apiBaseUrl, apiKey, convexClient, getAuthToken) {
147
172
  this.baseUrl = apiBaseUrl;
148
173
  this.apiKey = apiKey;
174
+ this.auth = { apiKey, getAuthToken };
149
175
  this.convex = convexClient;
150
176
  }
151
177
  post(path, body) {
152
178
  return __async(this, null, function* () {
153
179
  const res = yield fetch(`${this.baseUrl}/api${path}`, {
154
180
  method: "POST",
155
- headers: {
156
- "Content-Type": "application/json",
157
- Accept: "application/json",
158
- "X-API-Key": this.apiKey
159
- },
181
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
160
182
  body: JSON.stringify(body)
161
183
  });
162
184
  if (!res.ok) {
@@ -399,9 +421,10 @@ var ConversationMessagesSubresource = class {
399
421
  }
400
422
  };
401
423
  var _ConversationsResource = class _ConversationsResource {
402
- constructor(apiBaseUrl, apiKey, convex) {
424
+ constructor(apiBaseUrl, apiKey, convex, getAuthToken) {
403
425
  this.baseUrl = apiBaseUrl;
404
426
  this.apiKey = apiKey;
427
+ this.auth = { apiKey, getAuthToken };
405
428
  this.convex = convex != null ? convex : null;
406
429
  const post = (path, body) => this.postRequest(path, body);
407
430
  const patch = (path, body) => this.patchRequest(path, body);
@@ -484,11 +507,7 @@ var _ConversationsResource = class _ConversationsResource {
484
507
  try {
485
508
  res = yield fetch(`${this.baseUrl}/api${path}`, {
486
509
  method: "POST",
487
- headers: {
488
- "Content-Type": "application/json",
489
- Accept: "application/json",
490
- "X-API-Key": this.apiKey
491
- },
510
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
492
511
  body: JSON.stringify(body),
493
512
  signal: controller.signal
494
513
  });
@@ -530,11 +549,7 @@ var _ConversationsResource = class _ConversationsResource {
530
549
  try {
531
550
  res = yield fetch(`${this.baseUrl}/api${path}`, {
532
551
  method: "PATCH",
533
- headers: {
534
- "Content-Type": "application/json",
535
- Accept: "application/json",
536
- "X-API-Key": this.apiKey
537
- },
552
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
538
553
  body: JSON.stringify(body),
539
554
  signal: controller.signal
540
555
  });
@@ -563,9 +578,10 @@ var ConversationsResource = _ConversationsResource;
563
578
 
564
579
  // src/resources/dialpad.ts
565
580
  var DialpadResource = class {
566
- constructor(apiBaseUrl, apiKey) {
581
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
567
582
  this.baseUrl = apiBaseUrl;
568
583
  this.apiKey = apiKey;
584
+ this.auth = { apiKey, getAuthToken };
569
585
  }
570
586
  /**
571
587
  * Send an SMS or MMS message via Dialpad.
@@ -706,10 +722,7 @@ var DialpadResource = class {
706
722
  const url = `${this.baseUrl}/api/messages/dialpad/voicemail/authenticate`;
707
723
  const response = yield fetch(url, {
708
724
  method: "POST",
709
- headers: {
710
- "Content-Type": "application/json",
711
- "X-API-Key": this.apiKey
712
- },
725
+ headers: yield buildAuthHeaders(this.auth, { json: true, accept: false }),
713
726
  body: JSON.stringify({ voicemail_link: voicemailLink })
714
727
  });
715
728
  const result = yield response.json();
@@ -734,10 +747,7 @@ var DialpadResource = class {
734
747
  }
735
748
  const response = yield fetch(url.toString(), {
736
749
  method: "GET",
737
- headers: {
738
- Accept: "application/json",
739
- "X-API-Key": this.apiKey
740
- }
750
+ headers: yield buildAuthHeaders(this.auth)
741
751
  });
742
752
  if (!response.ok) {
743
753
  throw new DialpadProxyError("GET", path, response.status);
@@ -750,11 +760,7 @@ var DialpadResource = class {
750
760
  const url = `${this.baseUrl}/api/messages/dialpad${path}`;
751
761
  const response = yield fetch(url, {
752
762
  method: "POST",
753
- headers: {
754
- "Content-Type": "application/json",
755
- Accept: "application/json",
756
- "X-API-Key": this.apiKey
757
- },
763
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
758
764
  body: body !== void 0 ? JSON.stringify(body) : void 0
759
765
  });
760
766
  if (!response.ok) {
@@ -769,11 +775,7 @@ var DialpadResource = class {
769
775
  const url = `${this.baseUrl}/api/messages/dialpad${path}`;
770
776
  const response = yield fetch(url, {
771
777
  method: "PUT",
772
- headers: {
773
- "Content-Type": "application/json",
774
- Accept: "application/json",
775
- "X-API-Key": this.apiKey
776
- },
778
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
777
779
  body: body !== void 0 ? JSON.stringify(body) : void 0
778
780
  });
779
781
  if (!response.ok) {
@@ -787,10 +789,11 @@ var DialpadResource = class {
787
789
  }
788
790
  };
789
791
  var MessagesResource = class {
790
- constructor(apiBaseUrl, apiKey) {
791
- this.dialpad = new DialpadResource(apiBaseUrl, apiKey);
792
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
793
+ this.dialpad = new DialpadResource(apiBaseUrl, apiKey, getAuthToken);
792
794
  this.baseUrl = apiBaseUrl;
793
795
  this.apiKey = apiKey;
796
+ this.auth = { apiKey, getAuthToken };
794
797
  }
795
798
  /**
796
799
  * Get an authenticated URL for a Dialpad voicemail recording.
@@ -809,11 +812,7 @@ var MessagesResource = class {
809
812
  return __async(this, null, function* () {
810
813
  const res = yield fetch(`${this.baseUrl}/api/conversations/voicemail/url`, {
811
814
  method: "POST",
812
- headers: {
813
- "Content-Type": "application/json",
814
- Accept: "application/json",
815
- "X-API-Key": this.apiKey
816
- },
815
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
817
816
  body: JSON.stringify({ voicemailLink })
818
817
  });
819
818
  if (!res.ok) {
@@ -837,11 +836,7 @@ var MessagesResource = class {
837
836
  return __async(this, null, function* () {
838
837
  const res = yield fetch(`${this.baseUrl}/api/conversations/calls/end`, {
839
838
  method: "POST",
840
- headers: {
841
- "Content-Type": "application/json",
842
- Accept: "application/json",
843
- "X-API-Key": this.apiKey
844
- },
839
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
845
840
  body: JSON.stringify({ callId })
846
841
  });
847
842
  if (!res.ok) {
@@ -868,20 +863,10 @@ var DialpadProxyError = class extends Error {
868
863
 
869
864
  // src/resources/ehr.ts
870
865
  var EhrProviderProxy = class {
871
- constructor(apiBaseUrl, provider, apiKey) {
866
+ constructor(apiBaseUrl, provider, apiKey, getAuthToken) {
872
867
  this.baseUrl = apiBaseUrl;
873
868
  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;
869
+ this.auth = { apiKey, getAuthToken };
885
870
  }
886
871
  request(method, path, body, params) {
887
872
  return __async(this, null, function* () {
@@ -895,7 +880,7 @@ var EhrProviderProxy = class {
895
880
  }
896
881
  const response = yield fetch(url.toString(), {
897
882
  method,
898
- headers: this.headers(body !== void 0),
883
+ headers: yield buildAuthHeaders(this.auth, { json: body !== void 0 }),
899
884
  body: body !== void 0 ? JSON.stringify(body) : void 0
900
885
  });
901
886
  if (!response.ok) {
@@ -940,9 +925,14 @@ var EhrProviderProxy = class {
940
925
  }
941
926
  };
942
927
  var EhrResource = class {
943
- constructor(apiBaseUrl, apiKey) {
944
- this.elation = new EhrProviderProxy(apiBaseUrl, "elation", apiKey);
945
- this.hint = new EhrProviderProxy(apiBaseUrl, "hint", apiKey);
928
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
929
+ this.elation = new EhrProviderProxy(
930
+ apiBaseUrl,
931
+ "elation",
932
+ apiKey,
933
+ getAuthToken
934
+ );
935
+ this.hint = new EhrProviderProxy(apiBaseUrl, "hint", apiKey, getAuthToken);
946
936
  }
947
937
  };
948
938
  var EhrProxyError = class extends Error {
@@ -967,9 +957,10 @@ var NotesError = class extends Error {
967
957
  }
968
958
  };
969
959
  var _NotesResource = class _NotesResource {
970
- constructor(apiBaseUrl, apiKey) {
960
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
971
961
  this.baseUrl = apiBaseUrl;
972
962
  this.apiKey = apiKey;
963
+ this.auth = { apiKey, getAuthToken };
973
964
  }
974
965
  post(path, body) {
975
966
  return __async(this, null, function* () {
@@ -982,11 +973,7 @@ var _NotesResource = class _NotesResource {
982
973
  try {
983
974
  res = yield fetch(`${this.baseUrl}/api${path}`, {
984
975
  method: "POST",
985
- headers: {
986
- "Content-Type": "application/json",
987
- Accept: "application/json",
988
- "X-API-Key": this.apiKey
989
- },
976
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
990
977
  body: JSON.stringify(body),
991
978
  signal: controller.signal
992
979
  });
@@ -1047,19 +1034,16 @@ var NotificationsError = class extends Error {
1047
1034
  }
1048
1035
  };
1049
1036
  var NotificationsResource = class {
1050
- constructor(apiBaseUrl, apiKey) {
1037
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
1051
1038
  this.baseUrl = apiBaseUrl;
1052
1039
  this.apiKey = apiKey;
1040
+ this.auth = { apiKey, getAuthToken };
1053
1041
  }
1054
1042
  post(path, body) {
1055
1043
  return __async(this, null, function* () {
1056
1044
  const res = yield fetch(`${this.baseUrl}/api${path}`, {
1057
1045
  method: "POST",
1058
- headers: {
1059
- "Content-Type": "application/json",
1060
- Accept: "application/json",
1061
- "X-API-Key": this.apiKey
1062
- },
1046
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
1063
1047
  body: JSON.stringify(body)
1064
1048
  });
1065
1049
  if (!res.ok) {
@@ -1077,10 +1061,7 @@ var NotificationsResource = class {
1077
1061
  }
1078
1062
  const res = yield fetch(url.toString(), {
1079
1063
  method: "GET",
1080
- headers: {
1081
- Accept: "application/json",
1082
- "X-API-Key": this.apiKey
1083
- }
1064
+ headers: yield buildAuthHeaders(this.auth)
1084
1065
  });
1085
1066
  if (!res.ok) {
1086
1067
  const text = yield res.text().catch(() => "");
@@ -1093,10 +1074,7 @@ var NotificationsResource = class {
1093
1074
  return __async(this, null, function* () {
1094
1075
  const res = yield fetch(`${this.baseUrl}/api${path}`, {
1095
1076
  method: "DELETE",
1096
- headers: {
1097
- Accept: "application/json",
1098
- "X-API-Key": this.apiKey
1099
- }
1077
+ headers: yield buildAuthHeaders(this.auth)
1100
1078
  });
1101
1079
  if (!res.ok) {
1102
1080
  const text = yield res.text().catch(() => "");
@@ -1229,19 +1207,16 @@ var PatientDetailsError = class extends Error {
1229
1207
  }
1230
1208
  };
1231
1209
  var PatientDetailsResource = class {
1232
- constructor(apiBaseUrl, apiKey) {
1210
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
1233
1211
  this.baseUrl = apiBaseUrl;
1234
1212
  this.apiKey = apiKey;
1213
+ this.auth = { apiKey, getAuthToken };
1235
1214
  }
1236
1215
  post(path, body) {
1237
1216
  return __async(this, null, function* () {
1238
1217
  const res = yield fetch(`${this.baseUrl}/api${path}`, {
1239
1218
  method: "POST",
1240
- headers: {
1241
- "Content-Type": "application/json",
1242
- Accept: "application/json",
1243
- "X-API-Key": this.apiKey
1244
- },
1219
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
1245
1220
  body: JSON.stringify(body)
1246
1221
  });
1247
1222
  if (!res.ok) {
@@ -1563,20 +1538,17 @@ var TranslationError = class extends Error {
1563
1538
  }
1564
1539
  };
1565
1540
  var TranslationResource = class {
1566
- constructor(apiBaseUrl, apiKey) {
1541
+ constructor(apiBaseUrl, apiKey, getAuthToken) {
1567
1542
  this.baseUrl = apiBaseUrl;
1568
1543
  this.apiKey = apiKey;
1544
+ this.auth = { apiKey, getAuthToken };
1569
1545
  }
1570
1546
  post(path, body) {
1571
1547
  return __async(this, null, function* () {
1572
1548
  const url = `${this.baseUrl}/api${path}`;
1573
1549
  const res = yield fetch(url, {
1574
1550
  method: "POST",
1575
- headers: {
1576
- "Content-Type": "application/json",
1577
- Accept: "application/json",
1578
- "X-API-Key": this.apiKey
1579
- },
1551
+ headers: yield buildAuthHeaders(this.auth, { json: true }),
1580
1552
  body: JSON.stringify(body)
1581
1553
  });
1582
1554
  if (!res.ok) {
@@ -1907,24 +1879,42 @@ var TruthClient = class {
1907
1879
  const apiUrl = this.tracker.apiUrl;
1908
1880
  this.patients = new PatientResource(this.convex);
1909
1881
  this.appointments = new AppointmentResource(this.convex);
1910
- this.ehr = new EhrResource(apiUrl, config.apiKey);
1911
- this.messages = new MessagesResource(apiUrl, config.apiKey);
1882
+ this.ehr = new EhrResource(apiUrl, config.apiKey, config.getAuthToken);
1883
+ this.messages = new MessagesResource(
1884
+ apiUrl,
1885
+ config.apiKey,
1886
+ config.getAuthToken
1887
+ );
1912
1888
  this.reminders = new RemindersResource(this.convex);
1913
- this.translation = new TranslationResource(apiUrl, config.apiKey);
1889
+ this.translation = new TranslationResource(
1890
+ apiUrl,
1891
+ config.apiKey,
1892
+ config.getAuthToken
1893
+ );
1914
1894
  this.tasks = new TasksResource(this.convex);
1915
- this.patientDetails = new PatientDetailsResource(apiUrl, config.apiKey);
1895
+ this.patientDetails = new PatientDetailsResource(
1896
+ apiUrl,
1897
+ config.apiKey,
1898
+ config.getAuthToken
1899
+ );
1916
1900
  this.attachments = new AttachmentsResource(
1917
1901
  apiUrl,
1918
1902
  config.apiKey,
1919
- this.convex
1903
+ this.convex,
1904
+ config.getAuthToken
1920
1905
  );
1921
- this.notes = new NotesResource(apiUrl, config.apiKey);
1906
+ this.notes = new NotesResource(apiUrl, config.apiKey, config.getAuthToken);
1922
1907
  this.physicians = new PhysiciansResource(this.convex);
1923
- this.notifications = new NotificationsResource(apiUrl, config.apiKey);
1908
+ this.notifications = new NotificationsResource(
1909
+ apiUrl,
1910
+ config.apiKey,
1911
+ config.getAuthToken
1912
+ );
1924
1913
  this.conversations = new ConversationsResource(
1925
1914
  apiUrl,
1926
1915
  config.apiKey,
1927
- this.convex
1916
+ this.convex,
1917
+ config.getAuthToken
1928
1918
  );
1929
1919
  this.userSettings = new UserSettingsResource(this.convex);
1930
1920
  this._serviceWorkerPath = (_h = config.serviceWorkerPath) != null ? _h : "/truth-sw.js";