@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/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,26 @@ 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
+ var USER_TOKEN_HEADER = "X-Truth-Authorization";
209
+ function setUserToken(headers, token) {
210
+ headers.Authorization = `Bearer ${token}`;
211
+ headers[USER_TOKEN_HEADER] = `Bearer ${token}`;
212
+ }
213
+ function resolveAuthToken(getAuthToken) {
214
+ return __async(this, null, function* () {
215
+ try {
216
+ const token = yield getAuthToken();
217
+ return token != null ? token : null;
218
+ } catch (e) {
219
+ return null;
220
+ }
221
+ });
222
+ }
203
223
  function buildAuthHeaders(auth, options) {
204
224
  return __async(this, null, function* () {
205
225
  const headers = {
@@ -211,13 +231,30 @@ function buildAuthHeaders(auth, options) {
211
231
  if (options == null ? void 0 : options.json) {
212
232
  headers["Content-Type"] = "application/json";
213
233
  }
214
- if (auth.getAuthToken) {
215
- try {
216
- const token = yield auth.getAuthToken();
234
+ if (options == null ? void 0 : options.requireUserToken) {
235
+ if (!auth.getAuthToken) {
236
+ throw new Error(
237
+ "User authentication required to send a message, but no auth token provider is configured"
238
+ );
239
+ }
240
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
241
+ const token = yield resolveAuthToken(auth.getAuthToken);
217
242
  if (token) {
218
- headers.Authorization = `Bearer ${token}`;
243
+ setUserToken(headers, token);
244
+ return headers;
245
+ }
246
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
247
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
219
248
  }
220
- } catch (e) {
249
+ }
250
+ throw new Error(
251
+ "User authentication required to send a message \u2014 no auth token available"
252
+ );
253
+ }
254
+ if (auth.getAuthToken) {
255
+ const token = yield resolveAuthToken(auth.getAuthToken);
256
+ if (token) {
257
+ setUserToken(headers, token);
221
258
  }
222
259
  }
223
260
  return headers;
@@ -480,7 +517,8 @@ var ConversationMessagesSubresource = class {
480
517
  }
481
518
  return this.post(
482
519
  "/conversations/messages",
483
- input
520
+ input,
521
+ { requireUserToken: true }
484
522
  );
485
523
  });
486
524
  }
@@ -491,7 +529,7 @@ var _ConversationsResource = class _ConversationsResource {
491
529
  this.apiKey = apiKey;
492
530
  this.auth = { apiKey, getAuthToken };
493
531
  this.convex = convex != null ? convex : null;
494
- const post = (path, body) => this.postRequest(path, body);
532
+ const post = (path, body, options) => this.postRequest(path, body, options);
495
533
  const patch = (path, body) => this.patchRequest(path, body);
496
534
  this.notes = new ConversationNotesSubresource(post);
497
535
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -554,7 +592,7 @@ var _ConversationsResource = class _ConversationsResource {
554
592
  );
555
593
  });
556
594
  }
557
- postRequest(path, body) {
595
+ postRequest(path, body, options) {
558
596
  return __async(this, null, function* () {
559
597
  if (!this.apiKey) {
560
598
  throw new ConversationsError(
@@ -563,6 +601,19 @@ var _ConversationsResource = class _ConversationsResource {
563
601
  "Truth API key not configured \u2014 request blocked"
564
602
  );
565
603
  }
604
+ let headers;
605
+ try {
606
+ headers = yield buildAuthHeaders(this.auth, {
607
+ json: true,
608
+ requireUserToken: options == null ? void 0 : options.requireUserToken
609
+ });
610
+ } catch (err) {
611
+ throw new ConversationsError(
612
+ path,
613
+ 0,
614
+ err instanceof Error ? err.message : "Failed to build auth headers"
615
+ );
616
+ }
566
617
  const controller = new AbortController();
567
618
  const timeout = setTimeout(
568
619
  () => controller.abort(),
@@ -572,7 +623,7 @@ var _ConversationsResource = class _ConversationsResource {
572
623
  try {
573
624
  res = yield fetch(`${this.baseUrl}/api${path}`, {
574
625
  method: "POST",
575
- headers: yield buildAuthHeaders(this.auth, { json: true }),
626
+ headers,
576
627
  body: JSON.stringify(body),
577
628
  signal: controller.signal
578
629
  });
@@ -1818,9 +1869,9 @@ var Tracker = class {
1818
1869
  lastError = error;
1819
1870
  }
1820
1871
  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);
1872
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1873
+ const jitter = Math.random() * delay2 * 0.5;
1874
+ yield sleep(delay2 + jitter);
1824
1875
  }
1825
1876
  }
1826
1877
  throw lastError;