@hipnation-truth/sdk 0.25.2 → 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,16 +886,12 @@ var DialpadProxyError = class extends Error {
868
886
 
869
887
  // src/resources/ehr.ts
870
888
  var EhrProviderProxy = class {
871
- constructor(apiBaseUrl, provider) {
889
+ constructor(apiBaseUrl, provider, apiKey, getAuthToken) {
872
890
  this.baseUrl = apiBaseUrl;
873
891
  this.provider = provider;
892
+ this.auth = { apiKey, getAuthToken };
874
893
  }
875
- /**
876
- * GET request to the EHR proxy.
877
- * @param path — path relative to the provider root (e.g., "/patients/123/")
878
- * @param params — optional query parameters
879
- */
880
- get(path, params) {
894
+ request(method, path, body, params) {
881
895
  return __async(this, null, function* () {
882
896
  const url = new URL(`/api/ehr/${this.provider}${path}`, this.baseUrl);
883
897
  if (params) {
@@ -888,96 +902,60 @@ var EhrProviderProxy = class {
888
902
  }
889
903
  }
890
904
  const response = yield fetch(url.toString(), {
891
- method: "GET",
892
- headers: { Accept: "application/json" }
905
+ method,
906
+ headers: yield buildAuthHeaders(this.auth, { json: body !== void 0 }),
907
+ body: body !== void 0 ? JSON.stringify(body) : void 0
893
908
  });
894
909
  if (!response.ok) {
895
- throw new EhrProxyError(this.provider, "GET", path, response.status);
910
+ throw new EhrProxyError(this.provider, method, path, response.status);
896
911
  }
897
912
  return yield response.json();
898
913
  });
899
914
  }
900
915
  /**
901
- * POST request to the EHR proxy.
916
+ * GET request to the EHR proxy.
917
+ * @param path — path relative to the provider root (e.g., "/patients/123/")
918
+ * @param params — optional query parameters
902
919
  */
920
+ get(path, params) {
921
+ return __async(this, null, function* () {
922
+ return this.request("GET", path, void 0, params);
923
+ });
924
+ }
925
+ /** POST request to the EHR proxy. */
903
926
  post(path, body) {
904
927
  return __async(this, null, function* () {
905
- const url = `${this.baseUrl}/api/ehr/${this.provider}${path}`;
906
- const response = yield fetch(url, {
907
- method: "POST",
908
- headers: {
909
- "Content-Type": "application/json",
910
- Accept: "application/json"
911
- },
912
- body: body !== void 0 ? JSON.stringify(body) : void 0
913
- });
914
- if (!response.ok) {
915
- throw new EhrProxyError(this.provider, "POST", path, response.status);
916
- }
917
- return yield response.json();
928
+ return this.request("POST", path, body);
918
929
  });
919
930
  }
920
- /**
921
- * PUT request to the EHR proxy.
922
- */
931
+ /** PUT request to the EHR proxy. */
923
932
  put(path, body) {
924
933
  return __async(this, null, function* () {
925
- const url = `${this.baseUrl}/api/ehr/${this.provider}${path}`;
926
- const response = yield fetch(url, {
927
- method: "PUT",
928
- headers: {
929
- "Content-Type": "application/json",
930
- Accept: "application/json"
931
- },
932
- body: body !== void 0 ? JSON.stringify(body) : void 0
933
- });
934
- if (!response.ok) {
935
- throw new EhrProxyError(this.provider, "PUT", path, response.status);
936
- }
937
- return yield response.json();
934
+ return this.request("PUT", path, body);
938
935
  });
939
936
  }
940
- /**
941
- * PATCH request to the EHR proxy.
942
- */
937
+ /** PATCH request to the EHR proxy. */
943
938
  patch(path, body) {
944
939
  return __async(this, null, function* () {
945
- const url = `${this.baseUrl}/api/ehr/${this.provider}${path}`;
946
- const response = yield fetch(url, {
947
- method: "PATCH",
948
- headers: {
949
- "Content-Type": "application/json",
950
- Accept: "application/json"
951
- },
952
- body: body !== void 0 ? JSON.stringify(body) : void 0
953
- });
954
- if (!response.ok) {
955
- throw new EhrProxyError(this.provider, "PATCH", path, response.status);
956
- }
957
- return yield response.json();
940
+ return this.request("PATCH", path, body);
958
941
  });
959
942
  }
960
- /**
961
- * DELETE request to the EHR proxy.
962
- */
943
+ /** DELETE request to the EHR proxy. */
963
944
  delete(path) {
964
945
  return __async(this, null, function* () {
965
- const url = `${this.baseUrl}/api/ehr/${this.provider}${path}`;
966
- const response = yield fetch(url, {
967
- method: "DELETE",
968
- headers: { Accept: "application/json" }
969
- });
970
- if (!response.ok) {
971
- throw new EhrProxyError(this.provider, "DELETE", path, response.status);
972
- }
973
- return yield response.json();
946
+ return this.request("DELETE", path);
974
947
  });
975
948
  }
976
949
  };
977
950
  var EhrResource = class {
978
- constructor(apiBaseUrl) {
979
- this.elation = new EhrProviderProxy(apiBaseUrl, "elation");
980
- this.hint = new EhrProviderProxy(apiBaseUrl, "hint");
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);
981
959
  }
982
960
  };
983
961
  var EhrProxyError = class extends Error {
@@ -1942,7 +1920,7 @@ var TruthClient = class {
1942
1920
  const apiUrl = this.tracker.apiUrl;
1943
1921
  this.patients = new PatientResource(this.convex);
1944
1922
  this.appointments = new AppointmentResource(this.convex);
1945
- this.ehr = new EhrResource(apiUrl);
1923
+ this.ehr = new EhrResource(apiUrl, config.apiKey, config.getAuthToken);
1946
1924
  this.messages = new MessagesResource(apiUrl, config.apiKey);
1947
1925
  this.reminders = new RemindersResource(this.convex);
1948
1926
  this.translation = new TranslationResource(apiUrl, config.apiKey);
@@ -1959,7 +1937,8 @@ var TruthClient = class {
1959
1937
  this.conversations = new ConversationsResource(
1960
1938
  apiUrl,
1961
1939
  config.apiKey,
1962
- this.convex
1940
+ this.convex,
1941
+ config.getAuthToken
1963
1942
  );
1964
1943
  this.userSettings = new UserSettingsResource(this.convex);
1965
1944
  this._serviceWorkerPath = (_h = config.serviceWorkerPath) != null ? _h : "/truth-sw.js";