@neetru/sdk 3.1.0 → 3.1.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.
Files changed (95) hide show
  1. package/dist/auth.cjs +159 -15
  2. package/dist/auth.cjs.map +1 -1
  3. package/dist/auth.d.cts +1 -1
  4. package/dist/auth.d.ts +1 -1
  5. package/dist/auth.mjs +159 -15
  6. package/dist/auth.mjs.map +1 -1
  7. package/dist/catalog.cjs +50 -2
  8. package/dist/catalog.cjs.map +1 -1
  9. package/dist/catalog.d.cts +1 -1
  10. package/dist/catalog.d.ts +1 -1
  11. package/dist/catalog.mjs +50 -2
  12. package/dist/catalog.mjs.map +1 -1
  13. package/dist/checkout.cjs +52 -3
  14. package/dist/checkout.cjs.map +1 -1
  15. package/dist/checkout.d.cts +1 -1
  16. package/dist/checkout.d.ts +1 -1
  17. package/dist/checkout.mjs +52 -3
  18. package/dist/checkout.mjs.map +1 -1
  19. package/dist/db-react.cjs +3 -1
  20. package/dist/db-react.cjs.map +1 -1
  21. package/dist/db-react.d.cts +1 -1
  22. package/dist/db-react.d.ts +1 -1
  23. package/dist/db-react.mjs +3 -1
  24. package/dist/db-react.mjs.map +1 -1
  25. package/dist/db.cjs +93 -6
  26. package/dist/db.cjs.map +1 -1
  27. package/dist/db.d.cts +1 -1
  28. package/dist/db.d.ts +1 -1
  29. package/dist/db.mjs +93 -6
  30. package/dist/db.mjs.map +1 -1
  31. package/dist/entitlements.cjs +50 -2
  32. package/dist/entitlements.cjs.map +1 -1
  33. package/dist/entitlements.d.cts +1 -1
  34. package/dist/entitlements.d.ts +1 -1
  35. package/dist/entitlements.mjs +50 -2
  36. package/dist/entitlements.mjs.map +1 -1
  37. package/dist/errors.cjs.map +1 -1
  38. package/dist/errors.d.cts +3 -1
  39. package/dist/errors.d.ts +3 -1
  40. package/dist/errors.mjs.map +1 -1
  41. package/dist/firestore-compat.cjs +29 -4
  42. package/dist/firestore-compat.cjs.map +1 -1
  43. package/dist/firestore-compat.d.cts +28 -3
  44. package/dist/firestore-compat.d.ts +28 -3
  45. package/dist/firestore-compat.mjs +29 -4
  46. package/dist/firestore-compat.mjs.map +1 -1
  47. package/dist/index.cjs +195 -16
  48. package/dist/index.cjs.map +1 -1
  49. package/dist/index.d.cts +9 -3
  50. package/dist/index.d.ts +9 -3
  51. package/dist/index.mjs +194 -17
  52. package/dist/index.mjs.map +1 -1
  53. package/dist/mocks.cjs.map +1 -1
  54. package/dist/mocks.d.cts +1 -1
  55. package/dist/mocks.d.ts +1 -1
  56. package/dist/mocks.mjs.map +1 -1
  57. package/dist/notifications.cjs +50 -2
  58. package/dist/notifications.cjs.map +1 -1
  59. package/dist/notifications.d.cts +1 -1
  60. package/dist/notifications.d.ts +1 -1
  61. package/dist/notifications.mjs +50 -2
  62. package/dist/notifications.mjs.map +1 -1
  63. package/dist/react.cjs +2 -3
  64. package/dist/react.cjs.map +1 -1
  65. package/dist/react.d.cts +22 -4
  66. package/dist/react.d.ts +22 -4
  67. package/dist/react.mjs +2 -3
  68. package/dist/react.mjs.map +1 -1
  69. package/dist/support.cjs +50 -2
  70. package/dist/support.cjs.map +1 -1
  71. package/dist/support.d.cts +1 -1
  72. package/dist/support.d.ts +1 -1
  73. package/dist/support.mjs +50 -2
  74. package/dist/support.mjs.map +1 -1
  75. package/dist/telemetry.cjs +65 -4
  76. package/dist/telemetry.cjs.map +1 -1
  77. package/dist/telemetry.d.cts +1 -1
  78. package/dist/telemetry.d.ts +1 -1
  79. package/dist/telemetry.mjs +65 -4
  80. package/dist/telemetry.mjs.map +1 -1
  81. package/dist/{types-BRv8wBxX.d.ts → types-B3XFHD4A.d.ts} +60 -1
  82. package/dist/{types-nwErcRX8.d.cts → types-DePdSU3P.d.cts} +60 -1
  83. package/dist/usage.cjs +50 -2
  84. package/dist/usage.cjs.map +1 -1
  85. package/dist/usage.d.cts +1 -1
  86. package/dist/usage.d.ts +1 -1
  87. package/dist/usage.mjs +50 -2
  88. package/dist/usage.mjs.map +1 -1
  89. package/dist/webhooks.cjs +83 -2
  90. package/dist/webhooks.cjs.map +1 -1
  91. package/dist/webhooks.d.cts +1 -1
  92. package/dist/webhooks.d.ts +1 -1
  93. package/dist/webhooks.mjs +83 -3
  94. package/dist/webhooks.mjs.map +1 -1
  95. package/package.json +1 -1
package/dist/db.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export { i as ConflictRecord, j as CreateOfflineDocumentsOptions, l as DbBatchOp, m as DbChangeType, n as DbCollectionRef, o as DbDoc, p as DbDocRef, q as DbGetResult, r as DbListResult, t as DbQuery, u as DbSqlLease, w as DbWhereFilter, F as FieldSentinel, I as IncrementSentinel, N as NEETRU_SENTINEL_KEY, G as NeetruDb, H as NeetruDbDocuments, J as NeetruDbEngine, K as NeetruDbError, O as NeetruDbErrorCode, P as NeetruDbOptions, Q as NeetruDbTransport, S as NeetruSqlClient, $ as RealtimeTransport, a3 as ServerTimestampSentinel, a5 as SqlOptions, aa as SyncState, ap as WithFieldSentinels, ar as createNeetruDb, at as createRestSyncTransport, av as getWebSocketRealtimeClient, aw as increment, ax as isFieldSentinel, ay as isIncrementSentinel, az as isServerTimestampSentinel, aA as serverTimestamp } from './types-nwErcRX8.cjs';
1
+ export { i as ConflictRecord, j as CreateOfflineDocumentsOptions, l as DbBatchOp, m as DbChangeType, n as DbCollectionRef, o as DbDoc, p as DbDocRef, q as DbGetResult, r as DbListResult, t as DbQuery, u as DbSqlLease, w as DbWhereFilter, F as FieldSentinel, I as IncrementSentinel, N as NEETRU_SENTINEL_KEY, G as NeetruDb, H as NeetruDbDocuments, J as NeetruDbEngine, K as NeetruDbError, O as NeetruDbErrorCode, P as NeetruDbOptions, Q as NeetruDbTransport, S as NeetruSqlClient, $ as RealtimeTransport, a3 as ServerTimestampSentinel, a5 as SqlOptions, aa as SyncState, ar as WithFieldSentinels, at as createNeetruDb, av as createRestSyncTransport, ax as getWebSocketRealtimeClient, ay as increment, az as isFieldSentinel, aA as isIncrementSentinel, aB as isServerTimestampSentinel, aC as serverTimestamp } from './types-DePdSU3P.cjs';
2
2
  import 'drizzle-orm/node-postgres';
3
3
  import './errors.cjs';
package/dist/db.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { i as ConflictRecord, j as CreateOfflineDocumentsOptions, l as DbBatchOp, m as DbChangeType, n as DbCollectionRef, o as DbDoc, p as DbDocRef, q as DbGetResult, r as DbListResult, t as DbQuery, u as DbSqlLease, w as DbWhereFilter, F as FieldSentinel, I as IncrementSentinel, N as NEETRU_SENTINEL_KEY, G as NeetruDb, H as NeetruDbDocuments, J as NeetruDbEngine, K as NeetruDbError, O as NeetruDbErrorCode, P as NeetruDbOptions, Q as NeetruDbTransport, S as NeetruSqlClient, $ as RealtimeTransport, a3 as ServerTimestampSentinel, a5 as SqlOptions, aa as SyncState, ap as WithFieldSentinels, ar as createNeetruDb, at as createRestSyncTransport, av as getWebSocketRealtimeClient, aw as increment, ax as isFieldSentinel, ay as isIncrementSentinel, az as isServerTimestampSentinel, aA as serverTimestamp } from './types-BRv8wBxX.js';
1
+ export { i as ConflictRecord, j as CreateOfflineDocumentsOptions, l as DbBatchOp, m as DbChangeType, n as DbCollectionRef, o as DbDoc, p as DbDocRef, q as DbGetResult, r as DbListResult, t as DbQuery, u as DbSqlLease, w as DbWhereFilter, F as FieldSentinel, I as IncrementSentinel, N as NEETRU_SENTINEL_KEY, G as NeetruDb, H as NeetruDbDocuments, J as NeetruDbEngine, K as NeetruDbError, O as NeetruDbErrorCode, P as NeetruDbOptions, Q as NeetruDbTransport, S as NeetruSqlClient, $ as RealtimeTransport, a3 as ServerTimestampSentinel, a5 as SqlOptions, aa as SyncState, ar as WithFieldSentinels, at as createNeetruDb, av as createRestSyncTransport, ax as getWebSocketRealtimeClient, ay as increment, az as isFieldSentinel, aA as isIncrementSentinel, aB as isServerTimestampSentinel, aC as serverTimestamp } from './types-B3XFHD4A.js';
2
2
  import 'drizzle-orm/node-postgres';
3
3
  import './errors.js';
package/dist/db.mjs CHANGED
@@ -85,8 +85,47 @@ var init_idempotency = __esm({
85
85
  // src/http.ts
86
86
  var http_exports = {};
87
87
  __export(http_exports, {
88
+ checkCoreVersionCompat: () => checkCoreVersionCompat,
88
89
  httpRequest: () => httpRequest
89
90
  });
91
+ function stripRevision(v) {
92
+ return v.split("+")[0] ?? v;
93
+ }
94
+ function compareCalVer(a, b) {
95
+ const partsA = stripRevision(a).split(".").map(Number);
96
+ const partsB = stripRevision(b).split(".").map(Number);
97
+ const len = Math.max(partsA.length, partsB.length);
98
+ for (let i = 0; i < len; i++) {
99
+ const pa = partsA[i] ?? 0;
100
+ const pb = partsB[i] ?? 0;
101
+ if (pa !== pb) return pa - pb;
102
+ }
103
+ return 0;
104
+ }
105
+ function getSdkMajor() {
106
+ try {
107
+ const ver = globalThis.__NEETRU_SDK_VERSION__;
108
+ if (ver) return parseInt(ver.split(".")[0] ?? "3", 10);
109
+ } catch {
110
+ }
111
+ return 3;
112
+ }
113
+ function checkCoreVersionCompat(coreVersion) {
114
+ if (!coreVersion) return;
115
+ const sdkMajor = getSdkMajor();
116
+ const minCore = COMPAT_MATRIX[sdkMajor];
117
+ if (!minCore) return;
118
+ const stripped = stripRevision(coreVersion);
119
+ if (compareCalVer(stripped, minCore) < 0) {
120
+ const key = `${sdkMajor}:${stripped}`;
121
+ if (!_warnedCoreTooOld.has(key)) {
122
+ _warnedCoreTooOld.add(key);
123
+ console.warn(
124
+ `[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${coreVersion}" \xE9 anterior ao m\xEDnimo recomendado "${minCore}" para SDK major ${sdkMajor}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
125
+ );
126
+ }
127
+ }
128
+ }
90
129
  function backoffMs(attempt) {
91
130
  const base = 200 * Math.pow(4, attempt);
92
131
  const jitter = base * 0.2 * (Math.random() * 2 - 1);
@@ -95,11 +134,11 @@ function backoffMs(attempt) {
95
134
  function parseRetryAfter(value) {
96
135
  if (!value) return null;
97
136
  const secs = Number(value);
98
- if (Number.isFinite(secs) && secs >= 0) return Math.round(secs * 1e3);
137
+ if (Number.isFinite(secs) && secs >= 0) return Math.min(Math.round(secs * 1e3), MAX_RETRY_AFTER_MS);
99
138
  const dateMs = Date.parse(value);
100
139
  if (Number.isFinite(dateMs)) {
101
140
  const delta = dateMs - Date.now();
102
- if (delta > 0) return delta;
141
+ if (delta > 0) return Math.min(delta, MAX_RETRY_AFTER_MS);
103
142
  }
104
143
  return null;
105
144
  }
@@ -110,6 +149,7 @@ function statusToCode(status) {
110
149
  if (status === 401) return "unauthorized";
111
150
  if (status === 403) return "forbidden";
112
151
  if (status === 404) return "not_found";
152
+ if (status === 409) return "conflict";
113
153
  if (status === 422 || status === 400) return "validation_failed";
114
154
  if (status === 429) return "rate_limited";
115
155
  if (status >= 500) return "server_error";
@@ -181,6 +221,8 @@ async function httpRequest(config, opts) {
181
221
  }
182
222
  const requestId = res.headers.get("x-request-id") ?? res.headers.get("x-correlation-id") ?? void 0;
183
223
  if (res.ok) {
224
+ const coreVersion = res.headers.get("x-neetru-version");
225
+ if (coreVersion) checkCoreVersionCompat(coreVersion);
184
226
  const parsed = await safeJson(res);
185
227
  return parsed;
186
228
  }
@@ -209,11 +251,17 @@ async function httpRequest(config, opts) {
209
251
  }
210
252
  throw lastError ?? new NeetruError("unknown", "unexpected httpRequest exit");
211
253
  }
212
- var DEFAULT_RETRIES_READ, DEFAULT_RETRIES_WRITE, READ_METHODS, RETRYABLE_CODES2;
254
+ var COMPAT_MATRIX, _warnedCoreTooOld, DEFAULT_RETRIES_READ, DEFAULT_RETRIES_WRITE, READ_METHODS, RETRYABLE_CODES2, MAX_RETRY_AFTER_MS;
213
255
  var init_http = __esm({
214
256
  "src/http.ts"() {
215
257
  init_errors();
216
258
  init_idempotency();
259
+ COMPAT_MATRIX = {
260
+ // SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
261
+ // runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
262
+ 3: "2026.05.1"
263
+ };
264
+ _warnedCoreTooOld = /* @__PURE__ */ new Set();
217
265
  DEFAULT_RETRIES_READ = 2;
218
266
  DEFAULT_RETRIES_WRITE = 0;
219
267
  READ_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD"]);
@@ -222,6 +270,7 @@ var init_http = __esm({
222
270
  "server_error",
223
271
  "network_error"
224
272
  ]);
273
+ MAX_RETRY_AFTER_MS = 6e4;
225
274
  }
226
275
  });
227
276
 
@@ -788,6 +837,31 @@ var LocalStore = class {
788
837
  this.db?.close();
789
838
  this.db = null;
790
839
  }
840
+ /**
841
+ * Apaga completamente o banco IndexedDB e o recria do zero.
842
+ *
843
+ * Ao contrário de `close()` + `open()` (que reabre o banco existente sem
844
+ * apagar dados), `clearAll()` chama `indexedDB.deleteDatabase(dbName)` e
845
+ * só então chama `open()` — garantindo que nenhum documento, mutação ou
846
+ * metadado sobrevive.
847
+ *
848
+ * BUG fix: `clearCache()` em `NeetruDbDocumentsImpl` chamava close+open,
849
+ * que reabre o mesmo banco. `clearAll()` é o método correto.
850
+ */
851
+ async clearAll() {
852
+ if (this.db !== null) {
853
+ this.db.close();
854
+ this.db = null;
855
+ }
856
+ await new Promise((resolve, reject) => {
857
+ const req = indexedDB.deleteDatabase(this.dbName);
858
+ req.onsuccess = () => resolve();
859
+ req.onerror = () => reject(req.error);
860
+ req.onblocked = () => {
861
+ };
862
+ });
863
+ await this.open();
864
+ }
791
865
  assertOpen() {
792
866
  if (this.db === null) {
793
867
  throw new Error("LocalStore: banco n\xE3o est\xE1 aberto. Chame open() primeiro.");
@@ -995,6 +1069,12 @@ var MemoryStore = class {
995
1069
  async open() {
996
1070
  }
997
1071
  async close() {
1072
+ }
1073
+ /**
1074
+ * Apaga todos os dados em memória — equivalente ao `indexedDB.deleteDatabase`
1075
+ * que `LocalStore.clearAll()` executa em ambientes com IndexedDB.
1076
+ */
1077
+ async clearAll() {
998
1078
  this._docs.clear();
999
1079
  this._mutations.clear();
1000
1080
  this._meta.clear();
@@ -1520,6 +1600,8 @@ var SyncEngine = class {
1520
1600
  const phase1Ok = await this._phase1Push();
1521
1601
  if (!phase1Ok) {
1522
1602
  this._updateState({ status: "idle" });
1603
+ this._refreshPendingWrites().catch(() => {
1604
+ });
1523
1605
  this._syncing = false;
1524
1606
  return;
1525
1607
  }
@@ -1906,7 +1988,7 @@ var SyncEngine = class {
1906
1988
  ownerId: localDoc.meta.ownerId
1907
1989
  }
1908
1990
  };
1909
- const pendingMuts = await this._queue.drain();
1991
+ const pendingMuts = await this._queue.listAll();
1910
1992
  const docMut = pendingMuts.find(
1911
1993
  (m) => m.collection === serverDoc.collection && m.docId === serverDoc.id
1912
1994
  );
@@ -3260,8 +3342,13 @@ var NeetruDbDocumentsImpl = class {
3260
3342
  return this._engine.flush();
3261
3343
  }
3262
3344
  async clearCache() {
3263
- await this._store.close();
3264
- await this._store.open();
3345
+ const storeWithClear = this._store;
3346
+ if (typeof storeWithClear.clearAll === "function") {
3347
+ await storeWithClear.clearAll();
3348
+ } else {
3349
+ await this._store.close();
3350
+ await this._store.open();
3351
+ }
3265
3352
  }
3266
3353
  async getConflicts() {
3267
3354
  return this._store.listConflicts();