@hipnation-truth/sdk 0.26.12 → 0.26.14

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,26 @@ 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
+ var USER_TOKEN_HEADER = "X-Truth-Authorization";
243
+ function setUserToken(headers, token) {
244
+ headers.Authorization = `Bearer ${token}`;
245
+ headers[USER_TOKEN_HEADER] = `Bearer ${token}`;
246
+ }
247
+ function resolveAuthToken(getAuthToken) {
248
+ return __async(this, null, function* () {
249
+ try {
250
+ const token = yield getAuthToken();
251
+ return token != null ? token : null;
252
+ } catch (e) {
253
+ return null;
254
+ }
255
+ });
256
+ }
237
257
  function buildAuthHeaders(auth, options) {
238
258
  return __async(this, null, function* () {
239
259
  const headers = {
@@ -245,13 +265,30 @@ function buildAuthHeaders(auth, options) {
245
265
  if (options == null ? void 0 : options.json) {
246
266
  headers["Content-Type"] = "application/json";
247
267
  }
248
- if (auth.getAuthToken) {
249
- try {
250
- const token = yield auth.getAuthToken();
268
+ if (options == null ? void 0 : options.requireUserToken) {
269
+ if (!auth.getAuthToken) {
270
+ throw new Error(
271
+ "User authentication required to send a message, but no auth token provider is configured"
272
+ );
273
+ }
274
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
275
+ const token = yield resolveAuthToken(auth.getAuthToken);
251
276
  if (token) {
252
- headers.Authorization = `Bearer ${token}`;
277
+ setUserToken(headers, token);
278
+ return headers;
253
279
  }
254
- } catch (e) {
280
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
281
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
282
+ }
283
+ }
284
+ throw new Error(
285
+ "User authentication required to send a message \u2014 no auth token available"
286
+ );
287
+ }
288
+ if (auth.getAuthToken) {
289
+ const token = yield resolveAuthToken(auth.getAuthToken);
290
+ if (token) {
291
+ setUserToken(headers, token);
255
292
  }
256
293
  }
257
294
  return headers;
@@ -514,7 +551,8 @@ var ConversationMessagesSubresource = class {
514
551
  }
515
552
  return this.post(
516
553
  "/conversations/messages",
517
- input
554
+ input,
555
+ { requireUserToken: true }
518
556
  );
519
557
  });
520
558
  }
@@ -525,7 +563,7 @@ var _ConversationsResource = class _ConversationsResource {
525
563
  this.apiKey = apiKey;
526
564
  this.auth = { apiKey, getAuthToken };
527
565
  this.convex = convex != null ? convex : null;
528
- const post = (path, body) => this.postRequest(path, body);
566
+ const post = (path, body, options) => this.postRequest(path, body, options);
529
567
  const patch = (path, body) => this.patchRequest(path, body);
530
568
  this.notes = new ConversationNotesSubresource(post);
531
569
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -588,7 +626,7 @@ var _ConversationsResource = class _ConversationsResource {
588
626
  );
589
627
  });
590
628
  }
591
- postRequest(path, body) {
629
+ postRequest(path, body, options) {
592
630
  return __async(this, null, function* () {
593
631
  if (!this.apiKey) {
594
632
  throw new ConversationsError(
@@ -597,6 +635,19 @@ var _ConversationsResource = class _ConversationsResource {
597
635
  "Truth API key not configured \u2014 request blocked"
598
636
  );
599
637
  }
638
+ let headers;
639
+ try {
640
+ headers = yield buildAuthHeaders(this.auth, {
641
+ json: true,
642
+ requireUserToken: options == null ? void 0 : options.requireUserToken
643
+ });
644
+ } catch (err) {
645
+ throw new ConversationsError(
646
+ path,
647
+ 0,
648
+ err instanceof Error ? err.message : "Failed to build auth headers"
649
+ );
650
+ }
600
651
  const controller = new AbortController();
601
652
  const timeout = setTimeout(
602
653
  () => controller.abort(),
@@ -606,7 +657,7 @@ var _ConversationsResource = class _ConversationsResource {
606
657
  try {
607
658
  res = yield fetch(`${this.baseUrl}/api${path}`, {
608
659
  method: "POST",
609
- headers: yield buildAuthHeaders(this.auth, { json: true }),
660
+ headers,
610
661
  body: JSON.stringify(body),
611
662
  signal: controller.signal
612
663
  });
@@ -1852,9 +1903,9 @@ var Tracker = class {
1852
1903
  lastError = error;
1853
1904
  }
1854
1905
  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);
1906
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1907
+ const jitter = Math.random() * delay2 * 0.5;
1908
+ yield sleep(delay2 + jitter);
1858
1909
  }
1859
1910
  }
1860
1911
  throw lastError;