@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/index.d.mts CHANGED
@@ -360,7 +360,9 @@ declare class ConversationTasksSubresource {
360
360
  }
361
361
  declare class ConversationMessagesSubresource {
362
362
  private readonly post;
363
- constructor(post: <T>(path: string, body: unknown) => Promise<T>);
363
+ constructor(post: <T>(path: string, body: unknown, options?: {
364
+ requireUserToken?: boolean;
365
+ }) => Promise<T>);
364
366
  /**
365
367
  * Send an outbound SMS / MMS via the typed `sendMessage` oRPC procedure.
366
368
  *
package/dist/index.d.ts CHANGED
@@ -360,7 +360,9 @@ declare class ConversationTasksSubresource {
360
360
  }
361
361
  declare class ConversationMessagesSubresource {
362
362
  private readonly post;
363
- constructor(post: <T>(path: string, body: unknown) => Promise<T>);
363
+ constructor(post: <T>(path: string, body: unknown, options?: {
364
+ requireUserToken?: boolean;
365
+ }) => Promise<T>);
364
366
  /**
365
367
  * Send an outbound SMS / MMS via the typed `sendMessage` oRPC procedure.
366
368
  *
package/dist/index.js CHANGED
@@ -200,6 +200,21 @@ var AppointmentResource = class {
200
200
  };
201
201
 
202
202
  // src/resources/rest-auth.ts
203
+ var REQUIRE_TOKEN_MAX_ATTEMPTS = 4;
204
+ var REQUIRE_TOKEN_RETRY_DELAY_MS = 150;
205
+ function delay(ms) {
206
+ return new Promise((resolve) => setTimeout(resolve, ms));
207
+ }
208
+ function resolveAuthToken(getAuthToken) {
209
+ return __async(this, null, function* () {
210
+ try {
211
+ const token = yield getAuthToken();
212
+ return token != null ? token : null;
213
+ } catch (e) {
214
+ return null;
215
+ }
216
+ });
217
+ }
203
218
  function buildAuthHeaders(auth, options) {
204
219
  return __async(this, null, function* () {
205
220
  const headers = {
@@ -211,13 +226,30 @@ function buildAuthHeaders(auth, options) {
211
226
  if (options == null ? void 0 : options.json) {
212
227
  headers["Content-Type"] = "application/json";
213
228
  }
214
- if (auth.getAuthToken) {
215
- try {
216
- const token = yield auth.getAuthToken();
229
+ if (options == null ? void 0 : options.requireUserToken) {
230
+ if (!auth.getAuthToken) {
231
+ throw new Error(
232
+ "User authentication required to send a message, but no auth token provider is configured"
233
+ );
234
+ }
235
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
236
+ const token = yield resolveAuthToken(auth.getAuthToken);
217
237
  if (token) {
218
238
  headers.Authorization = `Bearer ${token}`;
239
+ return headers;
240
+ }
241
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
242
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
219
243
  }
220
- } catch (e) {
244
+ }
245
+ throw new Error(
246
+ "User authentication required to send a message \u2014 no auth token available"
247
+ );
248
+ }
249
+ if (auth.getAuthToken) {
250
+ const token = yield resolveAuthToken(auth.getAuthToken);
251
+ if (token) {
252
+ headers.Authorization = `Bearer ${token}`;
221
253
  }
222
254
  }
223
255
  return headers;
@@ -480,7 +512,8 @@ var ConversationMessagesSubresource = class {
480
512
  }
481
513
  return this.post(
482
514
  "/conversations/messages",
483
- input
515
+ input,
516
+ { requireUserToken: true }
484
517
  );
485
518
  });
486
519
  }
@@ -491,7 +524,7 @@ var _ConversationsResource = class _ConversationsResource {
491
524
  this.apiKey = apiKey;
492
525
  this.auth = { apiKey, getAuthToken };
493
526
  this.convex = convex != null ? convex : null;
494
- const post = (path, body) => this.postRequest(path, body);
527
+ const post = (path, body, options) => this.postRequest(path, body, options);
495
528
  const patch = (path, body) => this.patchRequest(path, body);
496
529
  this.notes = new ConversationNotesSubresource(post);
497
530
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -554,7 +587,7 @@ var _ConversationsResource = class _ConversationsResource {
554
587
  );
555
588
  });
556
589
  }
557
- postRequest(path, body) {
590
+ postRequest(path, body, options) {
558
591
  return __async(this, null, function* () {
559
592
  if (!this.apiKey) {
560
593
  throw new ConversationsError(
@@ -563,6 +596,19 @@ var _ConversationsResource = class _ConversationsResource {
563
596
  "Truth API key not configured \u2014 request blocked"
564
597
  );
565
598
  }
599
+ let headers;
600
+ try {
601
+ headers = yield buildAuthHeaders(this.auth, {
602
+ json: true,
603
+ requireUserToken: options == null ? void 0 : options.requireUserToken
604
+ });
605
+ } catch (err) {
606
+ throw new ConversationsError(
607
+ path,
608
+ 0,
609
+ err instanceof Error ? err.message : "Failed to build auth headers"
610
+ );
611
+ }
566
612
  const controller = new AbortController();
567
613
  const timeout = setTimeout(
568
614
  () => controller.abort(),
@@ -572,7 +618,7 @@ var _ConversationsResource = class _ConversationsResource {
572
618
  try {
573
619
  res = yield fetch(`${this.baseUrl}/api${path}`, {
574
620
  method: "POST",
575
- headers: yield buildAuthHeaders(this.auth, { json: true }),
621
+ headers,
576
622
  body: JSON.stringify(body),
577
623
  signal: controller.signal
578
624
  });
@@ -1818,9 +1864,9 @@ var Tracker = class {
1818
1864
  lastError = error;
1819
1865
  }
1820
1866
  if (attempt < MAX_RETRIES) {
1821
- const delay = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1822
- const jitter = Math.random() * delay * 0.5;
1823
- yield sleep(delay + jitter);
1867
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1868
+ const jitter = Math.random() * delay2 * 0.5;
1869
+ yield sleep(delay2 + jitter);
1824
1870
  }
1825
1871
  }
1826
1872
  throw lastError;