@neetru/sdk 3.8.1 → 3.9.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.
- package/CHANGELOG.md +33 -0
- package/README.md +4 -1
- package/dist/ai.cjs +1 -1
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +1 -1
- package/dist/auth.cjs +844 -803
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +841 -800
- package/dist/catalog.cjs +1 -1
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +1 -1
- package/dist/checkout.cjs +62 -49
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +62 -49
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +200 -187
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +193 -180
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +880 -839
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +847 -806
- package/dist/mobile.cjs +328 -276
- package/dist/mobile.d.cts +12 -2
- package/dist/mobile.d.ts +12 -2
- package/dist/mobile.mjs +328 -276
- package/dist/mocks.cjs +5 -5
- package/dist/mocks.d.cts +2 -2
- package/dist/mocks.d.ts +2 -2
- package/dist/mocks.mjs +4 -4
- package/dist/notifications.cjs +1 -1
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +1 -1
- package/dist/react.cjs +119 -119
- package/dist/react.d.cts +8 -2
- package/dist/react.d.ts +8 -2
- package/dist/react.mjs +119 -119
- package/dist/support.cjs +1 -1
- package/dist/support.d.cts +1 -1
- package/dist/support.d.ts +1 -1
- package/dist/support.mjs +1 -1
- package/dist/telemetry.cjs +62 -49
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +62 -49
- package/dist/{types-yv3EDKg2.d.cts → types-Bl_4HROd.d.cts} +62 -3
- package/dist/{types-BYPk_3HR.d.ts → types-Bqnsqvow.d.ts} +62 -3
- package/dist/usage.cjs +1 -1
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +1 -1
- package/dist/webhooks.cjs +1 -1
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +1 -1
- package/package.json +9 -1
package/dist/index.cjs
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
var jose = require('jose');
|
|
4
4
|
var idb = require('idb');
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var ur = Object.defineProperty;
|
|
7
7
|
var W = (n, e) => () => (n && (e = n(n = 0)), e);
|
|
8
|
-
var
|
|
8
|
+
var ht = (n, e) => {
|
|
9
9
|
for (var t in e)
|
|
10
|
-
|
|
10
|
+
ur(n, t, { get: e[t], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// src/errors.ts
|
|
14
|
-
exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var
|
|
14
|
+
exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var A = W(() => {
|
|
15
15
|
exports.NeetruError = class n extends Error {
|
|
16
16
|
code;
|
|
17
17
|
status;
|
|
@@ -43,7 +43,7 @@ exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var C =
|
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
// src/idempotency.ts
|
|
46
|
-
function
|
|
46
|
+
function Tt() {
|
|
47
47
|
let n = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
48
48
|
if (n && typeof n.randomUUID == "function")
|
|
49
49
|
return n.randomUUID();
|
|
@@ -52,15 +52,15 @@ function kt() {
|
|
|
52
52
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
53
53
|
);
|
|
54
54
|
}
|
|
55
|
-
var
|
|
56
|
-
|
|
55
|
+
var Rt = W(() => {
|
|
56
|
+
A();
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
// src/correlation.ts
|
|
60
|
-
function
|
|
61
|
-
return typeof n == "string" && n.length > 0 && n.length <= 128 &&
|
|
60
|
+
function Ct(n) {
|
|
61
|
+
return typeof n == "string" && n.length > 0 && n.length <= 128 && _r.test(n);
|
|
62
62
|
}
|
|
63
|
-
function
|
|
63
|
+
function br() {
|
|
64
64
|
let n = globalThis.crypto;
|
|
65
65
|
try {
|
|
66
66
|
if (n && typeof n.randomUUID == "function") return n.randomUUID();
|
|
@@ -78,24 +78,24 @@ function mr() {
|
|
|
78
78
|
let r = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
79
79
|
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
80
80
|
}
|
|
81
|
-
function
|
|
81
|
+
function be(n, e) {
|
|
82
82
|
let t, r = {};
|
|
83
83
|
for (let [i, s] of Object.entries(n)) {
|
|
84
|
-
if (i.toLowerCase() ===
|
|
85
|
-
|
|
84
|
+
if (i.toLowerCase() === It) {
|
|
85
|
+
Ct(s) && (t = s);
|
|
86
86
|
continue;
|
|
87
87
|
}
|
|
88
88
|
r[i] = s;
|
|
89
89
|
}
|
|
90
|
-
let o = t ?? (
|
|
91
|
-
return r[
|
|
90
|
+
let o = t ?? (Ct(e) ? e : void 0) ?? br();
|
|
91
|
+
return r[It] = o, { headers: r, correlationId: o };
|
|
92
92
|
}
|
|
93
|
-
var
|
|
94
|
-
|
|
93
|
+
var It, _r, $e = W(() => {
|
|
94
|
+
It = "x-correlation-id", _r = /^[A-Za-z0-9._:\-]+$/;
|
|
95
95
|
});
|
|
96
96
|
|
|
97
97
|
// src/server-only-key.ts
|
|
98
|
-
function
|
|
98
|
+
function J() {
|
|
99
99
|
try {
|
|
100
100
|
let n = globalThis.navigator;
|
|
101
101
|
if (n && n.product === "ReactNative") return "react-native";
|
|
@@ -108,108 +108,108 @@ function Q() {
|
|
|
108
108
|
}
|
|
109
109
|
return null;
|
|
110
110
|
}
|
|
111
|
-
function
|
|
112
|
-
let t =
|
|
111
|
+
function At(n, e) {
|
|
112
|
+
let t = vr[n];
|
|
113
113
|
return (`${e ? `[Neetru SDK] Recusado: a credencial nrt_ do produto foi passada ao SDK rodando ${t}.` : `[Neetru SDK] ATEN\xC7\xC3O: a credencial nrt_ do produto est\xE1 sendo usada ${t}.`} Essa chave \xE9 S\xD3 DE SERVIDOR \u2014 quem abrir o DevTools (ou descompactar o app) copia a chave e fala com o Core como se fosse o seu produto. Como corrigir: o site/app chama a API do SEU produto (ex.: https://<produto>.neetru.com/api/v1) com o token do usu\xE1rio (Authorization: Bearer <access_token>); o backend do produto valida com verifyAccessToken() e usa o SDK com a chave (NEETRU_API_KEY, sem prefixo NEXT_PUBLIC_/EXPO_PUBLIC_). ` + (e ? "" : "Nesta vers\xE3o \xE9 s\xF3 um aviso; use strictServerOnlyKey: true para bloquear j\xE1. Na pr\xF3xima vers\xE3o MAJOR do SDK isso passa a falhar sempre.")).trim();
|
|
114
114
|
}
|
|
115
|
-
function
|
|
115
|
+
function Pt(n) {
|
|
116
116
|
if (!n.hasApiKey) return;
|
|
117
|
-
let e = n.runtime === void 0 ?
|
|
117
|
+
let e = n.runtime === void 0 ? J() : n.runtime;
|
|
118
118
|
if (e) {
|
|
119
119
|
if (n.strict)
|
|
120
|
-
throw new exports.NeetruError("server_only_key",
|
|
121
|
-
if (!
|
|
122
|
-
|
|
120
|
+
throw new exports.NeetruError("server_only_key", At(e, true), void 0, void 0, e);
|
|
121
|
+
if (!Et) {
|
|
122
|
+
Et = true;
|
|
123
123
|
try {
|
|
124
|
-
typeof console < "u" && typeof console.warn == "function" && console.warn(
|
|
124
|
+
typeof console < "u" && typeof console.warn == "function" && console.warn(At(e, !1));
|
|
125
125
|
} catch {
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
var
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
var vr, Et, ve = W(() => {
|
|
131
|
+
A();
|
|
132
|
+
vr = {
|
|
133
133
|
browser: "no navegador",
|
|
134
134
|
"react-native": "num app React Native"
|
|
135
135
|
};
|
|
136
|
-
|
|
136
|
+
Et = false;
|
|
137
137
|
});
|
|
138
138
|
|
|
139
139
|
// src/http.ts
|
|
140
|
-
var
|
|
141
|
-
|
|
142
|
-
checkCoreVersionCompat: () =>
|
|
140
|
+
var re = {};
|
|
141
|
+
ht(re, {
|
|
142
|
+
checkCoreVersionCompat: () => ne,
|
|
143
143
|
httpRequest: () => S,
|
|
144
|
-
statusToCode: () =>
|
|
144
|
+
statusToCode: () => we
|
|
145
145
|
});
|
|
146
|
-
function
|
|
146
|
+
function Ve(n) {
|
|
147
147
|
return n.split("+")[0] ?? n;
|
|
148
148
|
}
|
|
149
|
-
function
|
|
150
|
-
let t =
|
|
149
|
+
function kr(n, e) {
|
|
150
|
+
let t = Ve(n).split(".").map(Number), r = Ve(e).split(".").map(Number), o = Math.max(t.length, r.length);
|
|
151
151
|
for (let i = 0; i < o; i++) {
|
|
152
152
|
let s = t[i] ?? 0, a = r[i] ?? 0;
|
|
153
153
|
if (s !== a) return s - a;
|
|
154
154
|
}
|
|
155
155
|
return 0;
|
|
156
156
|
}
|
|
157
|
-
function
|
|
157
|
+
function Ut() {
|
|
158
158
|
try {
|
|
159
|
-
return "3.
|
|
159
|
+
return "3.9.0";
|
|
160
160
|
} catch {
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
function
|
|
165
|
-
let n =
|
|
164
|
+
function Sr() {
|
|
165
|
+
let n = Ut();
|
|
166
166
|
return n ? parseInt(n.split(".")[0] ?? "3", 10) : 3;
|
|
167
167
|
}
|
|
168
|
-
function
|
|
168
|
+
function ne(n) {
|
|
169
169
|
if (!n) return;
|
|
170
|
-
let e =
|
|
170
|
+
let e = Sr(), t = wr[e];
|
|
171
171
|
if (!t) return;
|
|
172
|
-
let r =
|
|
173
|
-
if (
|
|
172
|
+
let r = Ve(n);
|
|
173
|
+
if (kr(r, t) < 0) {
|
|
174
174
|
let o = `${e}:${r}`;
|
|
175
|
-
|
|
175
|
+
Dt.has(o) || (Dt.add(o), console.warn(
|
|
176
176
|
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${n}" \xE9 anterior ao m\xEDnimo recomendado "${t}" para SDK major ${e}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
|
|
177
177
|
));
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
|
-
function
|
|
180
|
+
function Tr() {
|
|
181
181
|
let n = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
182
|
-
return
|
|
182
|
+
return J() ? "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. (" + n + ")" : n;
|
|
183
183
|
}
|
|
184
|
-
function
|
|
184
|
+
function Nt(n) {
|
|
185
185
|
let e = 200 * Math.pow(4, n), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
186
186
|
return Math.max(50, Math.round(e + t));
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function Er(n) {
|
|
189
189
|
if (!n) return null;
|
|
190
190
|
let e = Number(n);
|
|
191
|
-
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3),
|
|
191
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), xt);
|
|
192
192
|
let t = Date.parse(n);
|
|
193
193
|
if (Number.isFinite(t)) {
|
|
194
194
|
let r = t - Date.now();
|
|
195
|
-
if (r > 0) return Math.min(r,
|
|
195
|
+
if (r > 0) return Math.min(r, xt);
|
|
196
196
|
}
|
|
197
197
|
return null;
|
|
198
198
|
}
|
|
199
|
-
function
|
|
199
|
+
function Ot(n) {
|
|
200
200
|
return new Promise((e) => setTimeout(e, n));
|
|
201
201
|
}
|
|
202
|
-
function
|
|
202
|
+
function we(n) {
|
|
203
203
|
return n === 401 ? "unauthorized" : n === 403 ? "forbidden" : n === 404 ? "not_found" : n === 409 ? "conflict" : n === 410 ? "gone" : n === 422 || n === 400 ? "validation_failed" : n === 429 ? "rate_limited" : n >= 500 ? "server_error" : "unknown";
|
|
204
204
|
}
|
|
205
|
-
function
|
|
205
|
+
function Pr(n, e, t) {
|
|
206
206
|
let r = n.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${r}${o}`);
|
|
207
207
|
if (t)
|
|
208
208
|
for (let [s, a] of Object.entries(t))
|
|
209
209
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
210
210
|
return i.toString();
|
|
211
211
|
}
|
|
212
|
-
async function
|
|
212
|
+
async function Lt(n) {
|
|
213
213
|
let e = await n.text();
|
|
214
214
|
if (e)
|
|
215
215
|
try {
|
|
@@ -219,17 +219,17 @@ async function xt(n) {
|
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
async function S(n, e) {
|
|
222
|
-
let t = e.method ?? "GET", r =
|
|
222
|
+
let t = e.method ?? "GET", r = Pr(n.baseUrl, e.path, e.query), o = Cr.has(t) ? Rr : Ir, i = e.retries ?? o, { headers: s, correlationId: a } = be(
|
|
223
223
|
{ accept: "application/json", ...e.headers },
|
|
224
224
|
e.correlationId
|
|
225
|
-
), d =
|
|
225
|
+
), d = Ut();
|
|
226
226
|
if (d && (s["x-neetru-sdk-version"] = d), e.requireAuth) {
|
|
227
227
|
if (!n.apiKey)
|
|
228
|
-
throw new exports.NeetruError("missing_api_key",
|
|
228
|
+
throw new exports.NeetruError("missing_api_key", Tr());
|
|
229
229
|
s.authorization = `Bearer ${n.apiKey}`;
|
|
230
230
|
}
|
|
231
231
|
if (e.idempotencyKey) {
|
|
232
|
-
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey :
|
|
232
|
+
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey : Tt();
|
|
233
233
|
s["idempotency-key"] = f;
|
|
234
234
|
}
|
|
235
235
|
let p = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
@@ -249,69 +249,69 @@ async function S(n, e) {
|
|
|
249
249
|
p !== void 0 && (m.body = p);
|
|
250
250
|
let h = u != null ? Math.max(1, Math.min(3e4, u - Date.now())) : 3e4;
|
|
251
251
|
m.signal = AbortSignal.timeout(h);
|
|
252
|
-
let
|
|
252
|
+
let _;
|
|
253
253
|
try {
|
|
254
|
-
|
|
255
|
-
} catch (
|
|
256
|
-
let
|
|
257
|
-
if (l = new exports.NeetruError("network_error",
|
|
258
|
-
let x =
|
|
254
|
+
_ = await n.fetch(r, m);
|
|
255
|
+
} catch (g) {
|
|
256
|
+
let C = g instanceof DOMException && g.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${g instanceof Error ? g.message : "fetch failed"}`;
|
|
257
|
+
if (l = new exports.NeetruError("network_error", C, void 0, void 0, void 0, a), f < i) {
|
|
258
|
+
let x = Nt(f);
|
|
259
259
|
if (u == null || Date.now() + x < u) {
|
|
260
|
-
await
|
|
260
|
+
await Ot(x);
|
|
261
261
|
continue;
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
throw l;
|
|
265
265
|
}
|
|
266
|
-
let I =
|
|
267
|
-
if (
|
|
268
|
-
let
|
|
269
|
-
return
|
|
266
|
+
let I = _.headers.get("x-request-id") ?? _.headers.get("x-correlation-id") ?? void 0;
|
|
267
|
+
if (_.ok) {
|
|
268
|
+
let g = _.headers.get("x-neetru-version");
|
|
269
|
+
return g && ne(g), await Lt(_);
|
|
270
270
|
}
|
|
271
|
-
let w = await
|
|
271
|
+
let w = await Lt(_), k = we(_.status), D = `HTTP ${_.status}`, y;
|
|
272
272
|
if (w && typeof w == "object") {
|
|
273
|
-
let
|
|
274
|
-
typeof
|
|
273
|
+
let g = w.error;
|
|
274
|
+
typeof g == "string" ? D = g : g && typeof g == "object" ? (typeof g.code == "string" && (k = g.code), typeof g.message == "string" && (D = g.message), typeof g.reason == "string" && (y = g.reason)) : (typeof w.code == "string" && (k = w.code), typeof w.message == "string" && (D = w.message)), typeof w.reason == "string" && (y = w.reason);
|
|
275
275
|
}
|
|
276
|
-
if (
|
|
277
|
-
let
|
|
278
|
-
|
|
276
|
+
if (_.status === 410) {
|
|
277
|
+
let g = _.headers.get("x-neetru-replacement");
|
|
278
|
+
D === `HTTP ${_.status}` && (D = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), g && !D.includes(g) && (D += ` Substituto: ${g}.`);
|
|
279
279
|
}
|
|
280
|
-
let
|
|
281
|
-
if (l =
|
|
282
|
-
let
|
|
283
|
-
if (u == null || Date.now() +
|
|
284
|
-
await
|
|
280
|
+
let b = new exports.NeetruError(k, D, _.status, I, y, a);
|
|
281
|
+
if (l = b, Ar.has(k) && f < i) {
|
|
282
|
+
let C = Er(_.headers.get("retry-after")) ?? Nt(f);
|
|
283
|
+
if (u == null || Date.now() + C < u) {
|
|
284
|
+
await Ot(C);
|
|
285
285
|
continue;
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
|
-
throw
|
|
288
|
+
throw b;
|
|
289
289
|
}
|
|
290
290
|
throw l ?? new exports.NeetruError("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
291
291
|
}
|
|
292
|
-
var
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
var wr, Dt, Rr, Ir, Cr, Ar, xt, F = W(() => {
|
|
293
|
+
A();
|
|
294
|
+
Rt();
|
|
295
|
+
$e();
|
|
296
|
+
ve();
|
|
297
|
+
wr = {
|
|
298
298
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
299
299
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
300
300
|
3: "2026.05.1"
|
|
301
301
|
};
|
|
302
|
-
|
|
303
|
-
|
|
302
|
+
Dt = /* @__PURE__ */ new Set();
|
|
303
|
+
Rr = 2, Ir = 0, Cr = /* @__PURE__ */ new Set(["GET", "HEAD"]), Ar = /* @__PURE__ */ new Set([
|
|
304
304
|
"rate_limited",
|
|
305
305
|
"server_error",
|
|
306
306
|
"network_error"
|
|
307
307
|
]);
|
|
308
|
-
|
|
308
|
+
xt = 6e4;
|
|
309
309
|
});
|
|
310
310
|
|
|
311
311
|
// src/db-errors.ts
|
|
312
|
-
var
|
|
313
|
-
|
|
314
|
-
|
|
312
|
+
var Vr; exports.NeetruDbError = void 0; var H = W(() => {
|
|
313
|
+
A();
|
|
314
|
+
Vr = /* @__PURE__ */ new Set([
|
|
315
315
|
"db_unavailable",
|
|
316
316
|
"db_conflict",
|
|
317
317
|
"db_timeout"
|
|
@@ -329,16 +329,16 @@ var qr; exports.NeetruDbError = void 0; var z = W(() => {
|
|
|
329
329
|
*/
|
|
330
330
|
dbId;
|
|
331
331
|
constructor(e, t, r) {
|
|
332
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
332
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = Vr.has(e), this.dbId = r, Object.setPrototypeOf(this, n.prototype);
|
|
333
333
|
}
|
|
334
334
|
};
|
|
335
335
|
});
|
|
336
336
|
|
|
337
337
|
// src/db/sql/resilient-pool.ts
|
|
338
|
-
function
|
|
339
|
-
return typeof n == "object" && n !== null && n[
|
|
338
|
+
function Ne(n) {
|
|
339
|
+
return typeof n == "object" && n !== null && n[Ze] === true;
|
|
340
340
|
}
|
|
341
|
-
function
|
|
341
|
+
function po(n) {
|
|
342
342
|
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
343
343
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
344
344
|
return true;
|
|
@@ -350,17 +350,17 @@ function so(n) {
|
|
|
350
350
|
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
351
351
|
return r.length === 0 ? false : !!(r.includes("certificate expired") || r.includes("certificate has expired") || r.includes("cert_has_expired") || r.includes("alert certificate expired") || r.includes("err_ssl") || r.includes("sslv3 alert") || r.includes("tlsv1 alert") || r.includes("econnrefused") || r.includes("etimedout") || r.includes("enotfound") || r.includes("ehostunreach") || r.includes("getaddrinfo"));
|
|
352
352
|
}
|
|
353
|
-
function
|
|
353
|
+
function sn(n) {
|
|
354
354
|
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
355
|
-
if (typeof e == "string" &&
|
|
355
|
+
if (typeof e == "string" && fo.has(e.toUpperCase()))
|
|
356
356
|
return true;
|
|
357
357
|
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
358
358
|
return r.length === 0 ? false : r.includes("password authentication failed") || r.includes("sasl authentication failed");
|
|
359
359
|
}
|
|
360
|
-
function
|
|
361
|
-
return
|
|
360
|
+
function mo(n) {
|
|
361
|
+
return po(n) || sn(n);
|
|
362
362
|
}
|
|
363
|
-
function
|
|
363
|
+
function et(n) {
|
|
364
364
|
let e = { current: n }, t = null, r = {
|
|
365
365
|
__neetruSwapInner(l) {
|
|
366
366
|
let u = e.current;
|
|
@@ -388,10 +388,10 @@ function Ye(n) {
|
|
|
388
388
|
}
|
|
389
389
|
return await o(l, e.current, f);
|
|
390
390
|
}
|
|
391
|
-
})(), s = i("query",
|
|
391
|
+
})(), s = i("query", mo), a = i("connect", sn), d = Object.create(Object.getPrototypeOf(n));
|
|
392
392
|
return new Proxy(d, {
|
|
393
393
|
get(l, u) {
|
|
394
|
-
if (u ===
|
|
394
|
+
if (u === Ze) return true;
|
|
395
395
|
if (u === "__neetruSwapInner") return r.__neetruSwapInner;
|
|
396
396
|
if (u === "__neetruGetInner") return r.__neetruGetInner;
|
|
397
397
|
if (u === "__neetruSetReLease") return r.__neetruSetReLease;
|
|
@@ -407,16 +407,16 @@ function Ye(n) {
|
|
|
407
407
|
}
|
|
408
408
|
});
|
|
409
409
|
}
|
|
410
|
-
var
|
|
411
|
-
|
|
412
|
-
|
|
410
|
+
var Ze, fo, tt = W(() => {
|
|
411
|
+
Ze = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
412
|
+
fo = /* @__PURE__ */ new Set(["28P01", "28000"]);
|
|
413
413
|
});
|
|
414
414
|
|
|
415
415
|
// src/db/sql/lease.ts
|
|
416
|
-
function
|
|
416
|
+
function go(n, e) {
|
|
417
417
|
return n ? n.leaseId !== e.leaseId || n.credentialVersion !== e.credentialVersion || n.host !== e.host || n.port !== e.port || n.dbName !== e.dbName || n.user !== e.user || n.password !== e.password || n.sslca !== e.sslca || n.clientCert !== e.clientCert || n.clientKey !== e.clientKey : true;
|
|
418
418
|
}
|
|
419
|
-
function
|
|
419
|
+
function yo(n) {
|
|
420
420
|
if (n instanceof exports.NeetruDbError) return n;
|
|
421
421
|
let e = n instanceof Error ? n.message : String(n), t = n instanceof Error ? n.code : void 0;
|
|
422
422
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new exports.NeetruDbError(
|
|
@@ -424,10 +424,10 @@ function po(n) {
|
|
|
424
424
|
`Pool connection failed (${t}): ${e}`
|
|
425
425
|
) : e.includes("statement timeout") || e.includes("lock timeout") ? new exports.NeetruDbError("db_timeout", `Query timeout: ${e}`) : e.includes("40001") || e.includes("40P01") || e.includes("deadlock") ? new exports.NeetruDbError("db_conflict", `Transaction conflict: ${e}`) : e.includes("42501") || e.toLowerCase().includes("permission denied") ? new exports.NeetruDbError("db_permission_denied", `Permission denied: ${e}`) : new exports.NeetruDbError("db_unavailable", `Database error: ${e}`);
|
|
426
426
|
}
|
|
427
|
-
var
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
427
|
+
var ho, ee, ae, an = W(() => {
|
|
428
|
+
H();
|
|
429
|
+
tt();
|
|
430
|
+
ho = 0.8, ee = 3e4, ae = class {
|
|
431
431
|
/**
|
|
432
432
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
433
433
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -467,7 +467,7 @@ var lo, Z, se, on = W(() => {
|
|
|
467
467
|
isolationLevel: t.isolationLevel
|
|
468
468
|
}) : await this._orm.transaction(e);
|
|
469
469
|
} catch (r) {
|
|
470
|
-
throw
|
|
470
|
+
throw yo(r);
|
|
471
471
|
}
|
|
472
472
|
}
|
|
473
473
|
async close() {
|
|
@@ -485,13 +485,13 @@ var lo, Z, se, on = W(() => {
|
|
|
485
485
|
* usa `MIN_RENEWAL_DELAY_MS` como fallback seguro.
|
|
486
486
|
*/
|
|
487
487
|
_calcRenewalDelayMs() {
|
|
488
|
-
if (!this._lease) return
|
|
488
|
+
if (!this._lease) return ee;
|
|
489
489
|
let e = Date.parse(this._lease.expiresAt);
|
|
490
|
-
if (!Number.isFinite(e)) return
|
|
490
|
+
if (!Number.isFinite(e)) return ee;
|
|
491
491
|
let t = this._deps.now(), r = e - t;
|
|
492
|
-
if (r <= 0) return
|
|
493
|
-
let o = r * (1 -
|
|
494
|
-
return Math.max(
|
|
492
|
+
if (r <= 0) return ee;
|
|
493
|
+
let o = r * (1 - ho);
|
|
494
|
+
return Math.max(ee, Math.round(o));
|
|
495
495
|
}
|
|
496
496
|
_scheduleRenewal() {
|
|
497
497
|
if (this._closed) return;
|
|
@@ -518,8 +518,8 @@ var lo, Z, se, on = W(() => {
|
|
|
518
518
|
} catch (t) {
|
|
519
519
|
this._renewalTimer = setTimeout(() => {
|
|
520
520
|
this._doRenew();
|
|
521
|
-
},
|
|
522
|
-
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${
|
|
521
|
+
}, ee), console.warn(
|
|
522
|
+
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${ee}ms (o cert de cliente mTLS do lease expira ~15min ap\xF3s o \xFAltimo swap):`,
|
|
523
523
|
t instanceof Error ? t.message : t
|
|
524
524
|
);
|
|
525
525
|
return;
|
|
@@ -595,7 +595,7 @@ var lo, Z, se, on = W(() => {
|
|
|
595
595
|
"SqlLeaseManager foi fechado \u2014 aquisi\xE7\xE3o de lease inicial abortada."
|
|
596
596
|
);
|
|
597
597
|
if (this._lease)
|
|
598
|
-
return
|
|
598
|
+
return Ne(this._pool) ? this._pool.__neetruGetInner() : this._pool;
|
|
599
599
|
if (this._initInFlight !== null) return this._initInFlight;
|
|
600
600
|
let e = (async () => {
|
|
601
601
|
let t;
|
|
@@ -613,7 +613,7 @@ var lo, Z, se, on = W(() => {
|
|
|
613
613
|
let i = o instanceof Error ? o.message : String(o);
|
|
614
614
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${i}`);
|
|
615
615
|
}
|
|
616
|
-
return
|
|
616
|
+
return Ne(this._pool) ? this._pool.__neetruSwapInner(r).end().catch(() => {
|
|
617
617
|
}) : this._pool = r, this._lease = t, this._scheduleRenewal(), r;
|
|
618
618
|
})();
|
|
619
619
|
this._initInFlight = e;
|
|
@@ -629,12 +629,12 @@ var lo, Z, se, on = W(() => {
|
|
|
629
629
|
* Exposto como método protegido para testes.
|
|
630
630
|
*/
|
|
631
631
|
async _swapPool(e) {
|
|
632
|
-
if (!
|
|
632
|
+
if (!go(this._lease, e)) {
|
|
633
633
|
this._lease = e;
|
|
634
634
|
return;
|
|
635
635
|
}
|
|
636
636
|
let t = this._deps.createPool(e);
|
|
637
|
-
if (
|
|
637
|
+
if (Ne(this._pool)) {
|
|
638
638
|
let i = this._pool.__neetruSwapInner(t);
|
|
639
639
|
this._lease = e, i.end().catch(() => {
|
|
640
640
|
});
|
|
@@ -656,29 +656,29 @@ var lo, Z, se, on = W(() => {
|
|
|
656
656
|
});
|
|
657
657
|
|
|
658
658
|
// src/db/sql/sql-client.ts
|
|
659
|
-
var
|
|
660
|
-
|
|
661
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
662
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
663
|
-
DEFAULT_POOL_MAX: () =>
|
|
664
|
-
LEASE_ENDPOINT: () =>
|
|
665
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
666
|
-
createDrizzleHandle: () =>
|
|
667
|
-
createHttpLeaseFetcher: () =>
|
|
668
|
-
createPgPool: () =>
|
|
669
|
-
createSqlClientFromConfig: () =>
|
|
670
|
-
createSqlClientWithDeps: () =>
|
|
671
|
-
resolvePoolTuning: () =>
|
|
659
|
+
var hn = {};
|
|
660
|
+
ht(hn, {
|
|
661
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => fn,
|
|
662
|
+
DEFAULT_POOL_IDLE_MS: () => pn,
|
|
663
|
+
DEFAULT_POOL_MAX: () => un,
|
|
664
|
+
LEASE_ENDPOINT: () => cn,
|
|
665
|
+
LEASE_RENEW_ENDPOINT: () => ln,
|
|
666
|
+
createDrizzleHandle: () => wo,
|
|
667
|
+
createHttpLeaseFetcher: () => dn,
|
|
668
|
+
createPgPool: () => vo,
|
|
669
|
+
createSqlClientFromConfig: () => So,
|
|
670
|
+
createSqlClientWithDeps: () => mn,
|
|
671
|
+
resolvePoolTuning: () => rt
|
|
672
672
|
});
|
|
673
|
-
function
|
|
673
|
+
function _o(n) {
|
|
674
674
|
return n === "workspace" ? "staging" : n === "prod" ? "production" : "dev-local";
|
|
675
675
|
}
|
|
676
|
-
function
|
|
676
|
+
function dn(n) {
|
|
677
677
|
return async (e) => {
|
|
678
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
678
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (F(), re)), r = !!e?.leaseId, o = r ? ln : cn, i;
|
|
679
679
|
r ? i = { leaseId: e.leaseId } : (i = {
|
|
680
680
|
productId: n.productId ?? "",
|
|
681
|
-
environment:
|
|
681
|
+
environment: _o(n.env ?? "prod")
|
|
682
682
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
683
683
|
let s;
|
|
684
684
|
try {
|
|
@@ -704,10 +704,10 @@ function cn(n) {
|
|
|
704
704
|
`Falha ao ${d} lease SQL: ${p}`
|
|
705
705
|
);
|
|
706
706
|
}
|
|
707
|
-
return
|
|
707
|
+
return bo(s);
|
|
708
708
|
};
|
|
709
709
|
}
|
|
710
|
-
function
|
|
710
|
+
function bo(n) {
|
|
711
711
|
if (!n || typeof n != "object")
|
|
712
712
|
throw new exports.NeetruDbError("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
713
713
|
let t = n.lease;
|
|
@@ -739,22 +739,22 @@ function mo(n) {
|
|
|
739
739
|
expiresAt: o("expiresAt", "string")
|
|
740
740
|
};
|
|
741
741
|
}
|
|
742
|
-
function
|
|
742
|
+
function nt(n, e, t, r) {
|
|
743
743
|
let o = typeof process < "u" ? process.env?.[n] : void 0;
|
|
744
744
|
if (o === void 0 || o.trim() === "") return e;
|
|
745
745
|
let i = Number.parseInt(o.trim(), 10);
|
|
746
746
|
return !Number.isFinite(i) || i < t || i > r ? e : i;
|
|
747
747
|
}
|
|
748
|
-
function
|
|
748
|
+
function rt() {
|
|
749
749
|
return {
|
|
750
750
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
751
751
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
752
|
-
max:
|
|
752
|
+
max: nt("NEETRU_DB_POOL_MAX", un, 1, 100),
|
|
753
753
|
// Teto 15min = janela do cert; piso 1s.
|
|
754
|
-
idleTimeoutMillis:
|
|
755
|
-
connectionTimeoutMillis:
|
|
754
|
+
idleTimeoutMillis: nt("NEETRU_DB_POOL_IDLE_MS", pn, 1e3, 9e5),
|
|
755
|
+
connectionTimeoutMillis: nt(
|
|
756
756
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
757
|
-
|
|
757
|
+
fn,
|
|
758
758
|
1e3,
|
|
759
759
|
12e4
|
|
760
760
|
),
|
|
@@ -762,7 +762,7 @@ function tt() {
|
|
|
762
762
|
keepAliveInitialDelayMillis: 1e4
|
|
763
763
|
};
|
|
764
764
|
}
|
|
765
|
-
async function
|
|
765
|
+
async function vo(n) {
|
|
766
766
|
let { Pool: e } = await import(
|
|
767
767
|
/* webpackIgnore: true */
|
|
768
768
|
'pg'
|
|
@@ -779,17 +779,17 @@ async function ho(n) {
|
|
|
779
779
|
user: n.user,
|
|
780
780
|
password: n.password,
|
|
781
781
|
ssl: t,
|
|
782
|
-
...
|
|
782
|
+
...rt()
|
|
783
783
|
});
|
|
784
784
|
}
|
|
785
|
-
async function
|
|
785
|
+
async function wo(n, e) {
|
|
786
786
|
let { drizzle: t } = await import(
|
|
787
787
|
/* webpackIgnore: true */
|
|
788
788
|
'drizzle-orm/node-postgres'
|
|
789
789
|
);
|
|
790
790
|
return t(n, { schema: e });
|
|
791
791
|
}
|
|
792
|
-
function
|
|
792
|
+
function ko(n, e) {
|
|
793
793
|
let t = null, r = null, o = () => t ? Promise.resolve(t) : (r || (r = e().then((s) => (t = s, r = null, s)).catch((s) => {
|
|
794
794
|
throw r = null, s;
|
|
795
795
|
})), r), i = {
|
|
@@ -805,21 +805,21 @@ function yo(n, e) {
|
|
|
805
805
|
};
|
|
806
806
|
return n && Object.setPrototypeOf(i, n), i;
|
|
807
807
|
}
|
|
808
|
-
async function
|
|
808
|
+
async function mn(n, e) {
|
|
809
809
|
let t = e?.database !== void 0 ? { ...n, database: e.database } : n;
|
|
810
810
|
if (t.lazy) {
|
|
811
|
-
let d = null, p =
|
|
811
|
+
let d = null, p = ko(t.poolPrototype ?? null, () => {
|
|
812
812
|
if (!d)
|
|
813
813
|
throw new exports.NeetruDbError("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
814
814
|
return d._ensureInitialLease();
|
|
815
|
-
}), l =
|
|
815
|
+
}), l = et(p), u;
|
|
816
816
|
try {
|
|
817
817
|
u = t.createDrizzle(l, t.schema);
|
|
818
818
|
} catch (m) {
|
|
819
819
|
let h = m instanceof Error ? m.message : String(m);
|
|
820
820
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${h}`);
|
|
821
821
|
}
|
|
822
|
-
let f = new
|
|
822
|
+
let f = new ae(null, l, u, t);
|
|
823
823
|
return d = f, l.__neetruSetReLease(() => f._reactiveReLease()), f;
|
|
824
824
|
}
|
|
825
825
|
let r;
|
|
@@ -837,7 +837,7 @@ async function pn(n, e) {
|
|
|
837
837
|
let p = d instanceof Error ? d.message : String(d);
|
|
838
838
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${p}`);
|
|
839
839
|
}
|
|
840
|
-
let i =
|
|
840
|
+
let i = et(o), s;
|
|
841
841
|
try {
|
|
842
842
|
s = t.createDrizzle(i, t.schema);
|
|
843
843
|
} catch (d) {
|
|
@@ -846,11 +846,11 @@ async function pn(n, e) {
|
|
|
846
846
|
let p = d instanceof Error ? d.message : String(d);
|
|
847
847
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${p}`);
|
|
848
848
|
}
|
|
849
|
-
let a = new
|
|
849
|
+
let a = new ae(r, i, s, t);
|
|
850
850
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
851
851
|
}
|
|
852
|
-
async function
|
|
853
|
-
let r =
|
|
852
|
+
async function So(n, e, t) {
|
|
853
|
+
let r = dn(n), o, i;
|
|
854
854
|
try {
|
|
855
855
|
o = await import(
|
|
856
856
|
/* webpackIgnore: true */
|
|
@@ -866,7 +866,7 @@ async function _o(n, e, t) {
|
|
|
866
866
|
);
|
|
867
867
|
}
|
|
868
868
|
let s = o.Pool ?? o.default?.Pool;
|
|
869
|
-
return
|
|
869
|
+
return mn(
|
|
870
870
|
{
|
|
871
871
|
fetchLease: r,
|
|
872
872
|
createPool: (p) => {
|
|
@@ -885,7 +885,7 @@ async function _o(n, e, t) {
|
|
|
885
885
|
ssl: l,
|
|
886
886
|
// Tuning centralizado (env-tunável) — ver resolvePoolTuning + auditoria
|
|
887
887
|
// docs/_review/AUDITORIA_DB_CONN_PERF_2026-09-06.md no Core.
|
|
888
|
-
...
|
|
888
|
+
...rt()
|
|
889
889
|
});
|
|
890
890
|
},
|
|
891
891
|
createDrizzle: (p, l) => i.drizzle(p, { schema: l }),
|
|
@@ -901,36 +901,36 @@ async function _o(n, e, t) {
|
|
|
901
901
|
t
|
|
902
902
|
);
|
|
903
903
|
}
|
|
904
|
-
var
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
904
|
+
var cn, ln, un, pn, fn, gn = W(() => {
|
|
905
|
+
H();
|
|
906
|
+
A();
|
|
907
|
+
an();
|
|
908
|
+
tt();
|
|
909
|
+
cn = "/api/sdk/v1/db/lease", ln = "/api/sdk/v1/db/lease/renew";
|
|
910
|
+
un = 10, pn = 6e4, fn = 1e4;
|
|
911
911
|
});
|
|
912
|
-
var
|
|
913
|
-
function
|
|
914
|
-
let t = Date.now(), r =
|
|
912
|
+
var gt = /* @__PURE__ */ new Map(), te = 3600 * 1e3;
|
|
913
|
+
function _e(n, e = te) {
|
|
914
|
+
let t = Date.now(), r = gt.get(n);
|
|
915
915
|
if (r && t - r.createdAt < e)
|
|
916
916
|
return r.jwks;
|
|
917
917
|
let o = jose.createRemoteJWKSet(new URL(n));
|
|
918
|
-
return
|
|
918
|
+
return gt.set(n, { jwks: o, createdAt: t }), o;
|
|
919
919
|
}
|
|
920
920
|
|
|
921
921
|
// src/auth.ts
|
|
922
|
-
|
|
922
|
+
A();
|
|
923
923
|
|
|
924
924
|
// src/api-key.ts
|
|
925
|
-
|
|
926
|
-
var
|
|
927
|
-
function
|
|
925
|
+
A();
|
|
926
|
+
var yt = /^nrt_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/;
|
|
927
|
+
function _t(n) {
|
|
928
928
|
if (typeof n != "string" || n.length === 0)
|
|
929
929
|
throw new exports.NeetruError(
|
|
930
930
|
"invalid_config",
|
|
931
931
|
"apiKey is required (formato nrt_<keyId>_<secret>)"
|
|
932
932
|
);
|
|
933
|
-
let e =
|
|
933
|
+
let e = yt.exec(n);
|
|
934
934
|
if (!e)
|
|
935
935
|
throw new exports.NeetruError(
|
|
936
936
|
"invalid_config",
|
|
@@ -938,22 +938,22 @@ function gt(n) {
|
|
|
938
938
|
);
|
|
939
939
|
return { keyId: e[1], secret: e[2] };
|
|
940
940
|
}
|
|
941
|
-
function
|
|
941
|
+
function bt(n) {
|
|
942
942
|
if (typeof n != "string" || n.length === 0) return null;
|
|
943
|
-
let e =
|
|
943
|
+
let e = yt.exec(n);
|
|
944
944
|
return e ? { keyId: e[1], secret: e[2] } : null;
|
|
945
945
|
}
|
|
946
946
|
|
|
947
947
|
// src/base-url-allowlist.ts
|
|
948
|
-
|
|
949
|
-
var
|
|
950
|
-
function
|
|
951
|
-
return
|
|
948
|
+
A();
|
|
949
|
+
var vt = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), fr = ".neetru.com", mr = "neetru.com", hr = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
950
|
+
function wt(n) {
|
|
951
|
+
return hr.test(n);
|
|
952
952
|
}
|
|
953
|
-
function
|
|
953
|
+
function gr(n) {
|
|
954
954
|
return n.toLowerCase().replace(/\.+$/, "");
|
|
955
955
|
}
|
|
956
|
-
function
|
|
956
|
+
function kt(n, e = []) {
|
|
957
957
|
if (typeof n != "string" || n.length === 0)
|
|
958
958
|
throw new exports.NeetruError("invalid_config", "baseUrl is required");
|
|
959
959
|
let t;
|
|
@@ -962,8 +962,8 @@ function vt(n, e = []) {
|
|
|
962
962
|
} catch {
|
|
963
963
|
throw new exports.NeetruError("invalid_config", `baseUrl is not a valid URL: ${n}`);
|
|
964
964
|
}
|
|
965
|
-
let r = t.protocol, o =
|
|
966
|
-
if (
|
|
965
|
+
let r = t.protocol, o = gr(t.hostname);
|
|
966
|
+
if (yr())
|
|
967
967
|
return typeof console < "u" && console.warn(
|
|
968
968
|
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${n} aceito sem valida\xE7\xE3o.`
|
|
969
969
|
), n.replace(/\/+$/, "");
|
|
@@ -973,17 +973,17 @@ function vt(n, e = []) {
|
|
|
973
973
|
`baseUrl protocol must be http: or https:, got ${r}`
|
|
974
974
|
);
|
|
975
975
|
if (r === "http:") {
|
|
976
|
-
if (!(
|
|
976
|
+
if (!(vt.has(o) || wt(o)))
|
|
977
977
|
throw new exports.NeetruError(
|
|
978
978
|
"invalid_config",
|
|
979
979
|
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${o}. Use https: or set NEETRU_ALLOW_INSECURE_BASEURL=1 to bypass.`
|
|
980
980
|
);
|
|
981
981
|
return n.replace(/\/+$/, "");
|
|
982
982
|
}
|
|
983
|
-
let i = o ===
|
|
983
|
+
let i = o === mr || o.endsWith(fr), s = e.some((d) => {
|
|
984
984
|
let p = d.toLowerCase();
|
|
985
985
|
return o === p || o.endsWith(`.${p}`);
|
|
986
|
-
}), a =
|
|
986
|
+
}), a = vt.has(o) || wt(o);
|
|
987
987
|
if (!i && !s && !a)
|
|
988
988
|
throw new exports.NeetruError(
|
|
989
989
|
"invalid_config",
|
|
@@ -991,7 +991,7 @@ function vt(n, e = []) {
|
|
|
991
991
|
);
|
|
992
992
|
return n.replace(/\/+$/, "");
|
|
993
993
|
}
|
|
994
|
-
function
|
|
994
|
+
function yr() {
|
|
995
995
|
try {
|
|
996
996
|
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
997
997
|
if (e === "1" || e === "true") return !0;
|
|
@@ -1006,12 +1006,12 @@ function pr() {
|
|
|
1006
1006
|
}
|
|
1007
1007
|
|
|
1008
1008
|
// src/types.ts
|
|
1009
|
-
var
|
|
1009
|
+
var We = "https://api.neetru.com", St = "http://localhost:9003";
|
|
1010
1010
|
|
|
1011
1011
|
// src/catalog.ts
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
function
|
|
1012
|
+
A();
|
|
1013
|
+
F();
|
|
1014
|
+
function Mt(n) {
|
|
1015
1015
|
if (!n || typeof n != "object")
|
|
1016
1016
|
throw new exports.NeetruError("invalid_response", "Catalog response item is not an object");
|
|
1017
1017
|
let e = n;
|
|
@@ -1030,7 +1030,7 @@ function Lt(n) {
|
|
|
1030
1030
|
return typeof r.amountCents == "number" && (o.amountCents = r.amountCents), Array.isArray(r.features) && r.features.every((i) => typeof i == "string") && (o.features = r.features), o;
|
|
1031
1031
|
})), t;
|
|
1032
1032
|
}
|
|
1033
|
-
function
|
|
1033
|
+
function qt(n) {
|
|
1034
1034
|
return {
|
|
1035
1035
|
/**
|
|
1036
1036
|
* Lista produtos publicados. Por default só `published=true`; staff
|
|
@@ -1047,7 +1047,7 @@ function Ut(n) {
|
|
|
1047
1047
|
"Catalog list response missing products array"
|
|
1048
1048
|
);
|
|
1049
1049
|
return {
|
|
1050
|
-
products: t.products.map(
|
|
1050
|
+
products: t.products.map(Mt),
|
|
1051
1051
|
fetchedAt: typeof t.fetchedAt == "string" ? t.fetchedAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
1052
1052
|
};
|
|
1053
1053
|
},
|
|
@@ -1068,32 +1068,32 @@ function Ut(n) {
|
|
|
1068
1068
|
"invalid_response",
|
|
1069
1069
|
"Catalog get response missing product"
|
|
1070
1070
|
);
|
|
1071
|
-
return
|
|
1071
|
+
return Mt(t.product);
|
|
1072
1072
|
}
|
|
1073
1073
|
};
|
|
1074
1074
|
}
|
|
1075
1075
|
|
|
1076
1076
|
// src/entitlements.ts
|
|
1077
|
-
|
|
1078
|
-
var
|
|
1079
|
-
function
|
|
1080
|
-
throw new exports.NeetruError("validation_failed",
|
|
1077
|
+
A();
|
|
1078
|
+
var Dr = "client.entitlements.check()/checkDetailed() est\xE3o DEPRECADOS \u2014 apontavam para GET /api/v1/sdk/entitlements/check, um endpoint que o Core nunca escreve (sempre retornava not_subscribed). Use client.usage.check(resource, { productId, tenantId }) no lugar \u2014 mesmo resultado (allowed/reason/remaining/limit/behavior), endpoint real.";
|
|
1079
|
+
function Ft() {
|
|
1080
|
+
throw new exports.NeetruError("validation_failed", Dr);
|
|
1081
1081
|
}
|
|
1082
|
-
function
|
|
1082
|
+
function jt(n) {
|
|
1083
1083
|
return {
|
|
1084
1084
|
/**
|
|
1085
1085
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
1086
1086
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
1087
1087
|
*/
|
|
1088
1088
|
async check(e, t, r) {
|
|
1089
|
-
|
|
1089
|
+
Ft();
|
|
1090
1090
|
},
|
|
1091
1091
|
/**
|
|
1092
1092
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
1093
1093
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
1094
1094
|
*/
|
|
1095
1095
|
async checkDetailed(e, t, r) {
|
|
1096
|
-
|
|
1096
|
+
Ft();
|
|
1097
1097
|
},
|
|
1098
1098
|
/** Test helper legado — no-op (não há mais cache). Mantido por compat. */
|
|
1099
1099
|
__resetCache() {
|
|
@@ -1102,16 +1102,31 @@ function qt(n) {
|
|
|
1102
1102
|
}
|
|
1103
1103
|
|
|
1104
1104
|
// src/telemetry.ts
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1105
|
+
A();
|
|
1106
|
+
F();
|
|
1107
|
+
|
|
1108
|
+
// src/runtime-env.ts
|
|
1109
|
+
function Be() {
|
|
1110
|
+
try {
|
|
1111
|
+
if (typeof process < "u" && process.env)
|
|
1112
|
+
return process.env.NODE_ENV === "production";
|
|
1113
|
+
} catch {
|
|
1114
|
+
}
|
|
1115
|
+
return false;
|
|
1116
|
+
}
|
|
1117
|
+
function V(n) {
|
|
1118
|
+
return n === "dev" && !Be();
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
// src/telemetry.ts
|
|
1122
|
+
var Wt = ["debug", "info", "warn", "error", "fatal"];
|
|
1123
|
+
function Nr() {
|
|
1109
1124
|
return typeof globalThis < "u" && typeof globalThis.window < "u";
|
|
1110
1125
|
}
|
|
1111
|
-
function
|
|
1112
|
-
return
|
|
1126
|
+
function $t() {
|
|
1127
|
+
return Nr() ? { path: "/api/sdk/v1/telemetry/event/public", requireAuth: false } : { path: "/api/sdk/v1/telemetry/event", requireAuth: true };
|
|
1113
1128
|
}
|
|
1114
|
-
function
|
|
1129
|
+
function xr(n) {
|
|
1115
1130
|
switch (n) {
|
|
1116
1131
|
case "debug":
|
|
1117
1132
|
return console.debug.bind(console);
|
|
@@ -1127,8 +1142,8 @@ function Ar(n) {
|
|
|
1127
1142
|
return console.log.bind(console);
|
|
1128
1143
|
}
|
|
1129
1144
|
}
|
|
1130
|
-
var
|
|
1131
|
-
function
|
|
1145
|
+
var Or = 500, Lr = 50;
|
|
1146
|
+
function Vt(n) {
|
|
1132
1147
|
let e = [], t = null, r = null;
|
|
1133
1148
|
async function o() {
|
|
1134
1149
|
if (e.length === 0) return;
|
|
@@ -1137,7 +1152,7 @@ function Wt(n) {
|
|
|
1137
1152
|
try {
|
|
1138
1153
|
let p = { name: d.name }, l = d.productSlug ?? n.productId;
|
|
1139
1154
|
l && (p.productSlug = l), d.properties && typeof d.properties == "object" && (p.properties = d.properties), d.timestamp && (p.timestamp = d.timestamp);
|
|
1140
|
-
let u =
|
|
1155
|
+
let u = $t();
|
|
1141
1156
|
await S(n, {
|
|
1142
1157
|
method: "POST",
|
|
1143
1158
|
path: u.path,
|
|
@@ -1161,7 +1176,7 @@ function Wt(n) {
|
|
|
1161
1176
|
function i() {
|
|
1162
1177
|
t || (t = setTimeout(() => {
|
|
1163
1178
|
o();
|
|
1164
|
-
},
|
|
1179
|
+
}, Or));
|
|
1165
1180
|
}
|
|
1166
1181
|
return {
|
|
1167
1182
|
/**
|
|
@@ -1185,7 +1200,7 @@ function Wt(n) {
|
|
|
1185
1200
|
throw new exports.NeetruError("validation_failed", "event.name max 128 chars");
|
|
1186
1201
|
let a = { name: s.name }, d = s.productSlug ?? n.productId;
|
|
1187
1202
|
d && (a.productSlug = d), s.properties && typeof s.properties == "object" && (a.properties = s.properties), s.timestamp && (a.timestamp = s.timestamp);
|
|
1188
|
-
let p =
|
|
1203
|
+
let p = $t(), l = await S(n, {
|
|
1189
1204
|
method: "POST",
|
|
1190
1205
|
path: p.path,
|
|
1191
1206
|
body: a,
|
|
@@ -1206,7 +1221,7 @@ function Wt(n) {
|
|
|
1206
1221
|
* ```
|
|
1207
1222
|
*/
|
|
1208
1223
|
track(s) {
|
|
1209
|
-
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >=
|
|
1224
|
+
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >= Lr ? (t && (clearTimeout(t), t = null), o()) : i());
|
|
1210
1225
|
},
|
|
1211
1226
|
/**
|
|
1212
1227
|
* Força flush imediato da queue de `track()`. Resolva antes de
|
|
@@ -1233,14 +1248,14 @@ function Wt(n) {
|
|
|
1233
1248
|
async log(s) {
|
|
1234
1249
|
if (!s || typeof s != "object")
|
|
1235
1250
|
throw new exports.NeetruError("validation_failed", "log input is required");
|
|
1236
|
-
if (!
|
|
1237
|
-
throw new exports.NeetruError("validation_failed", `level must be one of ${
|
|
1251
|
+
if (!Wt.includes(s.level))
|
|
1252
|
+
throw new exports.NeetruError("validation_failed", `level must be one of ${Wt.join(", ")}`);
|
|
1238
1253
|
if (typeof s.message != "string" || s.message.length === 0)
|
|
1239
1254
|
throw new exports.NeetruError("validation_failed", "message is required");
|
|
1240
1255
|
if (s.message.length > 4e3)
|
|
1241
1256
|
throw new exports.NeetruError("validation_failed", "message max 4000 chars");
|
|
1242
|
-
if (n.env
|
|
1243
|
-
return
|
|
1257
|
+
if (V(n.env))
|
|
1258
|
+
return xr(s.level)(`[neetru-sdk] ${s.level}: ${s.message}`, s.metadata ?? {}), { ok: true, mode: "mock" };
|
|
1244
1259
|
let a = {
|
|
1245
1260
|
level: s.level,
|
|
1246
1261
|
message: s.message
|
|
@@ -1276,22 +1291,22 @@ function Wt(n) {
|
|
|
1276
1291
|
}
|
|
1277
1292
|
|
|
1278
1293
|
// src/usage.ts
|
|
1279
|
-
|
|
1280
|
-
|
|
1294
|
+
A();
|
|
1295
|
+
F();
|
|
1281
1296
|
|
|
1282
1297
|
// src/validators.ts
|
|
1283
|
-
|
|
1284
|
-
var
|
|
1298
|
+
A();
|
|
1299
|
+
var Bt = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
1285
1300
|
function O(n, e) {
|
|
1286
|
-
if (typeof e != "string" ||
|
|
1301
|
+
if (typeof e != "string" || !Bt.test(e))
|
|
1287
1302
|
throw new exports.NeetruError(
|
|
1288
1303
|
"validation_failed",
|
|
1289
|
-
`${n} must match ${
|
|
1304
|
+
`${n} must match ${Bt.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
1290
1305
|
);
|
|
1291
1306
|
}
|
|
1292
1307
|
|
|
1293
1308
|
// src/usage.ts
|
|
1294
|
-
function
|
|
1309
|
+
function Kt(n) {
|
|
1295
1310
|
return {
|
|
1296
1311
|
/**
|
|
1297
1312
|
* Reporta consumo metered de um resource. Hit no endpoint canônico
|
|
@@ -1379,10 +1394,10 @@ function Vt(n) {
|
|
|
1379
1394
|
}
|
|
1380
1395
|
|
|
1381
1396
|
// src/support.ts
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
var
|
|
1385
|
-
function
|
|
1397
|
+
A();
|
|
1398
|
+
F();
|
|
1399
|
+
var Ke = ["low", "normal", "high", "urgent"], Ur = ["open", "pending", "resolved", "closed"], zt = 50;
|
|
1400
|
+
function Ht(n) {
|
|
1386
1401
|
if (!n || typeof n != "object")
|
|
1387
1402
|
throw new exports.NeetruError("invalid_response", "Ticket response is not an object");
|
|
1388
1403
|
let e = n;
|
|
@@ -1393,8 +1408,8 @@ function Kt(n) {
|
|
|
1393
1408
|
id: e.id,
|
|
1394
1409
|
subject: typeof e.subject == "string" ? e.subject : "",
|
|
1395
1410
|
message: typeof e.message == "string" ? e.message : "",
|
|
1396
|
-
severity:
|
|
1397
|
-
status:
|
|
1411
|
+
severity: Ke.includes(e.severity) ? e.severity : "normal",
|
|
1412
|
+
status: Ur.includes(e.status) ? e.status : "open",
|
|
1398
1413
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
1399
1414
|
updatedAt: typeof e.updatedAt == "string" ? e.updatedAt : void 0,
|
|
1400
1415
|
productSlug: typeof e.productId == "string" ? e.productId : typeof e.productSlug == "string" ? e.productSlug : void 0,
|
|
@@ -1412,15 +1427,15 @@ function Kt(n) {
|
|
|
1412
1427
|
satisfactionComment: typeof e.satisfactionComment == "string" ? e.satisfactionComment : null
|
|
1413
1428
|
};
|
|
1414
1429
|
}
|
|
1415
|
-
function
|
|
1430
|
+
function ke(n) {
|
|
1416
1431
|
if (!n || typeof n != "string")
|
|
1417
1432
|
throw new exports.NeetruError("validation_failed", "ticketId is required");
|
|
1418
1433
|
}
|
|
1419
|
-
function
|
|
1434
|
+
function Qt(n) {
|
|
1420
1435
|
if (typeof n == "string" && n.length > 2e3)
|
|
1421
1436
|
throw new exports.NeetruError("validation_failed", "comment max 2000 chars");
|
|
1422
1437
|
}
|
|
1423
|
-
function
|
|
1438
|
+
function Gt(n, ...e) {
|
|
1424
1439
|
for (let t of e)
|
|
1425
1440
|
if (typeof t == "string" && t.length > 0)
|
|
1426
1441
|
return O("productSlug", t), t;
|
|
@@ -1431,12 +1446,12 @@ function Ht(n, ...e) {
|
|
|
1431
1446
|
"productId required (pass to input.productSlug or set on createNeetruClient)"
|
|
1432
1447
|
);
|
|
1433
1448
|
}
|
|
1434
|
-
var
|
|
1435
|
-
function
|
|
1449
|
+
var Mr = "X-Neetru-End-User-Token", qr = "Esta opera\xE7\xE3o de suporte exige um usu\xE1rio final logado: o Core s\xF3 mostra/altera os tickets do pr\xF3prio usu\xE1rio (header X-Neetru-End-User-Token com o id_token OIDC). Fa\xE7a login com client.auth.signIn() e tente de novo.", Fr = "O Core exige a identidade do usu\xE1rio final para esta opera\xE7\xE3o (403 end_user_token_required) e nenhum id_token foi aceito. Fa\xE7a login com client.auth.signIn() e tente de novo.", jr = "O Core recusou o token do usu\xE1rio final (401 invalid_end_user_token \u2014 sess\xE3o expirada, usu\xE1rio inativo ou token que n\xE3o \xE9 id_token) e a renova\xE7\xE3o autom\xE1tica n\xE3o resolveu. Fa\xE7a login novamente com client.auth.signIn() e tente de novo.", Wr = "O id_token do usu\xE1rio final foi emitido para o client OIDC de OUTRO produto (403 end_user_token_product_mismatch). Confira se o login (client.auth / clientId OIDC) e a apiKey/productId do createNeetruClient s\xE3o do MESMO produto.";
|
|
1450
|
+
function Jt(n) {
|
|
1436
1451
|
if (n instanceof exports.NeetruError && !(n instanceof exports.NeetruAuthRequiredError)) {
|
|
1437
1452
|
if (n.code === "end_user_token_required")
|
|
1438
1453
|
throw new exports.NeetruAuthRequiredError(
|
|
1439
|
-
|
|
1454
|
+
Fr,
|
|
1440
1455
|
"end_user_token_required",
|
|
1441
1456
|
n.status,
|
|
1442
1457
|
n.requestId,
|
|
@@ -1444,7 +1459,7 @@ function Gt(n) {
|
|
|
1444
1459
|
);
|
|
1445
1460
|
if (n.code === "invalid_end_user_token")
|
|
1446
1461
|
throw new exports.NeetruAuthRequiredError(
|
|
1447
|
-
|
|
1462
|
+
jr,
|
|
1448
1463
|
"invalid_end_user_token",
|
|
1449
1464
|
n.status,
|
|
1450
1465
|
n.requestId,
|
|
@@ -1453,7 +1468,7 @@ function Gt(n) {
|
|
|
1453
1468
|
if (n.code === "end_user_token_product_mismatch")
|
|
1454
1469
|
throw new exports.NeetruError(
|
|
1455
1470
|
"forbidden",
|
|
1456
|
-
|
|
1471
|
+
Wr,
|
|
1457
1472
|
n.status,
|
|
1458
1473
|
n.requestId,
|
|
1459
1474
|
"end_user_token_product_mismatch",
|
|
@@ -1462,10 +1477,10 @@ function Gt(n) {
|
|
|
1462
1477
|
}
|
|
1463
1478
|
throw n;
|
|
1464
1479
|
}
|
|
1465
|
-
function
|
|
1480
|
+
function $r(n) {
|
|
1466
1481
|
return n instanceof exports.NeetruError && n.code === "invalid_end_user_token";
|
|
1467
1482
|
}
|
|
1468
|
-
function
|
|
1483
|
+
function Xt(n, e = {}) {
|
|
1469
1484
|
async function t() {
|
|
1470
1485
|
try {
|
|
1471
1486
|
let a = await e.getEndUserToken?.();
|
|
@@ -1483,25 +1498,25 @@ function Qt(n, e = {}) {
|
|
|
1483
1498
|
}
|
|
1484
1499
|
}
|
|
1485
1500
|
function o(a) {
|
|
1486
|
-
return a ? { [
|
|
1501
|
+
return a ? { [Mr]: a } : {};
|
|
1487
1502
|
}
|
|
1488
1503
|
async function i(a, d) {
|
|
1489
1504
|
let p = await t();
|
|
1490
1505
|
if (!p && a && await r() && (p = await t()), !p && a)
|
|
1491
|
-
throw new exports.NeetruAuthRequiredError(
|
|
1506
|
+
throw new exports.NeetruAuthRequiredError(qr, "no_signed_in_user");
|
|
1492
1507
|
try {
|
|
1493
1508
|
return await d(o(p));
|
|
1494
1509
|
} catch (l) {
|
|
1495
|
-
if (p &&
|
|
1510
|
+
if (p && $r(l) && await r()) {
|
|
1496
1511
|
let u = await t();
|
|
1497
1512
|
if (u)
|
|
1498
1513
|
try {
|
|
1499
1514
|
return await d(o(u));
|
|
1500
1515
|
} catch (f) {
|
|
1501
|
-
|
|
1516
|
+
Jt(f);
|
|
1502
1517
|
}
|
|
1503
1518
|
}
|
|
1504
|
-
|
|
1519
|
+
Jt(l);
|
|
1505
1520
|
}
|
|
1506
1521
|
}
|
|
1507
1522
|
async function s(a, d = {}) {
|
|
@@ -1534,13 +1549,13 @@ function Qt(n, e = {}) {
|
|
|
1534
1549
|
throw new exports.NeetruError("validation_failed", "message is required");
|
|
1535
1550
|
if (a.message.length > 1e4)
|
|
1536
1551
|
throw new exports.NeetruError("validation_failed", "message max 10000 chars");
|
|
1537
|
-
if (a.severity && !
|
|
1552
|
+
if (a.severity && !Ke.includes(a.severity))
|
|
1538
1553
|
throw new exports.NeetruError(
|
|
1539
1554
|
"validation_failed",
|
|
1540
|
-
`severity must be one of ${
|
|
1555
|
+
`severity must be one of ${Ke.join(", ")}`
|
|
1541
1556
|
);
|
|
1542
1557
|
let p = {
|
|
1543
|
-
productId:
|
|
1558
|
+
productId: Gt(n, a.productSlug),
|
|
1544
1559
|
subject: a.subject,
|
|
1545
1560
|
message: a.message,
|
|
1546
1561
|
severity: a.severity ?? "normal"
|
|
@@ -1557,7 +1572,7 @@ function Qt(n, e = {}) {
|
|
|
1557
1572
|
headers: f
|
|
1558
1573
|
})
|
|
1559
1574
|
), u = l && typeof l == "object" && "ticket" in l ? l.ticket : l;
|
|
1560
|
-
return
|
|
1575
|
+
return Ht(u);
|
|
1561
1576
|
},
|
|
1562
1577
|
/**
|
|
1563
1578
|
* Lista tickets do customer no produto atual. `productSlug` no contexto.
|
|
@@ -1565,11 +1580,11 @@ function Qt(n, e = {}) {
|
|
|
1565
1580
|
* Endpoint canonical: `GET /api/sdk/v1/support/tickets?productId=...`.
|
|
1566
1581
|
*/
|
|
1567
1582
|
async listMyTickets(a) {
|
|
1568
|
-
let d =
|
|
1569
|
-
if (p !== void 0 && (!Number.isInteger(p) || p < 1 || p >
|
|
1583
|
+
let d = Gt(n, a?.productSlug), p = a?.limit;
|
|
1584
|
+
if (p !== void 0 && (!Number.isInteger(p) || p < 1 || p > zt))
|
|
1570
1585
|
throw new exports.NeetruError(
|
|
1571
1586
|
"validation_failed",
|
|
1572
|
-
`limit must be an integer 1-${
|
|
1587
|
+
`limit must be an integer 1-${zt} (Core default 20)`
|
|
1573
1588
|
);
|
|
1574
1589
|
let l = await i(
|
|
1575
1590
|
true,
|
|
@@ -1581,7 +1596,7 @@ function Qt(n, e = {}) {
|
|
|
1581
1596
|
headers: f
|
|
1582
1597
|
})
|
|
1583
1598
|
);
|
|
1584
|
-
return (Array.isArray(l) ? l : l && typeof l == "object" && "tickets" in l ? l.tickets ?? [] : []).map(
|
|
1599
|
+
return (Array.isArray(l) ? l : l && typeof l == "object" && "tickets" in l ? l.tickets ?? [] : []).map(Ht);
|
|
1585
1600
|
},
|
|
1586
1601
|
/**
|
|
1587
1602
|
* 3.4: marca a notificação de resolução como vista (`resolvedNotifiedAt`,
|
|
@@ -1589,7 +1604,7 @@ function Qt(n, e = {}) {
|
|
|
1589
1604
|
* `POST /api/sdk/v1/support/tickets/{id}/resolved-seen`.
|
|
1590
1605
|
*/
|
|
1591
1606
|
async markResolvedSeen(a) {
|
|
1592
|
-
|
|
1607
|
+
ke(a), await s(
|
|
1593
1608
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/resolved-seen`
|
|
1594
1609
|
);
|
|
1595
1610
|
},
|
|
@@ -1600,7 +1615,7 @@ function Qt(n, e = {}) {
|
|
|
1600
1615
|
* `POST /api/sdk/v1/support/tickets/{id}/satisfaction-attempt`.
|
|
1601
1616
|
*/
|
|
1602
1617
|
async recordSatisfactionAttempt(a) {
|
|
1603
|
-
|
|
1618
|
+
ke(a), await s(
|
|
1604
1619
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/satisfaction-attempt`,
|
|
1605
1620
|
// Incrementa satisfactionAttempts += 1 no Core — sem idempotencyKey,
|
|
1606
1621
|
// um retry de rede infla o contador (mesmo padrão de createTicket acima).
|
|
@@ -1614,9 +1629,9 @@ function Qt(n, e = {}) {
|
|
|
1614
1629
|
async submitSatisfaction(a) {
|
|
1615
1630
|
if (!a || typeof a != "object")
|
|
1616
1631
|
throw new exports.NeetruError("validation_failed", "input is required");
|
|
1617
|
-
if (
|
|
1632
|
+
if (ke(a.ticketId), a.rating !== null && a.rating !== void 0 && (!Number.isInteger(a.rating) || a.rating < 1 || a.rating > 5))
|
|
1618
1633
|
throw new exports.NeetruError("validation_failed", "rating must be an integer 1-5 or null");
|
|
1619
|
-
|
|
1634
|
+
Qt(a.comment), await s(
|
|
1620
1635
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a.ticketId)}/satisfaction`,
|
|
1621
1636
|
{
|
|
1622
1637
|
body: {
|
|
@@ -1633,7 +1648,7 @@ function Qt(n, e = {}) {
|
|
|
1633
1648
|
* `POST /api/sdk/v1/support/tickets/{id}/contest`.
|
|
1634
1649
|
*/
|
|
1635
1650
|
async contestTicket(a, d) {
|
|
1636
|
-
|
|
1651
|
+
ke(a), Qt(d?.comment), await s(
|
|
1637
1652
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/contest`,
|
|
1638
1653
|
{ body: { comment: d?.comment ?? void 0 } }
|
|
1639
1654
|
);
|
|
@@ -1642,14 +1657,14 @@ function Qt(n, e = {}) {
|
|
|
1642
1657
|
}
|
|
1643
1658
|
|
|
1644
1659
|
// src/db/client-db.ts
|
|
1645
|
-
|
|
1660
|
+
H();
|
|
1646
1661
|
|
|
1647
1662
|
// src/db/offline/query-engine.ts
|
|
1648
|
-
function
|
|
1663
|
+
function Yt(n) {
|
|
1649
1664
|
return n == null ? 0 : typeof n == "number" ? 1 : typeof n == "string" ? 2 : typeof n == "boolean" ? 3 : 4;
|
|
1650
1665
|
}
|
|
1651
|
-
function
|
|
1652
|
-
let t =
|
|
1666
|
+
function oe(n, e) {
|
|
1667
|
+
let t = Yt(n), r = Yt(e);
|
|
1653
1668
|
if (t !== r) return t - r;
|
|
1654
1669
|
if (n == null) return 0;
|
|
1655
1670
|
if (typeof n == "number" && typeof e == "number") return n - e;
|
|
@@ -1658,7 +1673,7 @@ function re(n, e) {
|
|
|
1658
1673
|
let o = String(n), i = String(e);
|
|
1659
1674
|
return o < i ? -1 : o > i ? 1 : 0;
|
|
1660
1675
|
}
|
|
1661
|
-
function
|
|
1676
|
+
function ze(n, e) {
|
|
1662
1677
|
let t = e.split("."), r = n;
|
|
1663
1678
|
for (let o of t) {
|
|
1664
1679
|
if (r == null || typeof r != "object")
|
|
@@ -1667,8 +1682,8 @@ function Be(n, e) {
|
|
|
1667
1682
|
}
|
|
1668
1683
|
return r;
|
|
1669
1684
|
}
|
|
1670
|
-
function
|
|
1671
|
-
let t =
|
|
1685
|
+
function Br(n, e) {
|
|
1686
|
+
let t = ze(n, e.field);
|
|
1672
1687
|
if (t === void 0) return false;
|
|
1673
1688
|
let { op: r, value: o } = e;
|
|
1674
1689
|
switch (r) {
|
|
@@ -1677,13 +1692,13 @@ function Fr(n, e) {
|
|
|
1677
1692
|
case "!=":
|
|
1678
1693
|
return t !== o;
|
|
1679
1694
|
case "<":
|
|
1680
|
-
return
|
|
1695
|
+
return oe(t, o) < 0;
|
|
1681
1696
|
case "<=":
|
|
1682
|
-
return
|
|
1697
|
+
return oe(t, o) <= 0;
|
|
1683
1698
|
case ">":
|
|
1684
|
-
return
|
|
1699
|
+
return oe(t, o) > 0;
|
|
1685
1700
|
case ">=":
|
|
1686
|
-
return
|
|
1701
|
+
return oe(t, o) >= 0;
|
|
1687
1702
|
case "array-contains":
|
|
1688
1703
|
return Array.isArray(t) && t.includes(o);
|
|
1689
1704
|
case "in":
|
|
@@ -1698,24 +1713,24 @@ function Fr(n, e) {
|
|
|
1698
1713
|
);
|
|
1699
1714
|
}
|
|
1700
1715
|
}
|
|
1701
|
-
function
|
|
1702
|
-
return e.every((t) =>
|
|
1716
|
+
function Kr(n, e) {
|
|
1717
|
+
return e.every((t) => Br(n, t));
|
|
1703
1718
|
}
|
|
1704
|
-
function
|
|
1719
|
+
function zr(n) {
|
|
1705
1720
|
return (e, t) => {
|
|
1706
1721
|
if (n) {
|
|
1707
|
-
let r =
|
|
1722
|
+
let r = ze(e.data, n.field), o = ze(t.data, n.field), i = oe(r, o);
|
|
1708
1723
|
if (i !== 0)
|
|
1709
1724
|
return n.direction === "desc" ? -i : i;
|
|
1710
1725
|
}
|
|
1711
1726
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
1712
1727
|
};
|
|
1713
1728
|
}
|
|
1714
|
-
function
|
|
1729
|
+
function Hr(n, e) {
|
|
1715
1730
|
let t = n.findIndex((r) => r.id === e.docId);
|
|
1716
1731
|
return t === -1 ? n : e.type === "startAfter" ? n.slice(t + 1) : n.slice(0, t);
|
|
1717
1732
|
}
|
|
1718
|
-
var
|
|
1733
|
+
var X = class n {
|
|
1719
1734
|
/**
|
|
1720
1735
|
* Avalia um `QueryDescriptor` contra um array de `OfflineDoc`.
|
|
1721
1736
|
*
|
|
@@ -1729,9 +1744,9 @@ var J = class n {
|
|
|
1729
1744
|
evaluate(e, t) {
|
|
1730
1745
|
let r = e.filter((l) => !l.meta.deleted), o = t.where ?? [];
|
|
1731
1746
|
o.length > 0 && (r = r.filter(
|
|
1732
|
-
(l) =>
|
|
1747
|
+
(l) => Kr(l.data, o)
|
|
1733
1748
|
));
|
|
1734
|
-
let i =
|
|
1749
|
+
let i = zr(t.orderBy), s = [...r].sort(i), a = t.cursor ? Hr(s, t.cursor) : s, d = Math.min(t.limit ?? 20, 500);
|
|
1735
1750
|
return {
|
|
1736
1751
|
docs: a.slice(0, d).map((l) => ({ id: l.id, data: l.data })),
|
|
1737
1752
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
@@ -1751,7 +1766,7 @@ var J = class n {
|
|
|
1751
1766
|
};
|
|
1752
1767
|
|
|
1753
1768
|
// src/db/offline/local-store.ts
|
|
1754
|
-
var
|
|
1769
|
+
var Gr = 1, B = "documents", K = "mutations", ie = "query_cache", Se = "sync_meta", Y = "conflict_log", Te = class {
|
|
1755
1770
|
db = null;
|
|
1756
1771
|
dbName;
|
|
1757
1772
|
constructor(e) {
|
|
@@ -1763,22 +1778,22 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1763
1778
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
1764
1779
|
*/
|
|
1765
1780
|
async open() {
|
|
1766
|
-
this.db === null && (this.db = await idb.openDB(this.dbName,
|
|
1781
|
+
this.db === null && (this.db = await idb.openDB(this.dbName, Gr, {
|
|
1767
1782
|
upgrade(e) {
|
|
1768
|
-
if (!e.objectStoreNames.contains(
|
|
1769
|
-
let t = e.createObjectStore(
|
|
1783
|
+
if (!e.objectStoreNames.contains(B)) {
|
|
1784
|
+
let t = e.createObjectStore(B, {
|
|
1770
1785
|
keyPath: ["collection", "id"]
|
|
1771
1786
|
});
|
|
1772
1787
|
t.createIndex("by_collection", "collection"), t.createIndex("by_collection_state", ["collection", "meta.state"]), t.createIndex("by_updatedAtServer", "meta.updatedAtServer");
|
|
1773
1788
|
}
|
|
1774
|
-
if (!e.objectStoreNames.contains(
|
|
1775
|
-
let t = e.createObjectStore(
|
|
1789
|
+
if (!e.objectStoreNames.contains(K)) {
|
|
1790
|
+
let t = e.createObjectStore(K, {
|
|
1776
1791
|
keyPath: "mutationId"
|
|
1777
1792
|
});
|
|
1778
1793
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
1779
1794
|
}
|
|
1780
|
-
if (e.objectStoreNames.contains(
|
|
1781
|
-
let t = e.createObjectStore(
|
|
1795
|
+
if (e.objectStoreNames.contains(ie) || e.createObjectStore(ie, { keyPath: "queryHash" }), e.objectStoreNames.contains(Se) || e.createObjectStore(Se, { keyPath: "key" }), !e.objectStoreNames.contains(Y)) {
|
|
1796
|
+
let t = e.createObjectStore(Y, {
|
|
1782
1797
|
keyPath: "id",
|
|
1783
1798
|
autoIncrement: true
|
|
1784
1799
|
});
|
|
@@ -1820,14 +1835,14 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1820
1835
|
* Retorna tombstones (deleted: true) — o chamador decide se deve mostrá-los.
|
|
1821
1836
|
*/
|
|
1822
1837
|
async getDoc(e, t) {
|
|
1823
|
-
return await this.assertOpen().get(
|
|
1838
|
+
return await this.assertOpen().get(B, [e, t]) ?? null;
|
|
1824
1839
|
}
|
|
1825
1840
|
/**
|
|
1826
1841
|
* Insere ou atualiza um documento na store `documents`.
|
|
1827
1842
|
* O documento é identificado pelo keyPath composto `[collection, id]`.
|
|
1828
1843
|
*/
|
|
1829
1844
|
async putDoc(e) {
|
|
1830
|
-
await this.assertOpen().put(
|
|
1845
|
+
await this.assertOpen().put(B, e);
|
|
1831
1846
|
}
|
|
1832
1847
|
/**
|
|
1833
1848
|
* Marca um documento como tombstone (`deleted: true`).
|
|
@@ -1835,8 +1850,8 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1835
1850
|
* No-op se o documento não existir.
|
|
1836
1851
|
*/
|
|
1837
1852
|
async deleteDoc(e, t) {
|
|
1838
|
-
let r = this.assertOpen(), o = await r.get(
|
|
1839
|
-
o && await r.put(
|
|
1853
|
+
let r = this.assertOpen(), o = await r.get(B, [e, t]);
|
|
1854
|
+
o && await r.put(B, {
|
|
1840
1855
|
...o,
|
|
1841
1856
|
meta: {
|
|
1842
1857
|
...o.meta,
|
|
@@ -1852,8 +1867,8 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1852
1867
|
* e delega a filtragem/ordenação/paginação ao QueryEngine (que opera em memória).
|
|
1853
1868
|
*/
|
|
1854
1869
|
async listDocs(e, t) {
|
|
1855
|
-
let o = await this.assertOpen().getAllFromIndex(
|
|
1856
|
-
return
|
|
1870
|
+
let o = await this.assertOpen().getAllFromIndex(B, "by_collection", e);
|
|
1871
|
+
return X.evaluate(o, t);
|
|
1857
1872
|
}
|
|
1858
1873
|
// ─── Sync meta (key-value) ──────────────────────────────────────────────────
|
|
1859
1874
|
/**
|
|
@@ -1864,14 +1879,14 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1864
1879
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1865
1880
|
*/
|
|
1866
1881
|
async getMeta(e) {
|
|
1867
|
-
let r = await this.assertOpen().get(
|
|
1882
|
+
let r = await this.assertOpen().get(Se, e);
|
|
1868
1883
|
return r ? r.value : null;
|
|
1869
1884
|
}
|
|
1870
1885
|
/**
|
|
1871
1886
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1872
1887
|
*/
|
|
1873
1888
|
async setMeta(e, t) {
|
|
1874
|
-
await this.assertOpen().put(
|
|
1889
|
+
await this.assertOpen().put(Se, { key: e, value: t });
|
|
1875
1890
|
}
|
|
1876
1891
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1877
1892
|
/**
|
|
@@ -1879,7 +1894,7 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1879
1894
|
* O `id` é autoIncrement — não deve ser fornecido pelo chamador.
|
|
1880
1895
|
*/
|
|
1881
1896
|
async appendConflict(e) {
|
|
1882
|
-
let r = await this.assertOpen().add(
|
|
1897
|
+
let r = await this.assertOpen().add(Y, e);
|
|
1883
1898
|
return { ...e, id: r };
|
|
1884
1899
|
}
|
|
1885
1900
|
/**
|
|
@@ -1887,15 +1902,15 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1887
1902
|
* Filtra por `delivered` se a opção for fornecida.
|
|
1888
1903
|
*/
|
|
1889
1904
|
async listConflicts(e) {
|
|
1890
|
-
let r = await this.assertOpen().getAll(
|
|
1905
|
+
let r = await this.assertOpen().getAll(Y);
|
|
1891
1906
|
return e?.delivered !== void 0 ? r.filter((o) => o.delivered === e.delivered) : r;
|
|
1892
1907
|
}
|
|
1893
1908
|
/**
|
|
1894
1909
|
* Marca um registro do `conflict_log` como entregue ao produto.
|
|
1895
1910
|
*/
|
|
1896
1911
|
async markConflictDelivered(e) {
|
|
1897
|
-
let t = this.assertOpen(), r = await t.get(
|
|
1898
|
-
r && await t.put(
|
|
1912
|
+
let t = this.assertOpen(), r = await t.get(Y, e);
|
|
1913
|
+
r && await t.put(Y, { ...r, delivered: true });
|
|
1899
1914
|
}
|
|
1900
1915
|
// ─── Mutations (acessores usados pelo MutationQueue) ────────────────────────
|
|
1901
1916
|
/**
|
|
@@ -1903,13 +1918,13 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1903
1918
|
* Key: `mutationId`.
|
|
1904
1919
|
*/
|
|
1905
1920
|
async putMutation(e) {
|
|
1906
|
-
await this.assertOpen().put(
|
|
1921
|
+
await this.assertOpen().put(K, e);
|
|
1907
1922
|
}
|
|
1908
1923
|
/**
|
|
1909
1924
|
* Retorna uma mutação pelo `mutationId`, ou `null` se não existir.
|
|
1910
1925
|
*/
|
|
1911
1926
|
async getMutation(e) {
|
|
1912
|
-
return await this.assertOpen().get(
|
|
1927
|
+
return await this.assertOpen().get(K, e) ?? null;
|
|
1913
1928
|
}
|
|
1914
1929
|
/**
|
|
1915
1930
|
* Lista mutações com filtros opcionais.
|
|
@@ -1917,14 +1932,14 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1917
1932
|
*/
|
|
1918
1933
|
async listMutations(e) {
|
|
1919
1934
|
let t = this.assertOpen(), r;
|
|
1920
|
-
return e?.status !== void 0 ? r = await t.getAllFromIndex(
|
|
1935
|
+
return e?.status !== void 0 ? r = await t.getAllFromIndex(K, "by_status", e.status) : e?.collection !== void 0 && e.docId !== void 0 ? r = await t.getAllFromIndex(K, "by_doc", [e.collection, e.docId]) : r = await t.getAllFromIndex(K, "by_seq"), r.sort((o, i) => o.seq - i.seq);
|
|
1921
1936
|
}
|
|
1922
1937
|
/**
|
|
1923
1938
|
* Remove uma mutação pelo `mutationId`.
|
|
1924
1939
|
* No-op se não existir.
|
|
1925
1940
|
*/
|
|
1926
1941
|
async deleteMutation(e) {
|
|
1927
|
-
await this.assertOpen().delete(
|
|
1942
|
+
await this.assertOpen().delete(K, e);
|
|
1928
1943
|
}
|
|
1929
1944
|
// ─── Collection discovery ────────────────────────────────────────────────────
|
|
1930
1945
|
/**
|
|
@@ -1937,7 +1952,7 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1937
1952
|
* coletar os valores únicos de forma eficiente (sem carregar os docs completos).
|
|
1938
1953
|
*/
|
|
1939
1954
|
async listCollections() {
|
|
1940
|
-
let t = await this.assertOpen().getAllFromIndex(
|
|
1955
|
+
let t = await this.assertOpen().getAllFromIndex(B, "by_collection"), r = /* @__PURE__ */ new Set();
|
|
1941
1956
|
for (let o of t)
|
|
1942
1957
|
r.add(o.collection);
|
|
1943
1958
|
return Array.from(r);
|
|
@@ -1947,24 +1962,24 @@ var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1947
1962
|
* Retorna a entrada de `query_cache` para um hash de query, ou `null`.
|
|
1948
1963
|
*/
|
|
1949
1964
|
async getQueryCache(e) {
|
|
1950
|
-
return await this.assertOpen().get(
|
|
1965
|
+
return await this.assertOpen().get(ie, e) ?? null;
|
|
1951
1966
|
}
|
|
1952
1967
|
/**
|
|
1953
1968
|
* Insere ou atualiza uma entrada de `query_cache`.
|
|
1954
1969
|
*/
|
|
1955
1970
|
async putQueryCache(e) {
|
|
1956
|
-
await this.assertOpen().put(
|
|
1971
|
+
await this.assertOpen().put(ie, e);
|
|
1957
1972
|
}
|
|
1958
1973
|
/**
|
|
1959
1974
|
* Remove uma entrada de `query_cache` pelo queryHash.
|
|
1960
1975
|
*/
|
|
1961
1976
|
async deleteQueryCache(e) {
|
|
1962
|
-
await this.assertOpen().delete(
|
|
1977
|
+
await this.assertOpen().delete(ie, e);
|
|
1963
1978
|
}
|
|
1964
1979
|
};
|
|
1965
1980
|
|
|
1966
1981
|
// src/db/offline/memory-store.ts
|
|
1967
|
-
var
|
|
1982
|
+
var Re = class {
|
|
1968
1983
|
// store `documents` — key: `${collection}\0${id}`
|
|
1969
1984
|
_docs = /* @__PURE__ */ new Map();
|
|
1970
1985
|
// store `mutations` — key: mutationId
|
|
@@ -2012,7 +2027,7 @@ var Te = class {
|
|
|
2012
2027
|
let r = Array.from(this._docs.values()).filter(
|
|
2013
2028
|
(o) => o.collection === e
|
|
2014
2029
|
);
|
|
2015
|
-
return
|
|
2030
|
+
return X.evaluate(r, t);
|
|
2016
2031
|
}
|
|
2017
2032
|
// ─── Sync meta ──────────────────────────────────────────────────────────────
|
|
2018
2033
|
async getMeta(e) {
|
|
@@ -2069,13 +2084,13 @@ var Te = class {
|
|
|
2069
2084
|
};
|
|
2070
2085
|
|
|
2071
2086
|
// src/db/offline/mutation-queue.ts
|
|
2072
|
-
function
|
|
2087
|
+
function He() {
|
|
2073
2088
|
return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (n) => {
|
|
2074
2089
|
let e = Math.random() * 16 | 0;
|
|
2075
2090
|
return (n === "x" ? e : e & 3 | 8).toString(16);
|
|
2076
2091
|
});
|
|
2077
2092
|
}
|
|
2078
|
-
var
|
|
2093
|
+
var Ie = class {
|
|
2079
2094
|
store;
|
|
2080
2095
|
/**
|
|
2081
2096
|
* Ponteiro de sequência local.
|
|
@@ -2171,7 +2186,7 @@ var Re = class {
|
|
|
2171
2186
|
* Atômico: a persistência final é uma única operação putMutation.
|
|
2172
2187
|
*/
|
|
2173
2188
|
async enqueue(e) {
|
|
2174
|
-
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e, d = e.docId ?? (r === "add" ?
|
|
2189
|
+
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e, d = e.docId ?? (r === "add" ? He() : "");
|
|
2175
2190
|
await this.syncSeq();
|
|
2176
2191
|
let l = (await this.store.listMutations({ collection: t, docId: d })).filter((m) => m.status === "queued").sort((m, h) => h.seq - m.seq)[0];
|
|
2177
2192
|
if (l !== void 0) {
|
|
@@ -2180,7 +2195,7 @@ var Re = class {
|
|
|
2180
2195
|
if ("removed" in m)
|
|
2181
2196
|
return await this.store.deleteMutation(l.mutationId), {
|
|
2182
2197
|
seq: l.seq,
|
|
2183
|
-
mutationId:
|
|
2198
|
+
mutationId: He(),
|
|
2184
2199
|
collection: t,
|
|
2185
2200
|
docId: d,
|
|
2186
2201
|
op: "remove",
|
|
@@ -2198,7 +2213,7 @@ var Re = class {
|
|
|
2198
2213
|
}
|
|
2199
2214
|
let f = {
|
|
2200
2215
|
seq: this.nextSeq(),
|
|
2201
|
-
mutationId:
|
|
2216
|
+
mutationId: He(),
|
|
2202
2217
|
collection: t,
|
|
2203
2218
|
docId: d,
|
|
2204
2219
|
op: r,
|
|
@@ -2311,55 +2326,55 @@ var Re = class {
|
|
|
2311
2326
|
};
|
|
2312
2327
|
|
|
2313
2328
|
// src/db/field-value.ts
|
|
2314
|
-
var
|
|
2315
|
-
function
|
|
2329
|
+
var Qe = "__neetru__";
|
|
2330
|
+
function Jr() {
|
|
2316
2331
|
return { __neetru__: "serverTimestamp" };
|
|
2317
2332
|
}
|
|
2318
|
-
function
|
|
2333
|
+
function Xr(n) {
|
|
2319
2334
|
if (typeof n != "number" || !Number.isFinite(n))
|
|
2320
2335
|
throw new TypeError(
|
|
2321
2336
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(n)}).`
|
|
2322
2337
|
);
|
|
2323
2338
|
return { __neetru__: "increment", operand: n };
|
|
2324
2339
|
}
|
|
2325
|
-
function
|
|
2326
|
-
return typeof n == "object" && n !== null &&
|
|
2340
|
+
function Q(n) {
|
|
2341
|
+
return typeof n == "object" && n !== null && Qe in n && typeof n[Qe] == "string";
|
|
2327
2342
|
}
|
|
2328
|
-
function
|
|
2329
|
-
return
|
|
2343
|
+
function Zt(n) {
|
|
2344
|
+
return Q(n) && n.__neetru__ === "serverTimestamp";
|
|
2330
2345
|
}
|
|
2331
|
-
function
|
|
2332
|
-
return
|
|
2346
|
+
function en(n) {
|
|
2347
|
+
return Q(n) && n.__neetru__ === "increment" && typeof n.operand == "number" && Number.isFinite(n.operand);
|
|
2333
2348
|
}
|
|
2334
|
-
function
|
|
2335
|
-
return
|
|
2349
|
+
function Yr(n, e) {
|
|
2350
|
+
return Zt(n) ? Date.now() : en(n) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + n.operand : n;
|
|
2336
2351
|
}
|
|
2337
2352
|
function G(n, e) {
|
|
2338
2353
|
let t = false, r = {};
|
|
2339
2354
|
for (let [o, i] of Object.entries(n))
|
|
2340
|
-
|
|
2355
|
+
Q(i) ? (t = true, r[o] = Yr(i, e?.[o])) : r[o] = i;
|
|
2341
2356
|
return t ? r : n;
|
|
2342
2357
|
}
|
|
2343
|
-
function
|
|
2358
|
+
function Ge(n) {
|
|
2344
2359
|
for (let e of Object.values(n))
|
|
2345
|
-
if (
|
|
2360
|
+
if (Q(e)) return true;
|
|
2346
2361
|
return false;
|
|
2347
2362
|
}
|
|
2348
2363
|
|
|
2349
2364
|
// src/db/offline/sync-engine.ts
|
|
2350
|
-
var
|
|
2351
|
-
function
|
|
2352
|
-
return n !== null &&
|
|
2365
|
+
var Zr = 8, eo = 1e3, to = 300 * 1e3, Je = "truncatedCollections";
|
|
2366
|
+
function no(n) {
|
|
2367
|
+
return n !== null && Ge(n);
|
|
2353
2368
|
}
|
|
2354
|
-
function
|
|
2369
|
+
function ro(n) {
|
|
2355
2370
|
if (n === null) return null;
|
|
2356
|
-
if (!
|
|
2371
|
+
if (!Ge(n)) return n;
|
|
2357
2372
|
let e = {};
|
|
2358
2373
|
for (let [t, r] of Object.entries(n))
|
|
2359
|
-
|
|
2374
|
+
Q(r) || (e[t] = r);
|
|
2360
2375
|
return e;
|
|
2361
2376
|
}
|
|
2362
|
-
var
|
|
2377
|
+
var Ce = class {
|
|
2363
2378
|
_store;
|
|
2364
2379
|
_queue;
|
|
2365
2380
|
_resolver;
|
|
@@ -2553,13 +2568,13 @@ var Ie = class {
|
|
|
2553
2568
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
2554
2569
|
return;
|
|
2555
2570
|
}
|
|
2556
|
-
let p =
|
|
2571
|
+
let p = ro(i.payload) ?? a?.data ?? {}, l = {
|
|
2557
2572
|
collection: e,
|
|
2558
2573
|
id: t,
|
|
2559
2574
|
data: i.op === "update" ? { ...a?.data ?? {}, ...p } : (
|
|
2560
2575
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
2561
2576
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
2562
|
-
|
|
2577
|
+
no(i.payload) ? { ...a?.data ?? {}, ...p } : p
|
|
2563
2578
|
),
|
|
2564
2579
|
meta: {
|
|
2565
2580
|
serverVersion: r,
|
|
@@ -2712,7 +2727,7 @@ var Ie = class {
|
|
|
2712
2727
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
2713
2728
|
*/
|
|
2714
2729
|
async _retryOrDeadLetter(e, t, r) {
|
|
2715
|
-
if (e.attempts + 1 <
|
|
2730
|
+
if (e.attempts + 1 < Zr) {
|
|
2716
2731
|
await this._queue.markRetry(e.mutationId, t);
|
|
2717
2732
|
return;
|
|
2718
2733
|
}
|
|
@@ -2753,7 +2768,7 @@ var Ie = class {
|
|
|
2753
2768
|
}
|
|
2754
2769
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
2755
2770
|
_backoffDelayMs(e) {
|
|
2756
|
-
return Math.min(
|
|
2771
|
+
return Math.min(eo * 2 ** (e - 1), to);
|
|
2757
2772
|
}
|
|
2758
2773
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
2759
2774
|
/**
|
|
@@ -2786,7 +2801,7 @@ var Ie = class {
|
|
|
2786
2801
|
let r = [];
|
|
2787
2802
|
await this._applyServerDocs(t.docs, r, true);
|
|
2788
2803
|
let o = t.truncatedCollections ?? [], i = new Set(o);
|
|
2789
|
-
await this._store.setMeta(
|
|
2804
|
+
await this._store.setMeta(Je, o);
|
|
2790
2805
|
let s = new Set(
|
|
2791
2806
|
t.docs.map((a) => `${a.collection}::${a.id}`)
|
|
2792
2807
|
);
|
|
@@ -3005,10 +3020,10 @@ var Ie = class {
|
|
|
3005
3020
|
};
|
|
3006
3021
|
|
|
3007
3022
|
// src/db/offline/conflict-resolver.ts
|
|
3008
|
-
function
|
|
3023
|
+
function tn(n, e) {
|
|
3009
3024
|
return e === null ? false : n === null ? true : e > n;
|
|
3010
3025
|
}
|
|
3011
|
-
var
|
|
3026
|
+
var Ae = class n {
|
|
3012
3027
|
/**
|
|
3013
3028
|
* Resolve um conflito com base na operação da mutação (auto-dispatch).
|
|
3014
3029
|
*
|
|
@@ -3042,7 +3057,7 @@ var Ce = class n {
|
|
|
3042
3057
|
resolvedData: t.data,
|
|
3043
3058
|
conflictRecord: null
|
|
3044
3059
|
};
|
|
3045
|
-
if (!
|
|
3060
|
+
if (!tn(
|
|
3046
3061
|
e.meta.updatedAtServer,
|
|
3047
3062
|
t.meta.updatedAtServer
|
|
3048
3063
|
))
|
|
@@ -3089,7 +3104,7 @@ var Ce = class n {
|
|
|
3089
3104
|
let i = t.data;
|
|
3090
3105
|
if (e === null)
|
|
3091
3106
|
return { outcome: "no_conflict", resolvedData: { ...i, ...r.payload ?? {} }, conflictRecord: null };
|
|
3092
|
-
if (!
|
|
3107
|
+
if (!tn(
|
|
3093
3108
|
e.meta.updatedAtServer,
|
|
3094
3109
|
t.meta.updatedAtServer
|
|
3095
3110
|
))
|
|
@@ -3167,10 +3182,10 @@ var Ce = class n {
|
|
|
3167
3182
|
};
|
|
3168
3183
|
|
|
3169
3184
|
// src/db/offline/change-bus.ts
|
|
3170
|
-
function
|
|
3185
|
+
function oo(n) {
|
|
3171
3186
|
return `${n.collection}::${n.doc.id}`;
|
|
3172
3187
|
}
|
|
3173
|
-
var
|
|
3188
|
+
var Ee = class {
|
|
3174
3189
|
/** Listeners filtrados por coleção. */
|
|
3175
3190
|
_collectionListeners = /* @__PURE__ */ new Map();
|
|
3176
3191
|
/** Listeners globais (todas as coleções). */
|
|
@@ -3215,7 +3230,7 @@ var Ae = class {
|
|
|
3215
3230
|
if (e.length === 0) return;
|
|
3216
3231
|
let t = /* @__PURE__ */ new Map();
|
|
3217
3232
|
for (let i of e)
|
|
3218
|
-
t.set(
|
|
3233
|
+
t.set(oo(i), i);
|
|
3219
3234
|
let r = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
3220
3235
|
for (let i of r)
|
|
3221
3236
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
@@ -3254,7 +3269,7 @@ var Ae = class {
|
|
|
3254
3269
|
};
|
|
3255
3270
|
|
|
3256
3271
|
// src/db/offline/connectivity-monitor.ts
|
|
3257
|
-
var
|
|
3272
|
+
var Pe = class {
|
|
3258
3273
|
_state;
|
|
3259
3274
|
_listeners = /* @__PURE__ */ new Set();
|
|
3260
3275
|
_nav;
|
|
@@ -3356,7 +3371,7 @@ var Ee = class {
|
|
|
3356
3371
|
};
|
|
3357
3372
|
|
|
3358
3373
|
// src/db/offline/tab-coordinator.ts
|
|
3359
|
-
var
|
|
3374
|
+
var se = class {
|
|
3360
3375
|
_lockName;
|
|
3361
3376
|
_channelName;
|
|
3362
3377
|
_lockManager;
|
|
@@ -3533,26 +3548,26 @@ var ie = class {
|
|
|
3533
3548
|
};
|
|
3534
3549
|
|
|
3535
3550
|
// src/db/collection-ref.ts
|
|
3536
|
-
|
|
3537
|
-
function
|
|
3551
|
+
H();
|
|
3552
|
+
function io() {
|
|
3538
3553
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
3539
3554
|
}
|
|
3540
|
-
var
|
|
3541
|
-
function
|
|
3542
|
-
if (!
|
|
3555
|
+
var nn = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
3556
|
+
function so(n) {
|
|
3557
|
+
if (!nn.test(n))
|
|
3543
3558
|
throw new exports.NeetruDbError(
|
|
3544
3559
|
"db_invalid_query",
|
|
3545
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${
|
|
3560
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${nn}.`
|
|
3546
3561
|
);
|
|
3547
3562
|
}
|
|
3548
|
-
function
|
|
3563
|
+
function Z(n, e = "id") {
|
|
3549
3564
|
if (!n || typeof n != "string" || n.trim() === "")
|
|
3550
3565
|
throw new exports.NeetruDbError(
|
|
3551
3566
|
"db_invalid_query",
|
|
3552
3567
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
3553
3568
|
);
|
|
3554
3569
|
}
|
|
3555
|
-
function
|
|
3570
|
+
function ao(n) {
|
|
3556
3571
|
let e = {};
|
|
3557
3572
|
if (n?.where && (e.where = n.where.map((t) => ({
|
|
3558
3573
|
field: t.field,
|
|
@@ -3569,12 +3584,12 @@ function no(n) {
|
|
|
3569
3584
|
}
|
|
3570
3585
|
return e;
|
|
3571
3586
|
}
|
|
3572
|
-
function
|
|
3587
|
+
function co(n, e) {
|
|
3573
3588
|
if (n.length < e) return null;
|
|
3574
3589
|
let t = n[n.length - 1];
|
|
3575
3590
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
3576
3591
|
}
|
|
3577
|
-
var
|
|
3592
|
+
var Xe = class {
|
|
3578
3593
|
constructor(e, t, r, o, i, s) {
|
|
3579
3594
|
this._collection = e;
|
|
3580
3595
|
this._store = t;
|
|
@@ -3616,7 +3631,7 @@ var Qe = class {
|
|
|
3616
3631
|
}
|
|
3617
3632
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
3618
3633
|
async _buildListResult(e) {
|
|
3619
|
-
let t =
|
|
3634
|
+
let t = ao(e), r = t.limit ?? 20, o = await this._store.listDocs(this._collection, t), i = await this._hasPendingWrites(), s = this._isOnline(), a = this._engine.getSyncState(), d = co(o.docs, r), p = await this._store.getMeta(Je), l = Array.isArray(p) && p.includes(this._collection), u = !s || a.status === "offline" || l, f = i || a.lastSyncedAt === null || u;
|
|
3620
3635
|
return {
|
|
3621
3636
|
docs: o.docs.map((m) => ({ id: m.id, data: m.data })),
|
|
3622
3637
|
nextCursor: d,
|
|
@@ -3629,7 +3644,7 @@ var Qe = class {
|
|
|
3629
3644
|
}
|
|
3630
3645
|
// ── CRUD ────────────────────────────────────────────────────────────────────
|
|
3631
3646
|
async get(e) {
|
|
3632
|
-
return
|
|
3647
|
+
return Z(e), this._buildGetResult(e);
|
|
3633
3648
|
}
|
|
3634
3649
|
async list(e) {
|
|
3635
3650
|
return this._buildListResult(e);
|
|
@@ -3662,7 +3677,7 @@ var Qe = class {
|
|
|
3662
3677
|
}]), { ok: true, id: i };
|
|
3663
3678
|
}
|
|
3664
3679
|
async set(e, t) {
|
|
3665
|
-
|
|
3680
|
+
Z(e);
|
|
3666
3681
|
let r = await this._store.getDoc(this._collection, e), o = t, i = G(o, void 0), s = await this._queue.enqueue({
|
|
3667
3682
|
collection: this._collection,
|
|
3668
3683
|
docId: e,
|
|
@@ -3691,7 +3706,7 @@ var Qe = class {
|
|
|
3691
3706
|
}]), { ok: true };
|
|
3692
3707
|
}
|
|
3693
3708
|
async update(e, t, r) {
|
|
3694
|
-
|
|
3709
|
+
Z(e);
|
|
3695
3710
|
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a = G(i, s), d = o?.meta.deleted ? { ...a } : { ...o?.data ?? {}, ...a }, p = await this._queue.enqueue({
|
|
3696
3711
|
collection: this._collection,
|
|
3697
3712
|
docId: e,
|
|
@@ -3725,7 +3740,7 @@ var Qe = class {
|
|
|
3725
3740
|
}]), { ok: true };
|
|
3726
3741
|
}
|
|
3727
3742
|
async remove(e) {
|
|
3728
|
-
|
|
3743
|
+
Z(e);
|
|
3729
3744
|
let t = await this._store.getDoc(this._collection, e);
|
|
3730
3745
|
return !t || t.meta.deleted, await this._queue.enqueue({
|
|
3731
3746
|
collection: this._collection,
|
|
@@ -3837,7 +3852,7 @@ var Qe = class {
|
|
|
3837
3852
|
}
|
|
3838
3853
|
// ── Realtime ─────────────────────────────────────────────────────────────────
|
|
3839
3854
|
onDoc(e, t) {
|
|
3840
|
-
return
|
|
3855
|
+
return Z(e), this._buildGetResult(e).then((r) => {
|
|
3841
3856
|
t(r ? r.docs[0]?.data ?? null : null);
|
|
3842
3857
|
}).catch(() => t(null)), this._bus.subscribe(this._collection, (r) => {
|
|
3843
3858
|
let o = r.find((i) => i.doc.id === e);
|
|
@@ -3896,7 +3911,7 @@ var Qe = class {
|
|
|
3896
3911
|
};
|
|
3897
3912
|
}
|
|
3898
3913
|
doc(e) {
|
|
3899
|
-
|
|
3914
|
+
Z(e);
|
|
3900
3915
|
let t = this;
|
|
3901
3916
|
return {
|
|
3902
3917
|
get id() {
|
|
@@ -3949,7 +3964,7 @@ var Qe = class {
|
|
|
3949
3964
|
}
|
|
3950
3965
|
};
|
|
3951
3966
|
}
|
|
3952
|
-
},
|
|
3967
|
+
}, Ye = class {
|
|
3953
3968
|
_store;
|
|
3954
3969
|
_queue;
|
|
3955
3970
|
_bus;
|
|
@@ -3959,7 +3974,7 @@ var Qe = class {
|
|
|
3959
3974
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3960
3975
|
}
|
|
3961
3976
|
collection(e) {
|
|
3962
|
-
return
|
|
3977
|
+
return so(e), new Xe(
|
|
3963
3978
|
e,
|
|
3964
3979
|
this._store,
|
|
3965
3980
|
this._queue,
|
|
@@ -3988,10 +4003,10 @@ var Qe = class {
|
|
|
3988
4003
|
return this._engine.onError(e);
|
|
3989
4004
|
}
|
|
3990
4005
|
};
|
|
3991
|
-
async function
|
|
3992
|
-
let e =
|
|
4006
|
+
async function rn(n) {
|
|
4007
|
+
let e = io() ? new Re() : new Te(n.dbName);
|
|
3993
4008
|
await e.open();
|
|
3994
|
-
let t = new
|
|
4009
|
+
let t = new Ie(e), r = new Ae(), o = new Ee(), i = new Pe({
|
|
3995
4010
|
// Se startOnline=false, injeta um navigator falso
|
|
3996
4011
|
navigator: n.startOnline === false ? { onLine: false } : typeof navigator < "u" ? navigator : void 0
|
|
3997
4012
|
}), s = n.singleTab ? (
|
|
@@ -4001,10 +4016,10 @@ async function tn(n) {
|
|
|
4001
4016
|
onRoleChange: (d) => () => {
|
|
4002
4017
|
}
|
|
4003
4018
|
}
|
|
4004
|
-
) : new
|
|
4019
|
+
) : new se({
|
|
4005
4020
|
lockName: `neetru-offline:${n.dbName}`,
|
|
4006
4021
|
channelName: `neetru-offline:${n.dbName}`
|
|
4007
|
-
}), a = new
|
|
4022
|
+
}), a = new Ce({
|
|
4008
4023
|
store: e,
|
|
4009
4024
|
queue: t,
|
|
4010
4025
|
resolver: r,
|
|
@@ -4014,7 +4029,7 @@ async function tn(n) {
|
|
|
4014
4029
|
connectivity: i,
|
|
4015
4030
|
periodicSyncIntervalMs: n.periodicSyncIntervalMs ?? 300 * 1e3
|
|
4016
4031
|
});
|
|
4017
|
-
return i.start(), !n.singleTab && s instanceof
|
|
4032
|
+
return i.start(), !n.singleTab && s instanceof se && s.start(), new Ye({
|
|
4018
4033
|
store: e,
|
|
4019
4034
|
queue: t,
|
|
4020
4035
|
bus: o,
|
|
@@ -4024,11 +4039,11 @@ async function tn(n) {
|
|
|
4024
4039
|
}
|
|
4025
4040
|
|
|
4026
4041
|
// src/db/realtime/realtime-client.ts
|
|
4027
|
-
var
|
|
4028
|
-
function
|
|
4029
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
4042
|
+
var lo = 0;
|
|
4043
|
+
function on() {
|
|
4044
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++lo}-${Math.random().toString(36).slice(2)}`;
|
|
4030
4045
|
}
|
|
4031
|
-
var
|
|
4046
|
+
var De = class {
|
|
4032
4047
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
4033
4048
|
_gatewayUrl;
|
|
4034
4049
|
_wsFactory;
|
|
@@ -4066,7 +4081,7 @@ var Pe = class {
|
|
|
4066
4081
|
_pendingFrames = [];
|
|
4067
4082
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
4068
4083
|
constructor(e) {
|
|
4069
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
4084
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? uo, this._setTimeout = e.setTimeoutFn ?? globalThis.setTimeout.bind(globalThis), this._clearTimeout = e.clearTimeoutFn ?? globalThis.clearTimeout.bind(globalThis), this._backoffBaseMs = e.backoffBaseMs ?? 1e3, this._backoffMaxMs = e.backoffMaxMs ?? 3e4, this._heartbeatIntervalMs = e.heartbeatIntervalMs ?? 25e3, this._ticketProvider = e.ticketProvider, this._dbId = e.dbId, this._connect();
|
|
4070
4085
|
}
|
|
4071
4086
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
4072
4087
|
/**
|
|
@@ -4080,8 +4095,8 @@ var Pe = class {
|
|
|
4080
4095
|
*/
|
|
4081
4096
|
subscribe(e, t, r) {
|
|
4082
4097
|
if (this._closed)
|
|
4083
|
-
return
|
|
4084
|
-
let o =
|
|
4098
|
+
return on();
|
|
4099
|
+
let o = on();
|
|
4085
4100
|
this._subscriptions.set(o, {
|
|
4086
4101
|
subscriptionId: o,
|
|
4087
4102
|
collection: e,
|
|
@@ -4345,7 +4360,7 @@ var Pe = class {
|
|
|
4345
4360
|
_detachHandlers(e) {
|
|
4346
4361
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
4347
4362
|
}
|
|
4348
|
-
},
|
|
4363
|
+
}, uo = (n) => {
|
|
4349
4364
|
if (typeof WebSocket > "u")
|
|
4350
4365
|
throw new Error(
|
|
4351
4366
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -4354,11 +4369,11 @@ var Pe = class {
|
|
|
4354
4369
|
};
|
|
4355
4370
|
|
|
4356
4371
|
// src/db/client-db.ts
|
|
4357
|
-
var
|
|
4358
|
-
function
|
|
4359
|
-
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
4372
|
+
var To = (n) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}`, xe = (n, e) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}/${encodeURIComponent(e)}`, yn = 200;
|
|
4373
|
+
function Ro(n, e, t) {
|
|
4374
|
+
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ? Io(e.realtimeGatewayUrl, t, e) : bn(t);
|
|
4360
4375
|
}
|
|
4361
|
-
function
|
|
4376
|
+
function bn(n) {
|
|
4362
4377
|
return {
|
|
4363
4378
|
async pushMutations(e) {
|
|
4364
4379
|
if (!n)
|
|
@@ -4366,34 +4381,34 @@ function yn(n) {
|
|
|
4366
4381
|
"db_unavailable",
|
|
4367
4382
|
"[RestSyncTransport] config n\xE3o dispon\xEDvel \u2014 n\xE3o \xE9 poss\xEDvel enviar ao Core. Inicialize o transporte via createNeetruDb."
|
|
4368
4383
|
);
|
|
4369
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4384
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (F(), re)), r = [], o = n.tenantId ?? n.productId, i = o ? { "x-neetru-tenant": o } : void 0;
|
|
4370
4385
|
for (let s of e)
|
|
4371
4386
|
try {
|
|
4372
4387
|
let a;
|
|
4373
4388
|
s.op === "add" ? a = await t(n, {
|
|
4374
4389
|
method: "PUT",
|
|
4375
|
-
path:
|
|
4390
|
+
path: xe(s.collection, s.docId),
|
|
4376
4391
|
body: { data: s.payload ?? {} },
|
|
4377
4392
|
requireAuth: !0,
|
|
4378
4393
|
retries: 2,
|
|
4379
4394
|
headers: i
|
|
4380
4395
|
}) : s.op === "set" ? a = await t(n, {
|
|
4381
4396
|
method: "PUT",
|
|
4382
|
-
path:
|
|
4397
|
+
path: xe(s.collection, s.docId),
|
|
4383
4398
|
body: { data: s.payload ?? {} },
|
|
4384
4399
|
requireAuth: !0,
|
|
4385
4400
|
retries: 2,
|
|
4386
4401
|
headers: i
|
|
4387
4402
|
}) : s.op === "update" ? a = await t(n, {
|
|
4388
4403
|
method: "PATCH",
|
|
4389
|
-
path:
|
|
4404
|
+
path: xe(s.collection, s.docId),
|
|
4390
4405
|
body: { data: s.payload ?? {}, ...s.upsert ? { upsert: !0 } : {} },
|
|
4391
4406
|
requireAuth: !0,
|
|
4392
4407
|
retries: 2,
|
|
4393
4408
|
headers: i
|
|
4394
4409
|
}) : a = await t(n, {
|
|
4395
4410
|
method: "DELETE",
|
|
4396
|
-
path:
|
|
4411
|
+
path: xe(s.collection, s.docId),
|
|
4397
4412
|
requireAuth: !0,
|
|
4398
4413
|
retries: 2,
|
|
4399
4414
|
headers: i
|
|
@@ -4430,13 +4445,13 @@ function yn(n) {
|
|
|
4430
4445
|
async fullResync(e) {
|
|
4431
4446
|
if (!n)
|
|
4432
4447
|
return { docs: [], newWatermark: Date.now() };
|
|
4433
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4448
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (F(), re)), r = n.tenantId ?? n.productId, o = r ? { "x-neetru-tenant": r } : void 0, i = Date.now(), s = [], a = [];
|
|
4434
4449
|
for (let d of e) {
|
|
4435
4450
|
let p;
|
|
4436
4451
|
try {
|
|
4437
4452
|
p = await t(n, {
|
|
4438
4453
|
method: "GET",
|
|
4439
|
-
path:
|
|
4454
|
+
path: To(d),
|
|
4440
4455
|
requireAuth: !0,
|
|
4441
4456
|
retries: 2,
|
|
4442
4457
|
headers: o,
|
|
@@ -4444,7 +4459,7 @@ function yn(n) {
|
|
|
4444
4459
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
4445
4460
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
4446
4461
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
4447
|
-
query: { limit:
|
|
4462
|
+
query: { limit: yn }
|
|
4448
4463
|
});
|
|
4449
4464
|
} catch (f) {
|
|
4450
4465
|
let m = f instanceof Error ? f.message : String(f);
|
|
@@ -4454,26 +4469,26 @@ function yn(n) {
|
|
|
4454
4469
|
);
|
|
4455
4470
|
}
|
|
4456
4471
|
let l = p, u = Array.isArray(l?.items) ? l.items : [];
|
|
4457
|
-
u.length >=
|
|
4472
|
+
u.length >= yn && a.push(d);
|
|
4458
4473
|
for (let f of u) {
|
|
4459
4474
|
if (!f || typeof f != "object") continue;
|
|
4460
4475
|
let m = f, h = typeof m.id == "string" ? m.id : null;
|
|
4461
4476
|
if (!h) continue;
|
|
4462
|
-
let { id:
|
|
4477
|
+
let { id: _, ...I } = m, w = i, k = I._updatedAt;
|
|
4463
4478
|
if (typeof k == "number")
|
|
4464
4479
|
w = k;
|
|
4465
4480
|
else if (k !== null && typeof k == "object" && typeof k._seconds == "number")
|
|
4466
4481
|
w = k._seconds * 1e3;
|
|
4467
4482
|
else if (typeof k == "string") {
|
|
4468
|
-
let
|
|
4469
|
-
Number.isNaN(
|
|
4483
|
+
let y = Date.parse(k);
|
|
4484
|
+
Number.isNaN(y) || (w = y);
|
|
4470
4485
|
}
|
|
4471
|
-
let
|
|
4486
|
+
let D = `rest_${h}_${w}`;
|
|
4472
4487
|
s.push({
|
|
4473
4488
|
collection: d,
|
|
4474
4489
|
id: h,
|
|
4475
4490
|
data: I,
|
|
4476
|
-
serverVersion:
|
|
4491
|
+
serverVersion: D,
|
|
4477
4492
|
serverTimestamp: w,
|
|
4478
4493
|
deleted: false
|
|
4479
4494
|
});
|
|
@@ -4483,10 +4498,10 @@ function yn(n) {
|
|
|
4483
4498
|
}
|
|
4484
4499
|
};
|
|
4485
4500
|
}
|
|
4486
|
-
function
|
|
4487
|
-
let r =
|
|
4501
|
+
function Io(n, e, t) {
|
|
4502
|
+
let r = bn(e), o = new De({
|
|
4488
4503
|
gatewayUrl: n,
|
|
4489
|
-
ticketProvider:
|
|
4504
|
+
ticketProvider: Co(e, t),
|
|
4490
4505
|
dbId: t.dbId,
|
|
4491
4506
|
webSocketFactory: t._wsFactory
|
|
4492
4507
|
});
|
|
@@ -4522,9 +4537,9 @@ function wo(n, e, t) {
|
|
|
4522
4537
|
}
|
|
4523
4538
|
};
|
|
4524
4539
|
}
|
|
4525
|
-
function
|
|
4540
|
+
function Co(n, e) {
|
|
4526
4541
|
return async () => {
|
|
4527
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4542
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (F(), re));
|
|
4528
4543
|
return await t(n, {
|
|
4529
4544
|
method: "POST",
|
|
4530
4545
|
path: "/api/sdk/v1/db/realtime/ticket",
|
|
@@ -4537,21 +4552,21 @@ function ko(n, e) {
|
|
|
4537
4552
|
});
|
|
4538
4553
|
};
|
|
4539
4554
|
}
|
|
4540
|
-
var
|
|
4541
|
-
function
|
|
4542
|
-
|
|
4555
|
+
var _n = false;
|
|
4556
|
+
function Ao(n, e) {
|
|
4557
|
+
_n || e.dbId && n.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (_n = true, console.warn(
|
|
4543
4558
|
'[neetru/sdk] client.db recebeu `dbId` ("' + e.dbId + '") mas ele N\xC3O roteia nem escopa nada no servidor: as escritas do mundo Documentos (add/set/update/remove/list) SEMPRE caem no datastore gen\xE9rico do Core, escopado s\xF3 por tenant/produto (header x-neetru-tenant). `dbId` apenas nomeia o cache offline LOCAL (IndexedDB). ATEN\xC7\xC3O: usar dbIds distintos no MESMO tenant/produto N\xC3O isola os dados no servidor \u2014 eles se misturam (co-mingling). Veja o JSDoc de NeetruDbOptions.dbId.'
|
|
4544
4559
|
)));
|
|
4545
4560
|
}
|
|
4546
|
-
function
|
|
4547
|
-
|
|
4561
|
+
function ot(n, e = {}) {
|
|
4562
|
+
Ao(n, e);
|
|
4548
4563
|
let t;
|
|
4549
4564
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
4550
4565
|
'[neetru/sdk] NeetruDbOptions.engine est\xE1 depreciado e ser\xE1 removido em v3.0. Use NeetruDbOptions.transport em vez de engine. Valores v\xE1lidos: "rest" | "firestore" | "nosql-vm". Nota: "engine" no SDK configura o TRANSPORTE (REST/Firestore/WebSocket), n\xE3o o storage engine do admin database (firestore-instance, cloud-sql-postgres, \u2026).'
|
|
4551
4566
|
), t = e.engine) : t = "rest";
|
|
4552
|
-
let r =
|
|
4567
|
+
let r = Ro(t, e, n), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${n.productId ?? "sdk"}__${o}__${n.env}`, s = null, a = null;
|
|
4553
4568
|
function d() {
|
|
4554
|
-
return s || (s =
|
|
4569
|
+
return s || (s = rn({
|
|
4555
4570
|
dbName: i,
|
|
4556
4571
|
transport: r,
|
|
4557
4572
|
singleTab: e.singleTab ?? n.env !== "prod"
|
|
@@ -4559,15 +4574,15 @@ function nt(n, e = {}) {
|
|
|
4559
4574
|
}
|
|
4560
4575
|
return {
|
|
4561
4576
|
collection(p) {
|
|
4562
|
-
return a ? a.collection(p) :
|
|
4577
|
+
return a ? a.collection(p) : Eo(p, d);
|
|
4563
4578
|
},
|
|
4564
4579
|
async sql(p, l) {
|
|
4565
|
-
if (n.env
|
|
4580
|
+
if (V(n.env))
|
|
4566
4581
|
throw new exports.NeetruDbError(
|
|
4567
4582
|
"db_unavailable",
|
|
4568
4583
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
4569
4584
|
);
|
|
4570
|
-
return (await Promise.resolve().then(() => (
|
|
4585
|
+
return (await Promise.resolve().then(() => (gn(), hn))).createSqlClientFromConfig(n, p, l);
|
|
4571
4586
|
},
|
|
4572
4587
|
get syncState() {
|
|
4573
4588
|
return a?.syncState ?? {
|
|
@@ -4606,7 +4621,7 @@ function nt(n, e = {}) {
|
|
|
4606
4621
|
}
|
|
4607
4622
|
};
|
|
4608
4623
|
}
|
|
4609
|
-
function
|
|
4624
|
+
function Eo(n, e) {
|
|
4610
4625
|
async function t() {
|
|
4611
4626
|
return (await e()).collection(n);
|
|
4612
4627
|
}
|
|
@@ -4685,9 +4700,9 @@ function To(n, e) {
|
|
|
4685
4700
|
}
|
|
4686
4701
|
|
|
4687
4702
|
// src/checkout.ts
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
function
|
|
4703
|
+
A();
|
|
4704
|
+
F();
|
|
4705
|
+
function it(n, e) {
|
|
4691
4706
|
let t = n?.accessToken || n?.idToken;
|
|
4692
4707
|
if (!t || typeof t != "string")
|
|
4693
4708
|
throw new exports.NeetruError(
|
|
@@ -4696,7 +4711,7 @@ function rt(n, e) {
|
|
|
4696
4711
|
);
|
|
4697
4712
|
return { requireAuth: false, headers: { authorization: `Bearer ${t}` } };
|
|
4698
4713
|
}
|
|
4699
|
-
function
|
|
4714
|
+
function Po(n) {
|
|
4700
4715
|
if (!n || typeof n != "object")
|
|
4701
4716
|
throw new exports.NeetruError("invalid_response", "checkout.start response is not an object");
|
|
4702
4717
|
let e = n;
|
|
@@ -4712,7 +4727,7 @@ function Ro(n) {
|
|
|
4712
4727
|
requiresKyc: e.requiresKyc === true
|
|
4713
4728
|
};
|
|
4714
4729
|
}
|
|
4715
|
-
function
|
|
4730
|
+
function Do(n) {
|
|
4716
4731
|
if (!n || typeof n != "object")
|
|
4717
4732
|
throw new exports.NeetruError("invalid_response", "checkout.get response is not an object");
|
|
4718
4733
|
let e = n, t = e.intent;
|
|
@@ -4734,20 +4749,20 @@ function Io(n) {
|
|
|
4734
4749
|
isStale: e.isStale === true
|
|
4735
4750
|
};
|
|
4736
4751
|
}
|
|
4737
|
-
function
|
|
4752
|
+
function No() {
|
|
4738
4753
|
try {
|
|
4739
4754
|
return typeof globalThis < "u" && typeof globalThis.window < "u" && typeof globalThis.location < "u" && typeof globalThis.location.assign == "function";
|
|
4740
4755
|
} catch {
|
|
4741
4756
|
return false;
|
|
4742
4757
|
}
|
|
4743
4758
|
}
|
|
4744
|
-
function
|
|
4759
|
+
function xo(n) {
|
|
4745
4760
|
try {
|
|
4746
4761
|
globalThis.location.assign(n);
|
|
4747
4762
|
} catch {
|
|
4748
4763
|
}
|
|
4749
4764
|
}
|
|
4750
|
-
function
|
|
4765
|
+
function Oo(n) {
|
|
4751
4766
|
return {
|
|
4752
4767
|
async start(e) {
|
|
4753
4768
|
if (!e?.productId)
|
|
@@ -4762,7 +4777,7 @@ function Eo(n) {
|
|
|
4762
4777
|
callbackUrl: e.callbackUrl
|
|
4763
4778
|
};
|
|
4764
4779
|
e.tenantType && (t.targetTenantType = e.tenantType), e.tenantId && (t.targetTenantId = e.tenantId);
|
|
4765
|
-
let { requireAuth: r, headers: o } =
|
|
4780
|
+
let { requireAuth: r, headers: o } = it(
|
|
4766
4781
|
{ accessToken: e.accessToken, idToken: e.idToken },
|
|
4767
4782
|
"start"
|
|
4768
4783
|
), i = await S(n, {
|
|
@@ -4772,13 +4787,13 @@ function Eo(n) {
|
|
|
4772
4787
|
requireAuth: r,
|
|
4773
4788
|
headers: o,
|
|
4774
4789
|
idempotencyKey: true
|
|
4775
|
-
}), s =
|
|
4776
|
-
return e.autoRedirect !== false &&
|
|
4790
|
+
}), s = Po(i);
|
|
4791
|
+
return e.autoRedirect !== false && No() && xo(s.redirectUrl), s;
|
|
4777
4792
|
},
|
|
4778
4793
|
async get(e, t) {
|
|
4779
4794
|
if (!e || typeof e != "string")
|
|
4780
4795
|
throw new exports.NeetruError("validation_failed", "checkout.get: intentId is required");
|
|
4781
|
-
let { requireAuth: r, headers: o } =
|
|
4796
|
+
let { requireAuth: r, headers: o } = it(
|
|
4782
4797
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4783
4798
|
"get"
|
|
4784
4799
|
), i = await S(n, {
|
|
@@ -4787,12 +4802,12 @@ function Eo(n) {
|
|
|
4787
4802
|
requireAuth: r,
|
|
4788
4803
|
headers: o
|
|
4789
4804
|
});
|
|
4790
|
-
return
|
|
4805
|
+
return Do(i);
|
|
4791
4806
|
},
|
|
4792
4807
|
async cancel(e, t) {
|
|
4793
4808
|
if (!e || typeof e != "string")
|
|
4794
4809
|
throw new exports.NeetruError("validation_failed", "checkout.cancel: intentId is required");
|
|
4795
|
-
let { requireAuth: r, headers: o } =
|
|
4810
|
+
let { requireAuth: r, headers: o } = it(
|
|
4796
4811
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4797
4812
|
"cancel"
|
|
4798
4813
|
);
|
|
@@ -4808,7 +4823,7 @@ function Eo(n) {
|
|
|
4808
4823
|
}
|
|
4809
4824
|
};
|
|
4810
4825
|
}
|
|
4811
|
-
var
|
|
4826
|
+
var Oe = class {
|
|
4812
4827
|
intents = /* @__PURE__ */ new Map();
|
|
4813
4828
|
async start(e) {
|
|
4814
4829
|
if (!e?.productId)
|
|
@@ -4851,14 +4866,14 @@ var xe = class {
|
|
|
4851
4866
|
return this.intents.set(e, { ...r, status: "cancelled" }), { ok: true, alreadyCancelled: o };
|
|
4852
4867
|
}
|
|
4853
4868
|
};
|
|
4854
|
-
function
|
|
4855
|
-
return n.env
|
|
4869
|
+
function st(n) {
|
|
4870
|
+
return V(n.env) ? new Oe() : Oo(n);
|
|
4856
4871
|
}
|
|
4857
4872
|
|
|
4858
4873
|
// src/webhooks.ts
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
async function
|
|
4874
|
+
A();
|
|
4875
|
+
F();
|
|
4876
|
+
async function wn(n, e, t, r) {
|
|
4862
4877
|
if (!e || !t) return false;
|
|
4863
4878
|
let o = e.startsWith("sha256=") ? e.slice(7) : e;
|
|
4864
4879
|
if (!/^[0-9a-f]+$/i.test(o)) return false;
|
|
@@ -4870,8 +4885,8 @@ async function bn(n, e, t, r) {
|
|
|
4870
4885
|
);
|
|
4871
4886
|
let s = new TextEncoder(), a = s.encode(t), d = typeof n == "string" ? s.encode(n) : n, p = d;
|
|
4872
4887
|
if (r !== void 0) {
|
|
4873
|
-
let
|
|
4874
|
-
I.set(
|
|
4888
|
+
let _ = s.encode(`${r}.`), I = new Uint8Array(_.length + d.length);
|
|
4889
|
+
I.set(_, 0), I.set(d, _.length), p = I;
|
|
4875
4890
|
}
|
|
4876
4891
|
let l = await i.importKey(
|
|
4877
4892
|
"raw",
|
|
@@ -4883,14 +4898,14 @@ async function bn(n, e, t, r) {
|
|
|
4883
4898
|
"HMAC",
|
|
4884
4899
|
l,
|
|
4885
4900
|
p
|
|
4886
|
-
), f = new Uint8Array(u), m =
|
|
4901
|
+
), f = new Uint8Array(u), m = Uo(o.toLowerCase());
|
|
4887
4902
|
if (!m || m.length !== f.length) return false;
|
|
4888
4903
|
let h = 0;
|
|
4889
|
-
for (let
|
|
4890
|
-
h |= f[
|
|
4904
|
+
for (let _ = 0; _ < f.length; _++)
|
|
4905
|
+
h |= f[_] ^ m[_];
|
|
4891
4906
|
return h === 0;
|
|
4892
4907
|
}
|
|
4893
|
-
async function
|
|
4908
|
+
async function Lo(n, e, t, r = {}) {
|
|
4894
4909
|
let o = r.toleranceMs ?? 3e5, i = (u) => {
|
|
4895
4910
|
if (typeof e.get == "function")
|
|
4896
4911
|
return e.get(u);
|
|
@@ -4907,9 +4922,9 @@ async function Po(n, e, t, r = {}) {
|
|
|
4907
4922
|
if (Math.abs(Date.now() - a) > o)
|
|
4908
4923
|
return { ok: false, reason: "timestamp_expired" };
|
|
4909
4924
|
let p = i("x-neetru-signature");
|
|
4910
|
-
return await
|
|
4925
|
+
return await wn(n, p, t, s) ? { ok: true } : { ok: false, reason: "invalid_signature" };
|
|
4911
4926
|
}
|
|
4912
|
-
function
|
|
4927
|
+
function Uo(n) {
|
|
4913
4928
|
if (n.length % 2 !== 0) return null;
|
|
4914
4929
|
let e = new Uint8Array(n.length / 2);
|
|
4915
4930
|
for (let t = 0; t < e.length; t++) {
|
|
@@ -4919,7 +4934,7 @@ function Do(n) {
|
|
|
4919
4934
|
}
|
|
4920
4935
|
return e;
|
|
4921
4936
|
}
|
|
4922
|
-
var
|
|
4937
|
+
var kn = [
|
|
4923
4938
|
"subscription.activated",
|
|
4924
4939
|
"subscription.cancelled",
|
|
4925
4940
|
"subscription.payment_failed",
|
|
@@ -4929,7 +4944,7 @@ var vn = [
|
|
|
4929
4944
|
"account.reactivated",
|
|
4930
4945
|
"support.ticket_replied"
|
|
4931
4946
|
];
|
|
4932
|
-
function
|
|
4947
|
+
function vn(n) {
|
|
4933
4948
|
if (!n || typeof n != "object")
|
|
4934
4949
|
throw new exports.NeetruError("invalid_response", "Webhook response is not an object");
|
|
4935
4950
|
let e = n;
|
|
@@ -4939,7 +4954,7 @@ function _n(n) {
|
|
|
4939
4954
|
id: e.id,
|
|
4940
4955
|
url: typeof e.url == "string" ? e.url : "",
|
|
4941
4956
|
events: Array.isArray(e.events) ? e.events.filter(
|
|
4942
|
-
(t) =>
|
|
4957
|
+
(t) => kn.includes(t)
|
|
4943
4958
|
) : [],
|
|
4944
4959
|
hasSecret: e.hasSecret === true,
|
|
4945
4960
|
status: e.status === "active" || e.status === "degraded" || e.status === "disabled" ? e.status : "active",
|
|
@@ -4948,7 +4963,7 @@ function _n(n) {
|
|
|
4948
4963
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
4949
4964
|
};
|
|
4950
4965
|
}
|
|
4951
|
-
function
|
|
4966
|
+
function Sn(n) {
|
|
4952
4967
|
if (!n.url || typeof n.url != "string")
|
|
4953
4968
|
throw new exports.NeetruError("validation_failed", "url \xE9 obrigat\xF3ria");
|
|
4954
4969
|
try {
|
|
@@ -4961,12 +4976,12 @@ function wn(n) {
|
|
|
4961
4976
|
if (!Array.isArray(n.events) || n.events.length === 0)
|
|
4962
4977
|
throw new exports.NeetruError("validation_failed", "events deve ter pelo menos 1 evento");
|
|
4963
4978
|
for (let e of n.events)
|
|
4964
|
-
if (!
|
|
4979
|
+
if (!kn.includes(e))
|
|
4965
4980
|
throw new exports.NeetruError("validation_failed", `evento desconhecido: ${e}`);
|
|
4966
4981
|
if (n.secret !== void 0 && n.secret.length < 16)
|
|
4967
4982
|
throw new exports.NeetruError("validation_failed", "secret deve ter \u226516 chars (recomendado 32+)");
|
|
4968
4983
|
}
|
|
4969
|
-
function
|
|
4984
|
+
function Le(n, ...e) {
|
|
4970
4985
|
for (let t of e)
|
|
4971
4986
|
if (typeof t == "string" && t.length > 0)
|
|
4972
4987
|
return O("productId", t), t;
|
|
@@ -4977,11 +4992,11 @@ function Oe(n, ...e) {
|
|
|
4977
4992
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
4978
4993
|
);
|
|
4979
4994
|
}
|
|
4980
|
-
function
|
|
4995
|
+
function at(n) {
|
|
4981
4996
|
return {
|
|
4982
4997
|
async register(e) {
|
|
4983
|
-
|
|
4984
|
-
let t =
|
|
4998
|
+
Sn(e);
|
|
4999
|
+
let t = Le(n, e.productId), r = await S(n, {
|
|
4985
5000
|
method: "POST",
|
|
4986
5001
|
path: "/api/sdk/v1/webhooks",
|
|
4987
5002
|
body: {
|
|
@@ -4997,20 +5012,20 @@ function it(n) {
|
|
|
4997
5012
|
// dedup TTL 24h (mesmo padrão de usage.report/support.createTicket).
|
|
4998
5013
|
idempotencyKey: true
|
|
4999
5014
|
});
|
|
5000
|
-
return
|
|
5015
|
+
return vn(r);
|
|
5001
5016
|
},
|
|
5002
5017
|
async list(e) {
|
|
5003
|
-
let t =
|
|
5018
|
+
let t = Le(n, e?.productId), r = await S(n, {
|
|
5004
5019
|
method: "GET",
|
|
5005
5020
|
path: "/api/sdk/v1/webhooks",
|
|
5006
5021
|
query: { productId: t },
|
|
5007
5022
|
requireAuth: true
|
|
5008
5023
|
});
|
|
5009
|
-
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(
|
|
5024
|
+
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(vn);
|
|
5010
5025
|
},
|
|
5011
5026
|
async unregister(e, t) {
|
|
5012
5027
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5013
|
-
let r =
|
|
5028
|
+
let r = Le(n, t?.productId);
|
|
5014
5029
|
return await S(n, {
|
|
5015
5030
|
method: "DELETE",
|
|
5016
5031
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}`,
|
|
@@ -5020,7 +5035,7 @@ function it(n) {
|
|
|
5020
5035
|
},
|
|
5021
5036
|
async test(e, t) {
|
|
5022
5037
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5023
|
-
let r =
|
|
5038
|
+
let r = Le(n, t?.productId), o = await S(n, {
|
|
5024
5039
|
method: "POST",
|
|
5025
5040
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}/test`,
|
|
5026
5041
|
query: { productId: r },
|
|
@@ -5039,11 +5054,11 @@ function it(n) {
|
|
|
5039
5054
|
}
|
|
5040
5055
|
};
|
|
5041
5056
|
}
|
|
5042
|
-
var
|
|
5057
|
+
var ce = class {
|
|
5043
5058
|
endpoints = /* @__PURE__ */ new Map();
|
|
5044
5059
|
nextId = 1;
|
|
5045
5060
|
async register(e) {
|
|
5046
|
-
|
|
5061
|
+
Sn(e);
|
|
5047
5062
|
let t = `mock_wh_${this.nextId++}`, r = {
|
|
5048
5063
|
id: t,
|
|
5049
5064
|
url: e.url,
|
|
@@ -5070,21 +5085,21 @@ var ae = class {
|
|
|
5070
5085
|
};
|
|
5071
5086
|
|
|
5072
5087
|
// src/notifications.ts
|
|
5073
|
-
|
|
5074
|
-
|
|
5075
|
-
var
|
|
5088
|
+
A();
|
|
5089
|
+
F();
|
|
5090
|
+
var ct = [
|
|
5076
5091
|
"info",
|
|
5077
5092
|
"success",
|
|
5078
5093
|
"warning",
|
|
5079
5094
|
"error"
|
|
5080
5095
|
];
|
|
5081
|
-
function
|
|
5096
|
+
function Tn(n) {
|
|
5082
5097
|
if (!n || typeof n != "object")
|
|
5083
5098
|
throw new exports.NeetruError("invalid_response", "Notification response is not an object");
|
|
5084
5099
|
let e = n;
|
|
5085
5100
|
if (typeof e.id != "string")
|
|
5086
5101
|
throw new exports.NeetruError("invalid_response", "Notification missing id");
|
|
5087
|
-
let t =
|
|
5102
|
+
let t = ct.includes(e.severity) ? e.severity : "info";
|
|
5088
5103
|
return {
|
|
5089
5104
|
id: e.id,
|
|
5090
5105
|
userId: typeof e.userId == "string" ? e.userId : "",
|
|
@@ -5099,21 +5114,21 @@ function kn(n) {
|
|
|
5099
5114
|
dismissedAt: typeof e.dismissedAt == "string" ? e.dismissedAt : void 0
|
|
5100
5115
|
};
|
|
5101
5116
|
}
|
|
5102
|
-
function
|
|
5117
|
+
function Rn(n) {
|
|
5103
5118
|
if (!n.userId) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
5104
5119
|
if (!n.kind) throw new exports.NeetruError("validation_failed", "kind obrigat\xF3rio");
|
|
5105
5120
|
if (!n.title) throw new exports.NeetruError("validation_failed", "title obrigat\xF3rio");
|
|
5106
|
-
if (n.severity && !
|
|
5121
|
+
if (n.severity && !ct.includes(n.severity))
|
|
5107
5122
|
throw new exports.NeetruError(
|
|
5108
5123
|
"validation_failed",
|
|
5109
|
-
`severity inv\xE1lida: ${n.severity} (use ${
|
|
5124
|
+
`severity inv\xE1lida: ${n.severity} (use ${ct.join("|")})`
|
|
5110
5125
|
);
|
|
5111
5126
|
if (n.title.length > 200)
|
|
5112
5127
|
throw new exports.NeetruError("validation_failed", "title m\xE1x 200 chars");
|
|
5113
5128
|
if (n.body && n.body.length > 2e3)
|
|
5114
5129
|
throw new exports.NeetruError("validation_failed", "body m\xE1x 2000 chars");
|
|
5115
5130
|
}
|
|
5116
|
-
function
|
|
5131
|
+
function Ue(n, ...e) {
|
|
5117
5132
|
for (let t of e)
|
|
5118
5133
|
if (typeof t == "string" && t.length > 0)
|
|
5119
5134
|
return O("productId", t), t;
|
|
@@ -5124,12 +5139,12 @@ function Le(n, ...e) {
|
|
|
5124
5139
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
5125
5140
|
);
|
|
5126
5141
|
}
|
|
5127
|
-
function
|
|
5142
|
+
function lt(n) {
|
|
5128
5143
|
return {
|
|
5129
5144
|
async send(e) {
|
|
5130
|
-
|
|
5145
|
+
Rn(e);
|
|
5131
5146
|
let r = {
|
|
5132
|
-
productId:
|
|
5147
|
+
productId: Ue(n, e.productId),
|
|
5133
5148
|
userId: e.userId,
|
|
5134
5149
|
kind: e.kind,
|
|
5135
5150
|
title: e.title,
|
|
@@ -5143,12 +5158,12 @@ function at(n) {
|
|
|
5143
5158
|
requireAuth: true,
|
|
5144
5159
|
idempotencyKey: true
|
|
5145
5160
|
});
|
|
5146
|
-
return
|
|
5161
|
+
return Tn(o);
|
|
5147
5162
|
},
|
|
5148
5163
|
async list(e, t) {
|
|
5149
5164
|
if (!e) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
5150
5165
|
let o = {
|
|
5151
|
-
productId:
|
|
5166
|
+
productId: Ue(n, t?.productId)
|
|
5152
5167
|
};
|
|
5153
5168
|
t?.includeDismissed && (o.includeDismissed = true), t?.onlyUnread && (o.onlyUnread = true), t?.limit && (o.limit = Math.min(Math.max(1, t.limit), 200));
|
|
5154
5169
|
let i = await S(n, {
|
|
@@ -5157,11 +5172,11 @@ function at(n) {
|
|
|
5157
5172
|
query: o,
|
|
5158
5173
|
requireAuth: true
|
|
5159
5174
|
});
|
|
5160
|
-
return (Array.isArray(i?.notifications) ? i.notifications : []).map(
|
|
5175
|
+
return (Array.isArray(i?.notifications) ? i.notifications : []).map(Tn);
|
|
5161
5176
|
},
|
|
5162
5177
|
async markRead(e, t) {
|
|
5163
5178
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5164
|
-
let r =
|
|
5179
|
+
let r = Ue(n, t?.productId);
|
|
5165
5180
|
return await S(n, {
|
|
5166
5181
|
method: "POST",
|
|
5167
5182
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/read`,
|
|
@@ -5171,7 +5186,7 @@ function at(n) {
|
|
|
5171
5186
|
},
|
|
5172
5187
|
async dismiss(e, t) {
|
|
5173
5188
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5174
|
-
let r =
|
|
5189
|
+
let r = Ue(n, t?.productId);
|
|
5175
5190
|
return await S(n, {
|
|
5176
5191
|
method: "POST",
|
|
5177
5192
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/dismiss`,
|
|
@@ -5181,11 +5196,11 @@ function at(n) {
|
|
|
5181
5196
|
}
|
|
5182
5197
|
};
|
|
5183
5198
|
}
|
|
5184
|
-
var
|
|
5199
|
+
var le = class {
|
|
5185
5200
|
notifications = /* @__PURE__ */ new Map();
|
|
5186
5201
|
nextId = 1;
|
|
5187
5202
|
async send(e) {
|
|
5188
|
-
if (
|
|
5203
|
+
if (Rn(e), e.fingerprint) {
|
|
5189
5204
|
let o = Date.now() - 864e5;
|
|
5190
5205
|
for (let i of this.notifications.values())
|
|
5191
5206
|
if ((i.metadata ?? {}).fingerprint === e.fingerprint && i.userId === e.userId && new Date(i.createdAt).getTime() > o)
|
|
@@ -5222,44 +5237,44 @@ var ce = class {
|
|
|
5222
5237
|
};
|
|
5223
5238
|
|
|
5224
5239
|
// src/ai.ts
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
var
|
|
5240
|
+
A();
|
|
5241
|
+
F();
|
|
5242
|
+
$e();
|
|
5243
|
+
var Mo = "/api/sdk/v1/ai/chat", In = 12e4, Me = 32768, Cn = 32, An = 4194304, En = 2048, qo = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, Fo = /^https:\/\/[^\s]+$/, jo = /* @__PURE__ */ new Set(["low", "high", "auto"]), Wo = /* @__PURE__ */ new Set(["system", "user", "assistant"]), Pn = 100, Dn = 128, Nn = 32768, xn = 0, On = 2, Ln = 200, Un = 20, Mn = 12 * 1024 * 1024, qn = 5e4, Fn = 16, jn = 500, $o = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), Vo = /^[a-zA-Z0-9_-]+$/;
|
|
5229
5244
|
function R(n) {
|
|
5230
5245
|
throw new exports.NeetruError("validation_failed", n);
|
|
5231
5246
|
}
|
|
5232
|
-
function
|
|
5247
|
+
function Bo(n) {
|
|
5233
5248
|
if (n.length === 0 || n.length % 4 !== 0) return false;
|
|
5234
5249
|
let e = n.endsWith("==") ? 2 : n.endsWith("=") ? 1 : 0;
|
|
5235
5250
|
return !(e > 0 ? n.slice(0, n.length - e) : n).includes("=");
|
|
5236
5251
|
}
|
|
5237
|
-
function
|
|
5252
|
+
function Ko(n) {
|
|
5238
5253
|
let e = n.indexOf(",");
|
|
5239
5254
|
if (e === -1) return 0;
|
|
5240
5255
|
let t = n.slice(e + 1), r = t.endsWith("==") ? 2 : t.endsWith("=") ? 1 : 0;
|
|
5241
5256
|
return Math.max(0, Math.floor(t.length * 3 / 4) - r);
|
|
5242
5257
|
}
|
|
5243
|
-
function
|
|
5258
|
+
function zo(n) {
|
|
5244
5259
|
let e = 0, t = 0, r = 0;
|
|
5245
5260
|
for (let o of n) {
|
|
5246
5261
|
let i = o?.content;
|
|
5247
5262
|
if (Array.isArray(i))
|
|
5248
5263
|
for (let s of i) {
|
|
5249
|
-
if (e++, e >
|
|
5250
|
-
return `ai: total content parts exceeds ${
|
|
5264
|
+
if (e++, e > Ln)
|
|
5265
|
+
return `ai: total content parts exceeds ${Ln} per request`;
|
|
5251
5266
|
if (s && typeof s == "object" && s.type === "image_url") {
|
|
5252
|
-
if (t++, t >
|
|
5253
|
-
return `ai: total images exceeds ${
|
|
5267
|
+
if (t++, t > Un)
|
|
5268
|
+
return `ai: total images exceeds ${Un} per request`;
|
|
5254
5269
|
let a = s.image_url?.url;
|
|
5255
|
-
if (typeof a == "string" && a.startsWith("data:") && (r +=
|
|
5256
|
-
return `ai: aggregate image bytes exceeds ${
|
|
5270
|
+
if (typeof a == "string" && a.startsWith("data:") && (r += Ko(a), r > Mn))
|
|
5271
|
+
return `ai: aggregate image bytes exceeds ${Mn} per request`;
|
|
5257
5272
|
}
|
|
5258
5273
|
}
|
|
5259
5274
|
}
|
|
5260
5275
|
return null;
|
|
5261
5276
|
}
|
|
5262
|
-
function
|
|
5277
|
+
function Ho(n) {
|
|
5263
5278
|
if (n === void 0) return null;
|
|
5264
5279
|
if (n === null || typeof n != "object")
|
|
5265
5280
|
return "ai: responseFormat json_schema.schema must be an object";
|
|
@@ -5270,11 +5285,11 @@ function $o(n) {
|
|
|
5270
5285
|
return "ai: responseFormat json_schema.schema is not serializable";
|
|
5271
5286
|
}
|
|
5272
5287
|
if (e === void 0) return "ai: responseFormat json_schema.schema is invalid";
|
|
5273
|
-
if (e.length >
|
|
5274
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5288
|
+
if (e.length > qn)
|
|
5289
|
+
return `ai: responseFormat json_schema.schema exceeds ${qn} serialized chars`;
|
|
5275
5290
|
let t = 0, r = (o, i) => {
|
|
5276
|
-
if (i >
|
|
5277
|
-
return `ai: responseFormat json_schema.schema exceeds max depth ${
|
|
5291
|
+
if (i > Fn)
|
|
5292
|
+
return `ai: responseFormat json_schema.schema exceeds max depth ${Fn}`;
|
|
5278
5293
|
if (Array.isArray(o)) {
|
|
5279
5294
|
for (let s of o) {
|
|
5280
5295
|
let a = r(s, i + 1);
|
|
@@ -5284,10 +5299,10 @@ function $o(n) {
|
|
|
5284
5299
|
}
|
|
5285
5300
|
if (o && typeof o == "object")
|
|
5286
5301
|
for (let s of Object.getOwnPropertyNames(o)) {
|
|
5287
|
-
if (
|
|
5302
|
+
if ($o.has(s))
|
|
5288
5303
|
return `ai: responseFormat json_schema.schema has forbidden key: ${s}`;
|
|
5289
|
-
if (t++, t >
|
|
5290
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5304
|
+
if (t++, t > jn)
|
|
5305
|
+
return `ai: responseFormat json_schema.schema exceeds ${jn} keys`;
|
|
5291
5306
|
let a = r(o[s], i + 1);
|
|
5292
5307
|
if (a) return a;
|
|
5293
5308
|
}
|
|
@@ -5295,44 +5310,44 @@ function $o(n) {
|
|
|
5295
5310
|
};
|
|
5296
5311
|
return r(n, 0);
|
|
5297
5312
|
}
|
|
5298
|
-
function
|
|
5313
|
+
function Qo(n) {
|
|
5299
5314
|
(typeof n != "string" || n.length === 0) && R("ai: image_url.url must be a non-empty string");
|
|
5300
5315
|
let e = n;
|
|
5301
5316
|
if (e.startsWith("data:")) {
|
|
5302
|
-
e.length >
|
|
5317
|
+
e.length > An && R(`ai: image_url.url (data:) exceeds ${An} chars`), qo.test(e) || R("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
|
|
5303
5318
|
let t = e.slice(e.indexOf(",") + 1);
|
|
5304
|
-
|
|
5319
|
+
Bo(t) || R("ai: image_url.url data: has invalid base64 (length/padding)");
|
|
5305
5320
|
return;
|
|
5306
5321
|
}
|
|
5307
5322
|
if (e.startsWith("https://")) {
|
|
5308
|
-
e.length >
|
|
5323
|
+
e.length > En && R(`ai: image_url.url (https:) exceeds ${En} chars`), Fo.test(e) || R("ai: image_url.url https: is invalid");
|
|
5309
5324
|
return;
|
|
5310
5325
|
}
|
|
5311
5326
|
R("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
|
|
5312
5327
|
}
|
|
5313
|
-
function
|
|
5328
|
+
function Go(n, e) {
|
|
5314
5329
|
if (typeof n == "string") {
|
|
5315
|
-
n.length === 0 && R("ai: message content string must be non-empty"), n.length >
|
|
5330
|
+
n.length === 0 && R("ai: message content string must be non-empty"), n.length > Me && R(`ai: message content exceeds ${Me} chars`);
|
|
5316
5331
|
return;
|
|
5317
5332
|
}
|
|
5318
|
-
(!Array.isArray(n) || n.length === 0) && R("ai: message content must be a non-empty string or array of parts"), n.length >
|
|
5333
|
+
(!Array.isArray(n) || n.length === 0) && R("ai: message content must be a non-empty string or array of parts"), n.length > Cn && R(`ai: message content has more than ${Cn} parts`);
|
|
5319
5334
|
for (let t of n) {
|
|
5320
5335
|
(!t || typeof t != "object") && R("ai: each content part must be an object");
|
|
5321
5336
|
let r = t.type;
|
|
5322
5337
|
if (r === "text") {
|
|
5323
5338
|
let o = t.text;
|
|
5324
|
-
(typeof o != "string" || o.length === 0) && R("ai: text part must have non-empty { text: string }"), o.length >
|
|
5339
|
+
(typeof o != "string" || o.length === 0) && R("ai: text part must have non-empty { text: string }"), o.length > Me && R(`ai: text part exceeds ${Me} chars`);
|
|
5325
5340
|
} else if (r === "image_url") {
|
|
5326
5341
|
e !== "user" && R(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
|
|
5327
5342
|
let o = t.image_url;
|
|
5328
|
-
(!o || typeof o != "object") && R("ai: image_url part must have { image_url: { url } }"),
|
|
5343
|
+
(!o || typeof o != "object") && R("ai: image_url part must have { image_url: { url } }"), Qo(o.url);
|
|
5329
5344
|
let i = o.detail;
|
|
5330
|
-
i !== void 0 && (typeof i != "string" || !
|
|
5345
|
+
i !== void 0 && (typeof i != "string" || !jo.has(i)) && R("ai: image_url.detail must be 'low' | 'high' | 'auto'");
|
|
5331
5346
|
} else
|
|
5332
5347
|
R("ai: content part type must be 'text' or 'image_url'");
|
|
5333
5348
|
}
|
|
5334
5349
|
}
|
|
5335
|
-
function
|
|
5350
|
+
function Jo(n) {
|
|
5336
5351
|
(!n || typeof n != "object") && R("ai: responseFormat must be an object");
|
|
5337
5352
|
let e = n.type;
|
|
5338
5353
|
if (!(e === "text" || e === "json_object")) {
|
|
@@ -5340,8 +5355,8 @@ function Ko(n) {
|
|
|
5340
5355
|
let t = n.json_schema;
|
|
5341
5356
|
(!t || typeof t != "object") && R("ai: responseFormat json_schema requires { json_schema: { name } }");
|
|
5342
5357
|
let r = t, o = r.name;
|
|
5343
|
-
if ((typeof o != "string" || o.length === 0) && R("ai: responseFormat json_schema.name is required"), o.length > 64 && R("ai: responseFormat json_schema.name exceeds 64 chars"),
|
|
5344
|
-
let i =
|
|
5358
|
+
if ((typeof o != "string" || o.length === 0) && R("ai: responseFormat json_schema.name is required"), o.length > 64 && R("ai: responseFormat json_schema.name exceeds 64 chars"), Vo.test(o) || R("ai: responseFormat json_schema.name must match a-zA-Z0-9_-"), r.description !== void 0 && (typeof r.description != "string" && R("ai: responseFormat json_schema.description must be a string"), r.description.length > 1e3 && R("ai: responseFormat json_schema.description exceeds 1000 chars")), r.strict !== void 0 && typeof r.strict != "boolean" && R("ai: responseFormat json_schema.strict must be a boolean"), r.schema !== void 0) {
|
|
5359
|
+
let i = Ho(r.schema);
|
|
5345
5360
|
i && R(i);
|
|
5346
5361
|
}
|
|
5347
5362
|
return;
|
|
@@ -5349,26 +5364,26 @@ function Ko(n) {
|
|
|
5349
5364
|
R("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
|
|
5350
5365
|
}
|
|
5351
5366
|
}
|
|
5352
|
-
function
|
|
5367
|
+
function Wn(n, e, t) {
|
|
5353
5368
|
if (!e || typeof e != "object")
|
|
5354
5369
|
throw new exports.NeetruError("validation_failed", "ai: options object is required");
|
|
5355
5370
|
if (typeof e.model != "string" || e.model.length === 0)
|
|
5356
5371
|
throw new exports.NeetruError("validation_failed", "ai: model is required");
|
|
5357
|
-
if (e.model.length >
|
|
5358
|
-
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${
|
|
5372
|
+
if (e.model.length > Dn)
|
|
5373
|
+
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${Dn} chars`);
|
|
5359
5374
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5360
5375
|
throw new exports.NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
5361
|
-
if (e.messages.length >
|
|
5362
|
-
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${
|
|
5376
|
+
if (e.messages.length > Pn)
|
|
5377
|
+
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${Pn} per request`);
|
|
5363
5378
|
for (let a of e.messages) {
|
|
5364
|
-
if (!a || typeof a != "object" || typeof a.role != "string" || !
|
|
5379
|
+
if (!a || typeof a != "object" || typeof a.role != "string" || !Wo.has(a.role))
|
|
5365
5380
|
throw new exports.NeetruError(
|
|
5366
5381
|
"validation_failed",
|
|
5367
5382
|
"ai: each message role must be 'system' | 'user' | 'assistant'"
|
|
5368
5383
|
);
|
|
5369
|
-
|
|
5384
|
+
Go(a.content, a.role);
|
|
5370
5385
|
}
|
|
5371
|
-
let r =
|
|
5386
|
+
let r = zo(e.messages);
|
|
5372
5387
|
if (r) throw new exports.NeetruError("validation_failed", r);
|
|
5373
5388
|
let o = e.productId ?? n.productId, i = e.tenantId ?? n.tenantId;
|
|
5374
5389
|
if (!o)
|
|
@@ -5390,24 +5405,24 @@ function Fn(n, e, t) {
|
|
|
5390
5405
|
stream: t
|
|
5391
5406
|
};
|
|
5392
5407
|
if (e.maxTokens !== void 0) {
|
|
5393
|
-
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens >
|
|
5408
|
+
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > Nn)
|
|
5394
5409
|
throw new exports.NeetruError(
|
|
5395
5410
|
"validation_failed",
|
|
5396
|
-
`ai: maxTokens must be a positive integer <= ${
|
|
5411
|
+
`ai: maxTokens must be a positive integer <= ${Nn}`
|
|
5397
5412
|
);
|
|
5398
5413
|
s.maxTokens = e.maxTokens;
|
|
5399
5414
|
}
|
|
5400
5415
|
if (e.temperature !== void 0) {
|
|
5401
|
-
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature <
|
|
5416
|
+
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature < xn || e.temperature > On)
|
|
5402
5417
|
throw new exports.NeetruError(
|
|
5403
5418
|
"validation_failed",
|
|
5404
|
-
`ai: temperature must be a finite number between ${
|
|
5419
|
+
`ai: temperature must be a finite number between ${xn} and ${On}`
|
|
5405
5420
|
);
|
|
5406
5421
|
s.temperature = e.temperature;
|
|
5407
5422
|
}
|
|
5408
|
-
return e.responseFormat !== void 0 && (
|
|
5423
|
+
return e.responseFormat !== void 0 && (Jo(e.responseFormat), s.responseFormat = e.responseFormat), s;
|
|
5409
5424
|
}
|
|
5410
|
-
async function
|
|
5425
|
+
async function Kn(n) {
|
|
5411
5426
|
try {
|
|
5412
5427
|
let e = await n.text();
|
|
5413
5428
|
return e ? JSON.parse(e) : void 0;
|
|
@@ -5415,32 +5430,32 @@ async function Vn(n) {
|
|
|
5415
5430
|
return;
|
|
5416
5431
|
}
|
|
5417
5432
|
}
|
|
5418
|
-
function
|
|
5433
|
+
function Xo(n, e) {
|
|
5419
5434
|
let t = n.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
|
|
5420
5435
|
return `${t}${r}`;
|
|
5421
5436
|
}
|
|
5422
|
-
async function
|
|
5437
|
+
async function $n(n, e, t, r) {
|
|
5423
5438
|
if (!n.apiKey)
|
|
5424
5439
|
throw new exports.NeetruError(
|
|
5425
5440
|
"missing_api_key",
|
|
5426
5441
|
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
5427
5442
|
);
|
|
5428
|
-
let o =
|
|
5443
|
+
let o = Xo(n.baseUrl, Mo), { headers: i, correlationId: s } = be({
|
|
5429
5444
|
"content-type": "application/json",
|
|
5430
5445
|
accept: r ? "text/event-stream" : "application/json",
|
|
5431
5446
|
authorization: `Bearer ${n.apiKey}`
|
|
5432
5447
|
}), a = { method: "POST", headers: i, body: JSON.stringify(e) };
|
|
5433
|
-
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(
|
|
5448
|
+
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(In);
|
|
5434
5449
|
let d;
|
|
5435
5450
|
try {
|
|
5436
5451
|
d = await n.fetch(o, a);
|
|
5437
5452
|
} catch (u) {
|
|
5438
|
-
let f = u instanceof DOMException && u.name === "TimeoutError" ? `Network error: timeout after ${
|
|
5453
|
+
let f = u instanceof DOMException && u.name === "TimeoutError" ? `Network error: timeout after ${In / 1e3}s` : u instanceof DOMException && u.name === "AbortError" ? "Network error: request aborted" : `Network error: ${u instanceof Error ? u.message : "fetch failed"}`;
|
|
5439
5454
|
throw new exports.NeetruError("network_error", f, void 0, void 0, void 0, s);
|
|
5440
5455
|
}
|
|
5441
5456
|
let p = d.headers.get("x-request-id") ?? d.headers.get("x-correlation-id") ?? void 0;
|
|
5442
5457
|
if (!d.ok) {
|
|
5443
|
-
let u = await
|
|
5458
|
+
let u = await Kn(d), f = we(d.status), m = `HTTP ${d.status}`;
|
|
5444
5459
|
if (u && typeof u == "object" && "error" in u) {
|
|
5445
5460
|
let h = u.error;
|
|
5446
5461
|
typeof h == "string" ? m = h : h && typeof h == "object" && (typeof h.code == "string" && (f = h.code), typeof h.message == "string" && (m = h.message));
|
|
@@ -5448,15 +5463,15 @@ async function jn(n, e, t, r) {
|
|
|
5448
5463
|
throw new exports.NeetruError(f, m, d.status, p, void 0, s);
|
|
5449
5464
|
}
|
|
5450
5465
|
let l = d.headers.get("x-neetru-version");
|
|
5451
|
-
return l &&
|
|
5466
|
+
return l && ne(l), d;
|
|
5452
5467
|
}
|
|
5453
|
-
function
|
|
5468
|
+
function Vn(n) {
|
|
5454
5469
|
return {
|
|
5455
5470
|
provider: n.headers.get("x-neetru-provider") ?? "",
|
|
5456
5471
|
usageWarning: n.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
5457
5472
|
};
|
|
5458
5473
|
}
|
|
5459
|
-
function
|
|
5474
|
+
function Bn(n) {
|
|
5460
5475
|
let e = [];
|
|
5461
5476
|
for (let r of n.split(`
|
|
5462
5477
|
`)) {
|
|
@@ -5477,10 +5492,10 @@ function $n(n) {
|
|
|
5477
5492
|
return null;
|
|
5478
5493
|
}
|
|
5479
5494
|
}
|
|
5480
|
-
function
|
|
5495
|
+
function dt(n) {
|
|
5481
5496
|
return {
|
|
5482
5497
|
async chat(e) {
|
|
5483
|
-
let t =
|
|
5498
|
+
let t = Wn(n, e, false), r = await $n(n, t, e.signal, false), { provider: o, usageWarning: i } = Vn(r), s = await Kn(r);
|
|
5484
5499
|
if (!s || s.ok !== true || !Array.isArray(s.choices))
|
|
5485
5500
|
throw new exports.NeetruError("invalid_response", "ai.chat response missing ok/choices");
|
|
5486
5501
|
return {
|
|
@@ -5492,7 +5507,7 @@ function ct(n) {
|
|
|
5492
5507
|
};
|
|
5493
5508
|
},
|
|
5494
5509
|
stream(e) {
|
|
5495
|
-
let t =
|
|
5510
|
+
let t = Wn(n, e, true), r = $n(n, t, e.signal, true), o = r.then(Vn);
|
|
5496
5511
|
o.catch(() => {
|
|
5497
5512
|
});
|
|
5498
5513
|
async function* i() {
|
|
@@ -5509,16 +5524,16 @@ function ct(n) {
|
|
|
5509
5524
|
for (; (h = l.indexOf(`
|
|
5510
5525
|
|
|
5511
5526
|
`)) !== -1; ) {
|
|
5512
|
-
let
|
|
5527
|
+
let _ = l.slice(0, h);
|
|
5513
5528
|
l = l.slice(h + 2);
|
|
5514
|
-
let I =
|
|
5529
|
+
let I = Bn(_);
|
|
5515
5530
|
if (I && "done" in I) return;
|
|
5516
5531
|
I && "content" in I && (yield I.content);
|
|
5517
5532
|
}
|
|
5518
5533
|
}
|
|
5519
5534
|
let u = l.trim();
|
|
5520
5535
|
if (u) {
|
|
5521
|
-
let f =
|
|
5536
|
+
let f = Bn(u);
|
|
5522
5537
|
f && "content" in f && (yield f.content);
|
|
5523
5538
|
}
|
|
5524
5539
|
} finally {
|
|
@@ -5541,20 +5556,8 @@ function ct(n) {
|
|
|
5541
5556
|
}
|
|
5542
5557
|
|
|
5543
5558
|
// src/mocks.ts
|
|
5544
|
-
|
|
5545
|
-
|
|
5546
|
-
|
|
5547
|
-
// src/runtime-env.ts
|
|
5548
|
-
function Me() {
|
|
5549
|
-
try {
|
|
5550
|
-
if (typeof process < "u" && process.env)
|
|
5551
|
-
return process.env.NODE_ENV === "production";
|
|
5552
|
-
} catch {
|
|
5553
|
-
}
|
|
5554
|
-
return false;
|
|
5555
|
-
}
|
|
5556
|
-
|
|
5557
|
-
// src/mocks.ts
|
|
5559
|
+
H();
|
|
5560
|
+
A();
|
|
5558
5561
|
var qe = Object.freeze({
|
|
5559
5562
|
uid: "dev-fixture-uid-0001",
|
|
5560
5563
|
email: "dev@neetru.local",
|
|
@@ -5562,12 +5565,12 @@ var qe = Object.freeze({
|
|
|
5562
5565
|
displayName: "Dev Fixture",
|
|
5563
5566
|
isCustomer: true,
|
|
5564
5567
|
isStaff: false
|
|
5565
|
-
}),
|
|
5568
|
+
}), de = class {
|
|
5566
5569
|
_user;
|
|
5567
5570
|
_listeners;
|
|
5568
5571
|
_allowUnsignedDecode;
|
|
5569
5572
|
constructor(e = null, t) {
|
|
5570
|
-
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode &&
|
|
5573
|
+
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode && Be())
|
|
5571
5574
|
throw new exports.NeetruError(
|
|
5572
5575
|
"invalid_config",
|
|
5573
5576
|
"MockAuth com allowUnsignedDecode=true \xE9 PROIBIDO em produ\xE7\xE3o (NODE_ENV=production): decodificar JWT sem verificar assinatura \xE9 bypass de autentica\xE7\xE3o. Este modo existe exclusivamente para testes."
|
|
@@ -5580,7 +5583,7 @@ var qe = Object.freeze({
|
|
|
5580
5583
|
return this._user || (this._user = { ...qe }), this._notify(), this._user;
|
|
5581
5584
|
}
|
|
5582
5585
|
async signOut() {
|
|
5583
|
-
this._user = null, this._notify();
|
|
5586
|
+
return this._user = null, this._notify(), { localSessionCleared: true, remoteRevocation: "skipped" };
|
|
5584
5587
|
}
|
|
5585
5588
|
getUser() {
|
|
5586
5589
|
return this._user;
|
|
@@ -5698,7 +5701,7 @@ var qe = Object.freeze({
|
|
|
5698
5701
|
} catch {
|
|
5699
5702
|
}
|
|
5700
5703
|
}
|
|
5701
|
-
},
|
|
5704
|
+
}, ue = class {
|
|
5702
5705
|
_reports = [];
|
|
5703
5706
|
_quotas;
|
|
5704
5707
|
_counters = /* @__PURE__ */ new Map();
|
|
@@ -5760,7 +5763,7 @@ var qe = Object.freeze({
|
|
|
5760
5763
|
__reset() {
|
|
5761
5764
|
this._reports = [], this._quotas.clear(), this._counters.clear();
|
|
5762
5765
|
}
|
|
5763
|
-
},
|
|
5766
|
+
}, pe = class {
|
|
5764
5767
|
_tickets = [];
|
|
5765
5768
|
_ticketSeq = 0;
|
|
5766
5769
|
async createTicket(e) {
|
|
@@ -5837,7 +5840,7 @@ var qe = Object.freeze({
|
|
|
5837
5840
|
__reset() {
|
|
5838
5841
|
this._tickets = [], this._ticketSeq = 0;
|
|
5839
5842
|
}
|
|
5840
|
-
},
|
|
5843
|
+
}, fe = class {
|
|
5841
5844
|
_denied = /* @__PURE__ */ new Set();
|
|
5842
5845
|
async check(e, t) {
|
|
5843
5846
|
return !this._denied.has(`${e}:${t}`);
|
|
@@ -5859,7 +5862,7 @@ var qe = Object.freeze({
|
|
|
5859
5862
|
__reset() {
|
|
5860
5863
|
this._denied.clear();
|
|
5861
5864
|
}
|
|
5862
|
-
},
|
|
5865
|
+
}, ut = class {
|
|
5863
5866
|
_store = /* @__PURE__ */ new Map();
|
|
5864
5867
|
_fixtures;
|
|
5865
5868
|
constructor(e = {}) {
|
|
@@ -5925,9 +5928,9 @@ var qe = Object.freeze({
|
|
|
5925
5928
|
(m) => l.where.every((h) => i(m, h))
|
|
5926
5929
|
)), l?.orderBy) {
|
|
5927
5930
|
let { field: m, direction: h } = l.orderBy;
|
|
5928
|
-
u = u.slice().sort((
|
|
5929
|
-
let w =
|
|
5930
|
-
return h === "desc" ? -
|
|
5931
|
+
u = u.slice().sort((_, I) => {
|
|
5932
|
+
let w = _[m], k = I[m], D = w ?? "", y = k ?? "", b = D < y ? -1 : D > y ? 1 : 0;
|
|
5933
|
+
return h === "desc" ? -b : b;
|
|
5931
5934
|
});
|
|
5932
5935
|
}
|
|
5933
5936
|
let f = l?.limit ?? 20;
|
|
@@ -6046,11 +6049,11 @@ var qe = Object.freeze({
|
|
|
6046
6049
|
), h = await f.PGlite.create();
|
|
6047
6050
|
for (let w of r)
|
|
6048
6051
|
await h.query(w);
|
|
6049
|
-
let
|
|
6052
|
+
let _ = m.drizzle(h, { schema: e });
|
|
6050
6053
|
return {
|
|
6051
|
-
orm:
|
|
6054
|
+
orm: _,
|
|
6052
6055
|
async transaction(w, k) {
|
|
6053
|
-
return await
|
|
6056
|
+
return await _.transaction(w);
|
|
6054
6057
|
},
|
|
6055
6058
|
async close() {
|
|
6056
6059
|
h.closed || await h.close();
|
|
@@ -6080,8 +6083,8 @@ var qe = Object.freeze({
|
|
|
6080
6083
|
let d = s.adapters.createPg().Pool, p = new d(), l = p.query.bind(p);
|
|
6081
6084
|
p.query = (f, ...m) => {
|
|
6082
6085
|
if (f !== null && typeof f == "object") {
|
|
6083
|
-
let
|
|
6084
|
-
|
|
6086
|
+
let _ = f.types;
|
|
6087
|
+
_ !== null && typeof _ == "object" && "getTypeParser" in _ && delete _.getTypeParser;
|
|
6085
6088
|
}
|
|
6086
6089
|
return l(f, ...m);
|
|
6087
6090
|
};
|
|
@@ -6125,7 +6128,7 @@ var qe = Object.freeze({
|
|
|
6125
6128
|
__reset() {
|
|
6126
6129
|
this._store.clear();
|
|
6127
6130
|
}
|
|
6128
|
-
},
|
|
6131
|
+
}, me = class {
|
|
6129
6132
|
_calls = [];
|
|
6130
6133
|
/** Extrai texto de um content string OU array multimodal (imagens ignoradas). */
|
|
6131
6134
|
_contentToText(e) {
|
|
@@ -6172,9 +6175,30 @@ var qe = Object.freeze({
|
|
|
6172
6175
|
}
|
|
6173
6176
|
};
|
|
6174
6177
|
|
|
6178
|
+
// src/timeout.ts
|
|
6179
|
+
function zn(n) {
|
|
6180
|
+
let e = globalThis.AbortSignal;
|
|
6181
|
+
if (typeof e?.timeout == "function")
|
|
6182
|
+
return { signal: e.timeout(n), clear: () => {
|
|
6183
|
+
} };
|
|
6184
|
+
if (typeof globalThis.AbortController != "function")
|
|
6185
|
+
return { signal: void 0, clear: () => {
|
|
6186
|
+
} };
|
|
6187
|
+
let t = new AbortController(), r = setTimeout(() => t.abort(), n);
|
|
6188
|
+
return r.unref?.(), { signal: t.signal, clear: () => clearTimeout(r) };
|
|
6189
|
+
}
|
|
6190
|
+
function Hn(n, e, t) {
|
|
6191
|
+
let r, o = new Promise((i) => {
|
|
6192
|
+
r = setTimeout(() => i(t()), e), r.unref?.();
|
|
6193
|
+
});
|
|
6194
|
+
return Promise.race([n, o]).finally(() => {
|
|
6195
|
+
r !== void 0 && clearTimeout(r);
|
|
6196
|
+
});
|
|
6197
|
+
}
|
|
6198
|
+
|
|
6175
6199
|
// src/auth-pkce-shared.ts
|
|
6176
|
-
|
|
6177
|
-
function
|
|
6200
|
+
A();
|
|
6201
|
+
function Qn() {
|
|
6178
6202
|
let n = globalThis.crypto;
|
|
6179
6203
|
if (!n || typeof n.getRandomValues != "function" || !n.subtle)
|
|
6180
6204
|
throw new exports.NeetruError(
|
|
@@ -6183,36 +6207,36 @@ function Bn() {
|
|
|
6183
6207
|
);
|
|
6184
6208
|
return n;
|
|
6185
6209
|
}
|
|
6186
|
-
function
|
|
6210
|
+
function Gn(n) {
|
|
6187
6211
|
let e = "";
|
|
6188
6212
|
for (let r of n) e += String.fromCharCode(r);
|
|
6189
6213
|
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
6190
6214
|
}
|
|
6191
6215
|
function Fe(n, e) {
|
|
6192
6216
|
let t = new Uint8Array(e);
|
|
6193
|
-
return n.getRandomValues(t),
|
|
6217
|
+
return n.getRandomValues(t), Gn(t);
|
|
6194
6218
|
}
|
|
6195
|
-
async function
|
|
6219
|
+
async function Jn(n, e) {
|
|
6196
6220
|
let t = new TextEncoder().encode(e), r = await n.subtle.digest("SHA-256", t);
|
|
6197
|
-
return
|
|
6221
|
+
return Gn(new Uint8Array(r));
|
|
6198
6222
|
}
|
|
6199
|
-
function
|
|
6223
|
+
function he(n) {
|
|
6200
6224
|
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.replace(/^https?:\/\/api\./, "https://auth.");
|
|
6201
6225
|
}
|
|
6202
|
-
function
|
|
6226
|
+
function Xn(n) {
|
|
6203
6227
|
let e = n.oidcClientId;
|
|
6204
|
-
if (!e && n.apiKey && (e =
|
|
6228
|
+
if (!e && n.apiKey && (e = _t(n.apiKey).keyId), !e)
|
|
6205
6229
|
throw new exports.NeetruError(
|
|
6206
6230
|
"invalid_config",
|
|
6207
6231
|
"auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
|
|
6208
6232
|
);
|
|
6209
6233
|
return e;
|
|
6210
6234
|
}
|
|
6211
|
-
function
|
|
6235
|
+
function Yn(n) {
|
|
6212
6236
|
let e = new URL("/api/v1/oauth/authorize", n.idpOrigin);
|
|
6213
6237
|
return e.searchParams.set("response_type", "code"), e.searchParams.set("client_id", n.clientId), e.searchParams.set("redirect_uri", n.redirectUri), e.searchParams.set("scope", n.scope), e.searchParams.set("state", n.state), e.searchParams.set("nonce", n.nonce), e.searchParams.set("code_challenge", n.codeChallenge), e.searchParams.set("code_challenge_method", "S256"), e;
|
|
6214
6238
|
}
|
|
6215
|
-
function
|
|
6239
|
+
function pt(n) {
|
|
6216
6240
|
let e = n.split(".");
|
|
6217
6241
|
if (e.length !== 3) return null;
|
|
6218
6242
|
try {
|
|
@@ -6222,8 +6246,8 @@ function dt(n) {
|
|
|
6222
6246
|
return null;
|
|
6223
6247
|
}
|
|
6224
6248
|
}
|
|
6225
|
-
function
|
|
6226
|
-
let e =
|
|
6249
|
+
function ge(n) {
|
|
6250
|
+
let e = pt(n);
|
|
6227
6251
|
return !e || typeof e.sub != "string" || typeof e.exp == "number" && e.exp * 1e3 < Date.now() ? null : {
|
|
6228
6252
|
uid: e.sub,
|
|
6229
6253
|
email: typeof e.email == "string" ? e.email : "",
|
|
@@ -6236,9 +6260,9 @@ function he(n) {
|
|
|
6236
6260
|
...e
|
|
6237
6261
|
};
|
|
6238
6262
|
}
|
|
6239
|
-
var
|
|
6240
|
-
async function
|
|
6241
|
-
let o = `${e}${
|
|
6263
|
+
var Yo = "/api/v1/oauth/revoke";
|
|
6264
|
+
async function Zn(n, e, t, r) {
|
|
6265
|
+
let o = `${e}${Yo}`, i = true;
|
|
6242
6266
|
for (let s of r) {
|
|
6243
6267
|
if (!s.token) continue;
|
|
6244
6268
|
let a = new URLSearchParams({
|
|
@@ -6262,17 +6286,17 @@ async function Qn(n, e, t, r) {
|
|
|
6262
6286
|
}
|
|
6263
6287
|
|
|
6264
6288
|
// src/auth.ts
|
|
6265
|
-
|
|
6289
|
+
ve();
|
|
6266
6290
|
|
|
6267
6291
|
// src/end-user-token.ts
|
|
6268
|
-
var
|
|
6269
|
-
function
|
|
6270
|
-
|
|
6292
|
+
var Zo = /* @__PURE__ */ new WeakMap();
|
|
6293
|
+
function er(n, e) {
|
|
6294
|
+
Zo.set(n, e);
|
|
6271
6295
|
}
|
|
6272
6296
|
|
|
6273
6297
|
// src/unsupported-db.ts
|
|
6274
|
-
|
|
6275
|
-
var
|
|
6298
|
+
A();
|
|
6299
|
+
var ei = "[Neetru SDK] client.db n\xE3o est\xE1 dispon\xEDvel em React Native. O app n\xE3o acessa o banco do produto diretamente: chame a API do SEU produto (ex.: https://<produto>.neetru.com/api/v1) com o access_token do usu\xE1rio (Authorization: Bearer) \u2014 o backend do produto usa client.db no servidor. (At\xE9 a 3.7.x o db ca\xEDa em mem\xF3ria em sil\xEAncio e os dados sumiam ao fechar o app.)", ti = /* @__PURE__ */ new Set([
|
|
6276
6300
|
"then",
|
|
6277
6301
|
"toJSON",
|
|
6278
6302
|
"valueOf",
|
|
@@ -6281,13 +6305,13 @@ var Qo = "[Neetru SDK] client.db n\xE3o est\xE1 dispon\xEDvel em React Native. O
|
|
|
6281
6305
|
"asymmetricMatch",
|
|
6282
6306
|
"nodeType"
|
|
6283
6307
|
]);
|
|
6284
|
-
function
|
|
6308
|
+
function tr() {
|
|
6285
6309
|
let n = () => {
|
|
6286
|
-
throw new exports.NeetruError("runtime_unsupported",
|
|
6310
|
+
throw new exports.NeetruError("runtime_unsupported", ei, void 0, void 0, "react-native");
|
|
6287
6311
|
}, e = {};
|
|
6288
6312
|
return new Proxy(e, {
|
|
6289
6313
|
get(t, r) {
|
|
6290
|
-
if (!(typeof r == "symbol" ||
|
|
6314
|
+
if (!(typeof r == "symbol" || ti.has(r)))
|
|
6291
6315
|
return r === "toString" ? () => "[NeetruDb indispon\xEDvel em React Native]" : n;
|
|
6292
6316
|
},
|
|
6293
6317
|
set() {
|
|
@@ -6297,7 +6321,7 @@ function Xn() {
|
|
|
6297
6321
|
}
|
|
6298
6322
|
|
|
6299
6323
|
// src/auth.ts
|
|
6300
|
-
function
|
|
6324
|
+
function ri() {
|
|
6301
6325
|
try {
|
|
6302
6326
|
let e = globalThis.process?.env?.NEETRU_API_KEY;
|
|
6303
6327
|
return typeof e == "string" && e.length > 0 ? e : void 0;
|
|
@@ -6305,7 +6329,7 @@ function Yo() {
|
|
|
6305
6329
|
return;
|
|
6306
6330
|
}
|
|
6307
6331
|
}
|
|
6308
|
-
function
|
|
6332
|
+
function oi(n) {
|
|
6309
6333
|
if (n) return n;
|
|
6310
6334
|
try {
|
|
6311
6335
|
let t = globalThis.process?.env?.NEETRU_ENV;
|
|
@@ -6319,8 +6343,8 @@ function Zo(n) {
|
|
|
6319
6343
|
}
|
|
6320
6344
|
return "prod";
|
|
6321
6345
|
}
|
|
6322
|
-
var
|
|
6323
|
-
function
|
|
6346
|
+
var ii = _e;
|
|
6347
|
+
function si(n) {
|
|
6324
6348
|
let e = n.sub ?? "", t = typeof n.email == "string" ? n.email : "";
|
|
6325
6349
|
return {
|
|
6326
6350
|
uid: e,
|
|
@@ -6335,8 +6359,8 @@ function ti(n) {
|
|
|
6335
6359
|
...n
|
|
6336
6360
|
};
|
|
6337
6361
|
}
|
|
6338
|
-
var
|
|
6339
|
-
function
|
|
6362
|
+
var ye = "neetru_oauth_tx";
|
|
6363
|
+
function ft(n) {
|
|
6340
6364
|
try {
|
|
6341
6365
|
let e = globalThis.history;
|
|
6342
6366
|
if (!e || typeof e.replaceState != "function") return;
|
|
@@ -6347,7 +6371,7 @@ function ut(n) {
|
|
|
6347
6371
|
} catch {
|
|
6348
6372
|
}
|
|
6349
6373
|
}
|
|
6350
|
-
function
|
|
6374
|
+
function ai(n) {
|
|
6351
6375
|
let e = "neetru_id_token", t = "neetru_refresh_token", r = /* @__PURE__ */ new Set(), o = null, i = null, s = null;
|
|
6352
6376
|
function a() {
|
|
6353
6377
|
try {
|
|
@@ -6365,21 +6389,21 @@ function ni(n) {
|
|
|
6365
6389
|
}
|
|
6366
6390
|
function p() {
|
|
6367
6391
|
if (o) {
|
|
6368
|
-
let
|
|
6369
|
-
return typeof
|
|
6392
|
+
let g = o.exp;
|
|
6393
|
+
return typeof g == "number" && g * 1e3 < Date.now() ? (o = null, a()?.removeItem(e), l(), _(), null) : o;
|
|
6370
6394
|
}
|
|
6371
|
-
let
|
|
6372
|
-
if (!
|
|
6373
|
-
let
|
|
6374
|
-
if (!
|
|
6375
|
-
return
|
|
6376
|
-
let T =
|
|
6377
|
-
return T ? (o = T, I(T), o) : (
|
|
6395
|
+
let y = a();
|
|
6396
|
+
if (!y) return null;
|
|
6397
|
+
let b = y.getItem(e);
|
|
6398
|
+
if (!b)
|
|
6399
|
+
return _(), null;
|
|
6400
|
+
let T = ge(b);
|
|
6401
|
+
return T ? (o = T, I(T), o) : (y.removeItem(e), _(), null);
|
|
6378
6402
|
}
|
|
6379
6403
|
function l() {
|
|
6380
|
-
for (let
|
|
6404
|
+
for (let y of r)
|
|
6381
6405
|
try {
|
|
6382
|
-
|
|
6406
|
+
y(o);
|
|
6383
6407
|
} catch {
|
|
6384
6408
|
}
|
|
6385
6409
|
}
|
|
@@ -6391,70 +6415,75 @@ function ni(n) {
|
|
|
6391
6415
|
}
|
|
6392
6416
|
function m() {
|
|
6393
6417
|
o = null;
|
|
6394
|
-
let
|
|
6395
|
-
|
|
6418
|
+
let y = a();
|
|
6419
|
+
y?.removeItem(e), y?.removeItem(t), l();
|
|
6396
6420
|
}
|
|
6397
6421
|
function h() {
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6401
|
-
|
|
6422
|
+
if (s) return s;
|
|
6423
|
+
let y = false, b = (async () => {
|
|
6424
|
+
let T = f();
|
|
6425
|
+
if (!T) return false;
|
|
6426
|
+
let g;
|
|
6402
6427
|
try {
|
|
6403
|
-
|
|
6428
|
+
g = w();
|
|
6404
6429
|
} catch {
|
|
6405
6430
|
return false;
|
|
6406
6431
|
}
|
|
6407
|
-
let
|
|
6432
|
+
let x = `${he(n.baseUrl)}/api/v1/oauth/token`, L = new URLSearchParams({
|
|
6408
6433
|
grant_type: "refresh_token",
|
|
6409
|
-
refresh_token:
|
|
6410
|
-
client_id:
|
|
6411
|
-
}),
|
|
6434
|
+
refresh_token: T,
|
|
6435
|
+
client_id: g
|
|
6436
|
+
}), { signal: P, clear: q } = zn(15e3), E;
|
|
6412
6437
|
try {
|
|
6413
|
-
|
|
6438
|
+
E = await n.fetch(x, {
|
|
6414
6439
|
method: "POST",
|
|
6415
6440
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6416
|
-
body:
|
|
6441
|
+
body: L.toString(),
|
|
6442
|
+
signal: P
|
|
6417
6443
|
});
|
|
6418
6444
|
} catch {
|
|
6419
6445
|
return false;
|
|
6446
|
+
} finally {
|
|
6447
|
+
q();
|
|
6420
6448
|
}
|
|
6421
|
-
let
|
|
6449
|
+
let U = null;
|
|
6422
6450
|
try {
|
|
6423
|
-
|
|
6451
|
+
U = await E.json();
|
|
6424
6452
|
} catch {
|
|
6425
|
-
|
|
6453
|
+
U = null;
|
|
6426
6454
|
}
|
|
6427
|
-
if (!
|
|
6428
|
-
let
|
|
6429
|
-
if (typeof
|
|
6430
|
-
let
|
|
6431
|
-
if (!
|
|
6432
|
-
let
|
|
6433
|
-
|
|
6434
|
-
let
|
|
6435
|
-
return typeof
|
|
6436
|
-
})()
|
|
6455
|
+
if (!E.ok || typeof U?.error == "string") return false;
|
|
6456
|
+
let M = U?.id_token;
|
|
6457
|
+
if (typeof M != "string" || M.length === 0) return false;
|
|
6458
|
+
let j = ge(M);
|
|
6459
|
+
if (!j || y) return false;
|
|
6460
|
+
let N = a();
|
|
6461
|
+
N?.setItem(e, M);
|
|
6462
|
+
let je = U?.refresh_token;
|
|
6463
|
+
return typeof je == "string" && je.length > 0 && N?.setItem(t, je), o = j, I(j), l(), true;
|
|
6464
|
+
})();
|
|
6465
|
+
return s = Hn(b, 15e3, () => (y = true, false)).finally(() => {
|
|
6437
6466
|
s = null;
|
|
6438
|
-
}), s
|
|
6467
|
+
}), s;
|
|
6439
6468
|
}
|
|
6440
|
-
function
|
|
6441
|
-
f() && h().then((
|
|
6442
|
-
|
|
6469
|
+
function _() {
|
|
6470
|
+
f() && h().then((y) => {
|
|
6471
|
+
y || m();
|
|
6443
6472
|
});
|
|
6444
6473
|
}
|
|
6445
|
-
function I(
|
|
6474
|
+
function I(y) {
|
|
6446
6475
|
u();
|
|
6447
|
-
let
|
|
6448
|
-
if (typeof
|
|
6449
|
-
let T =
|
|
6476
|
+
let b = y.exp;
|
|
6477
|
+
if (typeof b != "number") return;
|
|
6478
|
+
let T = b * 1e3 - Date.now();
|
|
6450
6479
|
if (!(T <= 0)) {
|
|
6451
6480
|
if (f()) {
|
|
6452
|
-
let
|
|
6481
|
+
let g = Math.min(T, Math.max(3e4, T * 0.2)), C = Math.max(1, T - g);
|
|
6453
6482
|
i = setTimeout(() => {
|
|
6454
6483
|
i = null, h().then((x) => {
|
|
6455
6484
|
x || m();
|
|
6456
6485
|
});
|
|
6457
|
-
},
|
|
6486
|
+
}, C);
|
|
6458
6487
|
return;
|
|
6459
6488
|
}
|
|
6460
6489
|
i = setTimeout(() => {
|
|
@@ -6463,140 +6492,140 @@ function ni(n) {
|
|
|
6463
6492
|
}
|
|
6464
6493
|
}
|
|
6465
6494
|
function w() {
|
|
6466
|
-
return
|
|
6495
|
+
return Xn(n);
|
|
6467
6496
|
}
|
|
6468
|
-
async function k(
|
|
6469
|
-
let
|
|
6470
|
-
if (!
|
|
6497
|
+
async function k(y) {
|
|
6498
|
+
let b = y ?? (typeof globalThis.location < "u" ? globalThis.location.href : void 0);
|
|
6499
|
+
if (!b) return null;
|
|
6471
6500
|
let T;
|
|
6472
6501
|
try {
|
|
6473
|
-
T = new URL(
|
|
6502
|
+
T = new URL(b);
|
|
6474
6503
|
} catch {
|
|
6475
6504
|
return null;
|
|
6476
6505
|
}
|
|
6477
|
-
let
|
|
6478
|
-
if (
|
|
6479
|
-
let
|
|
6480
|
-
throw d()?.removeItem(
|
|
6506
|
+
let g = T.searchParams, C = g.get("error");
|
|
6507
|
+
if (C) {
|
|
6508
|
+
let U = g.get("error_description") ?? C;
|
|
6509
|
+
throw d()?.removeItem(ye), ft(T), new exports.NeetruError(C, `OIDC authorize falhou: ${U}`);
|
|
6481
6510
|
}
|
|
6482
|
-
let x =
|
|
6511
|
+
let x = g.get("code");
|
|
6483
6512
|
if (!x) return null;
|
|
6484
|
-
let
|
|
6485
|
-
if (
|
|
6513
|
+
let L = g.get("state"), P = d(), q = P?.getItem(ye) ?? null, E = null;
|
|
6514
|
+
if (q)
|
|
6486
6515
|
try {
|
|
6487
|
-
|
|
6516
|
+
E = JSON.parse(q);
|
|
6488
6517
|
} catch {
|
|
6489
|
-
|
|
6518
|
+
E = null;
|
|
6490
6519
|
}
|
|
6491
|
-
if (!
|
|
6520
|
+
if (!E)
|
|
6492
6521
|
throw new exports.NeetruError(
|
|
6493
6522
|
"oidc_state_mismatch",
|
|
6494
6523
|
"Callback OIDC sem transa\xE7\xE3o pendente \u2014 `signIn` n\xE3o foi iniciado nesta sess\xE3o (ou j\xE1 consumido)."
|
|
6495
6524
|
);
|
|
6496
|
-
if (!
|
|
6497
|
-
throw
|
|
6525
|
+
if (!L || L !== E.state)
|
|
6526
|
+
throw P?.removeItem(ye), ft(T), new exports.NeetruError(
|
|
6498
6527
|
"oidc_state_mismatch",
|
|
6499
6528
|
"state do callback OIDC n\xE3o confere com o da transa\xE7\xE3o (poss\xEDvel CSRF)."
|
|
6500
6529
|
);
|
|
6501
|
-
return
|
|
6530
|
+
return P?.removeItem(ye), ft(T), {
|
|
6502
6531
|
code: x,
|
|
6503
|
-
codeVerifier:
|
|
6504
|
-
redirectUri:
|
|
6505
|
-
nonce:
|
|
6506
|
-
state:
|
|
6507
|
-
postLoginRedirect:
|
|
6532
|
+
codeVerifier: E.codeVerifier,
|
|
6533
|
+
redirectUri: E.redirectUri,
|
|
6534
|
+
nonce: E.nonce,
|
|
6535
|
+
state: E.state,
|
|
6536
|
+
postLoginRedirect: E.postLoginRedirect
|
|
6508
6537
|
};
|
|
6509
6538
|
}
|
|
6510
|
-
async function
|
|
6511
|
-
let
|
|
6512
|
-
if (!
|
|
6513
|
-
let T = w(),
|
|
6539
|
+
async function D(y) {
|
|
6540
|
+
let b = await k(y);
|
|
6541
|
+
if (!b) return null;
|
|
6542
|
+
let T = w(), C = `${he(n.baseUrl)}/api/v1/oauth/token`, x = new URLSearchParams({
|
|
6514
6543
|
grant_type: "authorization_code",
|
|
6515
|
-
code:
|
|
6516
|
-
redirect_uri:
|
|
6544
|
+
code: b.code,
|
|
6545
|
+
redirect_uri: b.redirectUri,
|
|
6517
6546
|
client_id: T,
|
|
6518
|
-
code_verifier:
|
|
6519
|
-
}),
|
|
6547
|
+
code_verifier: b.codeVerifier
|
|
6548
|
+
}), L;
|
|
6520
6549
|
try {
|
|
6521
|
-
|
|
6550
|
+
L = await n.fetch(C, {
|
|
6522
6551
|
method: "POST",
|
|
6523
6552
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6524
6553
|
body: x.toString()
|
|
6525
6554
|
});
|
|
6526
|
-
} catch (
|
|
6555
|
+
} catch (N) {
|
|
6527
6556
|
throw new exports.NeetruError(
|
|
6528
6557
|
"network_error",
|
|
6529
|
-
`POST ${
|
|
6558
|
+
`POST ${C} falhou (network): ${N?.message ?? String(N)}`
|
|
6530
6559
|
);
|
|
6531
6560
|
}
|
|
6532
|
-
let
|
|
6561
|
+
let P = null;
|
|
6533
6562
|
try {
|
|
6534
|
-
|
|
6563
|
+
P = await L.json();
|
|
6535
6564
|
} catch {
|
|
6536
|
-
|
|
6565
|
+
P = null;
|
|
6537
6566
|
}
|
|
6538
|
-
if (!
|
|
6539
|
-
let
|
|
6567
|
+
if (!L.ok || typeof P?.error == "string") {
|
|
6568
|
+
let N = typeof P?.error_description == "string" && P.error_description || typeof P?.error == "string" && P.error || `HTTP ${L.status}`;
|
|
6540
6569
|
throw new exports.NeetruError(
|
|
6541
6570
|
"token_exchange_failed",
|
|
6542
|
-
`Troca do authorization_code por tokens falhou no /token: ${
|
|
6543
|
-
|
|
6571
|
+
`Troca do authorization_code por tokens falhou no /token: ${N}`,
|
|
6572
|
+
L.status
|
|
6544
6573
|
);
|
|
6545
6574
|
}
|
|
6546
|
-
let
|
|
6547
|
-
if (typeof
|
|
6575
|
+
let q = P?.id_token;
|
|
6576
|
+
if (typeof q != "string" || q.length === 0)
|
|
6548
6577
|
throw new exports.NeetruError(
|
|
6549
6578
|
"token_exchange_failed",
|
|
6550
6579
|
"Resposta do /token sem `id_token` \u2014 o login n\xE3o pode ser completado.",
|
|
6551
|
-
|
|
6580
|
+
L.status
|
|
6552
6581
|
);
|
|
6553
|
-
let
|
|
6554
|
-
if (!
|
|
6582
|
+
let E = pt(q);
|
|
6583
|
+
if (!E)
|
|
6555
6584
|
throw new exports.NeetruError(
|
|
6556
6585
|
"token_exchange_failed",
|
|
6557
6586
|
"id_token retornado pelo /token \xE9 malformado (payload indecodific\xE1vel)."
|
|
6558
6587
|
);
|
|
6559
|
-
if (
|
|
6588
|
+
if (E.nonce !== b.nonce)
|
|
6560
6589
|
throw new exports.NeetruError(
|
|
6561
6590
|
"oidc_nonce_mismatch",
|
|
6562
6591
|
"nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
|
|
6563
6592
|
);
|
|
6564
|
-
let
|
|
6565
|
-
if (!
|
|
6593
|
+
let U = ge(q);
|
|
6594
|
+
if (!U)
|
|
6566
6595
|
throw new exports.NeetruError(
|
|
6567
6596
|
"token_exchange_failed",
|
|
6568
6597
|
"id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
|
|
6569
6598
|
);
|
|
6570
|
-
let
|
|
6571
|
-
|
|
6572
|
-
let j =
|
|
6573
|
-
return typeof j == "string" && j.length > 0 &&
|
|
6599
|
+
let M = a();
|
|
6600
|
+
M?.setItem(e, q);
|
|
6601
|
+
let j = P?.refresh_token;
|
|
6602
|
+
return typeof j == "string" && j.length > 0 && M?.setItem(t, j), o = U, I(U), l(), { user: U, postLoginRedirect: b.postLoginRedirect };
|
|
6574
6603
|
}
|
|
6575
6604
|
return {
|
|
6576
|
-
async signIn(
|
|
6605
|
+
async signIn(y) {
|
|
6577
6606
|
if (typeof globalThis.location < "u" && typeof globalThis.location.assign == "function") {
|
|
6578
|
-
let
|
|
6579
|
-
if (!
|
|
6607
|
+
let b = Qn(), T = y?.redirectUri ?? globalThis.location.origin, g = y?.scope ?? "openid profile email", C = w(), x = Fe(b, 16), L = Fe(b, 16), P = Fe(b, 32), q = await Jn(b, P), E = d();
|
|
6608
|
+
if (!E)
|
|
6580
6609
|
throw new exports.NeetruError(
|
|
6581
6610
|
"runtime_unsupported",
|
|
6582
6611
|
"auth.signIn requer sessionStorage para guardar a transa\xE7\xE3o PKCE entre o redirect e o callback."
|
|
6583
6612
|
);
|
|
6584
|
-
let
|
|
6613
|
+
let U = {
|
|
6585
6614
|
state: x,
|
|
6586
|
-
nonce:
|
|
6587
|
-
codeVerifier:
|
|
6615
|
+
nonce: L,
|
|
6616
|
+
codeVerifier: P,
|
|
6588
6617
|
redirectUri: T,
|
|
6589
|
-
postLoginRedirect:
|
|
6618
|
+
postLoginRedirect: y?.postLoginRedirect
|
|
6590
6619
|
};
|
|
6591
|
-
|
|
6592
|
-
let
|
|
6593
|
-
idpOrigin:
|
|
6594
|
-
clientId:
|
|
6620
|
+
E.setItem(ye, JSON.stringify(U));
|
|
6621
|
+
let M = he(n.baseUrl), j = Yn({
|
|
6622
|
+
idpOrigin: M,
|
|
6623
|
+
clientId: C,
|
|
6595
6624
|
redirectUri: T,
|
|
6596
|
-
scope:
|
|
6625
|
+
scope: g,
|
|
6597
6626
|
state: x,
|
|
6598
|
-
nonce:
|
|
6599
|
-
codeChallenge:
|
|
6627
|
+
nonce: L,
|
|
6628
|
+
codeChallenge: q
|
|
6600
6629
|
});
|
|
6601
6630
|
globalThis.location.assign(j.toString());
|
|
6602
6631
|
return;
|
|
@@ -6607,78 +6636,90 @@ function ni(n) {
|
|
|
6607
6636
|
);
|
|
6608
6637
|
},
|
|
6609
6638
|
handleRedirectCallback: k,
|
|
6610
|
-
completeSignIn:
|
|
6639
|
+
completeSignIn: D,
|
|
6611
6640
|
// 3.7.0: exposto pro client.support renovar a sessão 1x num 401
|
|
6612
6641
|
// invalid_end_user_token (mesma função do refresh proativo, com dedup).
|
|
6613
6642
|
refreshTokens: h,
|
|
6614
6643
|
getIdToken() {
|
|
6615
|
-
let
|
|
6616
|
-
return
|
|
6644
|
+
let b = a()?.getItem(e) ?? null;
|
|
6645
|
+
return b && ge(b) ? b : null;
|
|
6617
6646
|
},
|
|
6618
6647
|
async signOut() {
|
|
6619
|
-
let
|
|
6620
|
-
if (
|
|
6621
|
-
let
|
|
6648
|
+
let y = a(), b = y?.getItem(t) ?? null, T = true;
|
|
6649
|
+
if (y)
|
|
6650
|
+
for (let C of [e, t])
|
|
6651
|
+
try {
|
|
6652
|
+
y.removeItem(C);
|
|
6653
|
+
} catch {
|
|
6654
|
+
T = false;
|
|
6655
|
+
}
|
|
6656
|
+
u(), o = null;
|
|
6657
|
+
let g = "skipped";
|
|
6658
|
+
if (b) {
|
|
6659
|
+
let C = null;
|
|
6622
6660
|
try {
|
|
6623
|
-
|
|
6661
|
+
C = w();
|
|
6624
6662
|
} catch {
|
|
6625
|
-
|
|
6663
|
+
C = null;
|
|
6626
6664
|
}
|
|
6627
|
-
|
|
6628
|
-
|
|
6629
|
-
|
|
6630
|
-
|
|
6665
|
+
C ? g = await Zn(
|
|
6666
|
+
n.fetch,
|
|
6667
|
+
he(n.baseUrl),
|
|
6668
|
+
C,
|
|
6669
|
+
[{ token: b, hint: "refresh_token" }]
|
|
6670
|
+
) ? "accepted" : "failed" : g = "not_configured", g !== "accepted" && typeof console < "u" && console.warn(
|
|
6671
|
+
"[neetru-sdk] signOut: a revoga\xE7\xE3o do refresh_token no servidor n\xE3o foi confirmada (" + g + "). A sess\xE3o local foi encerrada, mas o refresh_token pode seguir v\xE1lido at\xE9 expirar. Trate o campo remoteRevocation do SignOutResult no produto."
|
|
6631
6672
|
);
|
|
6632
6673
|
}
|
|
6633
|
-
l();
|
|
6674
|
+
return l(), { localSessionCleared: T, remoteRevocation: g };
|
|
6634
6675
|
},
|
|
6635
6676
|
getUser() {
|
|
6636
6677
|
return p();
|
|
6637
6678
|
},
|
|
6638
|
-
onAuthStateChanged(
|
|
6639
|
-
r.add(
|
|
6679
|
+
onAuthStateChanged(y) {
|
|
6680
|
+
r.add(y);
|
|
6640
6681
|
try {
|
|
6641
|
-
|
|
6682
|
+
y(p());
|
|
6642
6683
|
} catch {
|
|
6643
6684
|
}
|
|
6644
6685
|
return () => {
|
|
6645
|
-
r.delete(
|
|
6686
|
+
r.delete(y);
|
|
6646
6687
|
};
|
|
6647
6688
|
},
|
|
6648
|
-
async verifyToken(
|
|
6649
|
-
if (!
|
|
6689
|
+
async verifyToken(y, b) {
|
|
6690
|
+
if (!y || typeof y != "string")
|
|
6650
6691
|
throw new exports.NeetruError("token_invalid_signature", "token must be a non-empty string");
|
|
6651
|
-
let
|
|
6652
|
-
if (!
|
|
6692
|
+
let g = (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.baseUrl.replace(/^https?:\/\/api\./, "https://auth."), C = g, x = `${g}/api/v1/oauth/.well-known/jwks`, L = b?.jwksCacheTtlMs ?? te, P = b?.audience, q = bt(n.apiKey), E = P ?? n.oidcClientId ?? q?.keyId;
|
|
6693
|
+
if (!E)
|
|
6653
6694
|
throw new exports.NeetruError(
|
|
6654
6695
|
"invalid_config",
|
|
6655
6696
|
"verifyToken requires an audience: provide oidcClientId/apiKey on the client or pass options.audience. Calling jwtVerify without an audience allows tokens issued for other OIDC clients to pass."
|
|
6656
6697
|
);
|
|
6657
|
-
let
|
|
6698
|
+
let U = ii(x, L);
|
|
6658
6699
|
try {
|
|
6659
|
-
let { payload:
|
|
6660
|
-
issuer:
|
|
6661
|
-
audience:
|
|
6700
|
+
let { payload: M } = await jose.jwtVerify(y, U, {
|
|
6701
|
+
issuer: C,
|
|
6702
|
+
audience: E,
|
|
6662
6703
|
algorithms: ["RS256"]
|
|
6663
6704
|
});
|
|
6664
|
-
return
|
|
6665
|
-
} catch (
|
|
6666
|
-
let j =
|
|
6667
|
-
throw j === "ERR_JWT_EXPIRED" ? new exports.NeetruError("token_expired", `Token expired: ${
|
|
6705
|
+
return si(M);
|
|
6706
|
+
} catch (M) {
|
|
6707
|
+
let j = M?.code ?? M?.name ?? "", N = M?.message ?? String(M);
|
|
6708
|
+
throw j === "ERR_JWT_EXPIRED" ? new exports.NeetruError("token_expired", `Token expired: ${N}`) : j === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" ? new exports.NeetruError("token_invalid_signature", `Token signature invalid: ${N}`) : j === "ERR_JWT_CLAIM_VALIDATION_FAILED" ? N.includes('"aud"') || N.includes("audience") ? new exports.NeetruError("token_invalid_audience", `Token audience mismatch: ${N}`) : N.includes('"iss"') || N.includes("issuer") ? new exports.NeetruError("token_invalid_issuer", `Token issuer mismatch: ${N}`) : new exports.NeetruError("token_invalid_signature", `Token claim validation failed: ${N}`) : new exports.NeetruError("token_invalid_signature", `Token verification failed: ${N}`);
|
|
6668
6709
|
}
|
|
6669
6710
|
}
|
|
6670
6711
|
};
|
|
6671
6712
|
}
|
|
6672
|
-
function
|
|
6713
|
+
function rr(n = {}) {
|
|
6673
6714
|
let e = n.fetch ?? globalThis.fetch;
|
|
6674
6715
|
if (typeof e != "function")
|
|
6675
6716
|
throw new exports.NeetruError(
|
|
6676
6717
|
"invalid_config",
|
|
6677
6718
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createNeetruClient."
|
|
6678
6719
|
);
|
|
6679
|
-
let t = n.apiKey ??
|
|
6680
|
-
|
|
6681
|
-
let r =
|
|
6720
|
+
let t = n.apiKey ?? ri();
|
|
6721
|
+
Pt({ hasApiKey: !!t, strict: n.strictServerOnlyKey === true });
|
|
6722
|
+
let r = oi(n.env), o = n.baseUrl ?? (r === "dev-local" ? St : We), i = kt(o, n.allowedHosts ?? []), s = Object.freeze({
|
|
6682
6723
|
apiKey: t,
|
|
6683
6724
|
oidcClientId: n.oidcClientId,
|
|
6684
6725
|
baseUrl: i,
|
|
@@ -6686,44 +6727,44 @@ function Yn(n = {}) {
|
|
|
6686
6727
|
env: r,
|
|
6687
6728
|
productId: n.productId,
|
|
6688
6729
|
tenantId: n.tenantId
|
|
6689
|
-
}), a = r
|
|
6730
|
+
}), a = V(r), d = n.mocks?.auth ?? (a ? new de({ ...qe }) : ai(s)), p = n.mocks?.usage ?? (a ? new ue() : Kt(s)), l = n.endUserAuth ?? d, u = n.mocks?.support ?? (a ? new pe() : Xt(s, {
|
|
6690
6731
|
getEndUserToken: () => l.getIdToken(),
|
|
6691
6732
|
// `refreshTokens` é opcional (MockAuth/mocks próprios omitem).
|
|
6692
6733
|
refreshEndUserToken: (() => {
|
|
6693
6734
|
let k = l.refreshTokens;
|
|
6694
6735
|
return typeof k == "function" ? () => k.call(l) : void 0;
|
|
6695
6736
|
})()
|
|
6696
|
-
})), f = n.mocks?.entitlements ?? (a ? new
|
|
6737
|
+
})), f = n.mocks?.entitlements ?? (a ? new fe() : jt()), m = n.mocks?.db ?? (J() === "react-native" ? tr() : ot(s, n.db)), h = n.mocks?.webhooks ?? (a ? new ce() : at(s)), _ = n.mocks?.notifications ?? (a ? new le() : lt(s)), I = n.mocks?.ai ?? (a ? new me() : dt(s)), w = Object.freeze({
|
|
6697
6738
|
config: s,
|
|
6698
6739
|
auth: d,
|
|
6699
|
-
catalog:
|
|
6740
|
+
catalog: qt(s),
|
|
6700
6741
|
entitlements: f,
|
|
6701
|
-
telemetry:
|
|
6742
|
+
telemetry: Vt(s),
|
|
6702
6743
|
usage: p,
|
|
6703
6744
|
support: u,
|
|
6704
6745
|
db: m,
|
|
6705
|
-
checkout:
|
|
6746
|
+
checkout: st(s),
|
|
6706
6747
|
webhooks: h,
|
|
6707
|
-
notifications:
|
|
6748
|
+
notifications: _,
|
|
6708
6749
|
ai: I
|
|
6709
6750
|
});
|
|
6710
|
-
return n.endUserAuth &&
|
|
6751
|
+
return n.endUserAuth && er(w, n.endUserAuth), w;
|
|
6711
6752
|
}
|
|
6712
|
-
var
|
|
6713
|
-
function
|
|
6753
|
+
var mt = /* @__PURE__ */ Symbol.for("@neetru/sdk:serverClient");
|
|
6754
|
+
function ci(n = {}) {
|
|
6714
6755
|
let e = globalThis;
|
|
6715
|
-
return e[
|
|
6756
|
+
return e[mt] || (e[mt] = rr(n)), e[mt];
|
|
6716
6757
|
}
|
|
6717
6758
|
|
|
6718
6759
|
// src/index.ts
|
|
6719
|
-
|
|
6720
|
-
|
|
6721
|
-
|
|
6760
|
+
A();
|
|
6761
|
+
H();
|
|
6762
|
+
F();
|
|
6722
6763
|
|
|
6723
6764
|
// src/access-token.ts
|
|
6724
|
-
|
|
6725
|
-
var
|
|
6726
|
-
function
|
|
6765
|
+
A();
|
|
6766
|
+
var ir = "https://auth.neetru.com", di = "/api/v1/oauth/.well-known/jwks";
|
|
6767
|
+
function sr(n) {
|
|
6727
6768
|
let e = (Array.isArray(n) ? n : [n]).filter(
|
|
6728
6769
|
(t) => typeof t == "string" && t.trim().length > 0
|
|
6729
6770
|
);
|
|
@@ -6734,17 +6775,17 @@ function tr(n) {
|
|
|
6734
6775
|
);
|
|
6735
6776
|
return e;
|
|
6736
6777
|
}
|
|
6737
|
-
var
|
|
6738
|
-
function
|
|
6778
|
+
var or = "O token apresentado n\xE3o \xE9 um access_token (parece um id_token). Mande o access_token no Authorization.";
|
|
6779
|
+
function z(n, e) {
|
|
6739
6780
|
return new exports.NeetruError("token_invalid", n, 401, void 0, e);
|
|
6740
6781
|
}
|
|
6741
|
-
async function
|
|
6742
|
-
let t =
|
|
6782
|
+
async function ar(n, e) {
|
|
6783
|
+
let t = sr(e?.audience);
|
|
6743
6784
|
if (typeof n != "string" || n.length === 0)
|
|
6744
|
-
throw
|
|
6785
|
+
throw z("access_token ausente.", "missing_token");
|
|
6745
6786
|
if (n.split(".").length !== 3)
|
|
6746
|
-
throw
|
|
6747
|
-
let r = (e.issuer ??
|
|
6787
|
+
throw z("O token n\xE3o \xE9 um JWT (access_token OIDC esperado).", "not_a_jwt");
|
|
6788
|
+
let r = (e.issuer ?? ir).replace(/\/+$/, ""), o = e.jwksUrl ?? `${r}${di}`, i = Math.min(Math.max(e.clockToleranceSec ?? 5, 0), 60), s = _e(o, e.jwksCacheTtlMs ?? te), a;
|
|
6748
6789
|
try {
|
|
6749
6790
|
({ payload: a } = await jose.jwtVerify(n, s, {
|
|
6750
6791
|
issuer: r,
|
|
@@ -6754,18 +6795,18 @@ async function nr(n, e) {
|
|
|
6754
6795
|
requiredClaims: ["sub", "exp", "iat", "jti"]
|
|
6755
6796
|
}));
|
|
6756
6797
|
} catch (w) {
|
|
6757
|
-
let k = w?.code ?? "",
|
|
6798
|
+
let k = w?.code ?? "", D = w?.message ?? String(w);
|
|
6758
6799
|
if (k === "ERR_JWT_EXPIRED")
|
|
6759
6800
|
throw new exports.NeetruError("token_expired", "access_token expirado.", 401, void 0, "expired");
|
|
6760
6801
|
if (k === "ERR_JWT_CLAIM_VALIDATION_FAILED") {
|
|
6761
|
-
let
|
|
6762
|
-
throw
|
|
6802
|
+
let b = w?.claim;
|
|
6803
|
+
throw b === "aud" ? new exports.NeetruError(
|
|
6763
6804
|
"token_invalid_audience",
|
|
6764
6805
|
"access_token emitido para um client OIDC que n\xE3o est\xE1 na lista desta API.",
|
|
6765
6806
|
401,
|
|
6766
6807
|
void 0,
|
|
6767
6808
|
"audience"
|
|
6768
|
-
) :
|
|
6809
|
+
) : b === "iss" ? new exports.NeetruError("token_invalid_issuer", "access_token de outro emissor.", 401, void 0, "issuer") : b === "jti" ? z(or, "not_access_token") : z(`Claim obrigat\xF3rio ausente ou inv\xE1lido no access_token (${b ?? "desconhecido"}).`, "claims");
|
|
6769
6810
|
}
|
|
6770
6811
|
throw k === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" || k === "ERR_JWKS_NO_MATCHING_KEY" || k === "ERR_JOSE_ALG_NOT_ALLOWED" || k === "ERR_JWKS_MULTIPLE_MATCHING_KEYS" ? new exports.NeetruError(
|
|
6771
6812
|
"token_invalid_signature",
|
|
@@ -6773,21 +6814,21 @@ async function nr(n, e) {
|
|
|
6773
6814
|
401,
|
|
6774
6815
|
void 0,
|
|
6775
6816
|
"signature"
|
|
6776
|
-
) : k === "ERR_JWKS_TIMEOUT" || k === "ERR_JOSE_GENERIC" && /JSON Web Key Set/i.test(
|
|
6817
|
+
) : k === "ERR_JWKS_TIMEOUT" || k === "ERR_JOSE_GENERIC" && /JSON Web Key Set/i.test(D) || k === "" && w instanceof TypeError ? new exports.NeetruError("network_error", `Falha ao obter o JWKS do Core: ${D}`, void 0, void 0, "jwks") : z(`access_token inv\xE1lido: ${D}`, "malformed");
|
|
6777
6818
|
}
|
|
6778
6819
|
let d = a.jti, p = a.scope, l = a.client_id;
|
|
6779
6820
|
if (typeof d != "string" || d.length === 0 || typeof p != "string" || typeof l != "string")
|
|
6780
|
-
throw
|
|
6821
|
+
throw z(or, "not_access_token");
|
|
6781
6822
|
let u = a.aud, f = typeof u == "string" ? u : Array.isArray(u) && u.length === 1 ? u[0] : void 0;
|
|
6782
6823
|
if (typeof f != "string" || f !== l)
|
|
6783
|
-
throw
|
|
6824
|
+
throw z("access_token inconsistente: client_id difere do aud.", "client_id_mismatch");
|
|
6784
6825
|
let m = typeof a.sub == "string" ? a.sub : "", h = m.startsWith("client:");
|
|
6785
6826
|
if (!m || h && e.allowClientCredentials !== true)
|
|
6786
|
-
throw
|
|
6827
|
+
throw z(
|
|
6787
6828
|
"access_token de m\xE1quina (client_credentials) n\xE3o representa um usu\xE1rio final.",
|
|
6788
6829
|
"client_credentials"
|
|
6789
6830
|
);
|
|
6790
|
-
let
|
|
6831
|
+
let _ = p.split(" ").filter(Boolean), I = (e.requiredScopes ?? []).filter((w) => !_.includes(w));
|
|
6791
6832
|
if (I.length > 0)
|
|
6792
6833
|
throw new exports.NeetruError(
|
|
6793
6834
|
"insufficient_scope",
|
|
@@ -6800,7 +6841,7 @@ async function nr(n, e) {
|
|
|
6800
6841
|
sub: m,
|
|
6801
6842
|
clientId: l,
|
|
6802
6843
|
audience: f,
|
|
6803
|
-
scopes:
|
|
6844
|
+
scopes: _,
|
|
6804
6845
|
jti: d,
|
|
6805
6846
|
issuedAt: a.iat,
|
|
6806
6847
|
expiresAt: a.exp,
|
|
@@ -6808,7 +6849,7 @@ async function nr(n, e) {
|
|
|
6808
6849
|
claims: a
|
|
6809
6850
|
};
|
|
6810
6851
|
}
|
|
6811
|
-
function
|
|
6852
|
+
function cr(n, e) {
|
|
6812
6853
|
let t = e.toLowerCase(), r = n && typeof n == "object" && "headers" in n && n.headers ? n.headers : n;
|
|
6813
6854
|
if (!r || typeof r != "object") return null;
|
|
6814
6855
|
if (typeof r.get == "function")
|
|
@@ -6818,14 +6859,14 @@ function rr(n, e) {
|
|
|
6818
6859
|
return Array.isArray(i) ? typeof i[0] == "string" ? i[0] : null : typeof i == "string" ? i : null;
|
|
6819
6860
|
return null;
|
|
6820
6861
|
}
|
|
6821
|
-
function
|
|
6822
|
-
let e =
|
|
6862
|
+
function lr(n) {
|
|
6863
|
+
let e = cr(n, "authorization");
|
|
6823
6864
|
if (!e) return null;
|
|
6824
6865
|
let t = /^\s*Bearer\s+(\S+)\s*$/i.exec(e);
|
|
6825
6866
|
return t ? t[1] : null;
|
|
6826
6867
|
}
|
|
6827
|
-
function
|
|
6828
|
-
let t =
|
|
6868
|
+
function dr(n, e) {
|
|
6869
|
+
let t = cr(n, "cookie");
|
|
6829
6870
|
if (!t) return null;
|
|
6830
6871
|
for (let r of t.split(";")) {
|
|
6831
6872
|
let o = r.indexOf("=");
|
|
@@ -6839,13 +6880,13 @@ function ir(n, e) {
|
|
|
6839
6880
|
}
|
|
6840
6881
|
return null;
|
|
6841
6882
|
}
|
|
6842
|
-
async function
|
|
6843
|
-
|
|
6844
|
-
let t =
|
|
6883
|
+
async function ui(n, e) {
|
|
6884
|
+
sr(e?.audience);
|
|
6885
|
+
let t = lr(n);
|
|
6845
6886
|
if (t)
|
|
6846
|
-
return { kind: "access_token", token: await
|
|
6887
|
+
return { kind: "access_token", token: await ar(t, e) };
|
|
6847
6888
|
if (e.cookie) {
|
|
6848
|
-
let r =
|
|
6889
|
+
let r = dr(n, e.cookie.name);
|
|
6849
6890
|
if (r) {
|
|
6850
6891
|
let o = await e.cookie.verify(r);
|
|
6851
6892
|
if (o != null) return { kind: "cookie", session: o };
|
|
@@ -6853,7 +6894,7 @@ async function si(n, e) {
|
|
|
6853
6894
|
}
|
|
6854
6895
|
return null;
|
|
6855
6896
|
}
|
|
6856
|
-
function
|
|
6897
|
+
function pi(n) {
|
|
6857
6898
|
if (n instanceof exports.NeetruError) {
|
|
6858
6899
|
if (n.code === "insufficient_scope")
|
|
6859
6900
|
return {
|
|
@@ -6885,41 +6926,41 @@ function ai(n) {
|
|
|
6885
6926
|
}
|
|
6886
6927
|
|
|
6887
6928
|
// src/index.ts
|
|
6888
|
-
|
|
6889
|
-
var
|
|
6929
|
+
ve();
|
|
6930
|
+
var Wa = "3.9.0";
|
|
6890
6931
|
|
|
6891
|
-
exports.DEFAULT_BASE_URL =
|
|
6932
|
+
exports.DEFAULT_BASE_URL = We;
|
|
6892
6933
|
exports.DEV_FIXTURE_USER = qe;
|
|
6893
|
-
exports.MockAi =
|
|
6894
|
-
exports.MockAuth =
|
|
6895
|
-
exports.MockCheckout =
|
|
6896
|
-
exports.MockDb =
|
|
6897
|
-
exports.MockEntitlements =
|
|
6898
|
-
exports.MockNotifications =
|
|
6899
|
-
exports.MockSupport =
|
|
6900
|
-
exports.MockUsage =
|
|
6901
|
-
exports.MockWebhooks =
|
|
6902
|
-
exports.NEETRU_OIDC_ISSUER =
|
|
6903
|
-
exports.NEETRU_SENTINEL_KEY =
|
|
6904
|
-
exports.VERSION =
|
|
6905
|
-
exports.accessTokenErrorToHttp =
|
|
6906
|
-
exports.authenticateProductRequest =
|
|
6907
|
-
exports.checkCoreVersionCompat =
|
|
6908
|
-
exports.createAiNamespace =
|
|
6909
|
-
exports.createCheckoutNamespace =
|
|
6910
|
-
exports.createNeetruClient =
|
|
6911
|
-
exports.createNeetruDb =
|
|
6912
|
-
exports.createNotificationsNamespace =
|
|
6913
|
-
exports.createWebhooksNamespace =
|
|
6914
|
-
exports.detectNonServerRuntime =
|
|
6915
|
-
exports.getServerNeetruClient =
|
|
6916
|
-
exports.increment =
|
|
6917
|
-
exports.isFieldSentinel =
|
|
6918
|
-
exports.isIncrementSentinel =
|
|
6919
|
-
exports.isServerTimestampSentinel =
|
|
6920
|
-
exports.readBearerToken =
|
|
6921
|
-
exports.readCookie =
|
|
6922
|
-
exports.serverTimestamp =
|
|
6923
|
-
exports.verifyAccessToken =
|
|
6924
|
-
exports.verifyWebhookRequest =
|
|
6925
|
-
exports.verifyWebhookSignature =
|
|
6934
|
+
exports.MockAi = me;
|
|
6935
|
+
exports.MockAuth = de;
|
|
6936
|
+
exports.MockCheckout = Oe;
|
|
6937
|
+
exports.MockDb = ut;
|
|
6938
|
+
exports.MockEntitlements = fe;
|
|
6939
|
+
exports.MockNotifications = le;
|
|
6940
|
+
exports.MockSupport = pe;
|
|
6941
|
+
exports.MockUsage = ue;
|
|
6942
|
+
exports.MockWebhooks = ce;
|
|
6943
|
+
exports.NEETRU_OIDC_ISSUER = ir;
|
|
6944
|
+
exports.NEETRU_SENTINEL_KEY = Qe;
|
|
6945
|
+
exports.VERSION = Wa;
|
|
6946
|
+
exports.accessTokenErrorToHttp = pi;
|
|
6947
|
+
exports.authenticateProductRequest = ui;
|
|
6948
|
+
exports.checkCoreVersionCompat = ne;
|
|
6949
|
+
exports.createAiNamespace = dt;
|
|
6950
|
+
exports.createCheckoutNamespace = st;
|
|
6951
|
+
exports.createNeetruClient = rr;
|
|
6952
|
+
exports.createNeetruDb = ot;
|
|
6953
|
+
exports.createNotificationsNamespace = lt;
|
|
6954
|
+
exports.createWebhooksNamespace = at;
|
|
6955
|
+
exports.detectNonServerRuntime = J;
|
|
6956
|
+
exports.getServerNeetruClient = ci;
|
|
6957
|
+
exports.increment = Xr;
|
|
6958
|
+
exports.isFieldSentinel = Q;
|
|
6959
|
+
exports.isIncrementSentinel = en;
|
|
6960
|
+
exports.isServerTimestampSentinel = Zt;
|
|
6961
|
+
exports.readBearerToken = lr;
|
|
6962
|
+
exports.readCookie = dr;
|
|
6963
|
+
exports.serverTimestamp = Jr;
|
|
6964
|
+
exports.verifyAccessToken = ar;
|
|
6965
|
+
exports.verifyWebhookRequest = Lo;
|
|
6966
|
+
exports.verifyWebhookSignature = wn;
|