@neetru/sdk 3.7.0 → 3.8.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 (73) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +66 -1
  3. package/dist/ai.cjs +221 -177
  4. package/dist/ai.d.cts +1 -1
  5. package/dist/ai.d.ts +1 -1
  6. package/dist/ai.mjs +221 -177
  7. package/dist/auth.cjs +1227 -1064
  8. package/dist/auth.d.cts +1 -1
  9. package/dist/auth.d.ts +1 -1
  10. package/dist/auth.mjs +1224 -1061
  11. package/dist/catalog.cjs +164 -97
  12. package/dist/catalog.d.cts +1 -1
  13. package/dist/catalog.d.ts +1 -1
  14. package/dist/catalog.mjs +164 -97
  15. package/dist/checkout.cjs +182 -115
  16. package/dist/checkout.d.cts +1 -1
  17. package/dist/checkout.d.ts +1 -1
  18. package/dist/checkout.mjs +181 -114
  19. package/dist/db-react.d.cts +1 -1
  20. package/dist/db-react.d.ts +1 -1
  21. package/dist/db.cjs +370 -298
  22. package/dist/db.d.cts +1 -1
  23. package/dist/db.d.ts +1 -1
  24. package/dist/db.mjs +362 -290
  25. package/dist/entitlements.cjs +17 -10
  26. package/dist/entitlements.d.cts +1 -1
  27. package/dist/entitlements.d.ts +1 -1
  28. package/dist/entitlements.mjs +17 -10
  29. package/dist/errors.cjs +15 -8
  30. package/dist/errors.d.cts +24 -3
  31. package/dist/errors.d.ts +24 -3
  32. package/dist/errors.mjs +14 -7
  33. package/dist/firestore-compat.d.cts +1 -1
  34. package/dist/firestore-compat.d.ts +1 -1
  35. package/dist/index.cjs +1409 -1075
  36. package/dist/index.d.cts +177 -3
  37. package/dist/index.d.ts +177 -3
  38. package/dist/index.mjs +1544 -1217
  39. package/dist/mobile.cjs +400 -181
  40. package/dist/mobile.d.cts +59 -4
  41. package/dist/mobile.d.ts +59 -4
  42. package/dist/mobile.mjs +400 -181
  43. package/dist/mocks.cjs +9 -2
  44. package/dist/mocks.d.cts +1 -1
  45. package/dist/mocks.d.ts +1 -1
  46. package/dist/mocks.mjs +9 -2
  47. package/dist/notifications.cjs +197 -130
  48. package/dist/notifications.d.cts +1 -1
  49. package/dist/notifications.d.ts +1 -1
  50. package/dist/notifications.mjs +196 -129
  51. package/dist/react.cjs +303 -282
  52. package/dist/react.d.cts +1 -1
  53. package/dist/react.d.ts +1 -1
  54. package/dist/react.mjs +295 -274
  55. package/dist/support.cjs +299 -229
  56. package/dist/support.d.cts +8 -4
  57. package/dist/support.d.ts +8 -4
  58. package/dist/support.mjs +298 -228
  59. package/dist/telemetry.cjs +234 -167
  60. package/dist/telemetry.d.cts +1 -1
  61. package/dist/telemetry.d.ts +1 -1
  62. package/dist/telemetry.mjs +234 -167
  63. package/dist/{types-Bx4kTLmk.d.cts → types-D-9bjlM0.d.cts} +48 -1
  64. package/dist/{types-Dx0IrOSF.d.ts → types-bVztFQTI.d.ts} +48 -1
  65. package/dist/usage.cjs +183 -116
  66. package/dist/usage.d.cts +1 -1
  67. package/dist/usage.d.ts +1 -1
  68. package/dist/usage.mjs +183 -116
  69. package/dist/webhooks.cjs +215 -148
  70. package/dist/webhooks.d.cts +1 -1
  71. package/dist/webhooks.d.ts +1 -1
  72. package/dist/webhooks.mjs +212 -145
  73. package/package.json +2 -1
package/dist/catalog.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/errors.ts
2
- var i = class t extends Error {
2
+ var a = class t extends Error {
3
3
  code;
4
4
  status;
5
5
  requestId;
@@ -12,98 +12,161 @@ var i = class t extends Error {
12
12
  * para um `NeetruDbErrorCode` mais específico (recuperável vs não).
13
13
  */
14
14
  reason;
15
- constructor(e, r, n, a, s) {
16
- super(r), this.name = "NeetruError", this.code = e, this.status = n, this.requestId = a, this.reason = s, Object.setPrototypeOf(this, t.prototype);
15
+ /**
16
+ * 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
17
+ * chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
18
+ * mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
19
+ * `undefined` em erros que acontecem antes de qualquer requisição.
20
+ */
21
+ correlationId;
22
+ constructor(e, r, n, o, s, u) {
23
+ super(r), this.name = "NeetruError", this.code = e, this.status = n, this.requestId = o, this.reason = s, this.correlationId = u, Object.setPrototypeOf(this, t.prototype);
17
24
  }
18
25
  };
19
26
 
20
27
  // src/idempotency.ts
21
- function T() {
28
+ function C() {
22
29
  let t = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
23
30
  if (t && typeof t.randomUUID == "function")
24
31
  return t.randomUUID();
25
- throw new i(
32
+ throw new a(
26
33
  "runtime_unsupported",
27
34
  "crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
28
35
  );
29
36
  }
30
37
 
38
+ // src/correlation.ts
39
+ var T = "x-correlation-id";
40
+ var $ = /^[A-Za-z0-9._:\-]+$/;
41
+ function I(t) {
42
+ return typeof t == "string" && t.length > 0 && t.length <= 128 && $.test(t);
43
+ }
44
+ function j() {
45
+ let t = globalThis.crypto;
46
+ try {
47
+ if (t && typeof t.randomUUID == "function") return t.randomUUID();
48
+ } catch {
49
+ }
50
+ let e = new Uint8Array(16), r = false;
51
+ try {
52
+ t && typeof t.getRandomValues == "function" && (t.getRandomValues(e), r = !0);
53
+ } catch {
54
+ r = false;
55
+ }
56
+ if (!r)
57
+ for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
58
+ e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
59
+ let n = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
60
+ return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
61
+ }
62
+ function N(t, e) {
63
+ let r, n = {};
64
+ for (let [s, u] of Object.entries(t)) {
65
+ if (s.toLowerCase() === T) {
66
+ I(u) && (r = u);
67
+ continue;
68
+ }
69
+ n[s] = u;
70
+ }
71
+ let o = r ?? (I(e) ? e : void 0) ?? j();
72
+ return n[T] = o, { headers: n, correlationId: o };
73
+ }
74
+
75
+ // src/server-only-key.ts
76
+ function S() {
77
+ try {
78
+ let t = globalThis.navigator;
79
+ if (t && t.product === "ReactNative") return "react-native";
80
+ } catch {
81
+ }
82
+ try {
83
+ let t = globalThis;
84
+ if (typeof t.window < "u" && typeof t.document < "u") return "browser";
85
+ } catch {
86
+ }
87
+ return null;
88
+ }
89
+
31
90
  // src/http.ts
32
- var q = {
91
+ var H = {
33
92
  // SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
34
93
  // runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
35
94
  3: "2026.05.1"
36
95
  };
37
- function R(t) {
96
+ function b(t) {
38
97
  return t.split("+")[0] ?? t;
39
98
  }
40
- function O(t, e) {
41
- let r = R(t).split(".").map(Number), n = R(e).split(".").map(Number), a = Math.max(r.length, n.length);
42
- for (let s = 0; s < a; s++) {
43
- let d = r[s] ?? 0, f = n[s] ?? 0;
44
- if (d !== f) return d - f;
99
+ function G(t, e) {
100
+ let r = b(t).split(".").map(Number), n = b(e).split(".").map(Number), o = Math.max(r.length, n.length);
101
+ for (let s = 0; s < o; s++) {
102
+ let u = r[s] ?? 0, c = n[s] ?? 0;
103
+ if (u !== c) return u - c;
45
104
  }
46
105
  return 0;
47
106
  }
48
- function N() {
107
+ function M() {
49
108
  try {
50
- return "3.7.0";
109
+ return "3.8.0";
51
110
  } catch {
52
111
  return;
53
112
  }
54
113
  }
55
- function U() {
56
- let t = N();
114
+ function V() {
115
+ let t = M();
57
116
  return t ? parseInt(t.split(".")[0] ?? "3", 10) : 3;
58
117
  }
59
- var A = /* @__PURE__ */ new Set();
60
- function K(t) {
118
+ var D = /* @__PURE__ */ new Set();
119
+ function F(t) {
61
120
  if (!t) return;
62
- let e = U(), r = q[e];
121
+ let e = V(), r = H[e];
63
122
  if (!r) return;
64
- let n = R(t);
65
- if (O(n, r) < 0) {
66
- let a = `${e}:${n}`;
67
- A.has(a) || (A.add(a), console.warn(
123
+ let n = b(t);
124
+ if (G(n, r) < 0) {
125
+ let o = `${e}:${n}`;
126
+ D.has(o) || (D.add(o), console.warn(
68
127
  `[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${t}" \xE9 anterior ao m\xEDnimo recomendado "${r}" para SDK major ${e}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
69
128
  ));
70
129
  }
71
130
  }
72
- var $ = 2, j = 0, L = /* @__PURE__ */ new Set(["GET", "HEAD"]), H = /* @__PURE__ */ new Set([
131
+ function z() {
132
+ let t = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
133
+ return S() ? "Esta opera\xE7\xE3o exige a credencial nrt_ do produto, que \xE9 S\xD3 DE SERVIDOR. No navegador/app, chame a API do seu produto (ex.: https://<produto>.neetru.com/api/v1) com o token do usu\xE1rio \u2014 o backend do produto faz esta chamada com o SDK e a chave. (" + t + ")" : t;
134
+ }
135
+ var B = 2, X = 0, Y = /* @__PURE__ */ new Set(["GET", "HEAD"]), J = /* @__PURE__ */ new Set([
73
136
  "rate_limited",
74
137
  "server_error",
75
138
  "network_error"
76
139
  ]);
77
- function v(t) {
140
+ function x(t) {
78
141
  let e = 200 * Math.pow(4, t), r = e * 0.2 * (Math.random() * 2 - 1);
79
142
  return Math.max(50, Math.round(e + r));
80
143
  }
81
- var P = 6e4;
82
- function G(t) {
144
+ var O = 6e4;
145
+ function W(t) {
83
146
  if (!t) return null;
84
147
  let e = Number(t);
85
- if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), P);
148
+ if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), O);
86
149
  let r = Date.parse(t);
87
150
  if (Number.isFinite(r)) {
88
151
  let n = r - Date.now();
89
- if (n > 0) return Math.min(n, P);
152
+ if (n > 0) return Math.min(n, O);
90
153
  }
91
154
  return null;
92
155
  }
93
- function x(t) {
156
+ function P(t) {
94
157
  return new Promise((e) => setTimeout(e, t));
95
158
  }
96
- function F(t) {
159
+ function Z(t) {
97
160
  return t === 401 ? "unauthorized" : t === 403 ? "forbidden" : t === 404 ? "not_found" : t === 409 ? "conflict" : t === 410 ? "gone" : t === 422 || t === 400 ? "validation_failed" : t === 429 ? "rate_limited" : t >= 500 ? "server_error" : "unknown";
98
161
  }
99
- function z(t, e, r) {
100
- let n = t.replace(/\/+$/, ""), a = e.startsWith("/") ? e : `/${e}`, s = new URL(`${n}${a}`);
162
+ function Q(t, e, r) {
163
+ let n = t.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, s = new URL(`${n}${o}`);
101
164
  if (r)
102
- for (let [d, f] of Object.entries(r))
103
- f !== void 0 && s.searchParams.set(d, String(f));
165
+ for (let [u, c] of Object.entries(r))
166
+ c !== void 0 && s.searchParams.set(u, String(c));
104
167
  return s.toString();
105
168
  }
106
- async function D(t) {
169
+ async function K(t) {
107
170
  let e = await t.text();
108
171
  if (e)
109
172
  try {
@@ -113,82 +176,86 @@ async function D(t) {
113
176
  }
114
177
  }
115
178
  async function E(t, e) {
116
- let r = e.method ?? "GET", n = z(t.baseUrl, e.path, e.query), a = L.has(r) ? $ : j, s = e.retries ?? a, d = {
117
- accept: "application/json",
118
- ...e.headers
119
- }, f = N();
120
- if (f && (d["x-neetru-sdk-version"] = f), e.requireAuth) {
179
+ let r = e.method ?? "GET", n = Q(t.baseUrl, e.path, e.query), o = Y.has(r) ? B : X, s = e.retries ?? o, { headers: u, correlationId: c } = N(
180
+ { accept: "application/json", ...e.headers },
181
+ e.correlationId
182
+ ), v = M();
183
+ if (v && (u["x-neetru-sdk-version"] = v), e.requireAuth) {
121
184
  if (!t.apiKey)
122
- throw new i(
123
- "missing_api_key",
124
- "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
125
- );
126
- d.authorization = `Bearer ${t.apiKey}`;
185
+ throw new a("missing_api_key", z());
186
+ u.authorization = `Bearer ${t.apiKey}`;
127
187
  }
128
188
  if (e.idempotencyKey) {
129
- let l = typeof e.idempotencyKey == "string" ? e.idempotencyKey : T();
130
- d["idempotency-key"] = l;
189
+ let p = typeof e.idempotencyKey == "string" ? e.idempotencyKey : C();
190
+ u["idempotency-key"] = p;
131
191
  }
132
- let w = e.body !== void 0 && r !== "GET" && r !== "DELETE" ? JSON.stringify(e.body) : void 0;
133
- w !== void 0 && (d["content-type"] = "application/json");
134
- let m = null, p = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
135
- for (let l = 0; l <= s; l++) {
136
- if (p != null && Date.now() >= p)
137
- throw m ?? new i("network_error", "Deadline excedido antes de completar a request");
138
- let b = { method: r, headers: d };
139
- w !== void 0 && (b.body = w);
140
- let M = p != null ? Math.max(1, Math.min(3e4, p - Date.now())) : 3e4;
141
- b.signal = AbortSignal.timeout(M);
142
- let u;
192
+ let _ = e.body !== void 0 && r !== "GET" && r !== "DELETE" ? JSON.stringify(e.body) : void 0;
193
+ _ !== void 0 && (u["content-type"] = "application/json");
194
+ let g = null, l = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
195
+ for (let p = 0; p <= s; p++) {
196
+ if (l != null && Date.now() >= l)
197
+ throw g ?? new a(
198
+ "network_error",
199
+ "Deadline excedido antes de completar a request",
200
+ void 0,
201
+ void 0,
202
+ void 0,
203
+ c
204
+ );
205
+ let w = { method: r, headers: u };
206
+ _ !== void 0 && (w.body = _);
207
+ let L = l != null ? Math.max(1, Math.min(3e4, l - Date.now())) : 3e4;
208
+ w.signal = AbortSignal.timeout(L);
209
+ let d;
143
210
  try {
144
- u = await t.fetch(n, b);
145
- } catch (o) {
146
- let y = o instanceof DOMException && o.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${o instanceof Error ? o.message : "fetch failed"}`;
147
- if (m = new i("network_error", y), l < s) {
148
- let C = v(l);
149
- if (p == null || Date.now() + C < p) {
150
- await x(C);
211
+ d = await t.fetch(n, w);
212
+ } catch (i) {
213
+ let y = i instanceof DOMException && i.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${i instanceof Error ? i.message : "fetch failed"}`;
214
+ if (g = new a("network_error", y, void 0, void 0, void 0, c), p < s) {
215
+ let A = x(p);
216
+ if (l == null || Date.now() + A < l) {
217
+ await P(A);
151
218
  continue;
152
219
  }
153
220
  }
154
- throw m;
221
+ throw g;
155
222
  }
156
- let I = u.headers.get("x-request-id") ?? u.headers.get("x-correlation-id") ?? void 0;
157
- if (u.ok) {
158
- let o = u.headers.get("x-neetru-version");
159
- return o && K(o), await D(u);
223
+ let q = d.headers.get("x-request-id") ?? d.headers.get("x-correlation-id") ?? void 0;
224
+ if (d.ok) {
225
+ let i = d.headers.get("x-neetru-version");
226
+ return i && F(i), await K(d);
160
227
  }
161
- let c = await D(u), h = F(u.status), g = `HTTP ${u.status}`, _;
162
- if (c && typeof c == "object") {
163
- let o = c.error;
164
- typeof o == "string" ? g = o : o && typeof o == "object" ? (typeof o.code == "string" && (h = o.code), typeof o.message == "string" && (g = o.message), typeof o.reason == "string" && (_ = o.reason)) : (typeof c.code == "string" && (h = c.code), typeof c.message == "string" && (g = c.message)), typeof c.reason == "string" && (_ = c.reason);
228
+ let f = await K(d), h = Z(d.status), m = `HTTP ${d.status}`, R;
229
+ if (f && typeof f == "object") {
230
+ let i = f.error;
231
+ typeof i == "string" ? m = i : i && typeof i == "object" ? (typeof i.code == "string" && (h = i.code), typeof i.message == "string" && (m = i.message), typeof i.reason == "string" && (R = i.reason)) : (typeof f.code == "string" && (h = f.code), typeof f.message == "string" && (m = f.message)), typeof f.reason == "string" && (R = f.reason);
165
232
  }
166
- if (u.status === 410) {
167
- let o = u.headers.get("x-neetru-replacement");
168
- g === `HTTP ${u.status}` && (g = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), o && !g.includes(o) && (g += ` Substituto: ${o}.`);
233
+ if (d.status === 410) {
234
+ let i = d.headers.get("x-neetru-replacement");
235
+ m === `HTTP ${d.status}` && (m = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), i && !m.includes(i) && (m += ` Substituto: ${i}.`);
169
236
  }
170
- let k = new i(h, g, u.status, I, _);
171
- if (m = k, H.has(h) && l < s) {
172
- let y = G(u.headers.get("retry-after")) ?? v(l);
173
- if (p == null || Date.now() + y < p) {
174
- await x(y);
237
+ let k = new a(h, m, d.status, q, R, c);
238
+ if (g = k, J.has(h) && p < s) {
239
+ let y = W(d.headers.get("retry-after")) ?? x(p);
240
+ if (l == null || Date.now() + y < l) {
241
+ await P(y);
175
242
  continue;
176
243
  }
177
244
  }
178
245
  throw k;
179
246
  }
180
- throw m ?? new i("unknown", "unexpected httpRequest exit");
247
+ throw g ?? new a("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, c);
181
248
  }
182
249
 
183
250
  // src/catalog.ts
184
- function S(t) {
251
+ function U(t) {
185
252
  if (!t || typeof t != "object")
186
- throw new i("invalid_response", "Catalog response item is not an object");
253
+ throw new a("invalid_response", "Catalog response item is not an object");
187
254
  let e = t;
188
255
  if (typeof e.slug != "string" || !e.slug)
189
- throw new i("invalid_response", "Catalog product missing slug");
256
+ throw new a("invalid_response", "Catalog product missing slug");
190
257
  if (typeof e.name != "string" || !e.name)
191
- throw new i("invalid_response", "Catalog product missing name");
258
+ throw new a("invalid_response", "Catalog product missing name");
192
259
  let r = {
193
260
  slug: e.slug,
194
261
  name: e.name
@@ -196,11 +263,11 @@ function S(t) {
196
263
  return typeof e.tagline == "string" && (r.tagline = e.tagline), typeof e.description == "string" && (r.description = e.description), (e.status === "live" || e.status === "soon" || e.status === "beta") && (r.status = e.status), typeof e.iconKey == "string" && (r.iconKey = e.iconKey), typeof e.ctaHref == "string" && (r.ctaHref = e.ctaHref), typeof e.ctaLabel == "string" && (r.ctaLabel = e.ctaLabel), Array.isArray(e.plans) && (r.plans = e.plans.filter(
197
264
  (n) => typeof n == "object" && n !== null && typeof n.id == "string" && typeof n.name == "string"
198
265
  ).map((n) => {
199
- let a = { id: n.id, name: n.name };
200
- return typeof n.amountCents == "number" && (a.amountCents = n.amountCents), Array.isArray(n.features) && n.features.every((s) => typeof s == "string") && (a.features = n.features), a;
266
+ let o = { id: n.id, name: n.name };
267
+ return typeof n.amountCents == "number" && (o.amountCents = n.amountCents), Array.isArray(n.features) && n.features.every((s) => typeof s == "string") && (o.features = n.features), o;
201
268
  })), r;
202
269
  }
203
- function te(t) {
270
+ function pe(t) {
204
271
  return {
205
272
  /**
206
273
  * Lista produtos publicados. Por default só `published=true`; staff
@@ -212,12 +279,12 @@ function te(t) {
212
279
  path: "/api/sdk/v1/catalog"
213
280
  });
214
281
  if (!r || !Array.isArray(r.products))
215
- throw new i(
282
+ throw new a(
216
283
  "invalid_response",
217
284
  "Catalog list response missing products array"
218
285
  );
219
286
  return {
220
- products: r.products.map(S),
287
+ products: r.products.map(U),
221
288
  fetchedAt: typeof r.fetchedAt == "string" ? r.fetchedAt : (/* @__PURE__ */ new Date()).toISOString()
222
289
  };
223
290
  },
@@ -228,19 +295,19 @@ function te(t) {
228
295
  */
229
296
  async get(e) {
230
297
  if (!e || typeof e != "string")
231
- throw new i("validation_failed", "slug is required");
298
+ throw new a("validation_failed", "slug is required");
232
299
  let r = await E(t, {
233
300
  method: "GET",
234
301
  path: `/api/sdk/v1/catalog/${encodeURIComponent(e)}`
235
302
  });
236
303
  if (!r || !r.product)
237
- throw new i(
304
+ throw new a(
238
305
  "invalid_response",
239
306
  "Catalog get response missing product"
240
307
  );
241
- return S(r.product);
308
+ return U(r.product);
242
309
  }
243
310
  };
244
311
  }
245
312
 
246
- export { te as createCatalogNamespace };
313
+ export { pe as createCatalogNamespace };