@neetru/sdk 3.0.1 → 3.1.0

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.
Files changed (60) hide show
  1. package/CHANGELOG.md +554 -492
  2. package/dist/auth.cjs +84 -15
  3. package/dist/auth.cjs.map +1 -1
  4. package/dist/auth.d.cts +1 -2
  5. package/dist/auth.d.ts +1 -2
  6. package/dist/auth.mjs +84 -15
  7. package/dist/auth.mjs.map +1 -1
  8. package/dist/catalog.d.cts +1 -2
  9. package/dist/catalog.d.ts +1 -2
  10. package/dist/checkout.d.cts +1 -2
  11. package/dist/checkout.d.ts +1 -2
  12. package/dist/db-react.cjs +15 -5
  13. package/dist/db-react.cjs.map +1 -1
  14. package/dist/db-react.d.cts +35 -13
  15. package/dist/db-react.d.ts +35 -13
  16. package/dist/db-react.mjs +15 -5
  17. package/dist/db-react.mjs.map +1 -1
  18. package/dist/db.cjs +104 -18
  19. package/dist/db.cjs.map +1 -1
  20. package/dist/db.d.cts +1 -2
  21. package/dist/db.d.ts +1 -2
  22. package/dist/db.mjs +99 -19
  23. package/dist/db.mjs.map +1 -1
  24. package/dist/entitlements.d.cts +1 -2
  25. package/dist/entitlements.d.ts +1 -2
  26. package/dist/firestore-compat.cjs +399 -0
  27. package/dist/firestore-compat.cjs.map +1 -0
  28. package/dist/firestore-compat.d.cts +294 -0
  29. package/dist/firestore-compat.d.ts +294 -0
  30. package/dist/firestore-compat.mjs +371 -0
  31. package/dist/firestore-compat.mjs.map +1 -0
  32. package/dist/index.cjs +105 -19
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +2 -3
  35. package/dist/index.d.ts +2 -3
  36. package/dist/index.mjs +100 -20
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/mocks.d.cts +1 -2
  39. package/dist/mocks.d.ts +1 -2
  40. package/dist/notifications.d.cts +1 -2
  41. package/dist/notifications.d.ts +1 -2
  42. package/dist/react.cjs +267 -0
  43. package/dist/react.cjs.map +1 -1
  44. package/dist/react.d.cts +117 -3
  45. package/dist/react.d.ts +117 -3
  46. package/dist/react.mjs +265 -1
  47. package/dist/react.mjs.map +1 -1
  48. package/dist/support.d.cts +1 -2
  49. package/dist/support.d.ts +1 -2
  50. package/dist/telemetry.d.cts +1 -2
  51. package/dist/telemetry.d.ts +1 -2
  52. package/dist/{types-DALIhcbq.d.ts → types-BRv8wBxX.d.ts} +581 -2
  53. package/dist/{types-Lfd3LiAF.d.cts → types-nwErcRX8.d.cts} +581 -2
  54. package/dist/usage.d.cts +1 -2
  55. package/dist/usage.d.ts +1 -2
  56. package/dist/webhooks.d.cts +1 -2
  57. package/dist/webhooks.d.ts +1 -2
  58. package/package.json +156 -151
  59. package/dist/collection-ref-DqAAhuhX.d.cts +0 -472
  60. package/dist/collection-ref-DqAAhuhX.d.ts +0 -472
package/dist/auth.cjs CHANGED
@@ -2043,7 +2043,60 @@ var MutationQueue = class {
2043
2043
  }
2044
2044
  };
2045
2045
 
2046
+ // src/db/field-value.ts
2047
+ var NEETRU_SENTINEL_KEY = "__neetru__";
2048
+ function isFieldSentinel(v) {
2049
+ return typeof v === "object" && v !== null && NEETRU_SENTINEL_KEY in v && typeof v[NEETRU_SENTINEL_KEY] === "string";
2050
+ }
2051
+ function isServerTimestampSentinel(v) {
2052
+ return isFieldSentinel(v) && v.__neetru__ === "serverTimestamp";
2053
+ }
2054
+ function isIncrementSentinel(v) {
2055
+ return isFieldSentinel(v) && v.__neetru__ === "increment" && typeof v.operand === "number" && Number.isFinite(v.operand);
2056
+ }
2057
+ function resolveSentinelLocally(value, baseValue) {
2058
+ if (isServerTimestampSentinel(value)) {
2059
+ return Date.now();
2060
+ }
2061
+ if (isIncrementSentinel(value)) {
2062
+ const base = typeof baseValue === "number" && Number.isFinite(baseValue) ? baseValue : 0;
2063
+ return base + value.operand;
2064
+ }
2065
+ return value;
2066
+ }
2067
+ function resolveSentinelsLocally(data, existing) {
2068
+ let touched = false;
2069
+ const out = {};
2070
+ for (const [k, v] of Object.entries(data)) {
2071
+ if (isFieldSentinel(v)) {
2072
+ touched = true;
2073
+ out[k] = resolveSentinelLocally(v, existing?.[k]);
2074
+ } else {
2075
+ out[k] = v;
2076
+ }
2077
+ }
2078
+ return touched ? out : data;
2079
+ }
2080
+ function hasFieldSentinel(data) {
2081
+ for (const v of Object.values(data)) {
2082
+ if (isFieldSentinel(v)) return true;
2083
+ }
2084
+ return false;
2085
+ }
2086
+
2046
2087
  // src/db/offline/sync-engine.ts
2088
+ function hasSentinels(payload) {
2089
+ return payload !== null && hasFieldSentinel(payload);
2090
+ }
2091
+ function sanitizeSentinelPayload(payload) {
2092
+ if (payload === null) return null;
2093
+ if (!hasFieldSentinel(payload)) return payload;
2094
+ const out = {};
2095
+ for (const [k, v] of Object.entries(payload)) {
2096
+ if (!isFieldSentinel(v)) out[k] = v;
2097
+ }
2098
+ return out;
2099
+ }
2047
2100
  var SyncEngine = class {
2048
2101
  _store;
2049
2102
  _queue;
@@ -2251,11 +2304,16 @@ var SyncEngine = class {
2251
2304
  }
2252
2305
  return;
2253
2306
  }
2254
- const newData = mut.payload ?? (existing?.data ?? {});
2307
+ const literalPayload = sanitizeSentinelPayload(mut.payload);
2308
+ const newData = literalPayload ?? (existing?.data ?? {});
2255
2309
  const updatedDoc = {
2256
2310
  collection,
2257
2311
  id: docId,
2258
- data: mut.op === "update" ? { ...existing?.data ?? {}, ...newData } : newData,
2312
+ data: mut.op === "update" ? { ...existing?.data ?? {}, ...newData } : (
2313
+ // add/set: se o payload tinha sentinels, o cache otimista (existing.data)
2314
+ // é a fonte de verdade até o pull; senão usa o payload literal.
2315
+ hasSentinels(mut.payload) ? { ...existing?.data ?? {}, ...newData } : newData
2316
+ ),
2259
2317
  meta: {
2260
2318
  serverVersion,
2261
2319
  updatedAtServer: serverTimestamp,
@@ -3465,10 +3523,12 @@ var DbCollectionRefImpl = class {
3465
3523
  return this._buildListResult(q);
3466
3524
  }
3467
3525
  async add(data) {
3526
+ const rawData = data;
3527
+ const localData = resolveSentinelsLocally(rawData, void 0);
3468
3528
  const mutation = await this._queue.enqueue({
3469
3529
  collection: this._collection,
3470
3530
  op: "add",
3471
- payload: data,
3531
+ payload: rawData,
3472
3532
  baseVersion: null,
3473
3533
  batchId: null
3474
3534
  });
@@ -3476,7 +3536,7 @@ var DbCollectionRefImpl = class {
3476
3536
  await this._store.putDoc({
3477
3537
  collection: this._collection,
3478
3538
  id: docId,
3479
- data,
3539
+ data: localData,
3480
3540
  meta: {
3481
3541
  serverVersion: null,
3482
3542
  updatedAtServer: null,
@@ -3490,25 +3550,27 @@ var DbCollectionRefImpl = class {
3490
3550
  this._bus.emit([{
3491
3551
  type: "added",
3492
3552
  collection: this._collection,
3493
- doc: { id: docId, data }
3553
+ doc: { id: docId, data: localData }
3494
3554
  }]);
3495
3555
  return { ok: true, id: docId };
3496
3556
  }
3497
3557
  async set(id, data) {
3498
3558
  assertValidId(id);
3499
3559
  const existing = await this._store.getDoc(this._collection, id);
3560
+ const rawData = data;
3561
+ const localData = resolveSentinelsLocally(rawData, void 0);
3500
3562
  const mutation = await this._queue.enqueue({
3501
3563
  collection: this._collection,
3502
3564
  docId: id,
3503
3565
  op: "set",
3504
- payload: data,
3566
+ payload: rawData,
3505
3567
  baseVersion: existing?.meta.serverVersion ?? null,
3506
3568
  batchId: null
3507
3569
  });
3508
3570
  await this._store.putDoc({
3509
3571
  collection: this._collection,
3510
3572
  id,
3511
- data,
3573
+ data: localData,
3512
3574
  meta: {
3513
3575
  serverVersion: existing?.meta.serverVersion ?? null,
3514
3576
  updatedAtServer: existing?.meta.updatedAtServer ?? null,
@@ -3522,19 +3584,22 @@ var DbCollectionRefImpl = class {
3522
3584
  this._bus.emit([{
3523
3585
  type: existing && !existing.meta.deleted ? "modified" : "added",
3524
3586
  collection: this._collection,
3525
- doc: { id, data }
3587
+ doc: { id, data: localData }
3526
3588
  }]);
3527
3589
  return { ok: true };
3528
3590
  }
3529
3591
  async update(id, data) {
3530
3592
  assertValidId(id);
3531
3593
  const existing = await this._store.getDoc(this._collection, id);
3532
- const mergedData = existing?.meta.deleted ? { ...data } : { ...existing?.data ?? {}, ...data };
3594
+ const rawData = data;
3595
+ const existingBase = existing?.meta.deleted ? void 0 : existing?.data;
3596
+ const localData = resolveSentinelsLocally(rawData, existingBase);
3597
+ const mergedData = existing?.meta.deleted ? { ...localData } : { ...existing?.data ?? {}, ...localData };
3533
3598
  const mutation = await this._queue.enqueue({
3534
3599
  collection: this._collection,
3535
3600
  docId: id,
3536
3601
  op: "update",
3537
- payload: data,
3602
+ payload: rawData,
3538
3603
  baseVersion: existing?.meta.serverVersion ?? null,
3539
3604
  batchId: null
3540
3605
  });
@@ -3591,6 +3656,7 @@ var DbCollectionRefImpl = class {
3591
3656
  const collection = this._collection;
3592
3657
  if (op.kind === "add") {
3593
3658
  const data = op.data;
3659
+ const localData = resolveSentinelsLocally(data, void 0);
3594
3660
  const mutation = await this._queue.enqueue({
3595
3661
  collection,
3596
3662
  op: "add",
@@ -3602,7 +3668,7 @@ var DbCollectionRefImpl = class {
3602
3668
  await this._store.putDoc({
3603
3669
  collection,
3604
3670
  id: docId,
3605
- data,
3671
+ data: localData,
3606
3672
  meta: {
3607
3673
  serverVersion: null,
3608
3674
  updatedAtServer: null,
@@ -3613,11 +3679,12 @@ var DbCollectionRefImpl = class {
3613
3679
  ownerId: null
3614
3680
  }
3615
3681
  });
3616
- busChanges.push({ type: "added", collection, doc: { id: docId, data } });
3682
+ busChanges.push({ type: "added", collection, doc: { id: docId, data: localData } });
3617
3683
  } else if (op.kind === "set") {
3618
3684
  const id = op.id;
3619
3685
  const data = op.data;
3620
3686
  const existing = await this._store.getDoc(collection, id);
3687
+ const localData = resolveSentinelsLocally(data, void 0);
3621
3688
  const mutation = await this._queue.enqueue({
3622
3689
  collection,
3623
3690
  docId: id,
@@ -3629,7 +3696,7 @@ var DbCollectionRefImpl = class {
3629
3696
  await this._store.putDoc({
3630
3697
  collection,
3631
3698
  id,
3632
- data,
3699
+ data: localData,
3633
3700
  meta: {
3634
3701
  serverVersion: existing?.meta.serverVersion ?? null,
3635
3702
  updatedAtServer: existing?.meta.updatedAtServer ?? null,
@@ -3643,13 +3710,15 @@ var DbCollectionRefImpl = class {
3643
3710
  busChanges.push({
3644
3711
  type: existing && !existing.meta.deleted ? "modified" : "added",
3645
3712
  collection,
3646
- doc: { id, data }
3713
+ doc: { id, data: localData }
3647
3714
  });
3648
3715
  } else if (op.kind === "update") {
3649
3716
  const id = op.id;
3650
3717
  const data = op.data;
3651
3718
  const existing = await this._store.getDoc(collection, id);
3652
- const merged = { ...existing?.data ?? {}, ...data };
3719
+ const existingBase = existing?.meta.deleted ? void 0 : existing?.data;
3720
+ const localData = resolveSentinelsLocally(data, existingBase);
3721
+ const merged = { ...existing?.data ?? {}, ...localData };
3653
3722
  const mutation = await this._queue.enqueue({
3654
3723
  collection,
3655
3724
  docId: id,