@hipnation-truth/sdk 0.26.12 → 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;
274
+ }
275
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
276
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
253
277
  }
254
- } catch (e) {
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;