@hipnation-truth/sdk 0.26.17 → 0.26.19

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.js CHANGED
@@ -2935,17 +2935,10 @@ var allergiesByPatientRef = (0, import_server5.makeFunctionReference)("medicalRe
2935
2935
  var appointmentsByPatientRef = (0, import_server5.makeFunctionReference)("medicalRecords:getAppointmentsByElationPatient");
2936
2936
  function postPatientRefresh(url, apiKey, getAuthToken, body, signal) {
2937
2937
  return __async(this, null, function* () {
2938
- const headers = {
2939
- "Content-Type": "application/json",
2940
- "X-API-Key": apiKey
2941
- };
2942
- try {
2943
- const token = yield getAuthToken == null ? void 0 : getAuthToken();
2944
- if (token) {
2945
- headers.Authorization = `Bearer ${token}`;
2946
- }
2947
- } catch (e) {
2948
- }
2938
+ const headers = yield buildAuthHeaders(
2939
+ { apiKey, getAuthToken },
2940
+ { json: true, requireUserToken: true }
2941
+ );
2949
2942
  yield fetch(url, {
2950
2943
  method: "POST",
2951
2944
  headers,
@@ -3132,6 +3125,7 @@ function useNotifications(options) {
3132
3125
  const sdkContext = useTruthSdkContext();
3133
3126
  const apiBaseUrl = (_b = (_a = options.apiBaseUrl) != null ? _a : sdkContext == null ? void 0 : sdkContext.apiBaseUrl) != null ? _b : "";
3134
3127
  const apiKey = (_d = (_c = options.apiKey) != null ? _c : sdkContext == null ? void 0 : sdkContext.apiKey) != null ? _d : "";
3128
+ const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
3135
3129
  const [permissionStatus, setPermissionStatus] = (0, import_react6.useState)("unknown");
3136
3130
  const [devicePushToken, setDevicePushToken] = (0, import_react6.useState)(null);
3137
3131
  const expoRef = (0, import_react6.useRef)(null);
@@ -3222,10 +3216,10 @@ function useNotifications(options) {
3222
3216
  `${apiBaseUrl}/api/notifications/devices/register`,
3223
3217
  {
3224
3218
  method: "POST",
3225
- headers: {
3226
- "Content-Type": "application/json",
3227
- "X-API-Key": apiKey
3228
- },
3219
+ headers: yield buildAuthHeaders(
3220
+ { apiKey, getAuthToken },
3221
+ { json: true, requireUserToken: true }
3222
+ ),
3229
3223
  body: JSON.stringify({
3230
3224
  userId: options.userId,
3231
3225
  platform: "web",
@@ -3274,14 +3268,23 @@ function useNotifications(options) {
3274
3268
  if (!apiBaseUrl || !apiKey) {
3275
3269
  return { ok: false, reason: "missing_truth_config" };
3276
3270
  }
3271
+ let nativeHeaders;
3272
+ try {
3273
+ nativeHeaders = yield buildAuthHeaders(
3274
+ { apiKey, getAuthToken },
3275
+ { json: true, requireUserToken: true }
3276
+ );
3277
+ } catch (err) {
3278
+ return {
3279
+ ok: false,
3280
+ reason: `missing_user_token: ${err instanceof Error ? err.message : String(err)}`
3281
+ };
3282
+ }
3277
3283
  const res = yield fetch(
3278
3284
  `${apiBaseUrl}/api/notifications/devices/register`,
3279
3285
  {
3280
3286
  method: "POST",
3281
- headers: {
3282
- "Content-Type": "application/json",
3283
- "X-API-Key": apiKey
3284
- },
3287
+ headers: nativeHeaders,
3285
3288
  body: JSON.stringify(__spreadValues({
3286
3289
  userId: options.userId,
3287
3290
  platform,
@@ -3303,6 +3306,7 @@ function useNotifications(options) {
3303
3306
  }), [
3304
3307
  apiBaseUrl,
3305
3308
  apiKey,
3309
+ getAuthToken,
3306
3310
  options.userId,
3307
3311
  options.appVersion,
3308
3312
  options.serviceWorkerPath
@@ -3311,17 +3315,20 @@ function useNotifications(options) {
3311
3315
  if (!devicePushToken) {
3312
3316
  return;
3313
3317
  }
3314
- yield fetch(`${apiBaseUrl}/api/notifications/devices/unregister`, {
3315
- method: "POST",
3316
- headers: {
3317
- "Content-Type": "application/json",
3318
- "X-API-Key": apiKey
3319
- },
3320
- body: JSON.stringify({ nativeToken: devicePushToken })
3321
- }).catch(() => {
3322
- });
3318
+ try {
3319
+ const headers = yield buildAuthHeaders(
3320
+ { apiKey, getAuthToken },
3321
+ { json: true, requireUserToken: true }
3322
+ );
3323
+ yield fetch(`${apiBaseUrl}/api/notifications/devices/unregister`, {
3324
+ method: "POST",
3325
+ headers,
3326
+ body: JSON.stringify({ nativeToken: devicePushToken })
3327
+ });
3328
+ } catch (e) {
3329
+ }
3323
3330
  setDevicePushToken(null);
3324
- }), [apiBaseUrl, apiKey, devicePushToken]);
3331
+ }), [apiBaseUrl, apiKey, getAuthToken, devicePushToken]);
3325
3332
  const addReceivedListener = (0, import_react6.useCallback)(
3326
3333
  (listener) => {
3327
3334
  if (isWebRef.current) {
@@ -3483,20 +3490,25 @@ function useNotificationsActions() {
3483
3490
  const sdkContext = useTruthSdkContext();
3484
3491
  const apiBaseUrl = (_a = sdkContext == null ? void 0 : sdkContext.apiBaseUrl) != null ? _a : "";
3485
3492
  const apiKey = (_b = sdkContext == null ? void 0 : sdkContext.apiKey) != null ? _b : "";
3493
+ const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
3486
3494
  const post = (0, import_react6.useCallback)(
3487
- (path, body) => __async(null, null, function* () {
3495
+ (path, body, opts) => __async(null, null, function* () {
3488
3496
  if (!apiBaseUrl || !apiKey) {
3489
3497
  throw new Error(
3490
3498
  "useNotificationsActions: missing apiBaseUrl/apiKey \u2014 wrap in <TruthProvider> or pass options"
3491
3499
  );
3492
3500
  }
3501
+ const headers = (opts == null ? void 0 : opts.requireUserToken) ? yield buildAuthHeaders(
3502
+ { apiKey, getAuthToken },
3503
+ { json: true, requireUserToken: true }
3504
+ ) : {
3505
+ "Content-Type": "application/json",
3506
+ Accept: "application/json",
3507
+ "X-API-Key": apiKey
3508
+ };
3493
3509
  const res = yield fetch(`${apiBaseUrl}/api${path}`, {
3494
3510
  method: "POST",
3495
- headers: {
3496
- "Content-Type": "application/json",
3497
- Accept: "application/json",
3498
- "X-API-Key": apiKey
3499
- },
3511
+ headers,
3500
3512
  body: JSON.stringify(body)
3501
3513
  });
3502
3514
  if (!res.ok) {
@@ -3507,13 +3519,17 @@ function useNotificationsActions() {
3507
3519
  }
3508
3520
  return yield res.json();
3509
3521
  }),
3510
- [apiBaseUrl, apiKey]
3522
+ [apiBaseUrl, apiKey, getAuthToken]
3511
3523
  );
3512
3524
  const get = (0, import_react6.useCallback)(
3513
- (path) => __async(null, null, function* () {
3525
+ (path, opts) => __async(null, null, function* () {
3526
+ const headers = (opts == null ? void 0 : opts.requireUserToken) ? yield buildAuthHeaders(
3527
+ { apiKey, getAuthToken },
3528
+ { requireUserToken: true }
3529
+ ) : { Accept: "application/json", "X-API-Key": apiKey };
3514
3530
  const res = yield fetch(`${apiBaseUrl}/api${path}`, {
3515
3531
  method: "GET",
3516
- headers: { Accept: "application/json", "X-API-Key": apiKey }
3532
+ headers
3517
3533
  });
3518
3534
  if (!res.ok) {
3519
3535
  const text = yield res.text().catch(() => "");
@@ -3523,7 +3539,7 @@ function useNotificationsActions() {
3523
3539
  }
3524
3540
  return yield res.json();
3525
3541
  }),
3526
- [apiBaseUrl, apiKey]
3542
+ [apiBaseUrl, apiKey, getAuthToken]
3527
3543
  );
3528
3544
  const send = (0, import_react6.useCallback)(
3529
3545
  (input) => post("/notifications/send", input),
@@ -3535,14 +3551,16 @@ function useNotificationsActions() {
3535
3551
  );
3536
3552
  const getPreferences = (0, import_react6.useCallback)(
3537
3553
  (userId) => get(
3538
- `/notifications/preferences/${encodeURIComponent(userId)}`
3554
+ `/notifications/preferences/${encodeURIComponent(userId)}`,
3555
+ { requireUserToken: true }
3539
3556
  ),
3540
3557
  [get]
3541
3558
  );
3542
3559
  const updatePreferences = (0, import_react6.useCallback)(
3543
3560
  (userId, prefs) => post(
3544
3561
  `/notifications/preferences/${encodeURIComponent(userId)}`,
3545
- prefs
3562
+ prefs,
3563
+ { requireUserToken: true }
3546
3564
  ),
3547
3565
  [post]
3548
3566
  );
@@ -3792,6 +3810,7 @@ function useUserSync(input) {
3792
3810
  const sdkContext = useTruthSdkContext();
3793
3811
  const apiBaseUrl = (_b = (_a = input.apiBaseUrl) != null ? _a : sdkContext == null ? void 0 : sdkContext.apiBaseUrl) != null ? _b : "";
3794
3812
  const apiKey = (_d = (_c = input.apiKey) != null ? _c : sdkContext == null ? void 0 : sdkContext.apiKey) != null ? _d : "";
3813
+ const getAuthToken = sdkContext == null ? void 0 : sdkContext.getAuthToken;
3795
3814
  const [status, setStatus] = (0, import_react11.useState)("idle");
3796
3815
  const [error, setError] = (0, import_react11.useState)(null);
3797
3816
  const lastKeyRef = (0, import_react11.useRef)(null);
@@ -3805,12 +3824,13 @@ function useUserSync(input) {
3805
3824
  setStatus("syncing");
3806
3825
  setError(null);
3807
3826
  try {
3827
+ const headers = yield buildAuthHeaders(
3828
+ { apiKey, getAuthToken },
3829
+ { json: true, requireUserToken: true }
3830
+ );
3808
3831
  const res = yield fetch(`${apiBaseUrl}/api/users/sync`, {
3809
3832
  method: "POST",
3810
- headers: {
3811
- "Content-Type": "application/json",
3812
- "X-API-Key": apiKey
3813
- },
3833
+ headers,
3814
3834
  body: JSON.stringify({
3815
3835
  userId: input.userId,
3816
3836
  email: input.email,