@hipnation-truth/sdk 0.26.11 → 0.26.13

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/react.d.ts CHANGED
@@ -1520,7 +1520,9 @@ declare class ConversationTasksSubresource {
1520
1520
  }
1521
1521
  declare class ConversationMessagesSubresource {
1522
1522
  private readonly post;
1523
- constructor(post: <T>(path: string, body: unknown) => Promise<T>);
1523
+ constructor(post: <T>(path: string, body: unknown, options?: {
1524
+ requireUserToken?: boolean;
1525
+ }) => Promise<T>);
1524
1526
  /**
1525
1527
  * Send an outbound SMS / MMS via the typed `sendMessage` oRPC procedure.
1526
1528
  *
package/dist/react.js CHANGED
@@ -234,6 +234,21 @@ var AppointmentResource = class {
234
234
  };
235
235
 
236
236
  // src/resources/rest-auth.ts
237
+ var REQUIRE_TOKEN_MAX_ATTEMPTS = 4;
238
+ var REQUIRE_TOKEN_RETRY_DELAY_MS = 150;
239
+ function delay(ms) {
240
+ return new Promise((resolve) => setTimeout(resolve, ms));
241
+ }
242
+ function resolveAuthToken(getAuthToken) {
243
+ return __async(this, null, function* () {
244
+ try {
245
+ const token = yield getAuthToken();
246
+ return token != null ? token : null;
247
+ } catch (e) {
248
+ return null;
249
+ }
250
+ });
251
+ }
237
252
  function buildAuthHeaders(auth, options) {
238
253
  return __async(this, null, function* () {
239
254
  const headers = {
@@ -245,13 +260,30 @@ function buildAuthHeaders(auth, options) {
245
260
  if (options == null ? void 0 : options.json) {
246
261
  headers["Content-Type"] = "application/json";
247
262
  }
248
- if (auth.getAuthToken) {
249
- try {
250
- const token = yield auth.getAuthToken();
263
+ if (options == null ? void 0 : options.requireUserToken) {
264
+ if (!auth.getAuthToken) {
265
+ throw new Error(
266
+ "User authentication required to send a message, but no auth token provider is configured"
267
+ );
268
+ }
269
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
270
+ const token = yield resolveAuthToken(auth.getAuthToken);
251
271
  if (token) {
252
272
  headers.Authorization = `Bearer ${token}`;
273
+ return headers;
253
274
  }
254
- } catch (e) {
275
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
276
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
277
+ }
278
+ }
279
+ throw new Error(
280
+ "User authentication required to send a message \u2014 no auth token available"
281
+ );
282
+ }
283
+ if (auth.getAuthToken) {
284
+ const token = yield resolveAuthToken(auth.getAuthToken);
285
+ if (token) {
286
+ headers.Authorization = `Bearer ${token}`;
255
287
  }
256
288
  }
257
289
  return headers;
@@ -514,7 +546,8 @@ var ConversationMessagesSubresource = class {
514
546
  }
515
547
  return this.post(
516
548
  "/conversations/messages",
517
- input
549
+ input,
550
+ { requireUserToken: true }
518
551
  );
519
552
  });
520
553
  }
@@ -525,7 +558,7 @@ var _ConversationsResource = class _ConversationsResource {
525
558
  this.apiKey = apiKey;
526
559
  this.auth = { apiKey, getAuthToken };
527
560
  this.convex = convex != null ? convex : null;
528
- const post = (path, body) => this.postRequest(path, body);
561
+ const post = (path, body, options) => this.postRequest(path, body, options);
529
562
  const patch = (path, body) => this.patchRequest(path, body);
530
563
  this.notes = new ConversationNotesSubresource(post);
531
564
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -588,7 +621,7 @@ var _ConversationsResource = class _ConversationsResource {
588
621
  );
589
622
  });
590
623
  }
591
- postRequest(path, body) {
624
+ postRequest(path, body, options) {
592
625
  return __async(this, null, function* () {
593
626
  if (!this.apiKey) {
594
627
  throw new ConversationsError(
@@ -597,6 +630,19 @@ var _ConversationsResource = class _ConversationsResource {
597
630
  "Truth API key not configured \u2014 request blocked"
598
631
  );
599
632
  }
633
+ let headers;
634
+ try {
635
+ headers = yield buildAuthHeaders(this.auth, {
636
+ json: true,
637
+ requireUserToken: options == null ? void 0 : options.requireUserToken
638
+ });
639
+ } catch (err) {
640
+ throw new ConversationsError(
641
+ path,
642
+ 0,
643
+ err instanceof Error ? err.message : "Failed to build auth headers"
644
+ );
645
+ }
600
646
  const controller = new AbortController();
601
647
  const timeout = setTimeout(
602
648
  () => controller.abort(),
@@ -606,7 +652,7 @@ var _ConversationsResource = class _ConversationsResource {
606
652
  try {
607
653
  res = yield fetch(`${this.baseUrl}/api${path}`, {
608
654
  method: "POST",
609
- headers: yield buildAuthHeaders(this.auth, { json: true }),
655
+ headers,
610
656
  body: JSON.stringify(body),
611
657
  signal: controller.signal
612
658
  });
@@ -1852,9 +1898,9 @@ var Tracker = class {
1852
1898
  lastError = error;
1853
1899
  }
1854
1900
  if (attempt < MAX_RETRIES) {
1855
- const delay = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1856
- const jitter = Math.random() * delay * 0.5;
1857
- yield sleep(delay + jitter);
1901
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1902
+ const jitter = Math.random() * delay2 * 0.5;
1903
+ yield sleep(delay2 + jitter);
1858
1904
  }
1859
1905
  }
1860
1906
  throw lastError;
@@ -2754,6 +2800,17 @@ function useConversationTasksByPhonePair(phonePair) {
2754
2800
 
2755
2801
  // src/react/hooks.ts
2756
2802
  var import_react5 = require("react");
2803
+
2804
+ // src/react/elation-id.ts
2805
+ function toFiniteElationId(value) {
2806
+ if (value === void 0 || value === null || value === "") {
2807
+ return void 0;
2808
+ }
2809
+ const n = typeof value === "number" ? value : Number(value);
2810
+ return Number.isFinite(n) ? n : void 0;
2811
+ }
2812
+
2813
+ // src/react/hooks.ts
2757
2814
  var import_server5 = require("convex/server");
2758
2815
  var patientsListRef = (0, import_server5.makeFunctionReference)("patients:list");
2759
2816
  var patientsGetRef = (0, import_server5.makeFunctionReference)("patients:get");
@@ -2837,24 +2894,25 @@ function usePatientMedical(elationId, options) {
2837
2894
  const apiBaseUrl = (_a = options == null ? void 0 : options.apiBaseUrl) != null ? _a : sdkContext == null ? void 0 : sdkContext.apiBaseUrl;
2838
2895
  const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : sdkContext == null ? void 0 : sdkContext.apiKey;
2839
2896
  const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
2897
+ const id = toFiniteElationId(elationId);
2840
2898
  const medications = usePersistentQuery(
2841
2899
  medicationsByPatientRef,
2842
- elationId !== void 0 ? { elationPatientId: elationId } : "skip"
2900
+ id !== void 0 ? { elationPatientId: id } : "skip"
2843
2901
  );
2844
2902
  const problems = usePersistentQuery(
2845
2903
  problemsByPatientRef,
2846
- elationId !== void 0 ? { elationPatientId: elationId } : "skip"
2904
+ id !== void 0 ? { elationPatientId: id } : "skip"
2847
2905
  );
2848
2906
  const allergies = usePersistentQuery(
2849
2907
  allergiesByPatientRef,
2850
- elationId !== void 0 ? { elationPatientId: elationId } : "skip"
2908
+ id !== void 0 ? { elationPatientId: id } : "skip"
2851
2909
  );
2852
2910
  const appointments = usePersistentQuery(
2853
2911
  appointmentsByPatientRef,
2854
- elationId !== void 0 ? { elationPatientId: elationId } : "skip"
2912
+ id !== void 0 ? { elationPatientId: id } : "skip"
2855
2913
  );
2856
2914
  (0, import_react5.useEffect)(() => {
2857
- if (elationId === void 0 || (options == null ? void 0 : options.skipRefresh)) {
2915
+ if (id === void 0 || (options == null ? void 0 : options.skipRefresh)) {
2858
2916
  return;
2859
2917
  }
2860
2918
  if (!apiBaseUrl || !apiKey) {
@@ -2865,12 +2923,12 @@ function usePatientMedical(elationId, options) {
2865
2923
  `${apiBaseUrl}/api/patients/medical/refresh`,
2866
2924
  apiKey,
2867
2925
  getAuthToken,
2868
- { elationId },
2926
+ { elationId: id },
2869
2927
  controller.signal
2870
2928
  ).catch(() => {
2871
2929
  });
2872
2930
  return () => controller.abort();
2873
- }, [elationId, apiBaseUrl, apiKey, getAuthToken, options == null ? void 0 : options.skipRefresh]);
2931
+ }, [id, apiBaseUrl, apiKey, getAuthToken, options == null ? void 0 : options.skipRefresh]);
2874
2932
  return { medications, problems, allergies, appointments };
2875
2933
  }
2876
2934
  var elationPatientByIdRef = (0, import_server5.makeFunctionReference)("elationPatients:getByElationId");
@@ -2883,9 +2941,10 @@ function usePatientBasic(input, options) {
2883
2941
  const apiBaseUrl = (_a = options == null ? void 0 : options.apiBaseUrl) != null ? _a : sdkContext == null ? void 0 : sdkContext.apiBaseUrl;
2884
2942
  const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : sdkContext == null ? void 0 : sdkContext.apiKey;
2885
2943
  const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
2944
+ const elationId = toFiniteElationId(input.elationId);
2886
2945
  const elationRow = usePersistentQuery(
2887
2946
  elationPatientByIdRef,
2888
- input.elationId !== void 0 ? { elationId: input.elationId } : "skip"
2947
+ elationId !== void 0 ? { elationId } : "skip"
2889
2948
  );
2890
2949
  const hintRow = usePersistentQuery(
2891
2950
  hintPatientByIdRef,
@@ -2895,7 +2954,7 @@ function usePatientBasic(input, options) {
2895
2954
  if (options == null ? void 0 : options.skipRefresh) {
2896
2955
  return;
2897
2956
  }
2898
- if (!input.hintId && input.elationId === void 0) {
2957
+ if (!input.hintId && elationId === void 0) {
2899
2958
  return;
2900
2959
  }
2901
2960
  if (!apiBaseUrl || !apiKey) {
@@ -2906,14 +2965,14 @@ function usePatientBasic(input, options) {
2906
2965
  `${apiBaseUrl}/api/patients/basic/refresh`,
2907
2966
  apiKey,
2908
2967
  getAuthToken,
2909
- { hintId: input.hintId, elationId: input.elationId },
2968
+ { hintId: input.hintId, elationId },
2910
2969
  controller.signal
2911
2970
  ).catch(() => {
2912
2971
  });
2913
2972
  return () => controller.abort();
2914
2973
  }, [
2915
2974
  input.hintId,
2916
- input.elationId,
2975
+ elationId,
2917
2976
  apiBaseUrl,
2918
2977
  apiKey,
2919
2978
  getAuthToken,
@@ -2921,7 +2980,7 @@ function usePatientBasic(input, options) {
2921
2980
  ]);
2922
2981
  const elationPatient = elationRow === void 0 ? void 0 : elationRow === null ? null : (_c = elationRow.raw) != null ? _c : null;
2923
2982
  const hintPatient = hintRow === void 0 ? void 0 : hintRow === null ? null : (_d = hintRow.raw) != null ? _d : null;
2924
- const elationLoading = input.elationId !== void 0 && elationRow === void 0;
2983
+ const elationLoading = elationId !== void 0 && elationRow === void 0;
2925
2984
  const hintLoading = input.hintId !== void 0 && hintRow === void 0;
2926
2985
  return {
2927
2986
  elationPatient,
@@ -2943,15 +3002,16 @@ function usePatientPhoto(elationId, options) {
2943
3002
  const apiBaseUrl = (_a = options == null ? void 0 : options.apiBaseUrl) != null ? _a : sdkContext == null ? void 0 : sdkContext.apiBaseUrl;
2944
3003
  const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : sdkContext == null ? void 0 : sdkContext.apiKey;
2945
3004
  const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
3005
+ const id = toFiniteElationId(elationId);
2946
3006
  const photo = usePersistentQuery(
2947
3007
  patientPhotoByIdRef,
2948
- elationId !== void 0 ? { elationPatientId: elationId } : "skip"
3008
+ id !== void 0 ? { elationPatientId: id } : "skip"
2949
3009
  );
2950
3010
  (0, import_react5.useEffect)(() => {
2951
3011
  if (options == null ? void 0 : options.skipRefresh) {
2952
3012
  return;
2953
3013
  }
2954
- if (elationId === void 0) {
3014
+ if (id === void 0) {
2955
3015
  return;
2956
3016
  }
2957
3017
  if (!apiBaseUrl || !apiKey) {
@@ -2962,12 +3022,12 @@ function usePatientPhoto(elationId, options) {
2962
3022
  `${apiBaseUrl}/api/patients/photo/refresh`,
2963
3023
  apiKey,
2964
3024
  getAuthToken,
2965
- { elationId },
3025
+ { elationId: id },
2966
3026
  controller.signal
2967
3027
  ).catch(() => {
2968
3028
  });
2969
3029
  return () => controller.abort();
2970
- }, [elationId, apiBaseUrl, apiKey, getAuthToken, options == null ? void 0 : options.skipRefresh]);
3030
+ }, [id, apiBaseUrl, apiKey, getAuthToken, options == null ? void 0 : options.skipRefresh]);
2971
3031
  return photo;
2972
3032
  }
2973
3033
  var messagesByPhonesRef = (0, import_server5.makeFunctionReference)("conversationMessages:getByPhones");