@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.mjs CHANGED
@@ -135,6 +135,21 @@ var AppointmentResource = class {
135
135
  };
136
136
 
137
137
  // src/resources/rest-auth.ts
138
+ var REQUIRE_TOKEN_MAX_ATTEMPTS = 4;
139
+ var REQUIRE_TOKEN_RETRY_DELAY_MS = 150;
140
+ function delay(ms) {
141
+ return new Promise((resolve) => setTimeout(resolve, ms));
142
+ }
143
+ function resolveAuthToken(getAuthToken) {
144
+ return __async(this, null, function* () {
145
+ try {
146
+ const token = yield getAuthToken();
147
+ return token != null ? token : null;
148
+ } catch (e) {
149
+ return null;
150
+ }
151
+ });
152
+ }
138
153
  function buildAuthHeaders(auth, options) {
139
154
  return __async(this, null, function* () {
140
155
  const headers = {
@@ -146,13 +161,30 @@ function buildAuthHeaders(auth, options) {
146
161
  if (options == null ? void 0 : options.json) {
147
162
  headers["Content-Type"] = "application/json";
148
163
  }
149
- if (auth.getAuthToken) {
150
- try {
151
- const token = yield auth.getAuthToken();
164
+ if (options == null ? void 0 : options.requireUserToken) {
165
+ if (!auth.getAuthToken) {
166
+ throw new Error(
167
+ "User authentication required to send a message, but no auth token provider is configured"
168
+ );
169
+ }
170
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
171
+ const token = yield resolveAuthToken(auth.getAuthToken);
152
172
  if (token) {
153
173
  headers.Authorization = `Bearer ${token}`;
174
+ return headers;
175
+ }
176
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
177
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
154
178
  }
155
- } catch (e) {
179
+ }
180
+ throw new Error(
181
+ "User authentication required to send a message \u2014 no auth token available"
182
+ );
183
+ }
184
+ if (auth.getAuthToken) {
185
+ const token = yield resolveAuthToken(auth.getAuthToken);
186
+ if (token) {
187
+ headers.Authorization = `Bearer ${token}`;
156
188
  }
157
189
  }
158
190
  return headers;
@@ -415,7 +447,8 @@ var ConversationMessagesSubresource = class {
415
447
  }
416
448
  return this.post(
417
449
  "/conversations/messages",
418
- input
450
+ input,
451
+ { requireUserToken: true }
419
452
  );
420
453
  });
421
454
  }
@@ -426,7 +459,7 @@ var _ConversationsResource = class _ConversationsResource {
426
459
  this.apiKey = apiKey;
427
460
  this.auth = { apiKey, getAuthToken };
428
461
  this.convex = convex != null ? convex : null;
429
- const post = (path, body) => this.postRequest(path, body);
462
+ const post = (path, body, options) => this.postRequest(path, body, options);
430
463
  const patch = (path, body) => this.patchRequest(path, body);
431
464
  this.notes = new ConversationNotesSubresource(post);
432
465
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -489,7 +522,7 @@ var _ConversationsResource = class _ConversationsResource {
489
522
  );
490
523
  });
491
524
  }
492
- postRequest(path, body) {
525
+ postRequest(path, body, options) {
493
526
  return __async(this, null, function* () {
494
527
  if (!this.apiKey) {
495
528
  throw new ConversationsError(
@@ -498,6 +531,19 @@ var _ConversationsResource = class _ConversationsResource {
498
531
  "Truth API key not configured \u2014 request blocked"
499
532
  );
500
533
  }
534
+ let headers;
535
+ try {
536
+ headers = yield buildAuthHeaders(this.auth, {
537
+ json: true,
538
+ requireUserToken: options == null ? void 0 : options.requireUserToken
539
+ });
540
+ } catch (err) {
541
+ throw new ConversationsError(
542
+ path,
543
+ 0,
544
+ err instanceof Error ? err.message : "Failed to build auth headers"
545
+ );
546
+ }
501
547
  const controller = new AbortController();
502
548
  const timeout = setTimeout(
503
549
  () => controller.abort(),
@@ -507,7 +553,7 @@ var _ConversationsResource = class _ConversationsResource {
507
553
  try {
508
554
  res = yield fetch(`${this.baseUrl}/api${path}`, {
509
555
  method: "POST",
510
- headers: yield buildAuthHeaders(this.auth, { json: true }),
556
+ headers,
511
557
  body: JSON.stringify(body),
512
558
  signal: controller.signal
513
559
  });
@@ -1753,9 +1799,9 @@ var Tracker = class {
1753
1799
  lastError = error;
1754
1800
  }
1755
1801
  if (attempt < MAX_RETRIES) {
1756
- const delay = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1757
- const jitter = Math.random() * delay * 0.5;
1758
- yield sleep(delay + jitter);
1802
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1803
+ const jitter = Math.random() * delay2 * 0.5;
1804
+ yield sleep(delay2 + jitter);
1759
1805
  }
1760
1806
  }
1761
1807
  throw lastError;