@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.mjs CHANGED
@@ -135,6 +135,26 @@ 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
+ var USER_TOKEN_HEADER = "X-Truth-Authorization";
144
+ function setUserToken(headers, token) {
145
+ headers.Authorization = `Bearer ${token}`;
146
+ headers[USER_TOKEN_HEADER] = `Bearer ${token}`;
147
+ }
148
+ function resolveAuthToken(getAuthToken) {
149
+ return __async(this, null, function* () {
150
+ try {
151
+ const token = yield getAuthToken();
152
+ return token != null ? token : null;
153
+ } catch (e) {
154
+ return null;
155
+ }
156
+ });
157
+ }
138
158
  function buildAuthHeaders(auth, options) {
139
159
  return __async(this, null, function* () {
140
160
  const headers = {
@@ -146,13 +166,30 @@ function buildAuthHeaders(auth, options) {
146
166
  if (options == null ? void 0 : options.json) {
147
167
  headers["Content-Type"] = "application/json";
148
168
  }
149
- if (auth.getAuthToken) {
150
- try {
151
- const token = yield auth.getAuthToken();
169
+ if (options == null ? void 0 : options.requireUserToken) {
170
+ if (!auth.getAuthToken) {
171
+ throw new Error(
172
+ "User authentication required to send a message, but no auth token provider is configured"
173
+ );
174
+ }
175
+ for (let attempt = 0; attempt < REQUIRE_TOKEN_MAX_ATTEMPTS; attempt++) {
176
+ const token = yield resolveAuthToken(auth.getAuthToken);
152
177
  if (token) {
153
- headers.Authorization = `Bearer ${token}`;
178
+ setUserToken(headers, token);
179
+ return headers;
180
+ }
181
+ if (attempt < REQUIRE_TOKEN_MAX_ATTEMPTS - 1) {
182
+ yield delay(REQUIRE_TOKEN_RETRY_DELAY_MS * (attempt + 1));
154
183
  }
155
- } catch (e) {
184
+ }
185
+ throw new Error(
186
+ "User authentication required to send a message \u2014 no auth token available"
187
+ );
188
+ }
189
+ if (auth.getAuthToken) {
190
+ const token = yield resolveAuthToken(auth.getAuthToken);
191
+ if (token) {
192
+ setUserToken(headers, token);
156
193
  }
157
194
  }
158
195
  return headers;
@@ -415,7 +452,8 @@ var ConversationMessagesSubresource = class {
415
452
  }
416
453
  return this.post(
417
454
  "/conversations/messages",
418
- input
455
+ input,
456
+ { requireUserToken: true }
419
457
  );
420
458
  });
421
459
  }
@@ -426,7 +464,7 @@ var _ConversationsResource = class _ConversationsResource {
426
464
  this.apiKey = apiKey;
427
465
  this.auth = { apiKey, getAuthToken };
428
466
  this.convex = convex != null ? convex : null;
429
- const post = (path, body) => this.postRequest(path, body);
467
+ const post = (path, body, options) => this.postRequest(path, body, options);
430
468
  const patch = (path, body) => this.patchRequest(path, body);
431
469
  this.notes = new ConversationNotesSubresource(post);
432
470
  this.tasks = new ConversationTasksSubresource(post, patch);
@@ -489,7 +527,7 @@ var _ConversationsResource = class _ConversationsResource {
489
527
  );
490
528
  });
491
529
  }
492
- postRequest(path, body) {
530
+ postRequest(path, body, options) {
493
531
  return __async(this, null, function* () {
494
532
  if (!this.apiKey) {
495
533
  throw new ConversationsError(
@@ -498,6 +536,19 @@ var _ConversationsResource = class _ConversationsResource {
498
536
  "Truth API key not configured \u2014 request blocked"
499
537
  );
500
538
  }
539
+ let headers;
540
+ try {
541
+ headers = yield buildAuthHeaders(this.auth, {
542
+ json: true,
543
+ requireUserToken: options == null ? void 0 : options.requireUserToken
544
+ });
545
+ } catch (err) {
546
+ throw new ConversationsError(
547
+ path,
548
+ 0,
549
+ err instanceof Error ? err.message : "Failed to build auth headers"
550
+ );
551
+ }
501
552
  const controller = new AbortController();
502
553
  const timeout = setTimeout(
503
554
  () => controller.abort(),
@@ -507,7 +558,7 @@ var _ConversationsResource = class _ConversationsResource {
507
558
  try {
508
559
  res = yield fetch(`${this.baseUrl}/api${path}`, {
509
560
  method: "POST",
510
- headers: yield buildAuthHeaders(this.auth, { json: true }),
561
+ headers,
511
562
  body: JSON.stringify(body),
512
563
  signal: controller.signal
513
564
  });
@@ -1753,9 +1804,9 @@ var Tracker = class {
1753
1804
  lastError = error;
1754
1805
  }
1755
1806
  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);
1807
+ const delay2 = BASE_RETRY_DELAY_MS * __pow(2, attempt);
1808
+ const jitter = Math.random() * delay2 * 0.5;
1809
+ yield sleep(delay2 + jitter);
1759
1810
  }
1760
1811
  }
1761
1812
  throw lastError;