@hipnation-truth/sdk 0.24.0 → 0.25.2

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
@@ -1257,12 +1257,26 @@ declare const ENVIRONMENTS: {
1257
1257
  readonly production: "production";
1258
1258
  };
1259
1259
  type Environment = (typeof ENVIRONMENTS)[keyof typeof ENVIRONMENTS];
1260
+ /**
1261
+ * Fetches a user-identity JWT for Convex calls. Return the token from
1262
+ * your identity provider's Clerk JWT template named "convex"
1263
+ * (e.g. `getToken({ template: "convex" })` from `@clerk/expo`), or
1264
+ * `null` when no user is signed in.
1265
+ */
1266
+ type AuthTokenFetcher = () => Promise<string | null | undefined>;
1260
1267
  /**
1261
1268
  * Configuration for initializing a TruthClient.
1262
1269
  */
1263
1270
  interface TruthClientConfig {
1264
1271
  /** API key for authenticating with the Truth platform (e.g. "hn_live_...") */
1265
1272
  apiKey: string;
1273
+ /**
1274
+ * Per-call Clerk JWT fetcher for Convex data access. When provided,
1275
+ * every Convex query/mutation/action carries the caller's identity;
1276
+ * Convex deployments with `CLERK_AUTH_REQUIRED` enabled reject calls
1277
+ * without it. Omit for service contexts that don't act as a user.
1278
+ */
1279
+ getAuthToken?: AuthTokenFetcher;
1266
1280
  /** Target environment */
1267
1281
  environment: Environment;
1268
1282
  /** Override the default Convex URL for data access */
@@ -2813,6 +2827,16 @@ interface TruthProviderProps {
2813
2827
  source?: string;
2814
2828
  sourceVersion?: string;
2815
2829
  tenantId?: string;
2830
+ /**
2831
+ * Per-call Clerk JWT fetcher (template "convex") identifying the
2832
+ * signed-in user to Convex. Wire it to your identity provider, e.g.
2833
+ * `useAuth().getToken({ template: "convex" })` from `@clerk/expo`.
2834
+ * Applied to both the live websocket client (React hooks) and the
2835
+ * shared `TruthClient` (resource methods). Identity changes are
2836
+ * picked up without remounting — the latest fetcher is read through
2837
+ * a ref on every call.
2838
+ */
2839
+ getAuthToken?: AuthTokenFetcher;
2816
2840
  /**
2817
2841
  * Synchronous encrypted KV mirror for durable offline reads, injected
2818
2842
  * by the consuming app (e.g. MMKV on `ch/`). Omit on web — the SDK
@@ -2830,7 +2854,7 @@ interface TruthProviderProps {
2830
2854
  offlineEnabled?: boolean;
2831
2855
  children: ReactNode;
2832
2856
  }
2833
- declare function TruthProvider({ environment, convexUrl, apiBaseUrl, apiKey, source, sourceVersion, tenantId, offlineStore, offlineEnabled, children, }: TruthProviderProps): react.FunctionComponentElement<react.ProviderProps<TruthSdkContextValue | null>>;
2857
+ declare function TruthProvider({ environment, convexUrl, apiBaseUrl, apiKey, source, sourceVersion, tenantId, getAuthToken, offlineStore, offlineEnabled, children, }: TruthProviderProps): react.FunctionComponentElement<react.ProviderProps<TruthSdkContextValue | null>>;
2834
2858
 
2835
2859
  /**
2836
2860
  * React hooks for conversation reminders — bulk lookup keyed by
package/dist/react.js CHANGED
@@ -4,8 +4,10 @@ var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __getProtoOf = Object.getPrototypeOf;
7
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
9
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __reflectGet = Reflect.get;
9
11
  var __pow = Math.pow;
10
12
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
13
  var __spreadValues = (a, b) => {
@@ -32,6 +34,7 @@ var __copyProps = (to, from, except, desc) => {
32
34
  return to;
33
35
  };
34
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
35
38
  var __async = (__this, __arguments, generator) => {
36
39
  return new Promise((resolve, reject) => {
37
40
  var fulfilled = (value) => {
@@ -373,8 +376,53 @@ var import_react_query_persist_client = require("@tanstack/react-query-persist-c
373
376
  var import_react3 = require("convex/react");
374
377
  var import_react4 = require("react");
375
378
 
376
- // src/client.ts
379
+ // src/auth-convex-client.ts
377
380
  var import_browser = require("convex/browser");
381
+ var AuthAwareConvexHttpClient = class _AuthAwareConvexHttpClient extends import_browser.ConvexHttpClient {
382
+ constructor(address, getAuthToken) {
383
+ super(address);
384
+ this.getAuthToken = getAuthToken;
385
+ }
386
+ /**
387
+ * Pull a fresh token from the fetcher and apply it. A fetcher error
388
+ * keeps the previously-applied token rather than dropping auth
389
+ * mid-session.
390
+ */
391
+ syncAuth() {
392
+ return __async(this, null, function* () {
393
+ if (!this.getAuthToken) {
394
+ return;
395
+ }
396
+ try {
397
+ const token = yield this.getAuthToken();
398
+ if (token) {
399
+ this.setAuth(token);
400
+ } else {
401
+ this.clearAuth();
402
+ }
403
+ } catch (e) {
404
+ }
405
+ });
406
+ }
407
+ query(query, ...args) {
408
+ return __async(this, null, function* () {
409
+ yield this.syncAuth();
410
+ return __superGet(_AuthAwareConvexHttpClient.prototype, this, "query").call(this, query, ...args);
411
+ });
412
+ }
413
+ mutation(mutation, ...args) {
414
+ return __async(this, null, function* () {
415
+ yield this.syncAuth();
416
+ return __superGet(_AuthAwareConvexHttpClient.prototype, this, "mutation").call(this, mutation, ...args);
417
+ });
418
+ }
419
+ action(action, ...args) {
420
+ return __async(this, null, function* () {
421
+ yield this.syncAuth();
422
+ return __superGet(_AuthAwareConvexHttpClient.prototype, this, "action").call(this, action, ...args);
423
+ });
424
+ }
425
+ };
378
426
 
379
427
  // src/resources/appointments.ts
380
428
  var AppointmentResource = class {
@@ -2203,7 +2251,7 @@ var TruthClient = class {
2203
2251
  this._webPushReady = null;
2204
2252
  var _a, _b, _c, _d, _e, _f, _g, _h;
2205
2253
  const convexUrl = (_b = (_a = config.convexUrl) != null ? _a : CONVEX_URLS[config.environment]) != null ? _b : CONVEX_URLS.local;
2206
- this.convex = new import_browser.ConvexHttpClient(convexUrl);
2254
+ this.convex = new AuthAwareConvexHttpClient(convexUrl, config.getAuthToken);
2207
2255
  this.tracker = new Tracker({
2208
2256
  apiKey: config.apiKey,
2209
2257
  environment: config.environment,
@@ -2494,6 +2542,7 @@ function TruthProvider({
2494
2542
  source,
2495
2543
  sourceVersion,
2496
2544
  tenantId,
2545
+ getAuthToken,
2497
2546
  offlineStore = NOOP_STORE,
2498
2547
  offlineEnabled = false,
2499
2548
  children
@@ -2503,6 +2552,26 @@ function TruthProvider({
2503
2552
  const resolvedApiBaseUrl = (_a = apiBaseUrl != null ? apiBaseUrl : readEnv("EXPO_PUBLIC_TRUTH_API_BASE_URL")) != null ? _a : resolveApiBaseUrl(environment);
2504
2553
  const resolvedApiKey = (_b = apiKey != null ? apiKey : readEnv("EXPO_PUBLIC_TRUTH_API_KEY")) != null ? _b : "";
2505
2554
  const convexClient = (0, import_react4.useMemo)(() => new import_react3.ConvexReactClient(url), [url]);
2555
+ const getAuthTokenRef = (0, import_react4.useRef)(getAuthToken);
2556
+ getAuthTokenRef.current = getAuthToken;
2557
+ const hasAuthFetcher = Boolean(getAuthToken);
2558
+ const stableGetAuthToken = (0, import_react4.useMemo)(
2559
+ () => hasAuthFetcher ? () => __async(null, null, function* () {
2560
+ var _a2, _b2;
2561
+ return (_b2 = yield (_a2 = getAuthTokenRef.current) == null ? void 0 : _a2.call(getAuthTokenRef)) != null ? _b2 : null;
2562
+ }) : void 0,
2563
+ [hasAuthFetcher]
2564
+ );
2565
+ (0, import_react4.useEffect)(() => {
2566
+ if (stableGetAuthToken) {
2567
+ convexClient.setAuth(() => __async(null, null, function* () {
2568
+ var _a2;
2569
+ return (_a2 = yield stableGetAuthToken()) != null ? _a2 : null;
2570
+ }));
2571
+ } else {
2572
+ convexClient.clearAuth();
2573
+ }
2574
+ }, [convexClient, stableGetAuthToken]);
2506
2575
  const convexQueryClient = (0, import_react4.useMemo)(
2507
2576
  () => new import_react_query3.ConvexQueryClient(convexClient),
2508
2577
  [convexClient]
@@ -2544,7 +2613,8 @@ function TruthProvider({
2544
2613
  source: source != null ? source : "unknown",
2545
2614
  sourceVersion: sourceVersion != null ? sourceVersion : "unknown",
2546
2615
  tenantId: tenantId != null ? tenantId : "",
2547
- autoInitServiceWorker: false
2616
+ autoInitServiceWorker: false,
2617
+ getAuthToken: stableGetAuthToken
2548
2618
  }),
2549
2619
  [
2550
2620
  url,
@@ -2553,7 +2623,8 @@ function TruthProvider({
2553
2623
  environment,
2554
2624
  source,
2555
2625
  sourceVersion,
2556
- tenantId
2626
+ tenantId,
2627
+ stableGetAuthToken
2557
2628
  ]
2558
2629
  );
2559
2630
  (0, import_react4.useEffect)(() => {