@neetru/sdk 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/README.md +66 -1
- package/dist/ai.cjs +221 -177
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +221 -177
- package/dist/auth.cjs +1227 -1064
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +1224 -1061
- package/dist/catalog.cjs +164 -97
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +164 -97
- package/dist/checkout.cjs +182 -115
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +181 -114
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +370 -298
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +362 -290
- package/dist/entitlements.cjs +17 -10
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +17 -10
- package/dist/errors.cjs +15 -8
- package/dist/errors.d.cts +24 -3
- package/dist/errors.d.ts +24 -3
- package/dist/errors.mjs +14 -7
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +1409 -1075
- package/dist/index.d.cts +177 -3
- package/dist/index.d.ts +177 -3
- package/dist/index.mjs +1544 -1217
- package/dist/mobile.cjs +400 -181
- package/dist/mobile.d.cts +59 -4
- package/dist/mobile.d.ts +59 -4
- package/dist/mobile.mjs +400 -181
- package/dist/mocks.cjs +9 -2
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +9 -2
- package/dist/notifications.cjs +197 -130
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +196 -129
- package/dist/react.cjs +303 -282
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +295 -274
- package/dist/support.cjs +299 -229
- package/dist/support.d.cts +8 -4
- package/dist/support.d.ts +8 -4
- package/dist/support.mjs +298 -228
- package/dist/telemetry.cjs +234 -167
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +234 -167
- package/dist/{types-Bx4kTLmk.d.cts → types-D-9bjlM0.d.cts} +48 -1
- package/dist/{types-Dx0IrOSF.d.ts → types-bVztFQTI.d.ts} +48 -1
- package/dist/usage.cjs +183 -116
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +183 -116
- package/dist/webhooks.cjs +215 -148
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +212 -145
- package/package.json +2 -1
package/dist/auth.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { jwtVerify, createRemoteJWKSet } from 'jose';
|
|
2
2
|
import { openDB } from 'idb';
|
|
3
3
|
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
4
|
+
var Hn = Object.defineProperty;
|
|
5
|
+
var $ = (n, e) => () => (n && (e = n(n = 0)), e);
|
|
6
|
+
var ot = (n, e) => {
|
|
7
7
|
for (var t in e)
|
|
8
|
-
|
|
8
|
+
Hn(n, t, { get: e[t], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
// src/errors.ts
|
|
12
|
-
var
|
|
13
|
-
|
|
12
|
+
var l, V, I = $(() => {
|
|
13
|
+
l = class n extends Error {
|
|
14
14
|
code;
|
|
15
15
|
status;
|
|
16
16
|
requestId;
|
|
@@ -23,100 +23,191 @@ var c, V, P = B(() => {
|
|
|
23
23
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
24
24
|
*/
|
|
25
25
|
reason;
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
28
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
29
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
30
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
31
|
+
*/
|
|
32
|
+
correlationId;
|
|
33
|
+
constructor(e, t, r, o, i, s) {
|
|
34
|
+
super(t), this.name = "NeetruError", this.code = e, this.status = r, this.requestId = o, this.reason = i, this.correlationId = s, Object.setPrototypeOf(this, n.prototype);
|
|
28
35
|
}
|
|
29
|
-
}, V = class n extends
|
|
30
|
-
constructor(e, t, r, o) {
|
|
31
|
-
super("auth_required", e, r, o, t), this.name = "NeetruAuthRequiredError", Object.setPrototypeOf(this, n.prototype);
|
|
36
|
+
}, V = class n extends l {
|
|
37
|
+
constructor(e, t, r, o, i) {
|
|
38
|
+
super("auth_required", e, r, o, t, i), this.name = "NeetruAuthRequiredError", Object.setPrototypeOf(this, n.prototype);
|
|
32
39
|
}
|
|
33
40
|
};
|
|
34
41
|
});
|
|
35
42
|
|
|
36
43
|
// src/idempotency.ts
|
|
37
|
-
function
|
|
44
|
+
function ht() {
|
|
38
45
|
let n = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
39
46
|
if (n && typeof n.randomUUID == "function")
|
|
40
47
|
return n.randomUUID();
|
|
41
|
-
throw new
|
|
48
|
+
throw new l(
|
|
42
49
|
"runtime_unsupported",
|
|
43
50
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
44
51
|
);
|
|
45
52
|
}
|
|
46
|
-
var
|
|
47
|
-
|
|
53
|
+
var gt = $(() => {
|
|
54
|
+
I();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// src/correlation.ts
|
|
58
|
+
function _t(n) {
|
|
59
|
+
return typeof n == "string" && n.length > 0 && n.length <= 128 && er.test(n);
|
|
60
|
+
}
|
|
61
|
+
function tr() {
|
|
62
|
+
let n = globalThis.crypto;
|
|
63
|
+
try {
|
|
64
|
+
if (n && typeof n.randomUUID == "function") return n.randomUUID();
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
let e = new Uint8Array(16), t = false;
|
|
68
|
+
try {
|
|
69
|
+
n && typeof n.getRandomValues == "function" && (n.getRandomValues(e), t = !0);
|
|
70
|
+
} catch {
|
|
71
|
+
t = false;
|
|
72
|
+
}
|
|
73
|
+
if (!t)
|
|
74
|
+
for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
|
|
75
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
76
|
+
let r = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
77
|
+
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
78
|
+
}
|
|
79
|
+
function ae(n, e) {
|
|
80
|
+
let t, r = {};
|
|
81
|
+
for (let [i, s] of Object.entries(n)) {
|
|
82
|
+
if (i.toLowerCase() === yt) {
|
|
83
|
+
_t(s) && (t = s);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
r[i] = s;
|
|
87
|
+
}
|
|
88
|
+
let o = t ?? (_t(e) ? e : void 0) ?? tr();
|
|
89
|
+
return r[yt] = o, { headers: r, correlationId: o };
|
|
90
|
+
}
|
|
91
|
+
var yt, er, Me = $(() => {
|
|
92
|
+
yt = "x-correlation-id", er = /^[A-Za-z0-9._:\-]+$/;
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// src/server-only-key.ts
|
|
96
|
+
function X() {
|
|
97
|
+
try {
|
|
98
|
+
let n = globalThis.navigator;
|
|
99
|
+
if (n && n.product === "ReactNative") return "react-native";
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
let n = globalThis;
|
|
104
|
+
if (typeof n.window < "u" && typeof n.document < "u") return "browser";
|
|
105
|
+
} catch {
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
function bt(n, e) {
|
|
110
|
+
let t = nr[n];
|
|
111
|
+
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();
|
|
112
|
+
}
|
|
113
|
+
function wt(n) {
|
|
114
|
+
if (!n.hasApiKey) return;
|
|
115
|
+
let e = n.runtime === void 0 ? X() : n.runtime;
|
|
116
|
+
if (e) {
|
|
117
|
+
if (n.strict)
|
|
118
|
+
throw new l("server_only_key", bt(e, true), void 0, void 0, e);
|
|
119
|
+
if (!vt) {
|
|
120
|
+
vt = true;
|
|
121
|
+
try {
|
|
122
|
+
typeof console < "u" && typeof console.warn == "function" && console.warn(bt(e, !1));
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
var nr, vt, qe = $(() => {
|
|
129
|
+
I();
|
|
130
|
+
nr = {
|
|
131
|
+
browser: "no navegador",
|
|
132
|
+
"react-native": "num app React Native"
|
|
133
|
+
};
|
|
134
|
+
vt = false;
|
|
48
135
|
});
|
|
49
136
|
|
|
50
137
|
// src/http.ts
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
checkCoreVersionCompat: () =>
|
|
138
|
+
var Y = {};
|
|
139
|
+
ot(Y, {
|
|
140
|
+
checkCoreVersionCompat: () => ce,
|
|
54
141
|
httpRequest: () => v,
|
|
55
|
-
statusToCode: () =>
|
|
142
|
+
statusToCode: () => le
|
|
56
143
|
});
|
|
57
|
-
function
|
|
144
|
+
function Fe(n) {
|
|
58
145
|
return n.split("+")[0] ?? n;
|
|
59
146
|
}
|
|
60
|
-
function
|
|
61
|
-
let t =
|
|
147
|
+
function or(n, e) {
|
|
148
|
+
let t = Fe(n).split(".").map(Number), r = Fe(e).split(".").map(Number), o = Math.max(t.length, r.length);
|
|
62
149
|
for (let i = 0; i < o; i++) {
|
|
63
150
|
let s = t[i] ?? 0, a = r[i] ?? 0;
|
|
64
151
|
if (s !== a) return s - a;
|
|
65
152
|
}
|
|
66
153
|
return 0;
|
|
67
154
|
}
|
|
68
|
-
function
|
|
155
|
+
function Ct() {
|
|
69
156
|
try {
|
|
70
|
-
return "3.
|
|
157
|
+
return "3.8.0";
|
|
71
158
|
} catch {
|
|
72
159
|
return;
|
|
73
160
|
}
|
|
74
161
|
}
|
|
75
|
-
function
|
|
76
|
-
let n =
|
|
162
|
+
function ir() {
|
|
163
|
+
let n = Ct();
|
|
77
164
|
return n ? parseInt(n.split(".")[0] ?? "3", 10) : 3;
|
|
78
165
|
}
|
|
79
|
-
function
|
|
166
|
+
function ce(n) {
|
|
80
167
|
if (!n) return;
|
|
81
|
-
let e =
|
|
168
|
+
let e = ir(), t = rr[e];
|
|
82
169
|
if (!t) return;
|
|
83
|
-
let r =
|
|
84
|
-
if (
|
|
170
|
+
let r = Fe(n);
|
|
171
|
+
if (or(r, t) < 0) {
|
|
85
172
|
let o = `${e}:${r}`;
|
|
86
|
-
|
|
173
|
+
kt.has(o) || (kt.add(o), console.warn(
|
|
87
174
|
`[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.`
|
|
88
175
|
));
|
|
89
176
|
}
|
|
90
177
|
}
|
|
91
|
-
function
|
|
178
|
+
function sr() {
|
|
179
|
+
let n = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
180
|
+
return X() ? "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;
|
|
181
|
+
}
|
|
182
|
+
function St(n) {
|
|
92
183
|
let e = 200 * Math.pow(4, n), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
93
184
|
return Math.max(50, Math.round(e + t));
|
|
94
185
|
}
|
|
95
|
-
function
|
|
186
|
+
function ur(n) {
|
|
96
187
|
if (!n) return null;
|
|
97
188
|
let e = Number(n);
|
|
98
|
-
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3),
|
|
189
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), Tt);
|
|
99
190
|
let t = Date.parse(n);
|
|
100
191
|
if (Number.isFinite(t)) {
|
|
101
192
|
let r = t - Date.now();
|
|
102
|
-
if (r > 0) return Math.min(r,
|
|
193
|
+
if (r > 0) return Math.min(r, Tt);
|
|
103
194
|
}
|
|
104
195
|
return null;
|
|
105
196
|
}
|
|
106
|
-
function
|
|
197
|
+
function Rt(n) {
|
|
107
198
|
return new Promise((e) => setTimeout(e, n));
|
|
108
199
|
}
|
|
109
|
-
function
|
|
200
|
+
function le(n) {
|
|
110
201
|
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";
|
|
111
202
|
}
|
|
112
|
-
function
|
|
203
|
+
function pr(n, e, t) {
|
|
113
204
|
let r = n.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${r}${o}`);
|
|
114
205
|
if (t)
|
|
115
206
|
for (let [s, a] of Object.entries(t))
|
|
116
207
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
117
208
|
return i.toString();
|
|
118
209
|
}
|
|
119
|
-
async function
|
|
210
|
+
async function It(n) {
|
|
120
211
|
let e = await n.text();
|
|
121
212
|
if (e)
|
|
122
213
|
try {
|
|
@@ -126,97 +217,103 @@ async function mt(n) {
|
|
|
126
217
|
}
|
|
127
218
|
}
|
|
128
219
|
async function v(n, e) {
|
|
129
|
-
let t = e.method ?? "GET", r =
|
|
130
|
-
accept: "application/json",
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (
|
|
220
|
+
let t = e.method ?? "GET", r = pr(n.baseUrl, e.path, e.query), o = lr.has(t) ? ar : cr, i = e.retries ?? o, { headers: s, correlationId: a } = ae(
|
|
221
|
+
{ accept: "application/json", ...e.headers },
|
|
222
|
+
e.correlationId
|
|
223
|
+
), c = Ct();
|
|
224
|
+
if (c && (s["x-neetru-sdk-version"] = c), e.requireAuth) {
|
|
134
225
|
if (!n.apiKey)
|
|
135
|
-
throw new
|
|
136
|
-
"missing_api_key",
|
|
137
|
-
"This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
138
|
-
);
|
|
226
|
+
throw new l("missing_api_key", sr());
|
|
139
227
|
s.authorization = `Bearer ${n.apiKey}`;
|
|
140
228
|
}
|
|
141
229
|
if (e.idempotencyKey) {
|
|
142
|
-
let
|
|
143
|
-
s["idempotency-key"] =
|
|
144
|
-
}
|
|
145
|
-
let
|
|
146
|
-
|
|
147
|
-
let
|
|
148
|
-
for (let
|
|
149
|
-
if (
|
|
150
|
-
throw
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
230
|
+
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey : ht();
|
|
231
|
+
s["idempotency-key"] = f;
|
|
232
|
+
}
|
|
233
|
+
let d = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
234
|
+
d !== void 0 && (s["content-type"] = "application/json");
|
|
235
|
+
let u = null, p = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
236
|
+
for (let f = 0; f <= i; f++) {
|
|
237
|
+
if (p != null && Date.now() >= p)
|
|
238
|
+
throw u ?? new l(
|
|
239
|
+
"network_error",
|
|
240
|
+
"Deadline excedido antes de completar a request",
|
|
241
|
+
void 0,
|
|
242
|
+
void 0,
|
|
243
|
+
void 0,
|
|
244
|
+
a
|
|
245
|
+
);
|
|
246
|
+
let _ = { method: t, headers: s };
|
|
247
|
+
d !== void 0 && (_.body = d);
|
|
248
|
+
let b = p != null ? Math.max(1, Math.min(3e4, p - Date.now())) : 3e4;
|
|
249
|
+
_.signal = AbortSignal.timeout(b);
|
|
250
|
+
let S;
|
|
156
251
|
try {
|
|
157
|
-
|
|
158
|
-
} catch (
|
|
159
|
-
let
|
|
160
|
-
if (
|
|
161
|
-
let
|
|
162
|
-
if (
|
|
163
|
-
await
|
|
252
|
+
S = await n.fetch(r, _);
|
|
253
|
+
} catch (h) {
|
|
254
|
+
let C = h instanceof DOMException && h.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${h instanceof Error ? h.message : "fetch failed"}`;
|
|
255
|
+
if (u = new l("network_error", C, void 0, void 0, void 0, a), f < i) {
|
|
256
|
+
let D = St(f);
|
|
257
|
+
if (p == null || Date.now() + D < p) {
|
|
258
|
+
await Rt(D);
|
|
164
259
|
continue;
|
|
165
260
|
}
|
|
166
261
|
}
|
|
167
|
-
throw
|
|
262
|
+
throw u;
|
|
168
263
|
}
|
|
169
|
-
let
|
|
170
|
-
if (
|
|
171
|
-
let
|
|
172
|
-
return
|
|
264
|
+
let x = S.headers.get("x-request-id") ?? S.headers.get("x-correlation-id") ?? void 0;
|
|
265
|
+
if (S.ok) {
|
|
266
|
+
let h = S.headers.get("x-neetru-version");
|
|
267
|
+
return h && ce(h), await It(S);
|
|
173
268
|
}
|
|
174
|
-
let
|
|
175
|
-
if (
|
|
176
|
-
let
|
|
177
|
-
typeof
|
|
269
|
+
let T = await It(S), P = le(S.status), q = `HTTP ${S.status}`, m;
|
|
270
|
+
if (T && typeof T == "object") {
|
|
271
|
+
let h = T.error;
|
|
272
|
+
typeof h == "string" ? q = h : h && typeof h == "object" ? (typeof h.code == "string" && (P = h.code), typeof h.message == "string" && (q = h.message), typeof h.reason == "string" && (m = h.reason)) : (typeof T.code == "string" && (P = T.code), typeof T.message == "string" && (q = T.message)), typeof T.reason == "string" && (m = T.reason);
|
|
178
273
|
}
|
|
179
|
-
if (
|
|
180
|
-
let
|
|
181
|
-
|
|
274
|
+
if (S.status === 410) {
|
|
275
|
+
let h = S.headers.get("x-neetru-replacement");
|
|
276
|
+
q === `HTTP ${S.status}` && (q = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), h && !q.includes(h) && (q += ` Substituto: ${h}.`);
|
|
182
277
|
}
|
|
183
|
-
let
|
|
184
|
-
if (
|
|
185
|
-
let
|
|
186
|
-
if (
|
|
187
|
-
await
|
|
278
|
+
let g = new l(P, q, S.status, x, m, a);
|
|
279
|
+
if (u = g, dr.has(P) && f < i) {
|
|
280
|
+
let C = ur(S.headers.get("retry-after")) ?? St(f);
|
|
281
|
+
if (p == null || Date.now() + C < p) {
|
|
282
|
+
await Rt(C);
|
|
188
283
|
continue;
|
|
189
284
|
}
|
|
190
285
|
}
|
|
191
|
-
throw
|
|
286
|
+
throw g;
|
|
192
287
|
}
|
|
193
|
-
throw
|
|
288
|
+
throw u ?? new l("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
194
289
|
}
|
|
195
|
-
var
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
290
|
+
var rr, kt, ar, cr, lr, dr, Tt, M = $(() => {
|
|
291
|
+
I();
|
|
292
|
+
gt();
|
|
293
|
+
Me();
|
|
294
|
+
qe();
|
|
295
|
+
rr = {
|
|
199
296
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
200
297
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
201
298
|
3: "2026.05.1"
|
|
202
299
|
};
|
|
203
|
-
|
|
204
|
-
|
|
300
|
+
kt = /* @__PURE__ */ new Set();
|
|
301
|
+
ar = 2, cr = 0, lr = /* @__PURE__ */ new Set(["GET", "HEAD"]), dr = /* @__PURE__ */ new Set([
|
|
205
302
|
"rate_limited",
|
|
206
303
|
"server_error",
|
|
207
304
|
"network_error"
|
|
208
305
|
]);
|
|
209
|
-
|
|
306
|
+
Tt = 6e4;
|
|
210
307
|
});
|
|
211
308
|
|
|
212
309
|
// src/db-errors.ts
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
|
|
310
|
+
var Rr, y, Z = $(() => {
|
|
311
|
+
I();
|
|
312
|
+
Rr = /* @__PURE__ */ new Set([
|
|
216
313
|
"db_unavailable",
|
|
217
314
|
"db_conflict",
|
|
218
315
|
"db_timeout"
|
|
219
|
-
]), y = class n extends
|
|
316
|
+
]), y = class n extends l {
|
|
220
317
|
/** Código de erro fechado — específico de DB. */
|
|
221
318
|
code;
|
|
222
319
|
/**
|
|
@@ -230,16 +327,16 @@ var ir, y, Y = B(() => {
|
|
|
230
327
|
*/
|
|
231
328
|
dbId;
|
|
232
329
|
constructor(e, t, r) {
|
|
233
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
330
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = Rr.has(e), this.dbId = r, Object.setPrototypeOf(this, n.prototype);
|
|
234
331
|
}
|
|
235
332
|
};
|
|
236
333
|
});
|
|
237
334
|
|
|
238
335
|
// src/db/sql/resilient-pool.ts
|
|
239
|
-
function
|
|
240
|
-
return typeof n == "object" && n !== null && n[
|
|
336
|
+
function ve(n) {
|
|
337
|
+
return typeof n == "object" && n !== null && n[He] === true;
|
|
241
338
|
}
|
|
242
|
-
function
|
|
339
|
+
function Hr(n) {
|
|
243
340
|
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
244
341
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
245
342
|
return true;
|
|
@@ -251,36 +348,36 @@ function Cr(n) {
|
|
|
251
348
|
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
252
349
|
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"));
|
|
253
350
|
}
|
|
254
|
-
function
|
|
351
|
+
function Qe(n) {
|
|
255
352
|
let e = { current: n }, t = null, r = {
|
|
256
|
-
__neetruSwapInner(
|
|
353
|
+
__neetruSwapInner(c) {
|
|
257
354
|
let d = e.current;
|
|
258
|
-
return e.current =
|
|
355
|
+
return e.current = c, d;
|
|
259
356
|
},
|
|
260
357
|
__neetruGetInner() {
|
|
261
358
|
return e.current;
|
|
262
359
|
},
|
|
263
|
-
__neetruSetReLease(
|
|
264
|
-
t =
|
|
360
|
+
__neetruSetReLease(c) {
|
|
361
|
+
t = c;
|
|
265
362
|
}
|
|
266
|
-
}, o = (
|
|
363
|
+
}, o = (c, d) => c.query(...d), i = (...c) => c.length > 0 && typeof c[c.length - 1] == "function" ? o(e.current, c) : (async () => {
|
|
267
364
|
try {
|
|
268
|
-
return await o(e.current,
|
|
365
|
+
return await o(e.current, c);
|
|
269
366
|
} catch (d) {
|
|
270
367
|
let u = t;
|
|
271
|
-
if (u === null || !
|
|
368
|
+
if (u === null || !Hr(d))
|
|
272
369
|
throw d;
|
|
273
370
|
try {
|
|
274
371
|
await u();
|
|
275
372
|
} catch {
|
|
276
373
|
throw d;
|
|
277
374
|
}
|
|
278
|
-
return await o(e.current,
|
|
375
|
+
return await o(e.current, c);
|
|
279
376
|
}
|
|
280
377
|
})(), s = Object.create(Object.getPrototypeOf(n));
|
|
281
378
|
return new Proxy(s, {
|
|
282
|
-
get(
|
|
283
|
-
if (d ===
|
|
379
|
+
get(c, d) {
|
|
380
|
+
if (d === He) return true;
|
|
284
381
|
if (d === "__neetruSwapInner") return r.__neetruSwapInner;
|
|
285
382
|
if (d === "__neetruGetInner") return r.__neetruGetInner;
|
|
286
383
|
if (d === "__neetruSetReLease") return r.__neetruSetReLease;
|
|
@@ -292,15 +389,15 @@ function $e(n) {
|
|
|
292
389
|
}
|
|
293
390
|
});
|
|
294
391
|
}
|
|
295
|
-
var
|
|
296
|
-
|
|
392
|
+
var He, Ge = $(() => {
|
|
393
|
+
He = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
297
394
|
});
|
|
298
395
|
|
|
299
396
|
// src/db/sql/lease.ts
|
|
300
|
-
function
|
|
397
|
+
function Gr(n, e) {
|
|
301
398
|
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;
|
|
302
399
|
}
|
|
303
|
-
function
|
|
400
|
+
function Jr(n) {
|
|
304
401
|
if (n instanceof y) return n;
|
|
305
402
|
let e = n instanceof Error ? n.message : String(n), t = n instanceof Error ? n.code : void 0;
|
|
306
403
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new y(
|
|
@@ -308,10 +405,10 @@ function Er(n) {
|
|
|
308
405
|
`Pool connection failed (${t}): ${e}`
|
|
309
406
|
) : e.includes("statement timeout") || e.includes("lock timeout") ? new y("db_timeout", `Query timeout: ${e}`) : e.includes("40001") || e.includes("40P01") || e.includes("deadlock") ? new y("db_conflict", `Transaction conflict: ${e}`) : e.includes("42501") || e.toLowerCase().includes("permission denied") ? new y("db_permission_denied", `Permission denied: ${e}`) : new y("db_unavailable", `Database error: ${e}`);
|
|
310
407
|
}
|
|
311
|
-
var
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
408
|
+
var Qr, J, re, Gt = $(() => {
|
|
409
|
+
Z();
|
|
410
|
+
Ge();
|
|
411
|
+
Qr = 0.8, J = 3e4, re = class {
|
|
315
412
|
/**
|
|
316
413
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
317
414
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -351,7 +448,7 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
351
448
|
isolationLevel: t.isolationLevel
|
|
352
449
|
}) : await this._orm.transaction(e);
|
|
353
450
|
} catch (r) {
|
|
354
|
-
throw
|
|
451
|
+
throw Jr(r);
|
|
355
452
|
}
|
|
356
453
|
}
|
|
357
454
|
async close() {
|
|
@@ -374,7 +471,7 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
374
471
|
if (!Number.isFinite(e)) return J;
|
|
375
472
|
let t = this._deps.now(), r = e - t;
|
|
376
473
|
if (r <= 0) return J;
|
|
377
|
-
let o = r * (1 -
|
|
474
|
+
let o = r * (1 - Qr);
|
|
378
475
|
return Math.max(J, Math.round(o));
|
|
379
476
|
}
|
|
380
477
|
_scheduleRenewal() {
|
|
@@ -476,7 +573,7 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
476
573
|
"SqlLeaseManager foi fechado \u2014 aquisi\xE7\xE3o de lease inicial abortada."
|
|
477
574
|
);
|
|
478
575
|
if (this._lease)
|
|
479
|
-
return
|
|
576
|
+
return ve(this._pool) ? this._pool.__neetruGetInner() : this._pool;
|
|
480
577
|
if (this._initInFlight !== null) return this._initInFlight;
|
|
481
578
|
let e = (async () => {
|
|
482
579
|
let t;
|
|
@@ -494,7 +591,7 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
494
591
|
let i = o instanceof Error ? o.message : String(o);
|
|
495
592
|
throw new y("db_unavailable", `Falha ao abrir pool: ${i}`);
|
|
496
593
|
}
|
|
497
|
-
return
|
|
594
|
+
return ve(this._pool) ? this._pool.__neetruSwapInner(r).end().catch(() => {
|
|
498
595
|
}) : this._pool = r, this._lease = t, this._scheduleRenewal(), r;
|
|
499
596
|
})();
|
|
500
597
|
this._initInFlight = e;
|
|
@@ -510,12 +607,12 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
510
607
|
* Exposto como método protegido para testes.
|
|
511
608
|
*/
|
|
512
609
|
async _swapPool(e) {
|
|
513
|
-
if (!
|
|
610
|
+
if (!Gr(this._lease, e)) {
|
|
514
611
|
this._lease = e;
|
|
515
612
|
return;
|
|
516
613
|
}
|
|
517
614
|
let t = this._deps.createPool(e);
|
|
518
|
-
if (
|
|
615
|
+
if (ve(this._pool)) {
|
|
519
616
|
let i = this._pool.__neetruSwapInner(t);
|
|
520
617
|
this._lease = e, i.end().catch(() => {
|
|
521
618
|
});
|
|
@@ -537,29 +634,29 @@ var Ar, J, ne, Mt = B(() => {
|
|
|
537
634
|
});
|
|
538
635
|
|
|
539
636
|
// src/db/sql/sql-client.ts
|
|
540
|
-
var
|
|
541
|
-
|
|
542
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
543
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
544
|
-
DEFAULT_POOL_MAX: () =>
|
|
545
|
-
LEASE_ENDPOINT: () =>
|
|
546
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
547
|
-
createDrizzleHandle: () =>
|
|
548
|
-
createHttpLeaseFetcher: () =>
|
|
549
|
-
createPgPool: () =>
|
|
550
|
-
createSqlClientFromConfig: () =>
|
|
551
|
-
createSqlClientWithDeps: () =>
|
|
552
|
-
resolvePoolTuning: () =>
|
|
637
|
+
var rn = {};
|
|
638
|
+
ot(rn, {
|
|
639
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => tn,
|
|
640
|
+
DEFAULT_POOL_IDLE_MS: () => en,
|
|
641
|
+
DEFAULT_POOL_MAX: () => Zt,
|
|
642
|
+
LEASE_ENDPOINT: () => Jt,
|
|
643
|
+
LEASE_RENEW_ENDPOINT: () => Xt,
|
|
644
|
+
createDrizzleHandle: () => eo,
|
|
645
|
+
createHttpLeaseFetcher: () => Yt,
|
|
646
|
+
createPgPool: () => Zr,
|
|
647
|
+
createSqlClientFromConfig: () => no,
|
|
648
|
+
createSqlClientWithDeps: () => nn,
|
|
649
|
+
resolvePoolTuning: () => Xe
|
|
553
650
|
});
|
|
554
|
-
function
|
|
651
|
+
function Xr(n) {
|
|
555
652
|
return n === "workspace" ? "staging" : n === "prod" ? "production" : "dev-local";
|
|
556
653
|
}
|
|
557
|
-
function
|
|
654
|
+
function Yt(n) {
|
|
558
655
|
return async (e) => {
|
|
559
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
656
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (M(), Y)), r = !!e?.leaseId, o = r ? Xt : Jt, i;
|
|
560
657
|
r ? i = { leaseId: e.leaseId } : (i = {
|
|
561
658
|
productId: n.productId ?? "",
|
|
562
|
-
environment:
|
|
659
|
+
environment: Xr(n.env ?? "prod")
|
|
563
660
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
564
661
|
let s;
|
|
565
662
|
try {
|
|
@@ -572,23 +669,23 @@ function jt(n) {
|
|
|
572
669
|
retries: 2
|
|
573
670
|
});
|
|
574
671
|
} catch (a) {
|
|
575
|
-
let
|
|
672
|
+
let c = r ? "renovar" : "obter", d = a instanceof Error ? a.message : String(a), u = a instanceof l ? a.reason : void 0;
|
|
576
673
|
if (u === "suspended" || u === "archived" || u === "revoked") {
|
|
577
674
|
let p = u === "suspended" ? "ambiente suspenso \u2014 a m\xE1quina que o servia parou; ser\xE1 religado quando o ambiente voltar" : u === "archived" ? "ambiente arquivado \u2014 este banco foi arquivado e n\xE3o aceita mais conex\xF5es" : "lease revogado \u2014 as credenciais deste ambiente foram revogadas pelo Core";
|
|
578
675
|
throw new y(
|
|
579
676
|
"db_suspended",
|
|
580
|
-
`Falha ao ${
|
|
677
|
+
`Falha ao ${c} lease SQL: ${p} (reason=${u}).`
|
|
581
678
|
);
|
|
582
679
|
}
|
|
583
680
|
throw new y(
|
|
584
681
|
"db_unavailable",
|
|
585
|
-
`Falha ao ${
|
|
682
|
+
`Falha ao ${c} lease SQL: ${d}`
|
|
586
683
|
);
|
|
587
684
|
}
|
|
588
|
-
return
|
|
685
|
+
return Yr(s);
|
|
589
686
|
};
|
|
590
687
|
}
|
|
591
|
-
function
|
|
688
|
+
function Yr(n) {
|
|
592
689
|
if (!n || typeof n != "object")
|
|
593
690
|
throw new y("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
594
691
|
let t = n.lease;
|
|
@@ -620,22 +717,22 @@ function Nr(n) {
|
|
|
620
717
|
expiresAt: o("expiresAt", "string")
|
|
621
718
|
};
|
|
622
719
|
}
|
|
623
|
-
function
|
|
720
|
+
function Je(n, e, t, r) {
|
|
624
721
|
let o = typeof process < "u" ? process.env?.[n] : void 0;
|
|
625
722
|
if (o === void 0 || o.trim() === "") return e;
|
|
626
723
|
let i = Number.parseInt(o.trim(), 10);
|
|
627
724
|
return !Number.isFinite(i) || i < t || i > r ? e : i;
|
|
628
725
|
}
|
|
629
|
-
function
|
|
726
|
+
function Xe() {
|
|
630
727
|
return {
|
|
631
728
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
632
729
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
633
|
-
max:
|
|
730
|
+
max: Je("NEETRU_DB_POOL_MAX", Zt, 1, 100),
|
|
634
731
|
// Teto 15min = janela do cert; piso 1s.
|
|
635
|
-
idleTimeoutMillis:
|
|
636
|
-
connectionTimeoutMillis:
|
|
732
|
+
idleTimeoutMillis: Je("NEETRU_DB_POOL_IDLE_MS", en, 1e3, 9e5),
|
|
733
|
+
connectionTimeoutMillis: Je(
|
|
637
734
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
638
|
-
|
|
735
|
+
tn,
|
|
639
736
|
1e3,
|
|
640
737
|
12e4
|
|
641
738
|
),
|
|
@@ -643,7 +740,7 @@ function ze() {
|
|
|
643
740
|
keepAliveInitialDelayMillis: 1e4
|
|
644
741
|
};
|
|
645
742
|
}
|
|
646
|
-
async function
|
|
743
|
+
async function Zr(n) {
|
|
647
744
|
let { Pool: e } = await import(
|
|
648
745
|
/* webpackIgnore: true */
|
|
649
746
|
'pg'
|
|
@@ -660,17 +757,17 @@ async function xr(n) {
|
|
|
660
757
|
user: n.user,
|
|
661
758
|
password: n.password,
|
|
662
759
|
ssl: t,
|
|
663
|
-
...
|
|
760
|
+
...Xe()
|
|
664
761
|
});
|
|
665
762
|
}
|
|
666
|
-
async function
|
|
763
|
+
async function eo(n, e) {
|
|
667
764
|
let { drizzle: t } = await import(
|
|
668
765
|
/* webpackIgnore: true */
|
|
669
766
|
'drizzle-orm/node-postgres'
|
|
670
767
|
);
|
|
671
768
|
return t(n, { schema: e });
|
|
672
769
|
}
|
|
673
|
-
function
|
|
770
|
+
function to(n, e) {
|
|
674
771
|
let t = null, r = null, o = () => t ? Promise.resolve(t) : (r || (r = e().then((s) => (t = s, r = null, s)).catch((s) => {
|
|
675
772
|
throw r = null, s;
|
|
676
773
|
})), r), i = {
|
|
@@ -686,52 +783,52 @@ function Lr(n, e) {
|
|
|
686
783
|
};
|
|
687
784
|
return n && Object.setPrototypeOf(i, n), i;
|
|
688
785
|
}
|
|
689
|
-
async function
|
|
786
|
+
async function nn(n, e) {
|
|
690
787
|
let t = e?.database !== void 0 ? { ...n, database: e.database } : n;
|
|
691
788
|
if (t.lazy) {
|
|
692
|
-
let
|
|
693
|
-
if (!
|
|
789
|
+
let c = null, d = to(t.poolPrototype ?? null, () => {
|
|
790
|
+
if (!c)
|
|
694
791
|
throw new y("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
695
|
-
return
|
|
696
|
-
}), u =
|
|
792
|
+
return c._ensureInitialLease();
|
|
793
|
+
}), u = Qe(d), p;
|
|
697
794
|
try {
|
|
698
795
|
p = t.createDrizzle(u, t.schema);
|
|
699
|
-
} catch (
|
|
700
|
-
let
|
|
701
|
-
throw new y("db_unavailable", `Falha ao criar handle Drizzle: ${
|
|
796
|
+
} catch (_) {
|
|
797
|
+
let b = _ instanceof Error ? _.message : String(_);
|
|
798
|
+
throw new y("db_unavailable", `Falha ao criar handle Drizzle: ${b}`);
|
|
702
799
|
}
|
|
703
|
-
let f = new
|
|
704
|
-
return
|
|
800
|
+
let f = new re(null, u, p, t);
|
|
801
|
+
return c = f, u.__neetruSetReLease(() => f._reactiveReLease()), f;
|
|
705
802
|
}
|
|
706
803
|
let r;
|
|
707
804
|
try {
|
|
708
805
|
e?.database !== void 0 ? r = await t.fetchLease({ database: e.database }) : r = await t.fetchLease();
|
|
709
|
-
} catch (
|
|
710
|
-
if (
|
|
711
|
-
let d =
|
|
806
|
+
} catch (c) {
|
|
807
|
+
if (c instanceof y) throw c;
|
|
808
|
+
let d = c instanceof Error ? c.message : String(c);
|
|
712
809
|
throw new y("db_unavailable", `Falha ao obter lease inicial: ${d}`);
|
|
713
810
|
}
|
|
714
811
|
let o;
|
|
715
812
|
try {
|
|
716
813
|
o = t.createPool(r);
|
|
717
|
-
} catch (
|
|
718
|
-
let d =
|
|
814
|
+
} catch (c) {
|
|
815
|
+
let d = c instanceof Error ? c.message : String(c);
|
|
719
816
|
throw new y("db_unavailable", `Falha ao abrir pool: ${d}`);
|
|
720
817
|
}
|
|
721
|
-
let i =
|
|
818
|
+
let i = Qe(o), s;
|
|
722
819
|
try {
|
|
723
820
|
s = t.createDrizzle(i, t.schema);
|
|
724
|
-
} catch (
|
|
821
|
+
} catch (c) {
|
|
725
822
|
await o.end().catch(() => {
|
|
726
823
|
});
|
|
727
|
-
let d =
|
|
824
|
+
let d = c instanceof Error ? c.message : String(c);
|
|
728
825
|
throw new y("db_unavailable", `Falha ao criar handle Drizzle: ${d}`);
|
|
729
826
|
}
|
|
730
|
-
let a = new
|
|
827
|
+
let a = new re(r, i, s, t);
|
|
731
828
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
732
829
|
}
|
|
733
|
-
async function
|
|
734
|
-
let r =
|
|
830
|
+
async function no(n, e, t) {
|
|
831
|
+
let r = Yt(n), o, i;
|
|
735
832
|
try {
|
|
736
833
|
o = await import(
|
|
737
834
|
/* webpackIgnore: true */
|
|
@@ -747,7 +844,7 @@ async function Ur(n, e, t) {
|
|
|
747
844
|
);
|
|
748
845
|
}
|
|
749
846
|
let s = o.Pool ?? o.default?.Pool;
|
|
750
|
-
return
|
|
847
|
+
return nn(
|
|
751
848
|
{
|
|
752
849
|
fetchLease: r,
|
|
753
850
|
createPool: (d) => {
|
|
@@ -766,7 +863,7 @@ async function Ur(n, e, t) {
|
|
|
766
863
|
ssl: u,
|
|
767
864
|
// Tuning centralizado (env-tunável) — ver resolvePoolTuning + auditoria
|
|
768
865
|
// docs/_review/AUDITORIA_DB_CONN_PERF_2026-09-06.md no Core.
|
|
769
|
-
...
|
|
866
|
+
...Xe()
|
|
770
867
|
});
|
|
771
868
|
},
|
|
772
869
|
createDrizzle: (d, u) => i.drizzle(d, { schema: u }),
|
|
@@ -782,89 +879,100 @@ async function Ur(n, e, t) {
|
|
|
782
879
|
t
|
|
783
880
|
);
|
|
784
881
|
}
|
|
785
|
-
var
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
882
|
+
var Jt, Xt, Zt, en, tn, on = $(() => {
|
|
883
|
+
Z();
|
|
884
|
+
I();
|
|
885
|
+
Gt();
|
|
886
|
+
Ge();
|
|
887
|
+
Jt = "/api/sdk/v1/db/lease", Xt = "/api/sdk/v1/db/lease/renew";
|
|
888
|
+
Zt = 10, en = 6e4, tn = 1e4;
|
|
792
889
|
});
|
|
890
|
+
var Le = /* @__PURE__ */ new Map(), Ue = 3600 * 1e3;
|
|
891
|
+
function it(n, e = Ue) {
|
|
892
|
+
let t = Date.now(), r = Le.get(n);
|
|
893
|
+
if (r && t - r.createdAt < e)
|
|
894
|
+
return r.jwks;
|
|
895
|
+
let o = createRemoteJWKSet(new URL(n));
|
|
896
|
+
return Le.set(n, { jwks: o, createdAt: t }), o;
|
|
897
|
+
}
|
|
898
|
+
function st() {
|
|
899
|
+
Le.clear();
|
|
900
|
+
}
|
|
793
901
|
|
|
794
902
|
// src/auth.ts
|
|
795
|
-
|
|
903
|
+
I();
|
|
796
904
|
|
|
797
905
|
// src/api-key.ts
|
|
798
|
-
|
|
799
|
-
var
|
|
800
|
-
function
|
|
906
|
+
I();
|
|
907
|
+
var at = /^nrt_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/;
|
|
908
|
+
function ct(n) {
|
|
801
909
|
if (typeof n != "string" || n.length === 0)
|
|
802
|
-
throw new
|
|
910
|
+
throw new l(
|
|
803
911
|
"invalid_config",
|
|
804
912
|
"apiKey is required (formato nrt_<keyId>_<secret>)"
|
|
805
913
|
);
|
|
806
|
-
let e =
|
|
914
|
+
let e = at.exec(n);
|
|
807
915
|
if (!e)
|
|
808
|
-
throw new
|
|
916
|
+
throw new l(
|
|
809
917
|
"invalid_config",
|
|
810
918
|
"apiKey malformado - esperado formato nrt_<keyId>_<secret>"
|
|
811
919
|
);
|
|
812
920
|
return { keyId: e[1], secret: e[2] };
|
|
813
921
|
}
|
|
814
|
-
function
|
|
922
|
+
function lt(n) {
|
|
815
923
|
if (typeof n != "string" || n.length === 0) return null;
|
|
816
|
-
let e =
|
|
924
|
+
let e = at.exec(n);
|
|
817
925
|
return e ? { keyId: e[1], secret: e[2] } : null;
|
|
818
926
|
}
|
|
819
927
|
|
|
820
928
|
// src/base-url-allowlist.ts
|
|
821
|
-
|
|
822
|
-
var
|
|
823
|
-
function
|
|
824
|
-
return
|
|
929
|
+
I();
|
|
930
|
+
var dt = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), Gn = ".neetru.com", Jn = "neetru.com", Xn = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
931
|
+
function ut(n) {
|
|
932
|
+
return Xn.test(n);
|
|
825
933
|
}
|
|
826
|
-
function
|
|
934
|
+
function Yn(n) {
|
|
827
935
|
return n.toLowerCase().replace(/\.+$/, "");
|
|
828
936
|
}
|
|
829
|
-
function
|
|
937
|
+
function pt(n, e = []) {
|
|
830
938
|
if (typeof n != "string" || n.length === 0)
|
|
831
|
-
throw new
|
|
939
|
+
throw new l("invalid_config", "baseUrl is required");
|
|
832
940
|
let t;
|
|
833
941
|
try {
|
|
834
942
|
t = new URL(n);
|
|
835
943
|
} catch {
|
|
836
|
-
throw new
|
|
944
|
+
throw new l("invalid_config", `baseUrl is not a valid URL: ${n}`);
|
|
837
945
|
}
|
|
838
|
-
let r = t.protocol, o =
|
|
839
|
-
if (
|
|
946
|
+
let r = t.protocol, o = Yn(t.hostname);
|
|
947
|
+
if (Zn())
|
|
840
948
|
return typeof console < "u" && console.warn(
|
|
841
949
|
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${n} aceito sem valida\xE7\xE3o.`
|
|
842
950
|
), n.replace(/\/+$/, "");
|
|
843
951
|
if (r !== "https:" && r !== "http:")
|
|
844
|
-
throw new
|
|
952
|
+
throw new l(
|
|
845
953
|
"invalid_config",
|
|
846
954
|
`baseUrl protocol must be http: or https:, got ${r}`
|
|
847
955
|
);
|
|
848
956
|
if (r === "http:") {
|
|
849
|
-
if (!(
|
|
850
|
-
throw new
|
|
957
|
+
if (!(dt.has(o) || ut(o)))
|
|
958
|
+
throw new l(
|
|
851
959
|
"invalid_config",
|
|
852
960
|
`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.`
|
|
853
961
|
);
|
|
854
962
|
return n.replace(/\/+$/, "");
|
|
855
963
|
}
|
|
856
|
-
let i = o ===
|
|
857
|
-
let d =
|
|
964
|
+
let i = o === Jn || o.endsWith(Gn), s = e.some((c) => {
|
|
965
|
+
let d = c.toLowerCase();
|
|
858
966
|
return o === d || o.endsWith(`.${d}`);
|
|
859
|
-
}), a =
|
|
967
|
+
}), a = dt.has(o) || ut(o);
|
|
860
968
|
if (!i && !s && !a)
|
|
861
|
-
throw new
|
|
969
|
+
throw new l(
|
|
862
970
|
"invalid_config",
|
|
863
971
|
`baseUrl ${o} is not in the allowlist. Allowed: *.neetru.com, localhost, or configure { allowedHosts: ['...'] }. Bypass: NEETRU_ALLOW_INSECURE_BASEURL=1.`
|
|
864
972
|
);
|
|
865
973
|
return n.replace(/\/+$/, "");
|
|
866
974
|
}
|
|
867
|
-
function
|
|
975
|
+
function Zn() {
|
|
868
976
|
try {
|
|
869
977
|
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
870
978
|
if (e === "1" || e === "true") return !0;
|
|
@@ -879,19 +987,19 @@ function Mn() {
|
|
|
879
987
|
}
|
|
880
988
|
|
|
881
989
|
// src/types.ts
|
|
882
|
-
var
|
|
990
|
+
var ft = "https://api.neetru.com", mt = "http://localhost:9003";
|
|
883
991
|
|
|
884
992
|
// src/catalog.ts
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
function
|
|
993
|
+
I();
|
|
994
|
+
M();
|
|
995
|
+
function At(n) {
|
|
888
996
|
if (!n || typeof n != "object")
|
|
889
|
-
throw new
|
|
997
|
+
throw new l("invalid_response", "Catalog response item is not an object");
|
|
890
998
|
let e = n;
|
|
891
999
|
if (typeof e.slug != "string" || !e.slug)
|
|
892
|
-
throw new
|
|
1000
|
+
throw new l("invalid_response", "Catalog product missing slug");
|
|
893
1001
|
if (typeof e.name != "string" || !e.name)
|
|
894
|
-
throw new
|
|
1002
|
+
throw new l("invalid_response", "Catalog product missing name");
|
|
895
1003
|
let t = {
|
|
896
1004
|
slug: e.slug,
|
|
897
1005
|
name: e.name
|
|
@@ -903,7 +1011,7 @@ function gt(n) {
|
|
|
903
1011
|
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;
|
|
904
1012
|
})), t;
|
|
905
1013
|
}
|
|
906
|
-
function
|
|
1014
|
+
function Et(n) {
|
|
907
1015
|
return {
|
|
908
1016
|
/**
|
|
909
1017
|
* Lista produtos publicados. Por default só `published=true`; staff
|
|
@@ -915,12 +1023,12 @@ function yt(n) {
|
|
|
915
1023
|
path: "/api/sdk/v1/catalog"
|
|
916
1024
|
});
|
|
917
1025
|
if (!t || !Array.isArray(t.products))
|
|
918
|
-
throw new
|
|
1026
|
+
throw new l(
|
|
919
1027
|
"invalid_response",
|
|
920
1028
|
"Catalog list response missing products array"
|
|
921
1029
|
);
|
|
922
1030
|
return {
|
|
923
|
-
products: t.products.map(
|
|
1031
|
+
products: t.products.map(At),
|
|
924
1032
|
fetchedAt: typeof t.fetchedAt == "string" ? t.fetchedAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
925
1033
|
};
|
|
926
1034
|
},
|
|
@@ -931,42 +1039,42 @@ function yt(n) {
|
|
|
931
1039
|
*/
|
|
932
1040
|
async get(e) {
|
|
933
1041
|
if (!e || typeof e != "string")
|
|
934
|
-
throw new
|
|
1042
|
+
throw new l("validation_failed", "slug is required");
|
|
935
1043
|
let t = await v(n, {
|
|
936
1044
|
method: "GET",
|
|
937
1045
|
path: `/api/sdk/v1/catalog/${encodeURIComponent(e)}`
|
|
938
1046
|
});
|
|
939
1047
|
if (!t || !t.product)
|
|
940
|
-
throw new
|
|
1048
|
+
throw new l(
|
|
941
1049
|
"invalid_response",
|
|
942
1050
|
"Catalog get response missing product"
|
|
943
1051
|
);
|
|
944
|
-
return
|
|
1052
|
+
return At(t.product);
|
|
945
1053
|
}
|
|
946
1054
|
};
|
|
947
1055
|
}
|
|
948
1056
|
|
|
949
1057
|
// src/entitlements.ts
|
|
950
|
-
|
|
951
|
-
var
|
|
952
|
-
function
|
|
953
|
-
throw new
|
|
1058
|
+
I();
|
|
1059
|
+
var fr = "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.";
|
|
1060
|
+
function Pt() {
|
|
1061
|
+
throw new l("validation_failed", fr);
|
|
954
1062
|
}
|
|
955
|
-
function
|
|
1063
|
+
function Dt(n) {
|
|
956
1064
|
return {
|
|
957
1065
|
/**
|
|
958
1066
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
959
1067
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
960
1068
|
*/
|
|
961
1069
|
async check(e, t, r) {
|
|
962
|
-
|
|
1070
|
+
Pt();
|
|
963
1071
|
},
|
|
964
1072
|
/**
|
|
965
1073
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
966
1074
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
967
1075
|
*/
|
|
968
1076
|
async checkDetailed(e, t, r) {
|
|
969
|
-
|
|
1077
|
+
Pt();
|
|
970
1078
|
},
|
|
971
1079
|
/** Test helper legado — no-op (não há mais cache). Mantido por compat. */
|
|
972
1080
|
__resetCache() {
|
|
@@ -975,16 +1083,16 @@ function bt(n) {
|
|
|
975
1083
|
}
|
|
976
1084
|
|
|
977
1085
|
// src/telemetry.ts
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
var
|
|
981
|
-
function
|
|
1086
|
+
I();
|
|
1087
|
+
M();
|
|
1088
|
+
var Nt = ["debug", "info", "warn", "error", "fatal"];
|
|
1089
|
+
function mr() {
|
|
982
1090
|
return typeof globalThis < "u" && typeof globalThis.window < "u";
|
|
983
1091
|
}
|
|
984
|
-
function
|
|
985
|
-
return
|
|
1092
|
+
function xt() {
|
|
1093
|
+
return mr() ? { path: "/api/sdk/v1/telemetry/event/public", requireAuth: false } : { path: "/api/sdk/v1/telemetry/event", requireAuth: true };
|
|
986
1094
|
}
|
|
987
|
-
function
|
|
1095
|
+
function hr(n) {
|
|
988
1096
|
switch (n) {
|
|
989
1097
|
case "debug":
|
|
990
1098
|
return console.debug.bind(console);
|
|
@@ -1000,17 +1108,17 @@ function Gn(n) {
|
|
|
1000
1108
|
return console.log.bind(console);
|
|
1001
1109
|
}
|
|
1002
1110
|
}
|
|
1003
|
-
var
|
|
1004
|
-
function
|
|
1111
|
+
var gr = 500, yr = 50;
|
|
1112
|
+
function Ot(n) {
|
|
1005
1113
|
let e = [], t = null, r = null;
|
|
1006
1114
|
async function o() {
|
|
1007
1115
|
if (e.length === 0) return;
|
|
1008
1116
|
let s = e.splice(0, e.length), a = Promise.allSettled(
|
|
1009
|
-
s.map(async (
|
|
1117
|
+
s.map(async (c) => {
|
|
1010
1118
|
try {
|
|
1011
|
-
let d = { name:
|
|
1012
|
-
u && (d.productSlug = u),
|
|
1013
|
-
let p =
|
|
1119
|
+
let d = { name: c.name }, u = c.productSlug ?? n.productId;
|
|
1120
|
+
u && (d.productSlug = u), c.properties && typeof c.properties == "object" && (d.properties = c.properties), c.timestamp && (d.timestamp = c.timestamp);
|
|
1121
|
+
let p = xt();
|
|
1014
1122
|
await v(n, {
|
|
1015
1123
|
method: "POST",
|
|
1016
1124
|
path: p.path,
|
|
@@ -1023,7 +1131,7 @@ function kt(n) {
|
|
|
1023
1131
|
idempotencyKey: !0
|
|
1024
1132
|
});
|
|
1025
1133
|
} catch (d) {
|
|
1026
|
-
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event",
|
|
1134
|
+
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event", c.name, d);
|
|
1027
1135
|
}
|
|
1028
1136
|
})
|
|
1029
1137
|
);
|
|
@@ -1034,7 +1142,7 @@ function kt(n) {
|
|
|
1034
1142
|
function i() {
|
|
1035
1143
|
t || (t = setTimeout(() => {
|
|
1036
1144
|
o();
|
|
1037
|
-
},
|
|
1145
|
+
}, gr));
|
|
1038
1146
|
}
|
|
1039
1147
|
return {
|
|
1040
1148
|
/**
|
|
@@ -1051,21 +1159,21 @@ function kt(n) {
|
|
|
1051
1159
|
*/
|
|
1052
1160
|
async event(s) {
|
|
1053
1161
|
if (!s || typeof s != "object")
|
|
1054
|
-
throw new
|
|
1162
|
+
throw new l("validation_failed", "event input is required");
|
|
1055
1163
|
if (typeof s.name != "string" || s.name.length === 0)
|
|
1056
|
-
throw new
|
|
1164
|
+
throw new l("validation_failed", "event.name is required");
|
|
1057
1165
|
if (s.name.length > 128)
|
|
1058
|
-
throw new
|
|
1059
|
-
let a = { name: s.name },
|
|
1060
|
-
|
|
1061
|
-
let d =
|
|
1166
|
+
throw new l("validation_failed", "event.name max 128 chars");
|
|
1167
|
+
let a = { name: s.name }, c = s.productSlug ?? n.productId;
|
|
1168
|
+
c && (a.productSlug = c), s.properties && typeof s.properties == "object" && (a.properties = s.properties), s.timestamp && (a.timestamp = s.timestamp);
|
|
1169
|
+
let d = xt(), u = await v(n, {
|
|
1062
1170
|
method: "POST",
|
|
1063
1171
|
path: d.path,
|
|
1064
1172
|
body: a,
|
|
1065
1173
|
requireAuth: d.requireAuth
|
|
1066
1174
|
});
|
|
1067
1175
|
if (!u || u.ok !== true || typeof u.eventId != "string")
|
|
1068
|
-
throw new
|
|
1176
|
+
throw new l("invalid_response", "Telemetry response missing eventId");
|
|
1069
1177
|
return { ok: true, eventId: u.eventId };
|
|
1070
1178
|
},
|
|
1071
1179
|
/**
|
|
@@ -1079,7 +1187,7 @@ function kt(n) {
|
|
|
1079
1187
|
* ```
|
|
1080
1188
|
*/
|
|
1081
1189
|
track(s) {
|
|
1082
|
-
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >=
|
|
1190
|
+
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >= yr ? (t && (clearTimeout(t), t = null), o()) : i());
|
|
1083
1191
|
},
|
|
1084
1192
|
/**
|
|
1085
1193
|
* Força flush imediato da queue de `track()`. Resolva antes de
|
|
@@ -1105,22 +1213,22 @@ function kt(n) {
|
|
|
1105
1213
|
*/
|
|
1106
1214
|
async log(s) {
|
|
1107
1215
|
if (!s || typeof s != "object")
|
|
1108
|
-
throw new
|
|
1109
|
-
if (!
|
|
1110
|
-
throw new
|
|
1216
|
+
throw new l("validation_failed", "log input is required");
|
|
1217
|
+
if (!Nt.includes(s.level))
|
|
1218
|
+
throw new l("validation_failed", `level must be one of ${Nt.join(", ")}`);
|
|
1111
1219
|
if (typeof s.message != "string" || s.message.length === 0)
|
|
1112
|
-
throw new
|
|
1220
|
+
throw new l("validation_failed", "message is required");
|
|
1113
1221
|
if (s.message.length > 4e3)
|
|
1114
|
-
throw new
|
|
1222
|
+
throw new l("validation_failed", "message max 4000 chars");
|
|
1115
1223
|
if (n.env === "dev")
|
|
1116
|
-
return
|
|
1224
|
+
return hr(s.level)(`[neetru-sdk] ${s.level}: ${s.message}`, s.metadata ?? {}), { ok: true, mode: "mock" };
|
|
1117
1225
|
let a = {
|
|
1118
1226
|
level: s.level,
|
|
1119
1227
|
message: s.message
|
|
1120
1228
|
};
|
|
1121
1229
|
s.metadata && (a.metadata = s.metadata);
|
|
1122
|
-
let
|
|
1123
|
-
|
|
1230
|
+
let c = s.productSlug ?? n.productId;
|
|
1231
|
+
c && (a.productSlug = c);
|
|
1124
1232
|
let d = s.correlationId;
|
|
1125
1233
|
if (!d)
|
|
1126
1234
|
try {
|
|
@@ -1138,7 +1246,7 @@ function kt(n) {
|
|
|
1138
1246
|
headers: u
|
|
1139
1247
|
});
|
|
1140
1248
|
if (!p || p.ok !== true)
|
|
1141
|
-
throw new
|
|
1249
|
+
throw new l("invalid_response", "Telemetry log response missing ok");
|
|
1142
1250
|
return {
|
|
1143
1251
|
ok: true,
|
|
1144
1252
|
logId: typeof p.logId == "string" ? p.logId : void 0,
|
|
@@ -1149,22 +1257,22 @@ function kt(n) {
|
|
|
1149
1257
|
}
|
|
1150
1258
|
|
|
1151
1259
|
// src/usage.ts
|
|
1152
|
-
|
|
1153
|
-
|
|
1260
|
+
I();
|
|
1261
|
+
M();
|
|
1154
1262
|
|
|
1155
1263
|
// src/validators.ts
|
|
1156
|
-
|
|
1157
|
-
var
|
|
1158
|
-
function
|
|
1159
|
-
if (typeof e != "string" || !
|
|
1160
|
-
throw new
|
|
1264
|
+
I();
|
|
1265
|
+
var Lt = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
1266
|
+
function N(n, e) {
|
|
1267
|
+
if (typeof e != "string" || !Lt.test(e))
|
|
1268
|
+
throw new l(
|
|
1161
1269
|
"validation_failed",
|
|
1162
|
-
`${n} must match ${
|
|
1270
|
+
`${n} must match ${Lt.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
1163
1271
|
);
|
|
1164
1272
|
}
|
|
1165
1273
|
|
|
1166
1274
|
// src/usage.ts
|
|
1167
|
-
function
|
|
1275
|
+
function Ut(n) {
|
|
1168
1276
|
return {
|
|
1169
1277
|
/**
|
|
1170
1278
|
* Reporta consumo metered de um resource. Hit no endpoint canônico
|
|
@@ -1180,21 +1288,21 @@ function Tt(n) {
|
|
|
1180
1288
|
*/
|
|
1181
1289
|
async report(e, t = 1, r) {
|
|
1182
1290
|
if (!e || typeof e != "string")
|
|
1183
|
-
throw new
|
|
1291
|
+
throw new l("validation_failed", "resource is required");
|
|
1184
1292
|
if (!Number.isFinite(t) || t <= 0 || !Number.isInteger(t))
|
|
1185
|
-
throw new
|
|
1293
|
+
throw new l("validation_failed", "qty must be a positive integer");
|
|
1186
1294
|
let o = r?.productId ?? n.productId, i = r?.tenantId ?? n.tenantId;
|
|
1187
1295
|
if (!o)
|
|
1188
|
-
throw new
|
|
1296
|
+
throw new l(
|
|
1189
1297
|
"validation_failed",
|
|
1190
1298
|
"productId required (pass to options or set on createNeetruClient)"
|
|
1191
1299
|
);
|
|
1192
1300
|
if (!i)
|
|
1193
|
-
throw new
|
|
1301
|
+
throw new l(
|
|
1194
1302
|
"validation_failed",
|
|
1195
1303
|
"tenantId required (pass to options or set on createNeetruClient)"
|
|
1196
1304
|
);
|
|
1197
|
-
|
|
1305
|
+
N("productId", o), N("tenantId", i);
|
|
1198
1306
|
let s = await v(n, {
|
|
1199
1307
|
method: "POST",
|
|
1200
1308
|
path: "/api/sdk/v1/usage/record",
|
|
@@ -1203,7 +1311,7 @@ function Tt(n) {
|
|
|
1203
1311
|
idempotencyKey: true
|
|
1204
1312
|
});
|
|
1205
1313
|
if (!s || s.ok !== true)
|
|
1206
|
-
throw new
|
|
1314
|
+
throw new l("invalid_response", "usage.report response missing ok");
|
|
1207
1315
|
return {
|
|
1208
1316
|
ok: true,
|
|
1209
1317
|
counterId: s.counterId,
|
|
@@ -1221,14 +1329,14 @@ function Tt(n) {
|
|
|
1221
1329
|
*/
|
|
1222
1330
|
async check(e, t) {
|
|
1223
1331
|
if (!e || typeof e != "string")
|
|
1224
|
-
throw new
|
|
1332
|
+
throw new l("validation_failed", "resource is required");
|
|
1225
1333
|
let r = t?.productId ?? n.productId, o = t?.tenantId ?? n.tenantId;
|
|
1226
1334
|
if (!r || !o)
|
|
1227
|
-
throw new
|
|
1335
|
+
throw new l(
|
|
1228
1336
|
"validation_failed",
|
|
1229
1337
|
"productId and tenantId required (pass to options or set on createNeetruClient)"
|
|
1230
1338
|
);
|
|
1231
|
-
|
|
1339
|
+
N("productId", r), N("tenantId", o);
|
|
1232
1340
|
let i = await v(n, {
|
|
1233
1341
|
method: "GET",
|
|
1234
1342
|
path: "/api/sdk/v1/entitlements",
|
|
@@ -1236,7 +1344,7 @@ function Tt(n) {
|
|
|
1236
1344
|
requireAuth: true
|
|
1237
1345
|
});
|
|
1238
1346
|
if (!i || typeof i.allowed != "boolean")
|
|
1239
|
-
throw new
|
|
1347
|
+
throw new l("invalid_response", "usage.check response missing allowed");
|
|
1240
1348
|
let s = i.behavior ?? (i.reason === "limit_exceeded" ? "readonly" : null);
|
|
1241
1349
|
return {
|
|
1242
1350
|
allowed: i.allowed,
|
|
@@ -1252,22 +1360,22 @@ function Tt(n) {
|
|
|
1252
1360
|
}
|
|
1253
1361
|
|
|
1254
1362
|
// src/support.ts
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
var
|
|
1258
|
-
function
|
|
1363
|
+
I();
|
|
1364
|
+
M();
|
|
1365
|
+
var je = ["low", "normal", "high", "urgent"], _r = ["open", "pending", "resolved", "closed"], Mt = 50;
|
|
1366
|
+
function qt(n) {
|
|
1259
1367
|
if (!n || typeof n != "object")
|
|
1260
|
-
throw new
|
|
1368
|
+
throw new l("invalid_response", "Ticket response is not an object");
|
|
1261
1369
|
let e = n;
|
|
1262
1370
|
if (typeof e.id != "string")
|
|
1263
|
-
throw new
|
|
1371
|
+
throw new l("invalid_response", "Ticket missing id");
|
|
1264
1372
|
let t = Array.isArray(e.attachments) && typeof e.attachments[0] == "string" ? e.attachments[0] : void 0, r = typeof e.screenshotUrl == "string" ? e.screenshotUrl : t ?? null;
|
|
1265
1373
|
return {
|
|
1266
1374
|
id: e.id,
|
|
1267
1375
|
subject: typeof e.subject == "string" ? e.subject : "",
|
|
1268
1376
|
message: typeof e.message == "string" ? e.message : "",
|
|
1269
|
-
severity:
|
|
1270
|
-
status:
|
|
1377
|
+
severity: je.includes(e.severity) ? e.severity : "normal",
|
|
1378
|
+
status: _r.includes(e.status) ? e.status : "open",
|
|
1271
1379
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
1272
1380
|
updatedAt: typeof e.updatedAt == "string" ? e.updatedAt : void 0,
|
|
1273
1381
|
productSlug: typeof e.productId == "string" ? e.productId : typeof e.productSlug == "string" ? e.productSlug : void 0,
|
|
@@ -1285,60 +1393,63 @@ function It(n) {
|
|
|
1285
1393
|
satisfactionComment: typeof e.satisfactionComment == "string" ? e.satisfactionComment : null
|
|
1286
1394
|
};
|
|
1287
1395
|
}
|
|
1288
|
-
function
|
|
1396
|
+
function de(n) {
|
|
1289
1397
|
if (!n || typeof n != "string")
|
|
1290
|
-
throw new
|
|
1398
|
+
throw new l("validation_failed", "ticketId is required");
|
|
1291
1399
|
}
|
|
1292
|
-
function
|
|
1400
|
+
function Ft(n) {
|
|
1293
1401
|
if (typeof n == "string" && n.length > 2e3)
|
|
1294
|
-
throw new
|
|
1402
|
+
throw new l("validation_failed", "comment max 2000 chars");
|
|
1295
1403
|
}
|
|
1296
|
-
function
|
|
1404
|
+
function jt(n, ...e) {
|
|
1297
1405
|
for (let t of e)
|
|
1298
1406
|
if (typeof t == "string" && t.length > 0)
|
|
1299
|
-
return
|
|
1407
|
+
return N("productSlug", t), t;
|
|
1300
1408
|
if (n.productId)
|
|
1301
|
-
return
|
|
1302
|
-
throw new
|
|
1409
|
+
return N("productId", n.productId), n.productId;
|
|
1410
|
+
throw new l(
|
|
1303
1411
|
"validation_failed",
|
|
1304
1412
|
"productId required (pass to input.productSlug or set on createNeetruClient)"
|
|
1305
1413
|
);
|
|
1306
1414
|
}
|
|
1307
|
-
var
|
|
1308
|
-
function
|
|
1309
|
-
if (n instanceof
|
|
1415
|
+
var br = "X-Neetru-End-User-Token", vr = "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.", wr = "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.", kr = "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.", Sr = "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.";
|
|
1416
|
+
function $t(n) {
|
|
1417
|
+
if (n instanceof l && !(n instanceof V)) {
|
|
1310
1418
|
if (n.code === "end_user_token_required")
|
|
1311
1419
|
throw new V(
|
|
1312
|
-
|
|
1420
|
+
wr,
|
|
1313
1421
|
"end_user_token_required",
|
|
1314
1422
|
n.status,
|
|
1315
|
-
n.requestId
|
|
1423
|
+
n.requestId,
|
|
1424
|
+
n.correlationId
|
|
1316
1425
|
);
|
|
1317
1426
|
if (n.code === "invalid_end_user_token")
|
|
1318
1427
|
throw new V(
|
|
1319
|
-
|
|
1428
|
+
kr,
|
|
1320
1429
|
"invalid_end_user_token",
|
|
1321
1430
|
n.status,
|
|
1322
|
-
n.requestId
|
|
1431
|
+
n.requestId,
|
|
1432
|
+
n.correlationId
|
|
1323
1433
|
);
|
|
1324
1434
|
if (n.code === "end_user_token_product_mismatch")
|
|
1325
|
-
throw new
|
|
1435
|
+
throw new l(
|
|
1326
1436
|
"forbidden",
|
|
1327
|
-
|
|
1437
|
+
Sr,
|
|
1328
1438
|
n.status,
|
|
1329
1439
|
n.requestId,
|
|
1330
|
-
"end_user_token_product_mismatch"
|
|
1440
|
+
"end_user_token_product_mismatch",
|
|
1441
|
+
n.correlationId
|
|
1331
1442
|
);
|
|
1332
1443
|
}
|
|
1333
1444
|
throw n;
|
|
1334
1445
|
}
|
|
1335
|
-
function
|
|
1336
|
-
return n instanceof
|
|
1446
|
+
function Tr(n) {
|
|
1447
|
+
return n instanceof l && n.code === "invalid_end_user_token";
|
|
1337
1448
|
}
|
|
1338
|
-
function
|
|
1339
|
-
function t() {
|
|
1449
|
+
function Wt(n, e = {}) {
|
|
1450
|
+
async function t() {
|
|
1340
1451
|
try {
|
|
1341
|
-
let a = e.getEndUserToken?.();
|
|
1452
|
+
let a = await e.getEndUserToken?.();
|
|
1342
1453
|
return typeof a == "string" && a.length > 0 ? a : null;
|
|
1343
1454
|
} catch {
|
|
1344
1455
|
return null;
|
|
@@ -1353,28 +1464,28 @@ function Et(n, e = {}) {
|
|
|
1353
1464
|
}
|
|
1354
1465
|
}
|
|
1355
1466
|
function o(a) {
|
|
1356
|
-
return a ? { [
|
|
1467
|
+
return a ? { [br]: a } : {};
|
|
1357
1468
|
}
|
|
1358
|
-
async function i(a,
|
|
1359
|
-
let d = t();
|
|
1360
|
-
if (!d && a && await r() && (d = t()), !d && a)
|
|
1361
|
-
throw new V(
|
|
1469
|
+
async function i(a, c) {
|
|
1470
|
+
let d = await t();
|
|
1471
|
+
if (!d && a && await r() && (d = await t()), !d && a)
|
|
1472
|
+
throw new V(vr, "no_signed_in_user");
|
|
1362
1473
|
try {
|
|
1363
|
-
return await
|
|
1474
|
+
return await c(o(d));
|
|
1364
1475
|
} catch (u) {
|
|
1365
|
-
if (d &&
|
|
1366
|
-
let p = t();
|
|
1476
|
+
if (d && Tr(u) && await r()) {
|
|
1477
|
+
let p = await t();
|
|
1367
1478
|
if (p)
|
|
1368
1479
|
try {
|
|
1369
|
-
return await
|
|
1480
|
+
return await c(o(p));
|
|
1370
1481
|
} catch (f) {
|
|
1371
|
-
|
|
1482
|
+
$t(f);
|
|
1372
1483
|
}
|
|
1373
1484
|
}
|
|
1374
|
-
|
|
1485
|
+
$t(u);
|
|
1375
1486
|
}
|
|
1376
1487
|
}
|
|
1377
|
-
async function s(a,
|
|
1488
|
+
async function s(a, c = {}) {
|
|
1378
1489
|
await i(
|
|
1379
1490
|
true,
|
|
1380
1491
|
(d) => v(n, {
|
|
@@ -1382,7 +1493,7 @@ function Et(n, e = {}) {
|
|
|
1382
1493
|
path: a,
|
|
1383
1494
|
requireAuth: !0,
|
|
1384
1495
|
headers: d,
|
|
1385
|
-
...
|
|
1496
|
+
...c
|
|
1386
1497
|
})
|
|
1387
1498
|
);
|
|
1388
1499
|
}
|
|
@@ -1395,22 +1506,22 @@ function Et(n, e = {}) {
|
|
|
1395
1506
|
*/
|
|
1396
1507
|
async createTicket(a) {
|
|
1397
1508
|
if (!a || typeof a != "object")
|
|
1398
|
-
throw new
|
|
1509
|
+
throw new l("validation_failed", "input is required");
|
|
1399
1510
|
if (!a.subject || typeof a.subject != "string")
|
|
1400
|
-
throw new
|
|
1511
|
+
throw new l("validation_failed", "subject is required");
|
|
1401
1512
|
if (a.subject.length > 200)
|
|
1402
|
-
throw new
|
|
1513
|
+
throw new l("validation_failed", "subject max 200 chars");
|
|
1403
1514
|
if (!a.message || typeof a.message != "string")
|
|
1404
|
-
throw new
|
|
1515
|
+
throw new l("validation_failed", "message is required");
|
|
1405
1516
|
if (a.message.length > 1e4)
|
|
1406
|
-
throw new
|
|
1407
|
-
if (a.severity && !
|
|
1408
|
-
throw new
|
|
1517
|
+
throw new l("validation_failed", "message max 10000 chars");
|
|
1518
|
+
if (a.severity && !je.includes(a.severity))
|
|
1519
|
+
throw new l(
|
|
1409
1520
|
"validation_failed",
|
|
1410
|
-
`severity must be one of ${
|
|
1521
|
+
`severity must be one of ${je.join(", ")}`
|
|
1411
1522
|
);
|
|
1412
1523
|
let d = {
|
|
1413
|
-
productId:
|
|
1524
|
+
productId: jt(n, a.productSlug),
|
|
1414
1525
|
subject: a.subject,
|
|
1415
1526
|
message: a.message,
|
|
1416
1527
|
severity: a.severity ?? "normal"
|
|
@@ -1427,7 +1538,7 @@ function Et(n, e = {}) {
|
|
|
1427
1538
|
headers: f
|
|
1428
1539
|
})
|
|
1429
1540
|
), p = u && typeof u == "object" && "ticket" in u ? u.ticket : u;
|
|
1430
|
-
return
|
|
1541
|
+
return qt(p);
|
|
1431
1542
|
},
|
|
1432
1543
|
/**
|
|
1433
1544
|
* Lista tickets do customer no produto atual. `productSlug` no contexto.
|
|
@@ -1435,23 +1546,23 @@ function Et(n, e = {}) {
|
|
|
1435
1546
|
* Endpoint canonical: `GET /api/sdk/v1/support/tickets?productId=...`.
|
|
1436
1547
|
*/
|
|
1437
1548
|
async listMyTickets(a) {
|
|
1438
|
-
let
|
|
1439
|
-
if (d !== void 0 && (!Number.isInteger(d) || d < 1 || d >
|
|
1440
|
-
throw new
|
|
1549
|
+
let c = jt(n, a?.productSlug), d = a?.limit;
|
|
1550
|
+
if (d !== void 0 && (!Number.isInteger(d) || d < 1 || d > Mt))
|
|
1551
|
+
throw new l(
|
|
1441
1552
|
"validation_failed",
|
|
1442
|
-
`limit must be an integer 1-${
|
|
1553
|
+
`limit must be an integer 1-${Mt} (Core default 20)`
|
|
1443
1554
|
);
|
|
1444
1555
|
let u = await i(
|
|
1445
1556
|
true,
|
|
1446
1557
|
(f) => v(n, {
|
|
1447
1558
|
method: "GET",
|
|
1448
1559
|
path: "/api/sdk/v1/support/tickets",
|
|
1449
|
-
query: { productId:
|
|
1560
|
+
query: { productId: c, limit: d },
|
|
1450
1561
|
requireAuth: !0,
|
|
1451
1562
|
headers: f
|
|
1452
1563
|
})
|
|
1453
1564
|
);
|
|
1454
|
-
return (Array.isArray(u) ? u : u && typeof u == "object" && "tickets" in u ? u.tickets ?? [] : []).map(
|
|
1565
|
+
return (Array.isArray(u) ? u : u && typeof u == "object" && "tickets" in u ? u.tickets ?? [] : []).map(qt);
|
|
1455
1566
|
},
|
|
1456
1567
|
/**
|
|
1457
1568
|
* 3.4: marca a notificação de resolução como vista (`resolvedNotifiedAt`,
|
|
@@ -1459,7 +1570,7 @@ function Et(n, e = {}) {
|
|
|
1459
1570
|
* `POST /api/sdk/v1/support/tickets/{id}/resolved-seen`.
|
|
1460
1571
|
*/
|
|
1461
1572
|
async markResolvedSeen(a) {
|
|
1462
|
-
|
|
1573
|
+
de(a), await s(
|
|
1463
1574
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/resolved-seen`
|
|
1464
1575
|
);
|
|
1465
1576
|
},
|
|
@@ -1470,7 +1581,7 @@ function Et(n, e = {}) {
|
|
|
1470
1581
|
* `POST /api/sdk/v1/support/tickets/{id}/satisfaction-attempt`.
|
|
1471
1582
|
*/
|
|
1472
1583
|
async recordSatisfactionAttempt(a) {
|
|
1473
|
-
|
|
1584
|
+
de(a), await s(
|
|
1474
1585
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/satisfaction-attempt`,
|
|
1475
1586
|
// Incrementa satisfactionAttempts += 1 no Core — sem idempotencyKey,
|
|
1476
1587
|
// um retry de rede infla o contador (mesmo padrão de createTicket acima).
|
|
@@ -1483,10 +1594,10 @@ function Et(n, e = {}) {
|
|
|
1483
1594
|
*/
|
|
1484
1595
|
async submitSatisfaction(a) {
|
|
1485
1596
|
if (!a || typeof a != "object")
|
|
1486
|
-
throw new
|
|
1487
|
-
if (
|
|
1488
|
-
throw new
|
|
1489
|
-
|
|
1597
|
+
throw new l("validation_failed", "input is required");
|
|
1598
|
+
if (de(a.ticketId), a.rating !== null && a.rating !== void 0 && (!Number.isInteger(a.rating) || a.rating < 1 || a.rating > 5))
|
|
1599
|
+
throw new l("validation_failed", "rating must be an integer 1-5 or null");
|
|
1600
|
+
Ft(a.comment), await s(
|
|
1490
1601
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a.ticketId)}/satisfaction`,
|
|
1491
1602
|
{
|
|
1492
1603
|
body: {
|
|
@@ -1502,24 +1613,24 @@ function Et(n, e = {}) {
|
|
|
1502
1613
|
* satisfação/notificação. Endpoint canonical:
|
|
1503
1614
|
* `POST /api/sdk/v1/support/tickets/{id}/contest`.
|
|
1504
1615
|
*/
|
|
1505
|
-
async contestTicket(a,
|
|
1506
|
-
|
|
1616
|
+
async contestTicket(a, c) {
|
|
1617
|
+
de(a), Ft(c?.comment), await s(
|
|
1507
1618
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/contest`,
|
|
1508
|
-
{ body: { comment:
|
|
1619
|
+
{ body: { comment: c?.comment ?? void 0 } }
|
|
1509
1620
|
);
|
|
1510
1621
|
}
|
|
1511
1622
|
};
|
|
1512
1623
|
}
|
|
1513
1624
|
|
|
1514
1625
|
// src/db/client-db.ts
|
|
1515
|
-
|
|
1626
|
+
Z();
|
|
1516
1627
|
|
|
1517
1628
|
// src/db/offline/query-engine.ts
|
|
1518
|
-
function
|
|
1629
|
+
function Bt(n) {
|
|
1519
1630
|
return n == null ? 0 : typeof n == "number" ? 1 : typeof n == "string" ? 2 : typeof n == "boolean" ? 3 : 4;
|
|
1520
1631
|
}
|
|
1521
|
-
function
|
|
1522
|
-
let t =
|
|
1632
|
+
function ee(n, e) {
|
|
1633
|
+
let t = Bt(n), r = Bt(e);
|
|
1523
1634
|
if (t !== r) return t - r;
|
|
1524
1635
|
if (n == null) return 0;
|
|
1525
1636
|
if (typeof n == "number" && typeof e == "number") return n - e;
|
|
@@ -1528,7 +1639,7 @@ function Z(n, e) {
|
|
|
1528
1639
|
let o = String(n), i = String(e);
|
|
1529
1640
|
return o < i ? -1 : o > i ? 1 : 0;
|
|
1530
1641
|
}
|
|
1531
|
-
function
|
|
1642
|
+
function $e(n, e) {
|
|
1532
1643
|
let t = e.split("."), r = n;
|
|
1533
1644
|
for (let o of t) {
|
|
1534
1645
|
if (r == null || typeof r != "object")
|
|
@@ -1537,8 +1648,8 @@ function Le(n, e) {
|
|
|
1537
1648
|
}
|
|
1538
1649
|
return r;
|
|
1539
1650
|
}
|
|
1540
|
-
function
|
|
1541
|
-
let t =
|
|
1651
|
+
function Ir(n, e) {
|
|
1652
|
+
let t = $e(n, e.field);
|
|
1542
1653
|
if (t === void 0) return false;
|
|
1543
1654
|
let { op: r, value: o } = e;
|
|
1544
1655
|
switch (r) {
|
|
@@ -1547,13 +1658,13 @@ function sr(n, e) {
|
|
|
1547
1658
|
case "!=":
|
|
1548
1659
|
return t !== o;
|
|
1549
1660
|
case "<":
|
|
1550
|
-
return
|
|
1661
|
+
return ee(t, o) < 0;
|
|
1551
1662
|
case "<=":
|
|
1552
|
-
return
|
|
1663
|
+
return ee(t, o) <= 0;
|
|
1553
1664
|
case ">":
|
|
1554
|
-
return
|
|
1665
|
+
return ee(t, o) > 0;
|
|
1555
1666
|
case ">=":
|
|
1556
|
-
return
|
|
1667
|
+
return ee(t, o) >= 0;
|
|
1557
1668
|
case "array-contains":
|
|
1558
1669
|
return Array.isArray(t) && t.includes(o);
|
|
1559
1670
|
case "in":
|
|
@@ -1568,24 +1679,24 @@ function sr(n, e) {
|
|
|
1568
1679
|
);
|
|
1569
1680
|
}
|
|
1570
1681
|
}
|
|
1571
|
-
function
|
|
1572
|
-
return e.every((t) =>
|
|
1682
|
+
function Cr(n, e) {
|
|
1683
|
+
return e.every((t) => Ir(n, t));
|
|
1573
1684
|
}
|
|
1574
|
-
function
|
|
1685
|
+
function Ar(n) {
|
|
1575
1686
|
return (e, t) => {
|
|
1576
1687
|
if (n) {
|
|
1577
|
-
let r =
|
|
1688
|
+
let r = $e(e.data, n.field), o = $e(t.data, n.field), i = ee(r, o);
|
|
1578
1689
|
if (i !== 0)
|
|
1579
1690
|
return n.direction === "desc" ? -i : i;
|
|
1580
1691
|
}
|
|
1581
1692
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
1582
1693
|
};
|
|
1583
1694
|
}
|
|
1584
|
-
function
|
|
1695
|
+
function Er(n, e) {
|
|
1585
1696
|
let t = n.findIndex((r) => r.id === e.docId);
|
|
1586
1697
|
return t === -1 ? n : e.type === "startAfter" ? n.slice(t + 1) : n.slice(0, t);
|
|
1587
1698
|
}
|
|
1588
|
-
var
|
|
1699
|
+
var z = class n {
|
|
1589
1700
|
/**
|
|
1590
1701
|
* Avalia um `QueryDescriptor` contra um array de `OfflineDoc`.
|
|
1591
1702
|
*
|
|
@@ -1599,11 +1710,11 @@ var K = class n {
|
|
|
1599
1710
|
evaluate(e, t) {
|
|
1600
1711
|
let r = e.filter((u) => !u.meta.deleted), o = t.where ?? [];
|
|
1601
1712
|
o.length > 0 && (r = r.filter(
|
|
1602
|
-
(u) =>
|
|
1713
|
+
(u) => Cr(u.data, o)
|
|
1603
1714
|
));
|
|
1604
|
-
let i =
|
|
1715
|
+
let i = Ar(t.orderBy), s = [...r].sort(i), a = t.cursor ? Er(s, t.cursor) : s, c = Math.min(t.limit ?? 20, 500);
|
|
1605
1716
|
return {
|
|
1606
|
-
docs: a.slice(0,
|
|
1717
|
+
docs: a.slice(0, c).map((u) => ({ id: u.id, data: u.data })),
|
|
1607
1718
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
1608
1719
|
// tem todos os docs da coleção. É responsabilidade do chamador (LocalStore)
|
|
1609
1720
|
// injetar o flag de completude baseado nos metadados do query_cache.
|
|
@@ -1621,7 +1732,7 @@ var K = class n {
|
|
|
1621
1732
|
};
|
|
1622
1733
|
|
|
1623
1734
|
// src/db/offline/local-store.ts
|
|
1624
|
-
var
|
|
1735
|
+
var Dr = 1, W = "documents", B = "mutations", te = "query_cache", ue = "sync_meta", H = "conflict_log", pe = class {
|
|
1625
1736
|
db = null;
|
|
1626
1737
|
dbName;
|
|
1627
1738
|
constructor(e) {
|
|
@@ -1633,21 +1744,21 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1633
1744
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
1634
1745
|
*/
|
|
1635
1746
|
async open() {
|
|
1636
|
-
this.db === null && (this.db = await openDB(this.dbName,
|
|
1747
|
+
this.db === null && (this.db = await openDB(this.dbName, Dr, {
|
|
1637
1748
|
upgrade(e) {
|
|
1638
|
-
if (!e.objectStoreNames.contains(
|
|
1639
|
-
let t = e.createObjectStore(
|
|
1749
|
+
if (!e.objectStoreNames.contains(W)) {
|
|
1750
|
+
let t = e.createObjectStore(W, {
|
|
1640
1751
|
keyPath: ["collection", "id"]
|
|
1641
1752
|
});
|
|
1642
1753
|
t.createIndex("by_collection", "collection"), t.createIndex("by_collection_state", ["collection", "meta.state"]), t.createIndex("by_updatedAtServer", "meta.updatedAtServer");
|
|
1643
1754
|
}
|
|
1644
|
-
if (!e.objectStoreNames.contains(
|
|
1645
|
-
let t = e.createObjectStore(
|
|
1755
|
+
if (!e.objectStoreNames.contains(B)) {
|
|
1756
|
+
let t = e.createObjectStore(B, {
|
|
1646
1757
|
keyPath: "mutationId"
|
|
1647
1758
|
});
|
|
1648
1759
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
1649
1760
|
}
|
|
1650
|
-
if (e.objectStoreNames.contains(
|
|
1761
|
+
if (e.objectStoreNames.contains(te) || e.createObjectStore(te, { keyPath: "queryHash" }), e.objectStoreNames.contains(ue) || e.createObjectStore(ue, { keyPath: "key" }), !e.objectStoreNames.contains(H)) {
|
|
1651
1762
|
let t = e.createObjectStore(H, {
|
|
1652
1763
|
keyPath: "id",
|
|
1653
1764
|
autoIncrement: true
|
|
@@ -1690,14 +1801,14 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1690
1801
|
* Retorna tombstones (deleted: true) — o chamador decide se deve mostrá-los.
|
|
1691
1802
|
*/
|
|
1692
1803
|
async getDoc(e, t) {
|
|
1693
|
-
return await this.assertOpen().get(
|
|
1804
|
+
return await this.assertOpen().get(W, [e, t]) ?? null;
|
|
1694
1805
|
}
|
|
1695
1806
|
/**
|
|
1696
1807
|
* Insere ou atualiza um documento na store `documents`.
|
|
1697
1808
|
* O documento é identificado pelo keyPath composto `[collection, id]`.
|
|
1698
1809
|
*/
|
|
1699
1810
|
async putDoc(e) {
|
|
1700
|
-
await this.assertOpen().put(
|
|
1811
|
+
await this.assertOpen().put(W, e);
|
|
1701
1812
|
}
|
|
1702
1813
|
/**
|
|
1703
1814
|
* Marca um documento como tombstone (`deleted: true`).
|
|
@@ -1705,8 +1816,8 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1705
1816
|
* No-op se o documento não existir.
|
|
1706
1817
|
*/
|
|
1707
1818
|
async deleteDoc(e, t) {
|
|
1708
|
-
let r = this.assertOpen(), o = await r.get(
|
|
1709
|
-
o && await r.put(
|
|
1819
|
+
let r = this.assertOpen(), o = await r.get(W, [e, t]);
|
|
1820
|
+
o && await r.put(W, {
|
|
1710
1821
|
...o,
|
|
1711
1822
|
meta: {
|
|
1712
1823
|
...o.meta,
|
|
@@ -1722,8 +1833,8 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1722
1833
|
* e delega a filtragem/ordenação/paginação ao QueryEngine (que opera em memória).
|
|
1723
1834
|
*/
|
|
1724
1835
|
async listDocs(e, t) {
|
|
1725
|
-
let o = await this.assertOpen().getAllFromIndex(
|
|
1726
|
-
return
|
|
1836
|
+
let o = await this.assertOpen().getAllFromIndex(W, "by_collection", e);
|
|
1837
|
+
return z.evaluate(o, t);
|
|
1727
1838
|
}
|
|
1728
1839
|
// ─── Sync meta (key-value) ──────────────────────────────────────────────────
|
|
1729
1840
|
/**
|
|
@@ -1734,14 +1845,14 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1734
1845
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1735
1846
|
*/
|
|
1736
1847
|
async getMeta(e) {
|
|
1737
|
-
let r = await this.assertOpen().get(
|
|
1848
|
+
let r = await this.assertOpen().get(ue, e);
|
|
1738
1849
|
return r ? r.value : null;
|
|
1739
1850
|
}
|
|
1740
1851
|
/**
|
|
1741
1852
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1742
1853
|
*/
|
|
1743
1854
|
async setMeta(e, t) {
|
|
1744
|
-
await this.assertOpen().put(
|
|
1855
|
+
await this.assertOpen().put(ue, { key: e, value: t });
|
|
1745
1856
|
}
|
|
1746
1857
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1747
1858
|
/**
|
|
@@ -1773,13 +1884,13 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1773
1884
|
* Key: `mutationId`.
|
|
1774
1885
|
*/
|
|
1775
1886
|
async putMutation(e) {
|
|
1776
|
-
await this.assertOpen().put(
|
|
1887
|
+
await this.assertOpen().put(B, e);
|
|
1777
1888
|
}
|
|
1778
1889
|
/**
|
|
1779
1890
|
* Retorna uma mutação pelo `mutationId`, ou `null` se não existir.
|
|
1780
1891
|
*/
|
|
1781
1892
|
async getMutation(e) {
|
|
1782
|
-
return await this.assertOpen().get(
|
|
1893
|
+
return await this.assertOpen().get(B, e) ?? null;
|
|
1783
1894
|
}
|
|
1784
1895
|
/**
|
|
1785
1896
|
* Lista mutações com filtros opcionais.
|
|
@@ -1787,14 +1898,14 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1787
1898
|
*/
|
|
1788
1899
|
async listMutations(e) {
|
|
1789
1900
|
let t = this.assertOpen(), r;
|
|
1790
|
-
return e?.status !== void 0 ? r = await t.getAllFromIndex(
|
|
1901
|
+
return e?.status !== void 0 ? r = await t.getAllFromIndex(B, "by_status", e.status) : e?.collection !== void 0 && e.docId !== void 0 ? r = await t.getAllFromIndex(B, "by_doc", [e.collection, e.docId]) : r = await t.getAllFromIndex(B, "by_seq"), r.sort((o, i) => o.seq - i.seq);
|
|
1791
1902
|
}
|
|
1792
1903
|
/**
|
|
1793
1904
|
* Remove uma mutação pelo `mutationId`.
|
|
1794
1905
|
* No-op se não existir.
|
|
1795
1906
|
*/
|
|
1796
1907
|
async deleteMutation(e) {
|
|
1797
|
-
await this.assertOpen().delete(
|
|
1908
|
+
await this.assertOpen().delete(B, e);
|
|
1798
1909
|
}
|
|
1799
1910
|
// ─── Collection discovery ────────────────────────────────────────────────────
|
|
1800
1911
|
/**
|
|
@@ -1807,7 +1918,7 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1807
1918
|
* coletar os valores únicos de forma eficiente (sem carregar os docs completos).
|
|
1808
1919
|
*/
|
|
1809
1920
|
async listCollections() {
|
|
1810
|
-
let t = await this.assertOpen().getAllFromIndex(
|
|
1921
|
+
let t = await this.assertOpen().getAllFromIndex(W, "by_collection"), r = /* @__PURE__ */ new Set();
|
|
1811
1922
|
for (let o of t)
|
|
1812
1923
|
r.add(o.collection);
|
|
1813
1924
|
return Array.from(r);
|
|
@@ -1817,24 +1928,24 @@ var ur = 1, j = "documents", W = "mutations", ee = "query_cache", ce = "sync_met
|
|
|
1817
1928
|
* Retorna a entrada de `query_cache` para um hash de query, ou `null`.
|
|
1818
1929
|
*/
|
|
1819
1930
|
async getQueryCache(e) {
|
|
1820
|
-
return await this.assertOpen().get(
|
|
1931
|
+
return await this.assertOpen().get(te, e) ?? null;
|
|
1821
1932
|
}
|
|
1822
1933
|
/**
|
|
1823
1934
|
* Insere ou atualiza uma entrada de `query_cache`.
|
|
1824
1935
|
*/
|
|
1825
1936
|
async putQueryCache(e) {
|
|
1826
|
-
await this.assertOpen().put(
|
|
1937
|
+
await this.assertOpen().put(te, e);
|
|
1827
1938
|
}
|
|
1828
1939
|
/**
|
|
1829
1940
|
* Remove uma entrada de `query_cache` pelo queryHash.
|
|
1830
1941
|
*/
|
|
1831
1942
|
async deleteQueryCache(e) {
|
|
1832
|
-
await this.assertOpen().delete(
|
|
1943
|
+
await this.assertOpen().delete(te, e);
|
|
1833
1944
|
}
|
|
1834
1945
|
};
|
|
1835
1946
|
|
|
1836
1947
|
// src/db/offline/memory-store.ts
|
|
1837
|
-
var
|
|
1948
|
+
var fe = class {
|
|
1838
1949
|
// store `documents` — key: `${collection}\0${id}`
|
|
1839
1950
|
_docs = /* @__PURE__ */ new Map();
|
|
1840
1951
|
// store `mutations` — key: mutationId
|
|
@@ -1882,7 +1993,7 @@ var de = class {
|
|
|
1882
1993
|
let r = Array.from(this._docs.values()).filter(
|
|
1883
1994
|
(o) => o.collection === e
|
|
1884
1995
|
);
|
|
1885
|
-
return
|
|
1996
|
+
return z.evaluate(r, t);
|
|
1886
1997
|
}
|
|
1887
1998
|
// ─── Sync meta ──────────────────────────────────────────────────────────────
|
|
1888
1999
|
async getMeta(e) {
|
|
@@ -1939,13 +2050,13 @@ var de = class {
|
|
|
1939
2050
|
};
|
|
1940
2051
|
|
|
1941
2052
|
// src/db/offline/mutation-queue.ts
|
|
1942
|
-
function
|
|
2053
|
+
function We() {
|
|
1943
2054
|
return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (n) => {
|
|
1944
2055
|
let e = Math.random() * 16 | 0;
|
|
1945
2056
|
return (n === "x" ? e : e & 3 | 8).toString(16);
|
|
1946
2057
|
});
|
|
1947
2058
|
}
|
|
1948
|
-
var
|
|
2059
|
+
var me = class {
|
|
1949
2060
|
store;
|
|
1950
2061
|
/**
|
|
1951
2062
|
* Ponteiro de sequência local.
|
|
@@ -2041,18 +2152,18 @@ var ue = class {
|
|
|
2041
2152
|
* Atômico: a persistência final é uma única operação putMutation.
|
|
2042
2153
|
*/
|
|
2043
2154
|
async enqueue(e) {
|
|
2044
|
-
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e,
|
|
2155
|
+
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e, c = e.docId ?? (r === "add" ? We() : "");
|
|
2045
2156
|
await this.syncSeq();
|
|
2046
|
-
let u = (await this.store.listMutations({ collection: t, docId:
|
|
2157
|
+
let u = (await this.store.listMutations({ collection: t, docId: c })).filter((_) => _.status === "queued").sort((_, b) => b.seq - _.seq)[0];
|
|
2047
2158
|
if (u !== void 0) {
|
|
2048
|
-
let
|
|
2049
|
-
if (
|
|
2050
|
-
if ("removed" in
|
|
2159
|
+
let _ = this.tryCoalesce(u, r, o, s, a);
|
|
2160
|
+
if (_ !== null) {
|
|
2161
|
+
if ("removed" in _)
|
|
2051
2162
|
return await this.store.deleteMutation(u.mutationId), {
|
|
2052
2163
|
seq: u.seq,
|
|
2053
|
-
mutationId:
|
|
2164
|
+
mutationId: We(),
|
|
2054
2165
|
collection: t,
|
|
2055
|
-
docId:
|
|
2166
|
+
docId: c,
|
|
2056
2167
|
op: "remove",
|
|
2057
2168
|
payload: null,
|
|
2058
2169
|
baseVersion: i,
|
|
@@ -2062,15 +2173,15 @@ var ue = class {
|
|
|
2062
2173
|
status: "queued",
|
|
2063
2174
|
batchId: s
|
|
2064
2175
|
};
|
|
2065
|
-
let { coalesced:
|
|
2066
|
-
return await this.store.putMutation(
|
|
2176
|
+
let { coalesced: b } = _;
|
|
2177
|
+
return await this.store.putMutation(b), b;
|
|
2067
2178
|
}
|
|
2068
2179
|
}
|
|
2069
2180
|
let f = {
|
|
2070
2181
|
seq: this.nextSeq(),
|
|
2071
|
-
mutationId:
|
|
2182
|
+
mutationId: We(),
|
|
2072
2183
|
collection: t,
|
|
2073
|
-
docId:
|
|
2184
|
+
docId: c,
|
|
2074
2185
|
op: r,
|
|
2075
2186
|
payload: o,
|
|
2076
2187
|
baseVersion: i,
|
|
@@ -2181,45 +2292,45 @@ var ue = class {
|
|
|
2181
2292
|
};
|
|
2182
2293
|
|
|
2183
2294
|
// src/db/field-value.ts
|
|
2184
|
-
var
|
|
2295
|
+
var Vt = "__neetru__";
|
|
2185
2296
|
function Q(n) {
|
|
2186
|
-
return typeof n == "object" && n !== null &&
|
|
2297
|
+
return typeof n == "object" && n !== null && Vt in n && typeof n[Vt] == "string";
|
|
2187
2298
|
}
|
|
2188
|
-
function
|
|
2299
|
+
function Nr(n) {
|
|
2189
2300
|
return Q(n) && n.__neetru__ === "serverTimestamp";
|
|
2190
2301
|
}
|
|
2191
|
-
function
|
|
2302
|
+
function xr(n) {
|
|
2192
2303
|
return Q(n) && n.__neetru__ === "increment" && typeof n.operand == "number" && Number.isFinite(n.operand);
|
|
2193
2304
|
}
|
|
2194
|
-
function
|
|
2195
|
-
return
|
|
2305
|
+
function Or(n, e) {
|
|
2306
|
+
return Nr(n) ? Date.now() : xr(n) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + n.operand : n;
|
|
2196
2307
|
}
|
|
2197
|
-
function
|
|
2308
|
+
function K(n, e) {
|
|
2198
2309
|
let t = false, r = {};
|
|
2199
2310
|
for (let [o, i] of Object.entries(n))
|
|
2200
|
-
Q(i) ? (t = true, r[o] =
|
|
2311
|
+
Q(i) ? (t = true, r[o] = Or(i, e?.[o])) : r[o] = i;
|
|
2201
2312
|
return t ? r : n;
|
|
2202
2313
|
}
|
|
2203
|
-
function
|
|
2314
|
+
function Be(n) {
|
|
2204
2315
|
for (let e of Object.values(n))
|
|
2205
2316
|
if (Q(e)) return true;
|
|
2206
2317
|
return false;
|
|
2207
2318
|
}
|
|
2208
2319
|
|
|
2209
2320
|
// src/db/offline/sync-engine.ts
|
|
2210
|
-
var
|
|
2211
|
-
function
|
|
2212
|
-
return n !== null &&
|
|
2321
|
+
var Lr = 8, Ur = 1e3, Mr = 300 * 1e3, Ve = "truncatedCollections";
|
|
2322
|
+
function qr(n) {
|
|
2323
|
+
return n !== null && Be(n);
|
|
2213
2324
|
}
|
|
2214
|
-
function
|
|
2325
|
+
function Fr(n) {
|
|
2215
2326
|
if (n === null) return null;
|
|
2216
|
-
if (!
|
|
2327
|
+
if (!Be(n)) return n;
|
|
2217
2328
|
let e = {};
|
|
2218
2329
|
for (let [t, r] of Object.entries(n))
|
|
2219
2330
|
Q(r) || (e[t] = r);
|
|
2220
2331
|
return e;
|
|
2221
2332
|
}
|
|
2222
|
-
var
|
|
2333
|
+
var he = class {
|
|
2223
2334
|
_store;
|
|
2224
2335
|
_queue;
|
|
2225
2336
|
_resolver;
|
|
@@ -2363,16 +2474,16 @@ var pe = class {
|
|
|
2363
2474
|
try {
|
|
2364
2475
|
t = await this._transport.pushMutations(e);
|
|
2365
2476
|
} catch (s) {
|
|
2366
|
-
let a = s instanceof Error ? s.message : String(s),
|
|
2367
|
-
await this._retryOrDeadLetter(d, a,
|
|
2477
|
+
let a = s instanceof Error ? s.message : String(s), c = [], [d, ...u] = e;
|
|
2478
|
+
await this._retryOrDeadLetter(d, a, c);
|
|
2368
2479
|
for (let p of u)
|
|
2369
2480
|
await this._queue.requeueInflight(p.mutationId);
|
|
2370
|
-
return
|
|
2481
|
+
return c.length > 0 && this._bus.emit(c), this._refreshPendingWrites().catch(() => {
|
|
2371
2482
|
}), false;
|
|
2372
2483
|
}
|
|
2373
2484
|
let r = [];
|
|
2374
2485
|
for (let s of t.results) {
|
|
2375
|
-
let a = e.find((
|
|
2486
|
+
let a = e.find((c) => c.mutationId === s.mutationId);
|
|
2376
2487
|
a && (s.outcome === "confirmed" ? (await this._queue.markApplied(a.mutationId), await this._applySyncedDoc(
|
|
2377
2488
|
a.collection,
|
|
2378
2489
|
a.docId,
|
|
@@ -2390,8 +2501,8 @@ var pe = class {
|
|
|
2390
2501
|
"partial_push: muta\xE7\xE3o n\xE3o processada no lote",
|
|
2391
2502
|
r
|
|
2392
2503
|
);
|
|
2393
|
-
for (let
|
|
2394
|
-
await this._queue.requeueInflight(
|
|
2504
|
+
for (let c of a)
|
|
2505
|
+
await this._queue.requeueInflight(c.mutationId);
|
|
2395
2506
|
}
|
|
2396
2507
|
return r.length > 0 && this._bus.emit(r), i.length === 0;
|
|
2397
2508
|
}
|
|
@@ -2413,13 +2524,13 @@ var pe = class {
|
|
|
2413
2524
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
2414
2525
|
return;
|
|
2415
2526
|
}
|
|
2416
|
-
let d =
|
|
2527
|
+
let d = Fr(i.payload) ?? a?.data ?? {}, u = {
|
|
2417
2528
|
collection: e,
|
|
2418
2529
|
id: t,
|
|
2419
2530
|
data: i.op === "update" ? { ...a?.data ?? {}, ...d } : (
|
|
2420
2531
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
2421
2532
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
2422
|
-
|
|
2533
|
+
qr(i.payload) ? { ...a?.data ?? {}, ...d } : d
|
|
2423
2534
|
),
|
|
2424
2535
|
meta: {
|
|
2425
2536
|
serverVersion: r,
|
|
@@ -2466,7 +2577,7 @@ var pe = class {
|
|
|
2466
2577
|
deleted: false,
|
|
2467
2578
|
ownerId: null
|
|
2468
2579
|
}
|
|
2469
|
-
},
|
|
2580
|
+
}, c = this._resolver.resolve(
|
|
2470
2581
|
s,
|
|
2471
2582
|
i,
|
|
2472
2583
|
e
|
|
@@ -2480,7 +2591,7 @@ var pe = class {
|
|
|
2480
2591
|
detectedAt: Date.now(),
|
|
2481
2592
|
delivered: false
|
|
2482
2593
|
};
|
|
2483
|
-
await this._store.appendConflict(
|
|
2594
|
+
await this._store.appendConflict(c);
|
|
2484
2595
|
let d = {
|
|
2485
2596
|
collection: e.collection,
|
|
2486
2597
|
id: e.docId,
|
|
@@ -2523,7 +2634,7 @@ var pe = class {
|
|
|
2523
2634
|
...i.meta,
|
|
2524
2635
|
serverVersion: t.serverVersion ?? i.meta.serverVersion
|
|
2525
2636
|
}
|
|
2526
|
-
} : null,
|
|
2637
|
+
} : null, c = this._resolver.resolveRejected(
|
|
2527
2638
|
i,
|
|
2528
2639
|
s,
|
|
2529
2640
|
e,
|
|
@@ -2538,7 +2649,7 @@ var pe = class {
|
|
|
2538
2649
|
detectedAt: Date.now(),
|
|
2539
2650
|
delivered: false
|
|
2540
2651
|
};
|
|
2541
|
-
await this._store.appendConflict(
|
|
2652
|
+
await this._store.appendConflict(c), t.serverData ? (await this._store.putDoc({
|
|
2542
2653
|
...i,
|
|
2543
2654
|
data: t.serverData,
|
|
2544
2655
|
meta: {
|
|
@@ -2572,7 +2683,7 @@ var pe = class {
|
|
|
2572
2683
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
2573
2684
|
*/
|
|
2574
2685
|
async _retryOrDeadLetter(e, t, r) {
|
|
2575
|
-
if (e.attempts + 1 <
|
|
2686
|
+
if (e.attempts + 1 < Lr) {
|
|
2576
2687
|
await this._queue.markRetry(e.mutationId, t);
|
|
2577
2688
|
return;
|
|
2578
2689
|
}
|
|
@@ -2613,7 +2724,7 @@ var pe = class {
|
|
|
2613
2724
|
}
|
|
2614
2725
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
2615
2726
|
_backoffDelayMs(e) {
|
|
2616
|
-
return Math.min(
|
|
2727
|
+
return Math.min(Ur * 2 ** (e - 1), Mr);
|
|
2617
2728
|
}
|
|
2618
2729
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
2619
2730
|
/**
|
|
@@ -2646,14 +2757,14 @@ var pe = class {
|
|
|
2646
2757
|
let r = [];
|
|
2647
2758
|
await this._applyServerDocs(t.docs, r, true);
|
|
2648
2759
|
let o = t.truncatedCollections ?? [], i = new Set(o);
|
|
2649
|
-
await this._store.setMeta(
|
|
2760
|
+
await this._store.setMeta(Ve, o);
|
|
2650
2761
|
let s = new Set(
|
|
2651
2762
|
t.docs.map((a) => `${a.collection}::${a.id}`)
|
|
2652
2763
|
);
|
|
2653
2764
|
for (let a of e) {
|
|
2654
2765
|
if (i.has(a)) continue;
|
|
2655
|
-
let
|
|
2656
|
-
for (let { id: d } of
|
|
2766
|
+
let c = await this._store.listDocs(a, {});
|
|
2767
|
+
for (let { id: d } of c.docs) {
|
|
2657
2768
|
let u = `${a}::${d}`;
|
|
2658
2769
|
if (!s.has(u)) {
|
|
2659
2770
|
let p = await this._store.getDoc(a, d);
|
|
@@ -2703,7 +2814,7 @@ var pe = class {
|
|
|
2703
2814
|
continue;
|
|
2704
2815
|
}
|
|
2705
2816
|
if (i && i.meta.state === "pending") {
|
|
2706
|
-
let
|
|
2817
|
+
let c = {
|
|
2707
2818
|
collection: o.collection,
|
|
2708
2819
|
id: o.id,
|
|
2709
2820
|
data: o.data,
|
|
@@ -2720,7 +2831,7 @@ var pe = class {
|
|
|
2720
2831
|
(p) => p.collection === o.collection && p.docId === o.id
|
|
2721
2832
|
);
|
|
2722
2833
|
if (u) {
|
|
2723
|
-
let p = this._resolver.resolve(i,
|
|
2834
|
+
let p = this._resolver.resolve(i, c, u);
|
|
2724
2835
|
p.conflictRecord && await this._store.appendConflict(p.conflictRecord), await this._store.putDoc({
|
|
2725
2836
|
...i,
|
|
2726
2837
|
meta: { ...i.meta, state: "conflict" }
|
|
@@ -2865,10 +2976,10 @@ var pe = class {
|
|
|
2865
2976
|
};
|
|
2866
2977
|
|
|
2867
2978
|
// src/db/offline/conflict-resolver.ts
|
|
2868
|
-
function
|
|
2979
|
+
function Kt(n, e) {
|
|
2869
2980
|
return e === null ? false : n === null ? true : e > n;
|
|
2870
2981
|
}
|
|
2871
|
-
var
|
|
2982
|
+
var ge = class n {
|
|
2872
2983
|
/**
|
|
2873
2984
|
* Resolve um conflito com base na operação da mutação (auto-dispatch).
|
|
2874
2985
|
*
|
|
@@ -2902,7 +3013,7 @@ var fe = class n {
|
|
|
2902
3013
|
resolvedData: t.data,
|
|
2903
3014
|
conflictRecord: null
|
|
2904
3015
|
};
|
|
2905
|
-
if (!
|
|
3016
|
+
if (!Kt(
|
|
2906
3017
|
e.meta.updatedAtServer,
|
|
2907
3018
|
t.meta.updatedAtServer
|
|
2908
3019
|
))
|
|
@@ -2949,7 +3060,7 @@ var fe = class n {
|
|
|
2949
3060
|
let i = t.data;
|
|
2950
3061
|
if (e === null)
|
|
2951
3062
|
return { outcome: "no_conflict", resolvedData: { ...i, ...r.payload ?? {} }, conflictRecord: null };
|
|
2952
|
-
if (!
|
|
3063
|
+
if (!Kt(
|
|
2953
3064
|
e.meta.updatedAtServer,
|
|
2954
3065
|
t.meta.updatedAtServer
|
|
2955
3066
|
))
|
|
@@ -2957,7 +3068,7 @@ var fe = class n {
|
|
|
2957
3068
|
let a = {};
|
|
2958
3069
|
for (let d of o)
|
|
2959
3070
|
a[d] = e.data[d];
|
|
2960
|
-
let
|
|
3071
|
+
let c = {
|
|
2961
3072
|
collection: r.collection,
|
|
2962
3073
|
docId: r.docId,
|
|
2963
3074
|
mutationId: r.mutationId,
|
|
@@ -2970,7 +3081,7 @@ var fe = class n {
|
|
|
2970
3081
|
return {
|
|
2971
3082
|
outcome: "server_wins",
|
|
2972
3083
|
resolvedData: i,
|
|
2973
|
-
conflictRecord:
|
|
3084
|
+
conflictRecord: c
|
|
2974
3085
|
};
|
|
2975
3086
|
}
|
|
2976
3087
|
/**
|
|
@@ -3027,10 +3138,10 @@ var fe = class n {
|
|
|
3027
3138
|
};
|
|
3028
3139
|
|
|
3029
3140
|
// src/db/offline/change-bus.ts
|
|
3030
|
-
function
|
|
3141
|
+
function jr(n) {
|
|
3031
3142
|
return `${n.collection}::${n.doc.id}`;
|
|
3032
3143
|
}
|
|
3033
|
-
var
|
|
3144
|
+
var ye = class {
|
|
3034
3145
|
/** Listeners filtrados por coleção. */
|
|
3035
3146
|
_collectionListeners = /* @__PURE__ */ new Map();
|
|
3036
3147
|
/** Listeners globais (todas as coleções). */
|
|
@@ -3075,15 +3186,15 @@ var me = class {
|
|
|
3075
3186
|
if (e.length === 0) return;
|
|
3076
3187
|
let t = /* @__PURE__ */ new Map();
|
|
3077
3188
|
for (let i of e)
|
|
3078
|
-
t.set(
|
|
3189
|
+
t.set(jr(i), i);
|
|
3079
3190
|
let r = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
3080
3191
|
for (let i of r)
|
|
3081
3192
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
3082
3193
|
for (let [i, s] of o) {
|
|
3083
3194
|
let a = this._collectionListeners.get(i);
|
|
3084
3195
|
if (a)
|
|
3085
|
-
for (let
|
|
3086
|
-
this._safeCall(
|
|
3196
|
+
for (let c of a)
|
|
3197
|
+
this._safeCall(c, s);
|
|
3087
3198
|
}
|
|
3088
3199
|
for (let i of this._globalListeners)
|
|
3089
3200
|
this._safeCall(i, r);
|
|
@@ -3114,7 +3225,7 @@ var me = class {
|
|
|
3114
3225
|
};
|
|
3115
3226
|
|
|
3116
3227
|
// src/db/offline/connectivity-monitor.ts
|
|
3117
|
-
var
|
|
3228
|
+
var _e = class {
|
|
3118
3229
|
_state;
|
|
3119
3230
|
_listeners = /* @__PURE__ */ new Set();
|
|
3120
3231
|
_nav;
|
|
@@ -3216,7 +3327,7 @@ var he = class {
|
|
|
3216
3327
|
};
|
|
3217
3328
|
|
|
3218
3329
|
// src/db/offline/tab-coordinator.ts
|
|
3219
|
-
var
|
|
3330
|
+
var ne = class {
|
|
3220
3331
|
_lockName;
|
|
3221
3332
|
_channelName;
|
|
3222
3333
|
_lockManager;
|
|
@@ -3393,16 +3504,16 @@ var te = class {
|
|
|
3393
3504
|
};
|
|
3394
3505
|
|
|
3395
3506
|
// src/db/collection-ref.ts
|
|
3396
|
-
|
|
3397
|
-
function
|
|
3507
|
+
Z();
|
|
3508
|
+
function $r() {
|
|
3398
3509
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
3399
3510
|
}
|
|
3400
|
-
var
|
|
3401
|
-
function
|
|
3402
|
-
if (!
|
|
3511
|
+
var zt = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
3512
|
+
function Wr(n) {
|
|
3513
|
+
if (!zt.test(n))
|
|
3403
3514
|
throw new y(
|
|
3404
3515
|
"db_invalid_query",
|
|
3405
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${
|
|
3516
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${zt}.`
|
|
3406
3517
|
);
|
|
3407
3518
|
}
|
|
3408
3519
|
function G(n, e = "id") {
|
|
@@ -3412,7 +3523,7 @@ function G(n, e = "id") {
|
|
|
3412
3523
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
3413
3524
|
);
|
|
3414
3525
|
}
|
|
3415
|
-
function
|
|
3526
|
+
function Br(n) {
|
|
3416
3527
|
let e = {};
|
|
3417
3528
|
if (n?.where && (e.where = n.where.map((t) => ({
|
|
3418
3529
|
field: t.field,
|
|
@@ -3429,12 +3540,12 @@ function Sr(n) {
|
|
|
3429
3540
|
}
|
|
3430
3541
|
return e;
|
|
3431
3542
|
}
|
|
3432
|
-
function
|
|
3543
|
+
function Vr(n, e) {
|
|
3433
3544
|
if (n.length < e) return null;
|
|
3434
3545
|
let t = n[n.length - 1];
|
|
3435
3546
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
3436
3547
|
}
|
|
3437
|
-
var
|
|
3548
|
+
var Ke = class {
|
|
3438
3549
|
constructor(e, t, r, o, i, s) {
|
|
3439
3550
|
this._collection = e;
|
|
3440
3551
|
this._store = t;
|
|
@@ -3476,10 +3587,10 @@ var Fe = class {
|
|
|
3476
3587
|
}
|
|
3477
3588
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
3478
3589
|
async _buildListResult(e) {
|
|
3479
|
-
let t =
|
|
3590
|
+
let t = Br(e), r = t.limit ?? 20, o = await this._store.listDocs(this._collection, t), i = await this._hasPendingWrites(), s = this._isOnline(), a = this._engine.getSyncState(), c = Vr(o.docs, r), d = await this._store.getMeta(Ve), u = Array.isArray(d) && d.includes(this._collection), p = !s || a.status === "offline" || u, f = i || a.lastSyncedAt === null || p;
|
|
3480
3591
|
return {
|
|
3481
|
-
docs: o.docs.map((
|
|
3482
|
-
nextCursor:
|
|
3592
|
+
docs: o.docs.map((_) => ({ id: _.id, data: _.data })),
|
|
3593
|
+
nextCursor: c,
|
|
3483
3594
|
fromCache: f,
|
|
3484
3595
|
stale: p,
|
|
3485
3596
|
hasPendingWrites: i,
|
|
@@ -3495,7 +3606,7 @@ var Fe = class {
|
|
|
3495
3606
|
return this._buildListResult(e);
|
|
3496
3607
|
}
|
|
3497
3608
|
async add(e) {
|
|
3498
|
-
let t = e, r =
|
|
3609
|
+
let t = e, r = K(t, void 0), o = await this._queue.enqueue({
|
|
3499
3610
|
collection: this._collection,
|
|
3500
3611
|
op: "add",
|
|
3501
3612
|
payload: t,
|
|
@@ -3523,7 +3634,7 @@ var Fe = class {
|
|
|
3523
3634
|
}
|
|
3524
3635
|
async set(e, t) {
|
|
3525
3636
|
G(e);
|
|
3526
|
-
let r = await this._store.getDoc(this._collection, e), o = t, i =
|
|
3637
|
+
let r = await this._store.getDoc(this._collection, e), o = t, i = K(o, void 0), s = await this._queue.enqueue({
|
|
3527
3638
|
collection: this._collection,
|
|
3528
3639
|
docId: e,
|
|
3529
3640
|
op: "set",
|
|
@@ -3552,7 +3663,7 @@ var Fe = class {
|
|
|
3552
3663
|
}
|
|
3553
3664
|
async update(e, t, r) {
|
|
3554
3665
|
G(e);
|
|
3555
|
-
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a =
|
|
3666
|
+
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a = K(i, s), c = o?.meta.deleted ? { ...a } : { ...o?.data ?? {}, ...a }, d = await this._queue.enqueue({
|
|
3556
3667
|
collection: this._collection,
|
|
3557
3668
|
docId: e,
|
|
3558
3669
|
op: "update",
|
|
@@ -3565,7 +3676,7 @@ var Fe = class {
|
|
|
3565
3676
|
return await this._store.putDoc({
|
|
3566
3677
|
collection: this._collection,
|
|
3567
3678
|
id: e,
|
|
3568
|
-
data:
|
|
3679
|
+
data: c,
|
|
3569
3680
|
meta: {
|
|
3570
3681
|
serverVersion: o?.meta.serverVersion ?? null,
|
|
3571
3682
|
updatedAtServer: o?.meta.updatedAtServer ?? null,
|
|
@@ -3581,7 +3692,7 @@ var Fe = class {
|
|
|
3581
3692
|
}), this._bus.emit([{
|
|
3582
3693
|
type: "modified",
|
|
3583
3694
|
collection: this._collection,
|
|
3584
|
-
doc: { id: e, data:
|
|
3695
|
+
doc: { id: e, data: c }
|
|
3585
3696
|
}]), { ok: true };
|
|
3586
3697
|
}
|
|
3587
3698
|
async remove(e) {
|
|
@@ -3605,13 +3716,13 @@ var Fe = class {
|
|
|
3605
3716
|
for (let o of e) {
|
|
3606
3717
|
let i = this._collection;
|
|
3607
3718
|
if (o.kind === "add") {
|
|
3608
|
-
let s = o.data, a =
|
|
3719
|
+
let s = o.data, a = K(s, void 0), c = await this._queue.enqueue({
|
|
3609
3720
|
collection: i,
|
|
3610
3721
|
op: "add",
|
|
3611
3722
|
payload: s,
|
|
3612
3723
|
baseVersion: null,
|
|
3613
3724
|
batchId: t
|
|
3614
|
-
}), d =
|
|
3725
|
+
}), d = c.docId;
|
|
3615
3726
|
await this._store.putDoc({
|
|
3616
3727
|
collection: i,
|
|
3617
3728
|
id: d,
|
|
@@ -3621,18 +3732,18 @@ var Fe = class {
|
|
|
3621
3732
|
updatedAtServer: null,
|
|
3622
3733
|
updatedAtLocal: Date.now(),
|
|
3623
3734
|
state: "pending",
|
|
3624
|
-
pendingMutationIds: [
|
|
3735
|
+
pendingMutationIds: [c.mutationId],
|
|
3625
3736
|
deleted: false,
|
|
3626
3737
|
ownerId: null
|
|
3627
3738
|
}
|
|
3628
3739
|
}), r.push({ type: "added", collection: i, doc: { id: d, data: a } });
|
|
3629
3740
|
} else if (o.kind === "set") {
|
|
3630
|
-
let s = o.id, a = o.data,
|
|
3741
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), d = K(a, void 0), u = await this._queue.enqueue({
|
|
3631
3742
|
collection: i,
|
|
3632
3743
|
docId: s,
|
|
3633
3744
|
op: "set",
|
|
3634
3745
|
payload: a,
|
|
3635
|
-
baseVersion:
|
|
3746
|
+
baseVersion: c?.meta.serverVersion ?? null,
|
|
3636
3747
|
batchId: t
|
|
3637
3748
|
});
|
|
3638
3749
|
await this._store.putDoc({
|
|
@@ -3640,8 +3751,8 @@ var Fe = class {
|
|
|
3640
3751
|
id: s,
|
|
3641
3752
|
data: d,
|
|
3642
3753
|
meta: {
|
|
3643
|
-
serverVersion:
|
|
3644
|
-
updatedAtServer:
|
|
3754
|
+
serverVersion: c?.meta.serverVersion ?? null,
|
|
3755
|
+
updatedAtServer: c?.meta.updatedAtServer ?? null,
|
|
3645
3756
|
updatedAtLocal: Date.now(),
|
|
3646
3757
|
state: "pending",
|
|
3647
3758
|
pendingMutationIds: [u.mutationId],
|
|
@@ -3649,17 +3760,17 @@ var Fe = class {
|
|
|
3649
3760
|
ownerId: null
|
|
3650
3761
|
}
|
|
3651
3762
|
}), r.push({
|
|
3652
|
-
type:
|
|
3763
|
+
type: c && !c.meta.deleted ? "modified" : "added",
|
|
3653
3764
|
collection: i,
|
|
3654
3765
|
doc: { id: s, data: d }
|
|
3655
3766
|
});
|
|
3656
3767
|
} else if (o.kind === "update") {
|
|
3657
|
-
let s = o.id, a = o.data,
|
|
3768
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), d = c?.meta.deleted ? void 0 : c?.data, u = K(a, d), p = { ...c?.data ?? {}, ...u }, f = await this._queue.enqueue({
|
|
3658
3769
|
collection: i,
|
|
3659
3770
|
docId: s,
|
|
3660
3771
|
op: "update",
|
|
3661
3772
|
payload: a,
|
|
3662
|
-
baseVersion:
|
|
3773
|
+
baseVersion: c?.meta.serverVersion ?? null,
|
|
3663
3774
|
batchId: t,
|
|
3664
3775
|
// bug_0273e4dd: propaga merge-upsert (writeBatch.set(merge) cria-se-ausente).
|
|
3665
3776
|
upsert: o.upsert
|
|
@@ -3669,16 +3780,16 @@ var Fe = class {
|
|
|
3669
3780
|
id: s,
|
|
3670
3781
|
data: p,
|
|
3671
3782
|
meta: {
|
|
3672
|
-
serverVersion:
|
|
3673
|
-
updatedAtServer:
|
|
3783
|
+
serverVersion: c?.meta.serverVersion ?? null,
|
|
3784
|
+
updatedAtServer: c?.meta.updatedAtServer ?? null,
|
|
3674
3785
|
updatedAtLocal: Date.now(),
|
|
3675
3786
|
state: "pending",
|
|
3676
3787
|
pendingMutationIds: [
|
|
3677
|
-
...
|
|
3788
|
+
...c?.meta.pendingMutationIds ?? [],
|
|
3678
3789
|
f.mutationId
|
|
3679
3790
|
],
|
|
3680
3791
|
deleted: false,
|
|
3681
|
-
ownerId:
|
|
3792
|
+
ownerId: c?.meta.ownerId ?? null
|
|
3682
3793
|
}
|
|
3683
3794
|
}), r.push({ type: "modified", collection: i, doc: { id: s, data: p } });
|
|
3684
3795
|
} else if (o.kind === "remove") {
|
|
@@ -3709,11 +3820,11 @@ var Fe = class {
|
|
|
3709
3820
|
});
|
|
3710
3821
|
let r = this._bus.subscribe(this._collection, async (s) => {
|
|
3711
3822
|
try {
|
|
3712
|
-
let a = await this._buildListResult(e),
|
|
3823
|
+
let a = await this._buildListResult(e), c = s.map((d) => ({
|
|
3713
3824
|
type: d.type,
|
|
3714
3825
|
doc: { id: d.doc.id, data: d.doc.data }
|
|
3715
3826
|
}));
|
|
3716
|
-
t({ ...a, changes:
|
|
3827
|
+
t({ ...a, changes: c });
|
|
3717
3828
|
} catch {
|
|
3718
3829
|
}
|
|
3719
3830
|
}), o = this._engine.onSyncStateChange(async () => {
|
|
@@ -3727,15 +3838,15 @@ var Fe = class {
|
|
|
3727
3838
|
this._collection,
|
|
3728
3839
|
async (s, a) => {
|
|
3729
3840
|
if (!a)
|
|
3730
|
-
for (let
|
|
3731
|
-
|
|
3841
|
+
for (let c of s)
|
|
3842
|
+
c.type === "removed" || c.data === null ? (await this._store.deleteDoc(this._collection, c.docId), this._bus.emit([{
|
|
3732
3843
|
type: "removed",
|
|
3733
3844
|
collection: this._collection,
|
|
3734
|
-
doc: { id:
|
|
3845
|
+
doc: { id: c.docId, data: {} }
|
|
3735
3846
|
}])) : (await this._store.putDoc({
|
|
3736
3847
|
collection: this._collection,
|
|
3737
|
-
id:
|
|
3738
|
-
data:
|
|
3848
|
+
id: c.docId,
|
|
3849
|
+
data: c.data,
|
|
3739
3850
|
meta: {
|
|
3740
3851
|
serverVersion: `rt_${Date.now()}`,
|
|
3741
3852
|
updatedAtServer: Date.now(),
|
|
@@ -3746,9 +3857,9 @@ var Fe = class {
|
|
|
3746
3857
|
ownerId: null
|
|
3747
3858
|
}
|
|
3748
3859
|
}), this._bus.emit([{
|
|
3749
|
-
type:
|
|
3860
|
+
type: c.type,
|
|
3750
3861
|
collection: this._collection,
|
|
3751
|
-
doc: { id:
|
|
3862
|
+
doc: { id: c.docId, data: c.data }
|
|
3752
3863
|
}]));
|
|
3753
3864
|
}
|
|
3754
3865
|
)), () => {
|
|
@@ -3783,14 +3894,14 @@ var Fe = class {
|
|
|
3783
3894
|
onSnapshot(r) {
|
|
3784
3895
|
t._buildGetResult(e).then((s) => r(s)).catch(() => r(null));
|
|
3785
3896
|
let o = t._bus.subscribe(t._collection, async (s) => {
|
|
3786
|
-
let a = s.find((
|
|
3897
|
+
let a = s.find((c) => c.doc.id === e);
|
|
3787
3898
|
if (a)
|
|
3788
3899
|
try {
|
|
3789
3900
|
if (a.type === "removed")
|
|
3790
3901
|
r(null);
|
|
3791
3902
|
else {
|
|
3792
|
-
let
|
|
3793
|
-
r(
|
|
3903
|
+
let c = await t._buildGetResult(e);
|
|
3904
|
+
r(c);
|
|
3794
3905
|
}
|
|
3795
3906
|
} catch {
|
|
3796
3907
|
r(null);
|
|
@@ -3809,7 +3920,7 @@ var Fe = class {
|
|
|
3809
3920
|
}
|
|
3810
3921
|
};
|
|
3811
3922
|
}
|
|
3812
|
-
},
|
|
3923
|
+
}, ze = class {
|
|
3813
3924
|
_store;
|
|
3814
3925
|
_queue;
|
|
3815
3926
|
_bus;
|
|
@@ -3819,7 +3930,7 @@ var Fe = class {
|
|
|
3819
3930
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3820
3931
|
}
|
|
3821
3932
|
collection(e) {
|
|
3822
|
-
return
|
|
3933
|
+
return Wr(e), new Ke(
|
|
3823
3934
|
e,
|
|
3824
3935
|
this._store,
|
|
3825
3936
|
this._queue,
|
|
@@ -3848,23 +3959,23 @@ var Fe = class {
|
|
|
3848
3959
|
return this._engine.onError(e);
|
|
3849
3960
|
}
|
|
3850
3961
|
};
|
|
3851
|
-
async function
|
|
3852
|
-
let e =
|
|
3962
|
+
async function Ht(n) {
|
|
3963
|
+
let e = $r() ? new fe() : new pe(n.dbName);
|
|
3853
3964
|
await e.open();
|
|
3854
|
-
let t = new
|
|
3965
|
+
let t = new me(e), r = new ge(), o = new ye(), i = new _e({
|
|
3855
3966
|
// Se startOnline=false, injeta um navigator falso
|
|
3856
3967
|
navigator: n.startOnline === false ? { onLine: false } : typeof navigator < "u" ? navigator : void 0
|
|
3857
3968
|
}), s = n.singleTab ? (
|
|
3858
3969
|
// Fake tab coordinator para single-tab mode (sempre líder)
|
|
3859
3970
|
{
|
|
3860
3971
|
isLeader: () => true,
|
|
3861
|
-
onRoleChange: (
|
|
3972
|
+
onRoleChange: (c) => () => {
|
|
3862
3973
|
}
|
|
3863
3974
|
}
|
|
3864
|
-
) : new
|
|
3975
|
+
) : new ne({
|
|
3865
3976
|
lockName: `neetru-offline:${n.dbName}`,
|
|
3866
3977
|
channelName: `neetru-offline:${n.dbName}`
|
|
3867
|
-
}), a = new
|
|
3978
|
+
}), a = new he({
|
|
3868
3979
|
store: e,
|
|
3869
3980
|
queue: t,
|
|
3870
3981
|
resolver: r,
|
|
@@ -3874,7 +3985,7 @@ async function Lt(n) {
|
|
|
3874
3985
|
connectivity: i,
|
|
3875
3986
|
periodicSyncIntervalMs: n.periodicSyncIntervalMs ?? 300 * 1e3
|
|
3876
3987
|
});
|
|
3877
|
-
return i.start(), !n.singleTab && s instanceof
|
|
3988
|
+
return i.start(), !n.singleTab && s instanceof ne && s.start(), new ze({
|
|
3878
3989
|
store: e,
|
|
3879
3990
|
queue: t,
|
|
3880
3991
|
bus: o,
|
|
@@ -3884,11 +3995,11 @@ async function Lt(n) {
|
|
|
3884
3995
|
}
|
|
3885
3996
|
|
|
3886
3997
|
// src/db/realtime/realtime-client.ts
|
|
3887
|
-
var
|
|
3888
|
-
function
|
|
3889
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
3998
|
+
var Kr = 0;
|
|
3999
|
+
function Qt() {
|
|
4000
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++Kr}-${Math.random().toString(36).slice(2)}`;
|
|
3890
4001
|
}
|
|
3891
|
-
var
|
|
4002
|
+
var be = class {
|
|
3892
4003
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
3893
4004
|
_gatewayUrl;
|
|
3894
4005
|
_wsFactory;
|
|
@@ -3926,7 +4037,7 @@ var ge = class {
|
|
|
3926
4037
|
_pendingFrames = [];
|
|
3927
4038
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
3928
4039
|
constructor(e) {
|
|
3929
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
4040
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? zr, 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();
|
|
3930
4041
|
}
|
|
3931
4042
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
3932
4043
|
/**
|
|
@@ -3940,8 +4051,8 @@ var ge = class {
|
|
|
3940
4051
|
*/
|
|
3941
4052
|
subscribe(e, t, r) {
|
|
3942
4053
|
if (this._closed)
|
|
3943
|
-
return
|
|
3944
|
-
let o =
|
|
4054
|
+
return Qt();
|
|
4055
|
+
let o = Qt();
|
|
3945
4056
|
this._subscriptions.set(o, {
|
|
3946
4057
|
subscriptionId: o,
|
|
3947
4058
|
collection: e,
|
|
@@ -4205,7 +4316,7 @@ var ge = class {
|
|
|
4205
4316
|
_detachHandlers(e) {
|
|
4206
4317
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
4207
4318
|
}
|
|
4208
|
-
},
|
|
4319
|
+
}, zr = (n) => {
|
|
4209
4320
|
if (typeof WebSocket > "u")
|
|
4210
4321
|
throw new Error(
|
|
4211
4322
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -4214,11 +4325,11 @@ var ge = class {
|
|
|
4214
4325
|
};
|
|
4215
4326
|
|
|
4216
4327
|
// src/db/client-db.ts
|
|
4217
|
-
var
|
|
4218
|
-
function
|
|
4219
|
-
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
4328
|
+
var ro = (n) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}`, we = (n, e) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}/${encodeURIComponent(e)}`, sn = 200;
|
|
4329
|
+
function oo(n, e, t) {
|
|
4330
|
+
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ? io(e.realtimeGatewayUrl, t, e) : cn(t);
|
|
4220
4331
|
}
|
|
4221
|
-
function
|
|
4332
|
+
function cn(n) {
|
|
4222
4333
|
return {
|
|
4223
4334
|
async pushMutations(e) {
|
|
4224
4335
|
if (!n)
|
|
@@ -4226,47 +4337,47 @@ function Gt(n) {
|
|
|
4226
4337
|
"db_unavailable",
|
|
4227
4338
|
"[RestSyncTransport] config n\xE3o dispon\xEDvel \u2014 n\xE3o \xE9 poss\xEDvel enviar ao Core. Inicialize o transporte via createNeetruDb."
|
|
4228
4339
|
);
|
|
4229
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4340
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (M(), Y)), r = [], o = n.tenantId ?? n.productId, i = o ? { "x-neetru-tenant": o } : void 0;
|
|
4230
4341
|
for (let s of e)
|
|
4231
4342
|
try {
|
|
4232
4343
|
let a;
|
|
4233
4344
|
s.op === "add" ? a = await t(n, {
|
|
4234
4345
|
method: "PUT",
|
|
4235
|
-
path:
|
|
4346
|
+
path: we(s.collection, s.docId),
|
|
4236
4347
|
body: { data: s.payload ?? {} },
|
|
4237
4348
|
requireAuth: !0,
|
|
4238
4349
|
retries: 2,
|
|
4239
4350
|
headers: i
|
|
4240
4351
|
}) : s.op === "set" ? a = await t(n, {
|
|
4241
4352
|
method: "PUT",
|
|
4242
|
-
path:
|
|
4353
|
+
path: we(s.collection, s.docId),
|
|
4243
4354
|
body: { data: s.payload ?? {} },
|
|
4244
4355
|
requireAuth: !0,
|
|
4245
4356
|
retries: 2,
|
|
4246
4357
|
headers: i
|
|
4247
4358
|
}) : s.op === "update" ? a = await t(n, {
|
|
4248
4359
|
method: "PATCH",
|
|
4249
|
-
path:
|
|
4360
|
+
path: we(s.collection, s.docId),
|
|
4250
4361
|
body: { data: s.payload ?? {}, ...s.upsert ? { upsert: !0 } : {} },
|
|
4251
4362
|
requireAuth: !0,
|
|
4252
4363
|
retries: 2,
|
|
4253
4364
|
headers: i
|
|
4254
4365
|
}) : a = await t(n, {
|
|
4255
4366
|
method: "DELETE",
|
|
4256
|
-
path:
|
|
4367
|
+
path: we(s.collection, s.docId),
|
|
4257
4368
|
requireAuth: !0,
|
|
4258
4369
|
retries: 2,
|
|
4259
4370
|
headers: i
|
|
4260
4371
|
});
|
|
4261
|
-
let
|
|
4372
|
+
let c = a;
|
|
4262
4373
|
r.push({
|
|
4263
4374
|
mutationId: s.mutationId,
|
|
4264
4375
|
outcome: "confirmed",
|
|
4265
|
-
serverVersion:
|
|
4376
|
+
serverVersion: c?.serverVersion ?? c?.id ?? `rest_${Date.now()}`,
|
|
4266
4377
|
serverTimestamp: Date.now()
|
|
4267
4378
|
});
|
|
4268
4379
|
} catch (a) {
|
|
4269
|
-
let
|
|
4380
|
+
let c = a instanceof Error ? a.message : String(a), d = a?.code, u = a?.status;
|
|
4270
4381
|
if (d === "forbidden" || d === "unauthorized" || d === "not_found" || d === "conflict" || d === "validation_failed" || typeof u == "number" && u >= 400 && u < 500 && u !== 408 && u !== 429) {
|
|
4271
4382
|
let f = d === "forbidden" || d === "unauthorized" ? "rejected_permission" : "rejected_validation";
|
|
4272
4383
|
r.push({
|
|
@@ -4279,7 +4390,7 @@ function Gt(n) {
|
|
|
4279
4390
|
continue;
|
|
4280
4391
|
}
|
|
4281
4392
|
if (r.length === 0)
|
|
4282
|
-
throw new y("db_unavailable", `pushMutations falhou: ${
|
|
4393
|
+
throw new y("db_unavailable", `pushMutations falhou: ${c}`);
|
|
4283
4394
|
break;
|
|
4284
4395
|
}
|
|
4285
4396
|
return { results: r };
|
|
@@ -4290,13 +4401,13 @@ function Gt(n) {
|
|
|
4290
4401
|
async fullResync(e) {
|
|
4291
4402
|
if (!n)
|
|
4292
4403
|
return { docs: [], newWatermark: Date.now() };
|
|
4293
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4294
|
-
for (let
|
|
4404
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (M(), Y)), r = n.tenantId ?? n.productId, o = r ? { "x-neetru-tenant": r } : void 0, i = Date.now(), s = [], a = [];
|
|
4405
|
+
for (let c of e) {
|
|
4295
4406
|
let d;
|
|
4296
4407
|
try {
|
|
4297
4408
|
d = await t(n, {
|
|
4298
4409
|
method: "GET",
|
|
4299
|
-
path:
|
|
4410
|
+
path: ro(c),
|
|
4300
4411
|
requireAuth: !0,
|
|
4301
4412
|
retries: 2,
|
|
4302
4413
|
headers: o,
|
|
@@ -4304,37 +4415,37 @@ function Gt(n) {
|
|
|
4304
4415
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
4305
4416
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
4306
4417
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
4307
|
-
query: { limit:
|
|
4418
|
+
query: { limit: sn }
|
|
4308
4419
|
});
|
|
4309
4420
|
} catch (f) {
|
|
4310
|
-
let
|
|
4421
|
+
let _ = f instanceof Error ? f.message : String(f);
|
|
4311
4422
|
throw new y(
|
|
4312
4423
|
"db_unavailable",
|
|
4313
|
-
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${
|
|
4424
|
+
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${_}`
|
|
4314
4425
|
);
|
|
4315
4426
|
}
|
|
4316
4427
|
let u = d, p = Array.isArray(u?.items) ? u.items : [];
|
|
4317
|
-
p.length >=
|
|
4428
|
+
p.length >= sn && a.push(c);
|
|
4318
4429
|
for (let f of p) {
|
|
4319
4430
|
if (!f || typeof f != "object") continue;
|
|
4320
|
-
let
|
|
4321
|
-
if (!
|
|
4322
|
-
let { id:
|
|
4323
|
-
if (typeof
|
|
4324
|
-
|
|
4325
|
-
else if (
|
|
4326
|
-
|
|
4327
|
-
else if (typeof
|
|
4328
|
-
let
|
|
4329
|
-
Number.isNaN(
|
|
4431
|
+
let _ = f, b = typeof _.id == "string" ? _.id : null;
|
|
4432
|
+
if (!b) continue;
|
|
4433
|
+
let { id: S, ...x } = _, T = i, P = x._updatedAt;
|
|
4434
|
+
if (typeof P == "number")
|
|
4435
|
+
T = P;
|
|
4436
|
+
else if (P !== null && typeof P == "object" && typeof P._seconds == "number")
|
|
4437
|
+
T = P._seconds * 1e3;
|
|
4438
|
+
else if (typeof P == "string") {
|
|
4439
|
+
let m = Date.parse(P);
|
|
4440
|
+
Number.isNaN(m) || (T = m);
|
|
4330
4441
|
}
|
|
4331
|
-
let
|
|
4442
|
+
let q = `rest_${b}_${T}`;
|
|
4332
4443
|
s.push({
|
|
4333
|
-
collection:
|
|
4334
|
-
id:
|
|
4335
|
-
data:
|
|
4336
|
-
serverVersion:
|
|
4337
|
-
serverTimestamp:
|
|
4444
|
+
collection: c,
|
|
4445
|
+
id: b,
|
|
4446
|
+
data: x,
|
|
4447
|
+
serverVersion: q,
|
|
4448
|
+
serverTimestamp: T,
|
|
4338
4449
|
deleted: false
|
|
4339
4450
|
});
|
|
4340
4451
|
}
|
|
@@ -4343,10 +4454,10 @@ function Gt(n) {
|
|
|
4343
4454
|
}
|
|
4344
4455
|
};
|
|
4345
4456
|
}
|
|
4346
|
-
function
|
|
4347
|
-
let r =
|
|
4457
|
+
function io(n, e, t) {
|
|
4458
|
+
let r = cn(e), o = new be({
|
|
4348
4459
|
gatewayUrl: n,
|
|
4349
|
-
ticketProvider:
|
|
4460
|
+
ticketProvider: so(e, t),
|
|
4350
4461
|
dbId: t.dbId,
|
|
4351
4462
|
webSocketFactory: t._wsFactory
|
|
4352
4463
|
});
|
|
@@ -4358,17 +4469,17 @@ function Fr(n, e, t) {
|
|
|
4358
4469
|
// quando o engine é nosql-vm. Registra a subscription no NeetruRealtimeClient
|
|
4359
4470
|
// e traduz os frames inbound para o contrato de `onChange`.
|
|
4360
4471
|
subscribeCollection(i, s) {
|
|
4361
|
-
let a = o.subscribe(i, {}, (
|
|
4362
|
-
if (
|
|
4472
|
+
let a = o.subscribe(i, {}, (c) => {
|
|
4473
|
+
if (c.op === "resync") {
|
|
4363
4474
|
s([], true);
|
|
4364
4475
|
return;
|
|
4365
4476
|
}
|
|
4366
|
-
if (
|
|
4477
|
+
if (c.op === "stale") {
|
|
4367
4478
|
s([], true);
|
|
4368
4479
|
return;
|
|
4369
4480
|
}
|
|
4370
|
-
if (
|
|
4371
|
-
let d =
|
|
4481
|
+
if (c.op === "delta" && c.changes) {
|
|
4482
|
+
let d = c.changes.map((u) => ({
|
|
4372
4483
|
type: u.type === "insert" ? "added" : u.type === "delete" ? "removed" : "modified",
|
|
4373
4484
|
docId: u.documentId,
|
|
4374
4485
|
data: u.data ?? null
|
|
@@ -4382,9 +4493,9 @@ function Fr(n, e, t) {
|
|
|
4382
4493
|
}
|
|
4383
4494
|
};
|
|
4384
4495
|
}
|
|
4385
|
-
function
|
|
4496
|
+
function so(n, e) {
|
|
4386
4497
|
return async () => {
|
|
4387
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4498
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (M(), Y));
|
|
4388
4499
|
return await t(n, {
|
|
4389
4500
|
method: "POST",
|
|
4390
4501
|
path: "/api/sdk/v1/db/realtime/ticket",
|
|
@@ -4397,21 +4508,21 @@ function jr(n, e) {
|
|
|
4397
4508
|
});
|
|
4398
4509
|
};
|
|
4399
4510
|
}
|
|
4400
|
-
var
|
|
4401
|
-
function
|
|
4402
|
-
|
|
4511
|
+
var an = false;
|
|
4512
|
+
function ao(n, e) {
|
|
4513
|
+
an || e.dbId && n.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (an = true, console.warn(
|
|
4403
4514
|
'[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.'
|
|
4404
4515
|
)));
|
|
4405
4516
|
}
|
|
4406
|
-
function
|
|
4407
|
-
|
|
4517
|
+
function ln(n, e = {}) {
|
|
4518
|
+
ao(n, e);
|
|
4408
4519
|
let t;
|
|
4409
4520
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
4410
4521
|
'[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).'
|
|
4411
4522
|
), t = e.engine) : t = "rest";
|
|
4412
|
-
let r =
|
|
4413
|
-
function
|
|
4414
|
-
return s || (s =
|
|
4523
|
+
let r = oo(t, e, n), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${n.productId ?? "sdk"}__${o}__${n.env}`, s = null, a = null;
|
|
4524
|
+
function c() {
|
|
4525
|
+
return s || (s = Ht({
|
|
4415
4526
|
dbName: i,
|
|
4416
4527
|
transport: r,
|
|
4417
4528
|
singleTab: e.singleTab ?? n.env !== "prod"
|
|
@@ -4419,7 +4530,7 @@ function Jt(n, e = {}) {
|
|
|
4419
4530
|
}
|
|
4420
4531
|
return {
|
|
4421
4532
|
collection(d) {
|
|
4422
|
-
return a ? a.collection(d) :
|
|
4533
|
+
return a ? a.collection(d) : co(d, c);
|
|
4423
4534
|
},
|
|
4424
4535
|
async sql(d, u) {
|
|
4425
4536
|
if (n.env === "dev")
|
|
@@ -4427,7 +4538,7 @@ function Jt(n, e = {}) {
|
|
|
4427
4538
|
"db_unavailable",
|
|
4428
4539
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
4429
4540
|
);
|
|
4430
|
-
return (await Promise.resolve().then(() => (
|
|
4541
|
+
return (await Promise.resolve().then(() => (on(), rn))).createSqlClientFromConfig(n, d, u);
|
|
4431
4542
|
},
|
|
4432
4543
|
get syncState() {
|
|
4433
4544
|
return a?.syncState ?? {
|
|
@@ -4440,25 +4551,25 @@ function Jt(n, e = {}) {
|
|
|
4440
4551
|
onSyncStateChanged(d) {
|
|
4441
4552
|
if (a) return a.onSyncStateChanged(d);
|
|
4442
4553
|
let u = null, p = false;
|
|
4443
|
-
return
|
|
4554
|
+
return c().then((f) => {
|
|
4444
4555
|
p || (u = f.onSyncStateChanged(d));
|
|
4445
4556
|
}), () => {
|
|
4446
4557
|
p = true, u?.();
|
|
4447
4558
|
};
|
|
4448
4559
|
},
|
|
4449
4560
|
async flush() {
|
|
4450
|
-
return (await
|
|
4561
|
+
return (await c()).flush();
|
|
4451
4562
|
},
|
|
4452
4563
|
async clearCache() {
|
|
4453
|
-
return (await
|
|
4564
|
+
return (await c()).clearCache();
|
|
4454
4565
|
},
|
|
4455
4566
|
async getConflicts() {
|
|
4456
|
-
return (await
|
|
4567
|
+
return (await c()).getConflicts();
|
|
4457
4568
|
},
|
|
4458
4569
|
onError(d) {
|
|
4459
4570
|
if (a) return a.onError(d);
|
|
4460
4571
|
let u = null, p = false;
|
|
4461
|
-
return
|
|
4572
|
+
return c().then((f) => {
|
|
4462
4573
|
p || (u = f.onError(d));
|
|
4463
4574
|
}), () => {
|
|
4464
4575
|
p = true, u?.();
|
|
@@ -4466,7 +4577,7 @@ function Jt(n, e = {}) {
|
|
|
4466
4577
|
}
|
|
4467
4578
|
};
|
|
4468
4579
|
}
|
|
4469
|
-
function
|
|
4580
|
+
function co(n, e) {
|
|
4470
4581
|
async function t() {
|
|
4471
4582
|
return (await e()).collection(n);
|
|
4472
4583
|
}
|
|
@@ -4545,25 +4656,25 @@ function $r(n, e) {
|
|
|
4545
4656
|
}
|
|
4546
4657
|
|
|
4547
4658
|
// src/checkout.ts
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
function
|
|
4659
|
+
I();
|
|
4660
|
+
M();
|
|
4661
|
+
function Ye(n, e) {
|
|
4551
4662
|
let t = n?.accessToken || n?.idToken;
|
|
4552
4663
|
if (!t || typeof t != "string")
|
|
4553
|
-
throw new
|
|
4664
|
+
throw new l(
|
|
4554
4665
|
"validation_failed",
|
|
4555
4666
|
`checkout.${e} requer o access_token OU o id_token OIDC do usu\xE1rio final (passe \`accessToken\` ou \`idToken\`). A chave de API do SDK (nrt_*) N\xC3O \xE9 aceita pelo endpoint de checkout \u2014 ele autentica a identidade do cliente que est\xE1 assinando (Bearer access_token/id_token OIDC ou cookie de sess\xE3o). No padr\xE3o BFF/cookie, passe o \`idToken\` que seu servidor guardou no cookie httpOnly.`
|
|
4556
4667
|
);
|
|
4557
4668
|
return { requireAuth: false, headers: { authorization: `Bearer ${t}` } };
|
|
4558
4669
|
}
|
|
4559
|
-
function
|
|
4670
|
+
function lo(n) {
|
|
4560
4671
|
if (!n || typeof n != "object")
|
|
4561
|
-
throw new
|
|
4672
|
+
throw new l("invalid_response", "checkout.start response is not an object");
|
|
4562
4673
|
let e = n;
|
|
4563
4674
|
if (typeof e.intentId != "string" || !e.intentId)
|
|
4564
|
-
throw new
|
|
4675
|
+
throw new l("invalid_response", "checkout.start response missing intentId");
|
|
4565
4676
|
if (typeof e.redirectUrl != "string" || !e.redirectUrl)
|
|
4566
|
-
throw new
|
|
4677
|
+
throw new l("invalid_response", "checkout.start response missing redirectUrl");
|
|
4567
4678
|
return {
|
|
4568
4679
|
intentId: e.intentId,
|
|
4569
4680
|
redirectUrl: e.redirectUrl,
|
|
@@ -4572,14 +4683,14 @@ function Br(n) {
|
|
|
4572
4683
|
requiresKyc: e.requiresKyc === true
|
|
4573
4684
|
};
|
|
4574
4685
|
}
|
|
4575
|
-
function
|
|
4686
|
+
function uo(n) {
|
|
4576
4687
|
if (!n || typeof n != "object")
|
|
4577
|
-
throw new
|
|
4688
|
+
throw new l("invalid_response", "checkout.get response is not an object");
|
|
4578
4689
|
let e = n, t = e.intent;
|
|
4579
4690
|
if (!t || typeof t != "object")
|
|
4580
|
-
throw new
|
|
4691
|
+
throw new l("invalid_response", "checkout.get response missing intent");
|
|
4581
4692
|
if (typeof t.intentId != "string")
|
|
4582
|
-
throw new
|
|
4693
|
+
throw new l("invalid_response", "checkout.get response missing intentId");
|
|
4583
4694
|
return {
|
|
4584
4695
|
intentId: t.intentId,
|
|
4585
4696
|
uid: t.uid ?? "",
|
|
@@ -4594,35 +4705,35 @@ function Vr(n) {
|
|
|
4594
4705
|
isStale: e.isStale === true
|
|
4595
4706
|
};
|
|
4596
4707
|
}
|
|
4597
|
-
function
|
|
4708
|
+
function po() {
|
|
4598
4709
|
try {
|
|
4599
4710
|
return typeof globalThis < "u" && typeof globalThis.window < "u" && typeof globalThis.location < "u" && typeof globalThis.location.assign == "function";
|
|
4600
4711
|
} catch {
|
|
4601
4712
|
return false;
|
|
4602
4713
|
}
|
|
4603
4714
|
}
|
|
4604
|
-
function
|
|
4715
|
+
function fo(n) {
|
|
4605
4716
|
try {
|
|
4606
4717
|
globalThis.location.assign(n);
|
|
4607
4718
|
} catch {
|
|
4608
4719
|
}
|
|
4609
4720
|
}
|
|
4610
|
-
function
|
|
4721
|
+
function mo(n) {
|
|
4611
4722
|
return {
|
|
4612
4723
|
async start(e) {
|
|
4613
4724
|
if (!e?.productId)
|
|
4614
|
-
throw new
|
|
4725
|
+
throw new l("validation_failed", "checkout.start: productId is required");
|
|
4615
4726
|
if (!e?.planId)
|
|
4616
|
-
throw new
|
|
4727
|
+
throw new l("validation_failed", "checkout.start: planId is required");
|
|
4617
4728
|
if (!e?.callbackUrl)
|
|
4618
|
-
throw new
|
|
4729
|
+
throw new l("validation_failed", "checkout.start: callbackUrl is required");
|
|
4619
4730
|
let t = {
|
|
4620
4731
|
productId: e.productId,
|
|
4621
4732
|
planId: e.planId,
|
|
4622
4733
|
callbackUrl: e.callbackUrl
|
|
4623
4734
|
};
|
|
4624
4735
|
e.tenantType && (t.targetTenantType = e.tenantType), e.tenantId && (t.targetTenantId = e.tenantId);
|
|
4625
|
-
let { requireAuth: r, headers: o } =
|
|
4736
|
+
let { requireAuth: r, headers: o } = Ye(
|
|
4626
4737
|
{ accessToken: e.accessToken, idToken: e.idToken },
|
|
4627
4738
|
"start"
|
|
4628
4739
|
), i = await v(n, {
|
|
@@ -4632,13 +4743,13 @@ function Hr(n) {
|
|
|
4632
4743
|
requireAuth: r,
|
|
4633
4744
|
headers: o,
|
|
4634
4745
|
idempotencyKey: true
|
|
4635
|
-
}), s =
|
|
4636
|
-
return e.autoRedirect !== false &&
|
|
4746
|
+
}), s = lo(i);
|
|
4747
|
+
return e.autoRedirect !== false && po() && fo(s.redirectUrl), s;
|
|
4637
4748
|
},
|
|
4638
4749
|
async get(e, t) {
|
|
4639
4750
|
if (!e || typeof e != "string")
|
|
4640
|
-
throw new
|
|
4641
|
-
let { requireAuth: r, headers: o } =
|
|
4751
|
+
throw new l("validation_failed", "checkout.get: intentId is required");
|
|
4752
|
+
let { requireAuth: r, headers: o } = Ye(
|
|
4642
4753
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4643
4754
|
"get"
|
|
4644
4755
|
), i = await v(n, {
|
|
@@ -4647,12 +4758,12 @@ function Hr(n) {
|
|
|
4647
4758
|
requireAuth: r,
|
|
4648
4759
|
headers: o
|
|
4649
4760
|
});
|
|
4650
|
-
return
|
|
4761
|
+
return uo(i);
|
|
4651
4762
|
},
|
|
4652
4763
|
async cancel(e, t) {
|
|
4653
4764
|
if (!e || typeof e != "string")
|
|
4654
|
-
throw new
|
|
4655
|
-
let { requireAuth: r, headers: o } =
|
|
4765
|
+
throw new l("validation_failed", "checkout.cancel: intentId is required");
|
|
4766
|
+
let { requireAuth: r, headers: o } = Ye(
|
|
4656
4767
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4657
4768
|
"cancel"
|
|
4658
4769
|
);
|
|
@@ -4668,15 +4779,15 @@ function Hr(n) {
|
|
|
4668
4779
|
}
|
|
4669
4780
|
};
|
|
4670
4781
|
}
|
|
4671
|
-
var
|
|
4782
|
+
var Ze = class {
|
|
4672
4783
|
intents = /* @__PURE__ */ new Map();
|
|
4673
4784
|
async start(e) {
|
|
4674
4785
|
if (!e?.productId)
|
|
4675
|
-
throw new
|
|
4786
|
+
throw new l("validation_failed", "checkout.start: productId is required");
|
|
4676
4787
|
if (!e?.planId)
|
|
4677
|
-
throw new
|
|
4788
|
+
throw new l("validation_failed", "checkout.start: planId is required");
|
|
4678
4789
|
if (!e?.callbackUrl)
|
|
4679
|
-
throw new
|
|
4790
|
+
throw new l("validation_failed", "checkout.start: callbackUrl is required");
|
|
4680
4791
|
let t = `chk_mock_${Math.random().toString(36).slice(2, 10)}`, r = new Date(Date.now() + 900 * 1e3).toISOString(), o = `https://localhost:9003/portal/checkout/${t}`;
|
|
4681
4792
|
return this.intents.set(t, {
|
|
4682
4793
|
intentId: t,
|
|
@@ -4700,25 +4811,25 @@ var He = class {
|
|
|
4700
4811
|
async get(e, t) {
|
|
4701
4812
|
let r = this.intents.get(e);
|
|
4702
4813
|
if (!r)
|
|
4703
|
-
throw new
|
|
4814
|
+
throw new l("not_found", `Mock intent ${e} not found`);
|
|
4704
4815
|
return { ...r };
|
|
4705
4816
|
}
|
|
4706
4817
|
async cancel(e, t) {
|
|
4707
4818
|
let r = this.intents.get(e);
|
|
4708
4819
|
if (!r)
|
|
4709
|
-
throw new
|
|
4820
|
+
throw new l("not_found", `Mock intent ${e} not found`);
|
|
4710
4821
|
let o = r.status === "cancelled";
|
|
4711
4822
|
return this.intents.set(e, { ...r, status: "cancelled" }), { ok: true, alreadyCancelled: o };
|
|
4712
4823
|
}
|
|
4713
4824
|
};
|
|
4714
|
-
function
|
|
4715
|
-
return n.env === "dev" ? new
|
|
4825
|
+
function dn(n) {
|
|
4826
|
+
return n.env === "dev" ? new Ze() : mo(n);
|
|
4716
4827
|
}
|
|
4717
4828
|
|
|
4718
4829
|
// src/webhooks.ts
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
var
|
|
4830
|
+
I();
|
|
4831
|
+
M();
|
|
4832
|
+
var pn = [
|
|
4722
4833
|
"subscription.activated",
|
|
4723
4834
|
"subscription.cancelled",
|
|
4724
4835
|
"subscription.payment_failed",
|
|
@@ -4728,17 +4839,17 @@ var Zt = [
|
|
|
4728
4839
|
"account.reactivated",
|
|
4729
4840
|
"support.ticket_replied"
|
|
4730
4841
|
];
|
|
4731
|
-
function
|
|
4842
|
+
function un(n) {
|
|
4732
4843
|
if (!n || typeof n != "object")
|
|
4733
|
-
throw new
|
|
4844
|
+
throw new l("invalid_response", "Webhook response is not an object");
|
|
4734
4845
|
let e = n;
|
|
4735
4846
|
if (typeof e.id != "string")
|
|
4736
|
-
throw new
|
|
4847
|
+
throw new l("invalid_response", "Webhook missing id");
|
|
4737
4848
|
return {
|
|
4738
4849
|
id: e.id,
|
|
4739
4850
|
url: typeof e.url == "string" ? e.url : "",
|
|
4740
4851
|
events: Array.isArray(e.events) ? e.events.filter(
|
|
4741
|
-
(t) =>
|
|
4852
|
+
(t) => pn.includes(t)
|
|
4742
4853
|
) : [],
|
|
4743
4854
|
hasSecret: e.hasSecret === true,
|
|
4744
4855
|
status: e.status === "active" || e.status === "degraded" || e.status === "disabled" ? e.status : "active",
|
|
@@ -4747,40 +4858,40 @@ function Yt(n) {
|
|
|
4747
4858
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
4748
4859
|
};
|
|
4749
4860
|
}
|
|
4750
|
-
function
|
|
4861
|
+
function fn(n) {
|
|
4751
4862
|
if (!n.url || typeof n.url != "string")
|
|
4752
|
-
throw new
|
|
4863
|
+
throw new l("validation_failed", "url \xE9 obrigat\xF3ria");
|
|
4753
4864
|
try {
|
|
4754
4865
|
let e = new URL(n.url);
|
|
4755
4866
|
if (e.protocol !== "https:" && e.protocol !== "http:")
|
|
4756
4867
|
throw new Error("invalid protocol");
|
|
4757
4868
|
} catch {
|
|
4758
|
-
throw new
|
|
4869
|
+
throw new l("validation_failed", `url inv\xE1lida: ${n.url}`);
|
|
4759
4870
|
}
|
|
4760
4871
|
if (!Array.isArray(n.events) || n.events.length === 0)
|
|
4761
|
-
throw new
|
|
4872
|
+
throw new l("validation_failed", "events deve ter pelo menos 1 evento");
|
|
4762
4873
|
for (let e of n.events)
|
|
4763
|
-
if (!
|
|
4764
|
-
throw new
|
|
4874
|
+
if (!pn.includes(e))
|
|
4875
|
+
throw new l("validation_failed", `evento desconhecido: ${e}`);
|
|
4765
4876
|
if (n.secret !== void 0 && n.secret.length < 16)
|
|
4766
|
-
throw new
|
|
4877
|
+
throw new l("validation_failed", "secret deve ter \u226516 chars (recomendado 32+)");
|
|
4767
4878
|
}
|
|
4768
|
-
function
|
|
4879
|
+
function ke(n, ...e) {
|
|
4769
4880
|
for (let t of e)
|
|
4770
4881
|
if (typeof t == "string" && t.length > 0)
|
|
4771
|
-
return
|
|
4882
|
+
return N("productId", t), t;
|
|
4772
4883
|
if (n.productId)
|
|
4773
|
-
return
|
|
4774
|
-
throw new
|
|
4884
|
+
return N("productId", n.productId), n.productId;
|
|
4885
|
+
throw new l(
|
|
4775
4886
|
"validation_failed",
|
|
4776
4887
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
4777
4888
|
);
|
|
4778
4889
|
}
|
|
4779
|
-
function
|
|
4890
|
+
function mn(n) {
|
|
4780
4891
|
return {
|
|
4781
4892
|
async register(e) {
|
|
4782
|
-
|
|
4783
|
-
let t =
|
|
4893
|
+
fn(e);
|
|
4894
|
+
let t = ke(n, e.productId), r = await v(n, {
|
|
4784
4895
|
method: "POST",
|
|
4785
4896
|
path: "/api/sdk/v1/webhooks",
|
|
4786
4897
|
body: {
|
|
@@ -4796,20 +4907,20 @@ function tn(n) {
|
|
|
4796
4907
|
// dedup TTL 24h (mesmo padrão de usage.report/support.createTicket).
|
|
4797
4908
|
idempotencyKey: true
|
|
4798
4909
|
});
|
|
4799
|
-
return
|
|
4910
|
+
return un(r);
|
|
4800
4911
|
},
|
|
4801
4912
|
async list(e) {
|
|
4802
|
-
let t =
|
|
4913
|
+
let t = ke(n, e?.productId), r = await v(n, {
|
|
4803
4914
|
method: "GET",
|
|
4804
4915
|
path: "/api/sdk/v1/webhooks",
|
|
4805
4916
|
query: { productId: t },
|
|
4806
4917
|
requireAuth: true
|
|
4807
4918
|
});
|
|
4808
|
-
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(
|
|
4919
|
+
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(un);
|
|
4809
4920
|
},
|
|
4810
4921
|
async unregister(e, t) {
|
|
4811
|
-
if (!e) throw new
|
|
4812
|
-
let r =
|
|
4922
|
+
if (!e) throw new l("validation_failed", "id obrigat\xF3rio");
|
|
4923
|
+
let r = ke(n, t?.productId);
|
|
4813
4924
|
return await v(n, {
|
|
4814
4925
|
method: "DELETE",
|
|
4815
4926
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}`,
|
|
@@ -4818,8 +4929,8 @@ function tn(n) {
|
|
|
4818
4929
|
}), { ok: true };
|
|
4819
4930
|
},
|
|
4820
4931
|
async test(e, t) {
|
|
4821
|
-
if (!e) throw new
|
|
4822
|
-
let r =
|
|
4932
|
+
if (!e) throw new l("validation_failed", "id obrigat\xF3rio");
|
|
4933
|
+
let r = ke(n, t?.productId), o = await v(n, {
|
|
4823
4934
|
method: "POST",
|
|
4824
4935
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}/test`,
|
|
4825
4936
|
query: { productId: r },
|
|
@@ -4838,11 +4949,11 @@ function tn(n) {
|
|
|
4838
4949
|
}
|
|
4839
4950
|
};
|
|
4840
4951
|
}
|
|
4841
|
-
var
|
|
4952
|
+
var Se = class {
|
|
4842
4953
|
endpoints = /* @__PURE__ */ new Map();
|
|
4843
4954
|
nextId = 1;
|
|
4844
4955
|
async register(e) {
|
|
4845
|
-
|
|
4956
|
+
fn(e);
|
|
4846
4957
|
let t = `mock_wh_${this.nextId++}`, r = {
|
|
4847
4958
|
id: t,
|
|
4848
4959
|
url: e.url,
|
|
@@ -4858,32 +4969,32 @@ var ve = class {
|
|
|
4858
4969
|
}
|
|
4859
4970
|
async unregister(e, t) {
|
|
4860
4971
|
if (!this.endpoints.has(e))
|
|
4861
|
-
throw new
|
|
4972
|
+
throw new l("not_found", `Webhook ${e} n\xE3o encontrado`);
|
|
4862
4973
|
return this.endpoints.delete(e), { ok: true };
|
|
4863
4974
|
}
|
|
4864
4975
|
async test(e, t) {
|
|
4865
4976
|
if (!this.endpoints.has(e))
|
|
4866
|
-
throw new
|
|
4977
|
+
throw new l("not_found", `Webhook ${e} n\xE3o encontrado`);
|
|
4867
4978
|
return { ok: true, statusCode: 200, durationMs: 42 };
|
|
4868
4979
|
}
|
|
4869
4980
|
};
|
|
4870
4981
|
|
|
4871
4982
|
// src/notifications.ts
|
|
4872
|
-
|
|
4873
|
-
|
|
4874
|
-
var
|
|
4983
|
+
I();
|
|
4984
|
+
M();
|
|
4985
|
+
var et = [
|
|
4875
4986
|
"info",
|
|
4876
4987
|
"success",
|
|
4877
4988
|
"warning",
|
|
4878
4989
|
"error"
|
|
4879
4990
|
];
|
|
4880
|
-
function
|
|
4991
|
+
function hn(n) {
|
|
4881
4992
|
if (!n || typeof n != "object")
|
|
4882
|
-
throw new
|
|
4993
|
+
throw new l("invalid_response", "Notification response is not an object");
|
|
4883
4994
|
let e = n;
|
|
4884
4995
|
if (typeof e.id != "string")
|
|
4885
|
-
throw new
|
|
4886
|
-
let t =
|
|
4996
|
+
throw new l("invalid_response", "Notification missing id");
|
|
4997
|
+
let t = et.includes(e.severity) ? e.severity : "info";
|
|
4887
4998
|
return {
|
|
4888
4999
|
id: e.id,
|
|
4889
5000
|
userId: typeof e.userId == "string" ? e.userId : "",
|
|
@@ -4898,37 +5009,37 @@ function nn(n) {
|
|
|
4898
5009
|
dismissedAt: typeof e.dismissedAt == "string" ? e.dismissedAt : void 0
|
|
4899
5010
|
};
|
|
4900
5011
|
}
|
|
4901
|
-
function
|
|
4902
|
-
if (!n.userId) throw new
|
|
4903
|
-
if (!n.kind) throw new
|
|
4904
|
-
if (!n.title) throw new
|
|
4905
|
-
if (n.severity && !
|
|
4906
|
-
throw new
|
|
5012
|
+
function gn(n) {
|
|
5013
|
+
if (!n.userId) throw new l("validation_failed", "userId obrigat\xF3rio");
|
|
5014
|
+
if (!n.kind) throw new l("validation_failed", "kind obrigat\xF3rio");
|
|
5015
|
+
if (!n.title) throw new l("validation_failed", "title obrigat\xF3rio");
|
|
5016
|
+
if (n.severity && !et.includes(n.severity))
|
|
5017
|
+
throw new l(
|
|
4907
5018
|
"validation_failed",
|
|
4908
|
-
`severity inv\xE1lida: ${n.severity} (use ${
|
|
5019
|
+
`severity inv\xE1lida: ${n.severity} (use ${et.join("|")})`
|
|
4909
5020
|
);
|
|
4910
5021
|
if (n.title.length > 200)
|
|
4911
|
-
throw new
|
|
5022
|
+
throw new l("validation_failed", "title m\xE1x 200 chars");
|
|
4912
5023
|
if (n.body && n.body.length > 2e3)
|
|
4913
|
-
throw new
|
|
5024
|
+
throw new l("validation_failed", "body m\xE1x 2000 chars");
|
|
4914
5025
|
}
|
|
4915
|
-
function
|
|
5026
|
+
function Te(n, ...e) {
|
|
4916
5027
|
for (let t of e)
|
|
4917
5028
|
if (typeof t == "string" && t.length > 0)
|
|
4918
|
-
return
|
|
5029
|
+
return N("productId", t), t;
|
|
4919
5030
|
if (n.productId)
|
|
4920
|
-
return
|
|
4921
|
-
throw new
|
|
5031
|
+
return N("productId", n.productId), n.productId;
|
|
5032
|
+
throw new l(
|
|
4922
5033
|
"validation_failed",
|
|
4923
5034
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
4924
5035
|
);
|
|
4925
5036
|
}
|
|
4926
|
-
function
|
|
5037
|
+
function yn(n) {
|
|
4927
5038
|
return {
|
|
4928
5039
|
async send(e) {
|
|
4929
|
-
|
|
5040
|
+
gn(e);
|
|
4930
5041
|
let r = {
|
|
4931
|
-
productId:
|
|
5042
|
+
productId: Te(n, e.productId),
|
|
4932
5043
|
userId: e.userId,
|
|
4933
5044
|
kind: e.kind,
|
|
4934
5045
|
title: e.title,
|
|
@@ -4942,12 +5053,12 @@ function on(n) {
|
|
|
4942
5053
|
requireAuth: true,
|
|
4943
5054
|
idempotencyKey: true
|
|
4944
5055
|
});
|
|
4945
|
-
return
|
|
5056
|
+
return hn(o);
|
|
4946
5057
|
},
|
|
4947
5058
|
async list(e, t) {
|
|
4948
|
-
if (!e) throw new
|
|
5059
|
+
if (!e) throw new l("validation_failed", "userId obrigat\xF3rio");
|
|
4949
5060
|
let o = {
|
|
4950
|
-
productId:
|
|
5061
|
+
productId: Te(n, t?.productId)
|
|
4951
5062
|
};
|
|
4952
5063
|
t?.includeDismissed && (o.includeDismissed = true), t?.onlyUnread && (o.onlyUnread = true), t?.limit && (o.limit = Math.min(Math.max(1, t.limit), 200));
|
|
4953
5064
|
let i = await v(n, {
|
|
@@ -4956,11 +5067,11 @@ function on(n) {
|
|
|
4956
5067
|
query: o,
|
|
4957
5068
|
requireAuth: true
|
|
4958
5069
|
});
|
|
4959
|
-
return (Array.isArray(i?.notifications) ? i.notifications : []).map(
|
|
5070
|
+
return (Array.isArray(i?.notifications) ? i.notifications : []).map(hn);
|
|
4960
5071
|
},
|
|
4961
5072
|
async markRead(e, t) {
|
|
4962
|
-
if (!e) throw new
|
|
4963
|
-
let r =
|
|
5073
|
+
if (!e) throw new l("validation_failed", "id obrigat\xF3rio");
|
|
5074
|
+
let r = Te(n, t?.productId);
|
|
4964
5075
|
return await v(n, {
|
|
4965
5076
|
method: "POST",
|
|
4966
5077
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/read`,
|
|
@@ -4969,8 +5080,8 @@ function on(n) {
|
|
|
4969
5080
|
}), { ok: true };
|
|
4970
5081
|
},
|
|
4971
5082
|
async dismiss(e, t) {
|
|
4972
|
-
if (!e) throw new
|
|
4973
|
-
let r =
|
|
5083
|
+
if (!e) throw new l("validation_failed", "id obrigat\xF3rio");
|
|
5084
|
+
let r = Te(n, t?.productId);
|
|
4974
5085
|
return await v(n, {
|
|
4975
5086
|
method: "POST",
|
|
4976
5087
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/dismiss`,
|
|
@@ -4980,11 +5091,11 @@ function on(n) {
|
|
|
4980
5091
|
}
|
|
4981
5092
|
};
|
|
4982
5093
|
}
|
|
4983
|
-
var
|
|
5094
|
+
var Re = class {
|
|
4984
5095
|
notifications = /* @__PURE__ */ new Map();
|
|
4985
5096
|
nextId = 1;
|
|
4986
5097
|
async send(e) {
|
|
4987
|
-
if (
|
|
5098
|
+
if (gn(e), e.fingerprint) {
|
|
4988
5099
|
let o = Date.now() - 864e5;
|
|
4989
5100
|
for (let i of this.notifications.values())
|
|
4990
5101
|
if ((i.metadata ?? {}).fingerprint === e.fingerprint && i.userId === e.userId && new Date(i.createdAt).getTime() > o)
|
|
@@ -5004,60 +5115,61 @@ var ke = class {
|
|
|
5004
5115
|
return this.notifications.set(t, r), r;
|
|
5005
5116
|
}
|
|
5006
5117
|
async list(e, t) {
|
|
5007
|
-
if (!e) throw new
|
|
5118
|
+
if (!e) throw new l("validation_failed", "userId obrigat\xF3rio");
|
|
5008
5119
|
let r = Math.min(Math.max(1, t?.limit ?? 50), 200);
|
|
5009
5120
|
return [...this.notifications.values()].filter((o) => o.userId === e).filter((o) => t?.includeDismissed || !o.dismissedAt).filter((o) => !t?.onlyUnread || !o.readAt).sort((o, i) => i.createdAt.localeCompare(o.createdAt)).slice(0, r);
|
|
5010
5121
|
}
|
|
5011
5122
|
async markRead(e, t) {
|
|
5012
5123
|
let r = this.notifications.get(e);
|
|
5013
|
-
if (!r) throw new
|
|
5124
|
+
if (!r) throw new l("not_found", `Notification ${e} n\xE3o encontrada`);
|
|
5014
5125
|
return r.readAt || (r.readAt = (/* @__PURE__ */ new Date()).toISOString(), this.notifications.set(e, r)), { ok: true };
|
|
5015
5126
|
}
|
|
5016
5127
|
async dismiss(e, t) {
|
|
5017
5128
|
let r = this.notifications.get(e);
|
|
5018
|
-
if (!r) throw new
|
|
5129
|
+
if (!r) throw new l("not_found", `Notification ${e} n\xE3o encontrada`);
|
|
5019
5130
|
return r.dismissedAt || (r.dismissedAt = (/* @__PURE__ */ new Date()).toISOString(), this.notifications.set(e, r)), { ok: true };
|
|
5020
5131
|
}
|
|
5021
5132
|
};
|
|
5022
5133
|
|
|
5023
5134
|
// src/ai.ts
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5135
|
+
I();
|
|
5136
|
+
M();
|
|
5137
|
+
Me();
|
|
5138
|
+
var ho = "/api/sdk/v1/ai/chat", _n = 12e4, Ie = 32768, bn = 32, vn = 4194304, wn = 2048, go = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, yo = /^https:\/\/[^\s]+$/, _o = /* @__PURE__ */ new Set(["low", "high", "auto"]), bo = /* @__PURE__ */ new Set(["system", "user", "assistant"]), kn = 100, Sn = 128, Tn = 32768, Rn = 0, In = 2, Cn = 200, An = 20, En = 12 * 1024 * 1024, Pn = 5e4, Dn = 16, Nn = 500, vo = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), wo = /^[a-zA-Z0-9_-]+$/;
|
|
5139
|
+
function k(n) {
|
|
5140
|
+
throw new l("validation_failed", n);
|
|
5141
|
+
}
|
|
5142
|
+
function ko(n) {
|
|
5031
5143
|
if (n.length === 0 || n.length % 4 !== 0) return false;
|
|
5032
5144
|
let e = n.endsWith("==") ? 2 : n.endsWith("=") ? 1 : 0;
|
|
5033
5145
|
return !(e > 0 ? n.slice(0, n.length - e) : n).includes("=");
|
|
5034
5146
|
}
|
|
5035
|
-
function
|
|
5147
|
+
function So(n) {
|
|
5036
5148
|
let e = n.indexOf(",");
|
|
5037
5149
|
if (e === -1) return 0;
|
|
5038
5150
|
let t = n.slice(e + 1), r = t.endsWith("==") ? 2 : t.endsWith("=") ? 1 : 0;
|
|
5039
5151
|
return Math.max(0, Math.floor(t.length * 3 / 4) - r);
|
|
5040
5152
|
}
|
|
5041
|
-
function
|
|
5153
|
+
function To(n) {
|
|
5042
5154
|
let e = 0, t = 0, r = 0;
|
|
5043
5155
|
for (let o of n) {
|
|
5044
5156
|
let i = o?.content;
|
|
5045
5157
|
if (Array.isArray(i))
|
|
5046
5158
|
for (let s of i) {
|
|
5047
|
-
if (e++, e >
|
|
5048
|
-
return `ai: total content parts exceeds ${
|
|
5159
|
+
if (e++, e > Cn)
|
|
5160
|
+
return `ai: total content parts exceeds ${Cn} per request`;
|
|
5049
5161
|
if (s && typeof s == "object" && s.type === "image_url") {
|
|
5050
|
-
if (t++, t >
|
|
5051
|
-
return `ai: total images exceeds ${
|
|
5162
|
+
if (t++, t > An)
|
|
5163
|
+
return `ai: total images exceeds ${An} per request`;
|
|
5052
5164
|
let a = s.image_url?.url;
|
|
5053
|
-
if (typeof a == "string" && a.startsWith("data:") && (r +=
|
|
5054
|
-
return `ai: aggregate image bytes exceeds ${
|
|
5165
|
+
if (typeof a == "string" && a.startsWith("data:") && (r += So(a), r > En))
|
|
5166
|
+
return `ai: aggregate image bytes exceeds ${En} per request`;
|
|
5055
5167
|
}
|
|
5056
5168
|
}
|
|
5057
5169
|
}
|
|
5058
5170
|
return null;
|
|
5059
5171
|
}
|
|
5060
|
-
function
|
|
5172
|
+
function Ro(n) {
|
|
5061
5173
|
if (n === void 0) return null;
|
|
5062
5174
|
if (n === null || typeof n != "object")
|
|
5063
5175
|
return "ai: responseFormat json_schema.schema must be an object";
|
|
@@ -5068,11 +5180,11 @@ function oo(n) {
|
|
|
5068
5180
|
return "ai: responseFormat json_schema.schema is not serializable";
|
|
5069
5181
|
}
|
|
5070
5182
|
if (e === void 0) return "ai: responseFormat json_schema.schema is invalid";
|
|
5071
|
-
if (e.length >
|
|
5072
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5183
|
+
if (e.length > Pn)
|
|
5184
|
+
return `ai: responseFormat json_schema.schema exceeds ${Pn} serialized chars`;
|
|
5073
5185
|
let t = 0, r = (o, i) => {
|
|
5074
|
-
if (i >
|
|
5075
|
-
return `ai: responseFormat json_schema.schema exceeds max depth ${
|
|
5186
|
+
if (i > Dn)
|
|
5187
|
+
return `ai: responseFormat json_schema.schema exceeds max depth ${Dn}`;
|
|
5076
5188
|
if (Array.isArray(o)) {
|
|
5077
5189
|
for (let s of o) {
|
|
5078
5190
|
let a = r(s, i + 1);
|
|
@@ -5082,10 +5194,10 @@ function oo(n) {
|
|
|
5082
5194
|
}
|
|
5083
5195
|
if (o && typeof o == "object")
|
|
5084
5196
|
for (let s of Object.getOwnPropertyNames(o)) {
|
|
5085
|
-
if (
|
|
5197
|
+
if (vo.has(s))
|
|
5086
5198
|
return `ai: responseFormat json_schema.schema has forbidden key: ${s}`;
|
|
5087
|
-
if (t++, t >
|
|
5088
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5199
|
+
if (t++, t > Nn)
|
|
5200
|
+
return `ai: responseFormat json_schema.schema exceeds ${Nn} keys`;
|
|
5089
5201
|
let a = r(o[s], i + 1);
|
|
5090
5202
|
if (a) return a;
|
|
5091
5203
|
}
|
|
@@ -5093,93 +5205,93 @@ function oo(n) {
|
|
|
5093
5205
|
};
|
|
5094
5206
|
return r(n, 0);
|
|
5095
5207
|
}
|
|
5096
|
-
function
|
|
5097
|
-
(typeof n != "string" || n.length === 0) &&
|
|
5208
|
+
function Io(n) {
|
|
5209
|
+
(typeof n != "string" || n.length === 0) && k("ai: image_url.url must be a non-empty string");
|
|
5098
5210
|
let e = n;
|
|
5099
5211
|
if (e.startsWith("data:")) {
|
|
5100
|
-
e.length >
|
|
5212
|
+
e.length > vn && k(`ai: image_url.url (data:) exceeds ${vn} chars`), go.test(e) || k("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
|
|
5101
5213
|
let t = e.slice(e.indexOf(",") + 1);
|
|
5102
|
-
|
|
5214
|
+
ko(t) || k("ai: image_url.url data: has invalid base64 (length/padding)");
|
|
5103
5215
|
return;
|
|
5104
5216
|
}
|
|
5105
5217
|
if (e.startsWith("https://")) {
|
|
5106
|
-
e.length >
|
|
5218
|
+
e.length > wn && k(`ai: image_url.url (https:) exceeds ${wn} chars`), yo.test(e) || k("ai: image_url.url https: is invalid");
|
|
5107
5219
|
return;
|
|
5108
5220
|
}
|
|
5109
|
-
|
|
5221
|
+
k("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
|
|
5110
5222
|
}
|
|
5111
|
-
function
|
|
5223
|
+
function Co(n, e) {
|
|
5112
5224
|
if (typeof n == "string") {
|
|
5113
|
-
n.length === 0 &&
|
|
5225
|
+
n.length === 0 && k("ai: message content string must be non-empty"), n.length > Ie && k(`ai: message content exceeds ${Ie} chars`);
|
|
5114
5226
|
return;
|
|
5115
5227
|
}
|
|
5116
|
-
(!Array.isArray(n) || n.length === 0) &&
|
|
5228
|
+
(!Array.isArray(n) || n.length === 0) && k("ai: message content must be a non-empty string or array of parts"), n.length > bn && k(`ai: message content has more than ${bn} parts`);
|
|
5117
5229
|
for (let t of n) {
|
|
5118
|
-
(!t || typeof t != "object") &&
|
|
5230
|
+
(!t || typeof t != "object") && k("ai: each content part must be an object");
|
|
5119
5231
|
let r = t.type;
|
|
5120
5232
|
if (r === "text") {
|
|
5121
5233
|
let o = t.text;
|
|
5122
|
-
(typeof o != "string" || o.length === 0) &&
|
|
5234
|
+
(typeof o != "string" || o.length === 0) && k("ai: text part must have non-empty { text: string }"), o.length > Ie && k(`ai: text part exceeds ${Ie} chars`);
|
|
5123
5235
|
} else if (r === "image_url") {
|
|
5124
|
-
e !== "user" &&
|
|
5236
|
+
e !== "user" && k(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
|
|
5125
5237
|
let o = t.image_url;
|
|
5126
|
-
(!o || typeof o != "object") &&
|
|
5238
|
+
(!o || typeof o != "object") && k("ai: image_url part must have { image_url: { url } }"), Io(o.url);
|
|
5127
5239
|
let i = o.detail;
|
|
5128
|
-
i !== void 0 && (typeof i != "string" || !
|
|
5240
|
+
i !== void 0 && (typeof i != "string" || !_o.has(i)) && k("ai: image_url.detail must be 'low' | 'high' | 'auto'");
|
|
5129
5241
|
} else
|
|
5130
|
-
|
|
5242
|
+
k("ai: content part type must be 'text' or 'image_url'");
|
|
5131
5243
|
}
|
|
5132
5244
|
}
|
|
5133
|
-
function
|
|
5134
|
-
(!n || typeof n != "object") &&
|
|
5245
|
+
function Ao(n) {
|
|
5246
|
+
(!n || typeof n != "object") && k("ai: responseFormat must be an object");
|
|
5135
5247
|
let e = n.type;
|
|
5136
5248
|
if (!(e === "text" || e === "json_object")) {
|
|
5137
5249
|
if (e === "json_schema") {
|
|
5138
5250
|
let t = n.json_schema;
|
|
5139
|
-
(!t || typeof t != "object") &&
|
|
5251
|
+
(!t || typeof t != "object") && k("ai: responseFormat json_schema requires { json_schema: { name } }");
|
|
5140
5252
|
let r = t, o = r.name;
|
|
5141
|
-
if ((typeof o != "string" || o.length === 0) &&
|
|
5142
|
-
let i =
|
|
5143
|
-
i &&
|
|
5253
|
+
if ((typeof o != "string" || o.length === 0) && k("ai: responseFormat json_schema.name is required"), o.length > 64 && k("ai: responseFormat json_schema.name exceeds 64 chars"), wo.test(o) || k("ai: responseFormat json_schema.name must match a-zA-Z0-9_-"), r.description !== void 0 && (typeof r.description != "string" && k("ai: responseFormat json_schema.description must be a string"), r.description.length > 1e3 && k("ai: responseFormat json_schema.description exceeds 1000 chars")), r.strict !== void 0 && typeof r.strict != "boolean" && k("ai: responseFormat json_schema.strict must be a boolean"), r.schema !== void 0) {
|
|
5254
|
+
let i = Ro(r.schema);
|
|
5255
|
+
i && k(i);
|
|
5144
5256
|
}
|
|
5145
5257
|
return;
|
|
5146
5258
|
}
|
|
5147
|
-
|
|
5259
|
+
k("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
|
|
5148
5260
|
}
|
|
5149
5261
|
}
|
|
5150
|
-
function
|
|
5262
|
+
function xn(n, e, t) {
|
|
5151
5263
|
if (!e || typeof e != "object")
|
|
5152
|
-
throw new
|
|
5264
|
+
throw new l("validation_failed", "ai: options object is required");
|
|
5153
5265
|
if (typeof e.model != "string" || e.model.length === 0)
|
|
5154
|
-
throw new
|
|
5155
|
-
if (e.model.length >
|
|
5156
|
-
throw new
|
|
5266
|
+
throw new l("validation_failed", "ai: model is required");
|
|
5267
|
+
if (e.model.length > Sn)
|
|
5268
|
+
throw new l("validation_failed", `ai: model exceeds ${Sn} chars`);
|
|
5157
5269
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5158
|
-
throw new
|
|
5159
|
-
if (e.messages.length >
|
|
5160
|
-
throw new
|
|
5270
|
+
throw new l("validation_failed", "ai: messages must be a non-empty array");
|
|
5271
|
+
if (e.messages.length > kn)
|
|
5272
|
+
throw new l("validation_failed", `ai: messages exceeds ${kn} per request`);
|
|
5161
5273
|
for (let a of e.messages) {
|
|
5162
|
-
if (!a || typeof a != "object" || typeof a.role != "string" || !
|
|
5163
|
-
throw new
|
|
5274
|
+
if (!a || typeof a != "object" || typeof a.role != "string" || !bo.has(a.role))
|
|
5275
|
+
throw new l(
|
|
5164
5276
|
"validation_failed",
|
|
5165
5277
|
"ai: each message role must be 'system' | 'user' | 'assistant'"
|
|
5166
5278
|
);
|
|
5167
|
-
|
|
5279
|
+
Co(a.content, a.role);
|
|
5168
5280
|
}
|
|
5169
|
-
let r =
|
|
5170
|
-
if (r) throw new
|
|
5281
|
+
let r = To(e.messages);
|
|
5282
|
+
if (r) throw new l("validation_failed", r);
|
|
5171
5283
|
let o = e.productId ?? n.productId, i = e.tenantId ?? n.tenantId;
|
|
5172
5284
|
if (!o)
|
|
5173
|
-
throw new
|
|
5285
|
+
throw new l(
|
|
5174
5286
|
"validation_failed",
|
|
5175
5287
|
"ai: productId required (pass to options or set on createNeetruClient)"
|
|
5176
5288
|
);
|
|
5177
5289
|
if (!i)
|
|
5178
|
-
throw new
|
|
5290
|
+
throw new l(
|
|
5179
5291
|
"validation_failed",
|
|
5180
5292
|
"ai: tenantId required (pass to options or set on createNeetruClient)"
|
|
5181
5293
|
);
|
|
5182
|
-
|
|
5294
|
+
N("productId", o), N("tenantId", i);
|
|
5183
5295
|
let s = {
|
|
5184
5296
|
productId: o,
|
|
5185
5297
|
tenantId: i,
|
|
@@ -5188,24 +5300,24 @@ function wn(n, e, t) {
|
|
|
5188
5300
|
stream: t
|
|
5189
5301
|
};
|
|
5190
5302
|
if (e.maxTokens !== void 0) {
|
|
5191
|
-
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens >
|
|
5192
|
-
throw new
|
|
5303
|
+
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > Tn)
|
|
5304
|
+
throw new l(
|
|
5193
5305
|
"validation_failed",
|
|
5194
|
-
`ai: maxTokens must be a positive integer <= ${
|
|
5306
|
+
`ai: maxTokens must be a positive integer <= ${Tn}`
|
|
5195
5307
|
);
|
|
5196
5308
|
s.maxTokens = e.maxTokens;
|
|
5197
5309
|
}
|
|
5198
5310
|
if (e.temperature !== void 0) {
|
|
5199
|
-
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature <
|
|
5200
|
-
throw new
|
|
5311
|
+
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature < Rn || e.temperature > In)
|
|
5312
|
+
throw new l(
|
|
5201
5313
|
"validation_failed",
|
|
5202
|
-
`ai: temperature must be a finite number between ${
|
|
5314
|
+
`ai: temperature must be a finite number between ${Rn} and ${In}`
|
|
5203
5315
|
);
|
|
5204
5316
|
s.temperature = e.temperature;
|
|
5205
5317
|
}
|
|
5206
|
-
return e.responseFormat !== void 0 && (
|
|
5318
|
+
return e.responseFormat !== void 0 && (Ao(e.responseFormat), s.responseFormat = e.responseFormat), s;
|
|
5207
5319
|
}
|
|
5208
|
-
async function
|
|
5320
|
+
async function Mn(n) {
|
|
5209
5321
|
try {
|
|
5210
5322
|
let e = await n.text();
|
|
5211
5323
|
return e ? JSON.parse(e) : void 0;
|
|
@@ -5213,48 +5325,48 @@ async function Rn(n) {
|
|
|
5213
5325
|
return;
|
|
5214
5326
|
}
|
|
5215
5327
|
}
|
|
5216
|
-
function
|
|
5328
|
+
function Eo(n, e) {
|
|
5217
5329
|
let t = n.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
|
|
5218
5330
|
return `${t}${r}`;
|
|
5219
5331
|
}
|
|
5220
|
-
async function
|
|
5332
|
+
async function On(n, e, t, r) {
|
|
5221
5333
|
if (!n.apiKey)
|
|
5222
|
-
throw new
|
|
5334
|
+
throw new l(
|
|
5223
5335
|
"missing_api_key",
|
|
5224
5336
|
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
5225
5337
|
);
|
|
5226
|
-
let o =
|
|
5338
|
+
let o = Eo(n.baseUrl, ho), { headers: i, correlationId: s } = ae({
|
|
5227
5339
|
"content-type": "application/json",
|
|
5228
5340
|
accept: r ? "text/event-stream" : "application/json",
|
|
5229
5341
|
authorization: `Bearer ${n.apiKey}`
|
|
5230
|
-
}, body: JSON.stringify(e) };
|
|
5231
|
-
r ? t && (
|
|
5232
|
-
let
|
|
5342
|
+
}), a = { method: "POST", headers: i, body: JSON.stringify(e) };
|
|
5343
|
+
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(_n);
|
|
5344
|
+
let c;
|
|
5233
5345
|
try {
|
|
5234
|
-
|
|
5235
|
-
} catch (
|
|
5236
|
-
let
|
|
5237
|
-
throw new
|
|
5238
|
-
}
|
|
5239
|
-
let
|
|
5240
|
-
if (!
|
|
5241
|
-
let
|
|
5242
|
-
if (
|
|
5243
|
-
let
|
|
5244
|
-
typeof
|
|
5346
|
+
c = await n.fetch(o, a);
|
|
5347
|
+
} catch (p) {
|
|
5348
|
+
let f = p instanceof DOMException && p.name === "TimeoutError" ? `Network error: timeout after ${_n / 1e3}s` : p instanceof DOMException && p.name === "AbortError" ? "Network error: request aborted" : `Network error: ${p instanceof Error ? p.message : "fetch failed"}`;
|
|
5349
|
+
throw new l("network_error", f, void 0, void 0, void 0, s);
|
|
5350
|
+
}
|
|
5351
|
+
let d = c.headers.get("x-request-id") ?? c.headers.get("x-correlation-id") ?? void 0;
|
|
5352
|
+
if (!c.ok) {
|
|
5353
|
+
let p = await Mn(c), f = le(c.status), _ = `HTTP ${c.status}`;
|
|
5354
|
+
if (p && typeof p == "object" && "error" in p) {
|
|
5355
|
+
let b = p.error;
|
|
5356
|
+
typeof b == "string" ? _ = b : b && typeof b == "object" && (typeof b.code == "string" && (f = b.code), typeof b.message == "string" && (_ = b.message));
|
|
5245
5357
|
}
|
|
5246
|
-
throw new
|
|
5358
|
+
throw new l(f, _, c.status, d, void 0, s);
|
|
5247
5359
|
}
|
|
5248
|
-
let
|
|
5249
|
-
return
|
|
5360
|
+
let u = c.headers.get("x-neetru-version");
|
|
5361
|
+
return u && ce(u), c;
|
|
5250
5362
|
}
|
|
5251
|
-
function
|
|
5363
|
+
function Ln(n) {
|
|
5252
5364
|
return {
|
|
5253
5365
|
provider: n.headers.get("x-neetru-provider") ?? "",
|
|
5254
5366
|
usageWarning: n.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
5255
5367
|
};
|
|
5256
5368
|
}
|
|
5257
|
-
function
|
|
5369
|
+
function Un(n) {
|
|
5258
5370
|
let e = [];
|
|
5259
5371
|
for (let r of n.split(`
|
|
5260
5372
|
`)) {
|
|
@@ -5275,12 +5387,12 @@ function Tn(n) {
|
|
|
5275
5387
|
return null;
|
|
5276
5388
|
}
|
|
5277
5389
|
}
|
|
5278
|
-
function
|
|
5390
|
+
function qn(n) {
|
|
5279
5391
|
return {
|
|
5280
5392
|
async chat(e) {
|
|
5281
|
-
let t =
|
|
5393
|
+
let t = xn(n, e, false), r = await On(n, t, e.signal, false), { provider: o, usageWarning: i } = Ln(r), s = await Mn(r);
|
|
5282
5394
|
if (!s || s.ok !== true || !Array.isArray(s.choices))
|
|
5283
|
-
throw new
|
|
5395
|
+
throw new l("invalid_response", "ai.chat response missing ok/choices");
|
|
5284
5396
|
return {
|
|
5285
5397
|
choices: s.choices,
|
|
5286
5398
|
usage: s.usage ?? { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
@@ -5290,38 +5402,38 @@ function In(n) {
|
|
|
5290
5402
|
};
|
|
5291
5403
|
},
|
|
5292
5404
|
stream(e) {
|
|
5293
|
-
let t =
|
|
5405
|
+
let t = xn(n, e, true), r = On(n, t, e.signal, true), o = r.then(Ln);
|
|
5294
5406
|
o.catch(() => {
|
|
5295
5407
|
});
|
|
5296
5408
|
async function* i() {
|
|
5297
5409
|
let a = (await r).body;
|
|
5298
5410
|
if (!a)
|
|
5299
|
-
throw new
|
|
5300
|
-
let
|
|
5411
|
+
throw new l("invalid_response", "ai.stream: response has no body");
|
|
5412
|
+
let c = a.getReader(), d = new TextDecoder(), u = "";
|
|
5301
5413
|
try {
|
|
5302
5414
|
for (; ; ) {
|
|
5303
|
-
let { done: f, value:
|
|
5415
|
+
let { done: f, value: _ } = await c.read();
|
|
5304
5416
|
if (f) break;
|
|
5305
|
-
u += d.decode(
|
|
5306
|
-
let
|
|
5307
|
-
for (; (
|
|
5417
|
+
u += d.decode(_, { stream: !0 });
|
|
5418
|
+
let b;
|
|
5419
|
+
for (; (b = u.indexOf(`
|
|
5308
5420
|
|
|
5309
5421
|
`)) !== -1; ) {
|
|
5310
|
-
let
|
|
5311
|
-
u = u.slice(
|
|
5312
|
-
let
|
|
5313
|
-
if (
|
|
5314
|
-
|
|
5422
|
+
let S = u.slice(0, b);
|
|
5423
|
+
u = u.slice(b + 2);
|
|
5424
|
+
let x = Un(S);
|
|
5425
|
+
if (x && "done" in x) return;
|
|
5426
|
+
x && "content" in x && (yield x.content);
|
|
5315
5427
|
}
|
|
5316
5428
|
}
|
|
5317
5429
|
let p = u.trim();
|
|
5318
5430
|
if (p) {
|
|
5319
|
-
let f =
|
|
5431
|
+
let f = Un(p);
|
|
5320
5432
|
f && "content" in f && (yield f.content);
|
|
5321
5433
|
}
|
|
5322
5434
|
} finally {
|
|
5323
5435
|
try {
|
|
5324
|
-
|
|
5436
|
+
c.releaseLock();
|
|
5325
5437
|
} catch {
|
|
5326
5438
|
}
|
|
5327
5439
|
try {
|
|
@@ -5339,10 +5451,10 @@ function In(n) {
|
|
|
5339
5451
|
}
|
|
5340
5452
|
|
|
5341
5453
|
// src/mocks.ts
|
|
5342
|
-
|
|
5454
|
+
I();
|
|
5343
5455
|
|
|
5344
5456
|
// src/runtime-env.ts
|
|
5345
|
-
function
|
|
5457
|
+
function Ce() {
|
|
5346
5458
|
try {
|
|
5347
5459
|
if (typeof process < "u" && process.env)
|
|
5348
5460
|
return process.env.NODE_ENV === "production";
|
|
@@ -5352,20 +5464,20 @@ function Te() {
|
|
|
5352
5464
|
}
|
|
5353
5465
|
|
|
5354
5466
|
// src/mocks.ts
|
|
5355
|
-
var
|
|
5467
|
+
var tt = Object.freeze({
|
|
5356
5468
|
uid: "dev-fixture-uid-0001",
|
|
5357
5469
|
email: "dev@neetru.local",
|
|
5358
5470
|
emailVerified: true,
|
|
5359
5471
|
displayName: "Dev Fixture",
|
|
5360
5472
|
isCustomer: true,
|
|
5361
5473
|
isStaff: false
|
|
5362
|
-
}),
|
|
5474
|
+
}), Ae = class {
|
|
5363
5475
|
_user;
|
|
5364
5476
|
_listeners;
|
|
5365
5477
|
_allowUnsignedDecode;
|
|
5366
5478
|
constructor(e = null, t) {
|
|
5367
|
-
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode &&
|
|
5368
|
-
throw new
|
|
5479
|
+
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode && Ce())
|
|
5480
|
+
throw new l(
|
|
5369
5481
|
"invalid_config",
|
|
5370
5482
|
"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."
|
|
5371
5483
|
);
|
|
@@ -5374,7 +5486,7 @@ var Ge = Object.freeze({
|
|
|
5374
5486
|
);
|
|
5375
5487
|
}
|
|
5376
5488
|
async signIn(e) {
|
|
5377
|
-
return this._user || (this._user = { ...
|
|
5489
|
+
return this._user || (this._user = { ...tt }), this._notify(), this._user;
|
|
5378
5490
|
}
|
|
5379
5491
|
async signOut() {
|
|
5380
5492
|
this._user = null, this._notify();
|
|
@@ -5406,9 +5518,9 @@ var Ge = Object.freeze({
|
|
|
5406
5518
|
if (this._verifyTokenOverride)
|
|
5407
5519
|
return this._verifyTokenOverride(e);
|
|
5408
5520
|
if (!e)
|
|
5409
|
-
throw new
|
|
5521
|
+
throw new l("token_invalid_signature", "Token inv\xE1lido: string vazia");
|
|
5410
5522
|
if (!this._allowUnsignedDecode)
|
|
5411
|
-
throw new
|
|
5523
|
+
throw new l(
|
|
5412
5524
|
"unauthorized",
|
|
5413
5525
|
"MockAuth.verifyToken n\xE3o verifica assinaturas JWT e N\xC3O deve ser usado como security boundary em c\xF3digo deployado. Em testes, use new MockAuth(user, { allowUnsignedDecode: true }) para habilitar decodifica\xE7\xE3o sem assinatura, ou __mockVerifyToken(fn) para injetar resultado customizado."
|
|
5414
5526
|
);
|
|
@@ -5434,7 +5546,7 @@ var Ge = Object.freeze({
|
|
|
5434
5546
|
} catch {
|
|
5435
5547
|
}
|
|
5436
5548
|
if (this._user) return this._user;
|
|
5437
|
-
throw new
|
|
5549
|
+
throw new l("token_invalid_signature", "Nenhum user mock dispon\xEDvel e JWT sem sub v\xE1lido");
|
|
5438
5550
|
}
|
|
5439
5551
|
/**
|
|
5440
5552
|
* Mock de `handleRedirectCallback` (Bug 2 / 3.1.3) — não há redirect real no
|
|
@@ -5495,7 +5607,7 @@ var Ge = Object.freeze({
|
|
|
5495
5607
|
} catch {
|
|
5496
5608
|
}
|
|
5497
5609
|
}
|
|
5498
|
-
},
|
|
5610
|
+
}, Ee = class {
|
|
5499
5611
|
_reports = [];
|
|
5500
5612
|
_quotas;
|
|
5501
5613
|
_counters = /* @__PURE__ */ new Map();
|
|
@@ -5512,14 +5624,14 @@ var Ge = Object.freeze({
|
|
|
5512
5624
|
tenantId: r?.tenantId,
|
|
5513
5625
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
5514
5626
|
});
|
|
5515
|
-
let
|
|
5627
|
+
let c = this._quotas.get(e)?.limit ?? -1;
|
|
5516
5628
|
return {
|
|
5517
5629
|
ok: true,
|
|
5518
5630
|
counterId: `mock_${e}`,
|
|
5519
5631
|
value: s,
|
|
5520
|
-
limit:
|
|
5521
|
-
remaining:
|
|
5522
|
-
status:
|
|
5632
|
+
limit: c,
|
|
5633
|
+
remaining: c < 0 ? -1 : Math.max(0, c - s),
|
|
5634
|
+
status: c > 0 && s >= c ? "read_only_overlimit" : "ok"
|
|
5523
5635
|
};
|
|
5524
5636
|
}
|
|
5525
5637
|
async check(e, t) {
|
|
@@ -5557,14 +5669,14 @@ var Ge = Object.freeze({
|
|
|
5557
5669
|
__reset() {
|
|
5558
5670
|
this._reports = [], this._quotas.clear(), this._counters.clear();
|
|
5559
5671
|
}
|
|
5560
|
-
},
|
|
5672
|
+
}, Pe = class {
|
|
5561
5673
|
_tickets = [];
|
|
5562
5674
|
_ticketSeq = 0;
|
|
5563
5675
|
async createTicket(e) {
|
|
5564
|
-
if (!e?.subject) throw new
|
|
5565
|
-
if (!e?.message) throw new
|
|
5676
|
+
if (!e?.subject) throw new l("validation_failed", "subject required");
|
|
5677
|
+
if (!e?.message) throw new l("validation_failed", "message required");
|
|
5566
5678
|
if (!e?.productSlug)
|
|
5567
|
-
throw new
|
|
5679
|
+
throw new l(
|
|
5568
5680
|
"validation_failed",
|
|
5569
5681
|
"productSlug required (3.0 breaking \u2014 pass to input or set on createNeetruClient)"
|
|
5570
5682
|
);
|
|
@@ -5596,10 +5708,10 @@ var Ge = Object.freeze({
|
|
|
5596
5708
|
}
|
|
5597
5709
|
_find(e) {
|
|
5598
5710
|
if (!e || typeof e != "string")
|
|
5599
|
-
throw new
|
|
5711
|
+
throw new l("validation_failed", "ticketId is required");
|
|
5600
5712
|
let t = this._tickets.find((r) => r.id === e);
|
|
5601
5713
|
if (!t)
|
|
5602
|
-
throw new
|
|
5714
|
+
throw new l("not_found", `ticket ${e} not found`);
|
|
5603
5715
|
return t;
|
|
5604
5716
|
}
|
|
5605
5717
|
/** 3.4: espelha `markResolvedSeen` — idempotente (só seta se ainda null). */
|
|
@@ -5615,18 +5727,18 @@ var Ge = Object.freeze({
|
|
|
5615
5727
|
/** 3.4: espelha `submitSatisfaction` — terminal, seta `satisfiedAt`. */
|
|
5616
5728
|
async submitSatisfaction(e) {
|
|
5617
5729
|
if (!e || typeof e != "object")
|
|
5618
|
-
throw new
|
|
5730
|
+
throw new l("validation_failed", "input is required");
|
|
5619
5731
|
if (e.rating !== null && e.rating !== void 0 && (!Number.isInteger(e.rating) || e.rating < 1 || e.rating > 5))
|
|
5620
|
-
throw new
|
|
5732
|
+
throw new l("validation_failed", "rating must be an integer 1-5 or null");
|
|
5621
5733
|
if (typeof e.comment == "string" && e.comment.length > 2e3)
|
|
5622
|
-
throw new
|
|
5734
|
+
throw new l("validation_failed", "comment max 2000 chars");
|
|
5623
5735
|
let t = this._find(e.ticketId);
|
|
5624
5736
|
t.satisfiedAt = (/* @__PURE__ */ new Date()).toISOString(), t.rating = e.rating ?? null, t.satisfactionComment = e.comment ?? null;
|
|
5625
5737
|
}
|
|
5626
5738
|
/** 3.4: espelha `contestTicket` — reabre e zera o ciclo de satisfação. */
|
|
5627
5739
|
async contestTicket(e, t) {
|
|
5628
5740
|
if (typeof t?.comment == "string" && t.comment.length > 2e3)
|
|
5629
|
-
throw new
|
|
5741
|
+
throw new l("validation_failed", "comment max 2000 chars");
|
|
5630
5742
|
let r = this._find(e);
|
|
5631
5743
|
r.status = "open", r.resolvedAt = null, r.resolvedNotifiedAt = null, r.satisfactionAttempts = 0, r.lastSatisfactionAttemptAt = null, r.satisfiedAt = null, r.rating = null, r.satisfactionComment = null;
|
|
5632
5744
|
}
|
|
@@ -5634,7 +5746,7 @@ var Ge = Object.freeze({
|
|
|
5634
5746
|
__reset() {
|
|
5635
5747
|
this._tickets = [], this._ticketSeq = 0;
|
|
5636
5748
|
}
|
|
5637
|
-
},
|
|
5749
|
+
}, De = class {
|
|
5638
5750
|
_denied = /* @__PURE__ */ new Set();
|
|
5639
5751
|
async check(e, t) {
|
|
5640
5752
|
return !this._denied.has(`${e}:${t}`);
|
|
@@ -5657,7 +5769,7 @@ var Ge = Object.freeze({
|
|
|
5657
5769
|
this._denied.clear();
|
|
5658
5770
|
}
|
|
5659
5771
|
};
|
|
5660
|
-
var
|
|
5772
|
+
var Ne = class {
|
|
5661
5773
|
_calls = [];
|
|
5662
5774
|
/** Extrai texto de um content string OU array multimodal (imagens ignoradas). */
|
|
5663
5775
|
_contentToText(e) {
|
|
@@ -5669,9 +5781,9 @@ var Pe = class {
|
|
|
5669
5781
|
}
|
|
5670
5782
|
async chat(e) {
|
|
5671
5783
|
if (!e || typeof e.model != "string" || e.model.length === 0)
|
|
5672
|
-
throw new
|
|
5784
|
+
throw new l("validation_failed", "ai: model is required");
|
|
5673
5785
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5674
|
-
throw new
|
|
5786
|
+
throw new l("validation_failed", "ai: messages must be a non-empty array");
|
|
5675
5787
|
this._calls.push({ model: e.model, messages: e.messages, stream: false });
|
|
5676
5788
|
let t = this._reply(e), r = e.messages.reduce((i, s) => i + this._contentToText(s.content).length, 0), o = t.length;
|
|
5677
5789
|
return {
|
|
@@ -5688,9 +5800,9 @@ var Pe = class {
|
|
|
5688
5800
|
}
|
|
5689
5801
|
stream(e) {
|
|
5690
5802
|
if (!e || typeof e.model != "string" || e.model.length === 0)
|
|
5691
|
-
throw new
|
|
5803
|
+
throw new l("validation_failed", "ai: model is required");
|
|
5692
5804
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5693
|
-
throw new
|
|
5805
|
+
throw new l("validation_failed", "ai: messages must be a non-empty array");
|
|
5694
5806
|
this._calls.push({ model: e.model, messages: e.messages, stream: true });
|
|
5695
5807
|
let t = this._reply(e), r = t.match(/\S+\s*/g) ?? [t], o = Promise.resolve({ provider: "mock", usageWarning: false });
|
|
5696
5808
|
async function* i() {
|
|
@@ -5705,46 +5817,46 @@ var Pe = class {
|
|
|
5705
5817
|
};
|
|
5706
5818
|
|
|
5707
5819
|
// src/auth-pkce-shared.ts
|
|
5708
|
-
|
|
5709
|
-
function
|
|
5820
|
+
I();
|
|
5821
|
+
function Fn() {
|
|
5710
5822
|
let n = globalThis.crypto;
|
|
5711
5823
|
if (!n || typeof n.getRandomValues != "function" || !n.subtle)
|
|
5712
|
-
throw new
|
|
5824
|
+
throw new l(
|
|
5713
5825
|
"runtime_unsupported",
|
|
5714
5826
|
"auth.signIn requer WebCrypto (crypto.subtle) \u2014 indispon\xEDvel neste runtime. Use HTTPS (ou http://localhost) num browser moderno, ou instale um polyfill WebCrypto (ex: react-native-quick-crypto) em React Native."
|
|
5715
5827
|
);
|
|
5716
5828
|
return n;
|
|
5717
5829
|
}
|
|
5718
|
-
function
|
|
5830
|
+
function jn(n) {
|
|
5719
5831
|
let e = "";
|
|
5720
5832
|
for (let r of n) e += String.fromCharCode(r);
|
|
5721
5833
|
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
5722
5834
|
}
|
|
5723
|
-
function
|
|
5835
|
+
function xe(n, e) {
|
|
5724
5836
|
let t = new Uint8Array(e);
|
|
5725
|
-
return n.getRandomValues(t),
|
|
5837
|
+
return n.getRandomValues(t), jn(t);
|
|
5726
5838
|
}
|
|
5727
|
-
async function
|
|
5839
|
+
async function $n(n, e) {
|
|
5728
5840
|
let t = new TextEncoder().encode(e), r = await n.subtle.digest("SHA-256", t);
|
|
5729
|
-
return
|
|
5841
|
+
return jn(new Uint8Array(r));
|
|
5730
5842
|
}
|
|
5731
|
-
function
|
|
5843
|
+
function oe(n) {
|
|
5732
5844
|
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.replace(/^https?:\/\/api\./, "https://auth.");
|
|
5733
5845
|
}
|
|
5734
|
-
function
|
|
5846
|
+
function Wn(n) {
|
|
5735
5847
|
let e = n.oidcClientId;
|
|
5736
|
-
if (!e && n.apiKey && (e =
|
|
5737
|
-
throw new
|
|
5848
|
+
if (!e && n.apiKey && (e = ct(n.apiKey).keyId), !e)
|
|
5849
|
+
throw new l(
|
|
5738
5850
|
"invalid_config",
|
|
5739
5851
|
"auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
|
|
5740
5852
|
);
|
|
5741
5853
|
return e;
|
|
5742
5854
|
}
|
|
5743
|
-
function
|
|
5855
|
+
function Bn(n) {
|
|
5744
5856
|
let e = new URL("/api/v1/oauth/authorize", n.idpOrigin);
|
|
5745
5857
|
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;
|
|
5746
5858
|
}
|
|
5747
|
-
function
|
|
5859
|
+
function nt(n) {
|
|
5748
5860
|
let e = n.split(".");
|
|
5749
5861
|
if (e.length !== 3) return null;
|
|
5750
5862
|
try {
|
|
@@ -5754,8 +5866,8 @@ function Je(n) {
|
|
|
5754
5866
|
return null;
|
|
5755
5867
|
}
|
|
5756
5868
|
}
|
|
5757
|
-
function
|
|
5758
|
-
let e =
|
|
5869
|
+
function ie(n) {
|
|
5870
|
+
let e = nt(n);
|
|
5759
5871
|
return !e || typeof e.sub != "string" || typeof e.exp == "number" && e.exp * 1e3 < Date.now() ? null : {
|
|
5760
5872
|
uid: e.sub,
|
|
5761
5873
|
email: typeof e.email == "string" ? e.email : "",
|
|
@@ -5768,9 +5880,68 @@ function re(n) {
|
|
|
5768
5880
|
...e
|
|
5769
5881
|
};
|
|
5770
5882
|
}
|
|
5883
|
+
var Po = "/api/v1/oauth/revoke";
|
|
5884
|
+
async function Vn(n, e, t, r) {
|
|
5885
|
+
let o = `${e}${Po}`, i = true;
|
|
5886
|
+
for (let s of r) {
|
|
5887
|
+
if (!s.token) continue;
|
|
5888
|
+
let a = new URLSearchParams({
|
|
5889
|
+
token: s.token,
|
|
5890
|
+
token_type_hint: s.hint,
|
|
5891
|
+
client_id: t
|
|
5892
|
+
}).toString(), c = false;
|
|
5893
|
+
for (let d = 0; d < 2 && !c; d++)
|
|
5894
|
+
try {
|
|
5895
|
+
if (c = (await n(o, {
|
|
5896
|
+
method: "POST",
|
|
5897
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
5898
|
+
body: a
|
|
5899
|
+
})).ok, !c) break;
|
|
5900
|
+
} catch {
|
|
5901
|
+
d === 0 && await new Promise((u) => setTimeout(u, 500));
|
|
5902
|
+
}
|
|
5903
|
+
c || (i = false);
|
|
5904
|
+
}
|
|
5905
|
+
return i;
|
|
5906
|
+
}
|
|
5771
5907
|
|
|
5772
5908
|
// src/auth.ts
|
|
5773
|
-
|
|
5909
|
+
qe();
|
|
5910
|
+
|
|
5911
|
+
// src/end-user-token.ts
|
|
5912
|
+
var Do = /* @__PURE__ */ new WeakMap();
|
|
5913
|
+
function Kn(n, e) {
|
|
5914
|
+
Do.set(n, e);
|
|
5915
|
+
}
|
|
5916
|
+
|
|
5917
|
+
// src/unsupported-db.ts
|
|
5918
|
+
I();
|
|
5919
|
+
var No = "[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.)", xo = /* @__PURE__ */ new Set([
|
|
5920
|
+
"then",
|
|
5921
|
+
"toJSON",
|
|
5922
|
+
"valueOf",
|
|
5923
|
+
"constructor",
|
|
5924
|
+
"$$typeof",
|
|
5925
|
+
"asymmetricMatch",
|
|
5926
|
+
"nodeType"
|
|
5927
|
+
]);
|
|
5928
|
+
function zn() {
|
|
5929
|
+
let n = () => {
|
|
5930
|
+
throw new l("runtime_unsupported", No, void 0, void 0, "react-native");
|
|
5931
|
+
}, e = {};
|
|
5932
|
+
return new Proxy(e, {
|
|
5933
|
+
get(t, r) {
|
|
5934
|
+
if (!(typeof r == "symbol" || xo.has(r)))
|
|
5935
|
+
return r === "toString" ? () => "[NeetruDb indispon\xEDvel em React Native]" : n;
|
|
5936
|
+
},
|
|
5937
|
+
set() {
|
|
5938
|
+
return n(), false;
|
|
5939
|
+
}
|
|
5940
|
+
});
|
|
5941
|
+
}
|
|
5942
|
+
|
|
5943
|
+
// src/auth.ts
|
|
5944
|
+
function Lo() {
|
|
5774
5945
|
try {
|
|
5775
5946
|
let e = globalThis.process?.env?.NEETRU_API_KEY;
|
|
5776
5947
|
return typeof e == "string" && e.length > 0 ? e : void 0;
|
|
@@ -5778,7 +5949,7 @@ function po() {
|
|
|
5778
5949
|
return;
|
|
5779
5950
|
}
|
|
5780
5951
|
}
|
|
5781
|
-
function
|
|
5952
|
+
function Uo(n) {
|
|
5782
5953
|
if (n) return n;
|
|
5783
5954
|
try {
|
|
5784
5955
|
let t = globalThis.process?.env?.NEETRU_ENV;
|
|
@@ -5792,18 +5963,11 @@ function fo(n) {
|
|
|
5792
5963
|
}
|
|
5793
5964
|
return "prod";
|
|
5794
5965
|
}
|
|
5795
|
-
var
|
|
5796
|
-
function
|
|
5797
|
-
|
|
5798
|
-
if (r && t - r.createdAt < e)
|
|
5799
|
-
return r.jwks;
|
|
5800
|
-
let o = createRemoteJWKSet(new URL(n));
|
|
5801
|
-
return Ye.set(n, { jwks: o, createdAt: t }), o;
|
|
5802
|
-
}
|
|
5803
|
-
function vs() {
|
|
5804
|
-
Ye.clear();
|
|
5966
|
+
var Mo = it;
|
|
5967
|
+
function ra() {
|
|
5968
|
+
st();
|
|
5805
5969
|
}
|
|
5806
|
-
function
|
|
5970
|
+
function qo(n) {
|
|
5807
5971
|
let e = n.sub ?? "", t = typeof n.email == "string" ? n.email : "";
|
|
5808
5972
|
return {
|
|
5809
5973
|
uid: e,
|
|
@@ -5818,8 +5982,8 @@ function go(n) {
|
|
|
5818
5982
|
...n
|
|
5819
5983
|
};
|
|
5820
5984
|
}
|
|
5821
|
-
var
|
|
5822
|
-
function
|
|
5985
|
+
var se = "neetru_oauth_tx";
|
|
5986
|
+
function rt(n) {
|
|
5823
5987
|
try {
|
|
5824
5988
|
let e = globalThis.history;
|
|
5825
5989
|
if (!e || typeof e.replaceState != "function") return;
|
|
@@ -5830,7 +5994,7 @@ function Xe(n) {
|
|
|
5830
5994
|
} catch {
|
|
5831
5995
|
}
|
|
5832
5996
|
}
|
|
5833
|
-
function
|
|
5997
|
+
function Fo(n) {
|
|
5834
5998
|
let e = "neetru_id_token", t = "neetru_refresh_token", r = /* @__PURE__ */ new Set(), o = null, i = null, s = null;
|
|
5835
5999
|
function a() {
|
|
5836
6000
|
try {
|
|
@@ -5839,7 +6003,7 @@ function yo(n) {
|
|
|
5839
6003
|
return null;
|
|
5840
6004
|
}
|
|
5841
6005
|
}
|
|
5842
|
-
function
|
|
6006
|
+
function c() {
|
|
5843
6007
|
try {
|
|
5844
6008
|
return typeof globalThis.sessionStorage < "u" ? globalThis.sessionStorage : null;
|
|
5845
6009
|
} catch {
|
|
@@ -5848,21 +6012,21 @@ function yo(n) {
|
|
|
5848
6012
|
}
|
|
5849
6013
|
function d() {
|
|
5850
6014
|
if (o) {
|
|
5851
|
-
let
|
|
5852
|
-
return typeof
|
|
6015
|
+
let h = o.exp;
|
|
6016
|
+
return typeof h == "number" && h * 1e3 < Date.now() ? (o = null, a()?.removeItem(e), u(), S(), null) : o;
|
|
5853
6017
|
}
|
|
5854
|
-
let
|
|
5855
|
-
if (!
|
|
5856
|
-
let
|
|
5857
|
-
if (!
|
|
5858
|
-
return
|
|
5859
|
-
let
|
|
5860
|
-
return
|
|
6018
|
+
let m = a();
|
|
6019
|
+
if (!m) return null;
|
|
6020
|
+
let g = m.getItem(e);
|
|
6021
|
+
if (!g)
|
|
6022
|
+
return S(), null;
|
|
6023
|
+
let w = ie(g);
|
|
6024
|
+
return w ? (o = w, x(w), o) : (m.removeItem(e), S(), null);
|
|
5861
6025
|
}
|
|
5862
6026
|
function u() {
|
|
5863
|
-
for (let
|
|
6027
|
+
for (let m of r)
|
|
5864
6028
|
try {
|
|
5865
|
-
|
|
6029
|
+
m(o);
|
|
5866
6030
|
} catch {
|
|
5867
6031
|
}
|
|
5868
6032
|
}
|
|
@@ -5872,297 +6036,296 @@ function yo(n) {
|
|
|
5872
6036
|
function f() {
|
|
5873
6037
|
return a()?.getItem(t) ?? null;
|
|
5874
6038
|
}
|
|
5875
|
-
function
|
|
6039
|
+
function _() {
|
|
5876
6040
|
o = null;
|
|
5877
|
-
let
|
|
5878
|
-
|
|
6041
|
+
let m = a();
|
|
6042
|
+
m?.removeItem(e), m?.removeItem(t), u();
|
|
5879
6043
|
}
|
|
5880
|
-
function
|
|
6044
|
+
function b() {
|
|
5881
6045
|
return s || (s = (async () => {
|
|
5882
|
-
let
|
|
5883
|
-
if (!
|
|
5884
|
-
let
|
|
6046
|
+
let m = f();
|
|
6047
|
+
if (!m) return false;
|
|
6048
|
+
let g;
|
|
5885
6049
|
try {
|
|
5886
|
-
|
|
6050
|
+
g = T();
|
|
5887
6051
|
} catch {
|
|
5888
6052
|
return false;
|
|
5889
6053
|
}
|
|
5890
|
-
let
|
|
6054
|
+
let h = `${oe(n.baseUrl)}/api/v1/oauth/token`, C = new URLSearchParams({
|
|
5891
6055
|
grant_type: "refresh_token",
|
|
5892
|
-
refresh_token:
|
|
5893
|
-
client_id:
|
|
5894
|
-
}),
|
|
6056
|
+
refresh_token: m,
|
|
6057
|
+
client_id: g
|
|
6058
|
+
}), D;
|
|
5895
6059
|
try {
|
|
5896
|
-
|
|
6060
|
+
D = await n.fetch(h, {
|
|
5897
6061
|
method: "POST",
|
|
5898
6062
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
5899
|
-
body:
|
|
6063
|
+
body: C.toString()
|
|
5900
6064
|
});
|
|
5901
6065
|
} catch {
|
|
5902
6066
|
return false;
|
|
5903
6067
|
}
|
|
5904
|
-
let
|
|
6068
|
+
let E = null;
|
|
5905
6069
|
try {
|
|
5906
|
-
|
|
6070
|
+
E = await D.json();
|
|
5907
6071
|
} catch {
|
|
5908
|
-
|
|
6072
|
+
E = null;
|
|
5909
6073
|
}
|
|
5910
|
-
if (!
|
|
5911
|
-
let
|
|
5912
|
-
if (typeof
|
|
5913
|
-
let
|
|
5914
|
-
if (!
|
|
5915
|
-
let
|
|
5916
|
-
|
|
5917
|
-
let
|
|
5918
|
-
return typeof
|
|
6074
|
+
if (!D.ok || typeof E?.error == "string") return false;
|
|
6075
|
+
let R = E?.id_token;
|
|
6076
|
+
if (typeof R != "string" || R.length === 0) return false;
|
|
6077
|
+
let O = ie(R);
|
|
6078
|
+
if (!O) return false;
|
|
6079
|
+
let A = a();
|
|
6080
|
+
A?.setItem(e, R);
|
|
6081
|
+
let U = E?.refresh_token;
|
|
6082
|
+
return typeof U == "string" && U.length > 0 && A?.setItem(t, U), o = O, x(O), u(), true;
|
|
5919
6083
|
})().finally(() => {
|
|
5920
6084
|
s = null;
|
|
5921
6085
|
}), s);
|
|
5922
6086
|
}
|
|
5923
|
-
function
|
|
5924
|
-
f() &&
|
|
5925
|
-
|
|
6087
|
+
function S() {
|
|
6088
|
+
f() && b().then((m) => {
|
|
6089
|
+
m || _();
|
|
5926
6090
|
});
|
|
5927
6091
|
}
|
|
5928
|
-
function
|
|
6092
|
+
function x(m) {
|
|
5929
6093
|
p();
|
|
5930
|
-
let
|
|
5931
|
-
if (typeof
|
|
5932
|
-
let
|
|
5933
|
-
if (!(
|
|
6094
|
+
let g = m.exp;
|
|
6095
|
+
if (typeof g != "number") return;
|
|
6096
|
+
let w = g * 1e3 - Date.now();
|
|
6097
|
+
if (!(w <= 0)) {
|
|
5934
6098
|
if (f()) {
|
|
5935
|
-
let
|
|
6099
|
+
let h = Math.min(w, Math.max(3e4, w * 0.2)), C = Math.max(1, w - h);
|
|
5936
6100
|
i = setTimeout(() => {
|
|
5937
|
-
i = null,
|
|
5938
|
-
|
|
6101
|
+
i = null, b().then((D) => {
|
|
6102
|
+
D || _();
|
|
5939
6103
|
});
|
|
5940
|
-
},
|
|
6104
|
+
}, C);
|
|
5941
6105
|
return;
|
|
5942
6106
|
}
|
|
5943
6107
|
i = setTimeout(() => {
|
|
5944
6108
|
i = null, d();
|
|
5945
|
-
},
|
|
6109
|
+
}, w + 1);
|
|
5946
6110
|
}
|
|
5947
6111
|
}
|
|
5948
|
-
function
|
|
5949
|
-
return
|
|
6112
|
+
function T() {
|
|
6113
|
+
return Wn(n);
|
|
5950
6114
|
}
|
|
5951
|
-
async function
|
|
5952
|
-
let
|
|
5953
|
-
if (!
|
|
5954
|
-
let
|
|
6115
|
+
async function P(m) {
|
|
6116
|
+
let g = m ?? (typeof globalThis.location < "u" ? globalThis.location.href : void 0);
|
|
6117
|
+
if (!g) return null;
|
|
6118
|
+
let w;
|
|
5955
6119
|
try {
|
|
5956
|
-
|
|
6120
|
+
w = new URL(g);
|
|
5957
6121
|
} catch {
|
|
5958
6122
|
return null;
|
|
5959
6123
|
}
|
|
5960
|
-
let
|
|
5961
|
-
if (
|
|
5962
|
-
let
|
|
5963
|
-
throw
|
|
6124
|
+
let h = w.searchParams, C = h.get("error");
|
|
6125
|
+
if (C) {
|
|
6126
|
+
let U = h.get("error_description") ?? C;
|
|
6127
|
+
throw c()?.removeItem(se), rt(w), new l(C, `OIDC authorize falhou: ${U}`);
|
|
5964
6128
|
}
|
|
5965
|
-
let
|
|
5966
|
-
if (!
|
|
5967
|
-
let
|
|
5968
|
-
if (
|
|
6129
|
+
let D = h.get("code");
|
|
6130
|
+
if (!D) return null;
|
|
6131
|
+
let E = h.get("state"), R = c(), O = R?.getItem(se) ?? null, A = null;
|
|
6132
|
+
if (O)
|
|
5969
6133
|
try {
|
|
5970
|
-
|
|
6134
|
+
A = JSON.parse(O);
|
|
5971
6135
|
} catch {
|
|
5972
|
-
|
|
6136
|
+
A = null;
|
|
5973
6137
|
}
|
|
5974
|
-
if (!
|
|
5975
|
-
throw new
|
|
6138
|
+
if (!A)
|
|
6139
|
+
throw new l(
|
|
5976
6140
|
"oidc_state_mismatch",
|
|
5977
6141
|
"Callback OIDC sem transa\xE7\xE3o pendente \u2014 `signIn` n\xE3o foi iniciado nesta sess\xE3o (ou j\xE1 consumido)."
|
|
5978
6142
|
);
|
|
5979
|
-
if (!
|
|
5980
|
-
throw
|
|
6143
|
+
if (!E || E !== A.state)
|
|
6144
|
+
throw R?.removeItem(se), rt(w), new l(
|
|
5981
6145
|
"oidc_state_mismatch",
|
|
5982
6146
|
"state do callback OIDC n\xE3o confere com o da transa\xE7\xE3o (poss\xEDvel CSRF)."
|
|
5983
6147
|
);
|
|
5984
|
-
return
|
|
5985
|
-
code:
|
|
5986
|
-
codeVerifier:
|
|
5987
|
-
redirectUri:
|
|
5988
|
-
nonce:
|
|
5989
|
-
state:
|
|
5990
|
-
postLoginRedirect:
|
|
6148
|
+
return R?.removeItem(se), rt(w), {
|
|
6149
|
+
code: D,
|
|
6150
|
+
codeVerifier: A.codeVerifier,
|
|
6151
|
+
redirectUri: A.redirectUri,
|
|
6152
|
+
nonce: A.nonce,
|
|
6153
|
+
state: A.state,
|
|
6154
|
+
postLoginRedirect: A.postLoginRedirect
|
|
5991
6155
|
};
|
|
5992
6156
|
}
|
|
5993
|
-
async function
|
|
5994
|
-
let
|
|
5995
|
-
if (!
|
|
5996
|
-
let
|
|
6157
|
+
async function q(m) {
|
|
6158
|
+
let g = await P(m);
|
|
6159
|
+
if (!g) return null;
|
|
6160
|
+
let w = T(), C = `${oe(n.baseUrl)}/api/v1/oauth/token`, D = new URLSearchParams({
|
|
5997
6161
|
grant_type: "authorization_code",
|
|
5998
|
-
code:
|
|
5999
|
-
redirect_uri:
|
|
6000
|
-
client_id:
|
|
6001
|
-
code_verifier:
|
|
6002
|
-
}),
|
|
6162
|
+
code: g.code,
|
|
6163
|
+
redirect_uri: g.redirectUri,
|
|
6164
|
+
client_id: w,
|
|
6165
|
+
code_verifier: g.codeVerifier
|
|
6166
|
+
}), E;
|
|
6003
6167
|
try {
|
|
6004
|
-
|
|
6168
|
+
E = await n.fetch(C, {
|
|
6005
6169
|
method: "POST",
|
|
6006
6170
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6007
|
-
body:
|
|
6171
|
+
body: D.toString()
|
|
6008
6172
|
});
|
|
6009
|
-
} catch (
|
|
6010
|
-
throw new
|
|
6173
|
+
} catch (L) {
|
|
6174
|
+
throw new l(
|
|
6011
6175
|
"network_error",
|
|
6012
|
-
`POST ${
|
|
6176
|
+
`POST ${C} falhou (network): ${L?.message ?? String(L)}`
|
|
6013
6177
|
);
|
|
6014
6178
|
}
|
|
6015
|
-
let
|
|
6179
|
+
let R = null;
|
|
6016
6180
|
try {
|
|
6017
|
-
|
|
6181
|
+
R = await E.json();
|
|
6018
6182
|
} catch {
|
|
6019
|
-
|
|
6183
|
+
R = null;
|
|
6020
6184
|
}
|
|
6021
|
-
if (!
|
|
6022
|
-
let
|
|
6023
|
-
throw new
|
|
6185
|
+
if (!E.ok || typeof R?.error == "string") {
|
|
6186
|
+
let L = typeof R?.error_description == "string" && R.error_description || typeof R?.error == "string" && R.error || `HTTP ${E.status}`;
|
|
6187
|
+
throw new l(
|
|
6024
6188
|
"token_exchange_failed",
|
|
6025
|
-
`Troca do authorization_code por tokens falhou no /token: ${
|
|
6026
|
-
|
|
6189
|
+
`Troca do authorization_code por tokens falhou no /token: ${L}`,
|
|
6190
|
+
E.status
|
|
6027
6191
|
);
|
|
6028
6192
|
}
|
|
6029
|
-
let
|
|
6030
|
-
if (typeof
|
|
6031
|
-
throw new
|
|
6193
|
+
let O = R?.id_token;
|
|
6194
|
+
if (typeof O != "string" || O.length === 0)
|
|
6195
|
+
throw new l(
|
|
6032
6196
|
"token_exchange_failed",
|
|
6033
6197
|
"Resposta do /token sem `id_token` \u2014 o login n\xE3o pode ser completado.",
|
|
6034
|
-
|
|
6198
|
+
E.status
|
|
6035
6199
|
);
|
|
6036
|
-
let
|
|
6037
|
-
if (!
|
|
6038
|
-
throw new
|
|
6200
|
+
let A = nt(O);
|
|
6201
|
+
if (!A)
|
|
6202
|
+
throw new l(
|
|
6039
6203
|
"token_exchange_failed",
|
|
6040
6204
|
"id_token retornado pelo /token \xE9 malformado (payload indecodific\xE1vel)."
|
|
6041
6205
|
);
|
|
6042
|
-
if (
|
|
6043
|
-
throw new
|
|
6206
|
+
if (A.nonce !== g.nonce)
|
|
6207
|
+
throw new l(
|
|
6044
6208
|
"oidc_nonce_mismatch",
|
|
6045
6209
|
"nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
|
|
6046
6210
|
);
|
|
6047
|
-
let
|
|
6048
|
-
if (!
|
|
6049
|
-
throw new
|
|
6211
|
+
let U = ie(O);
|
|
6212
|
+
if (!U)
|
|
6213
|
+
throw new l(
|
|
6050
6214
|
"token_exchange_failed",
|
|
6051
6215
|
"id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
|
|
6052
6216
|
);
|
|
6053
|
-
let
|
|
6054
|
-
|
|
6055
|
-
let
|
|
6056
|
-
return typeof
|
|
6217
|
+
let F = a();
|
|
6218
|
+
F?.setItem(e, O);
|
|
6219
|
+
let j = R?.refresh_token;
|
|
6220
|
+
return typeof j == "string" && j.length > 0 && F?.setItem(t, j), o = U, x(U), u(), { user: U, postLoginRedirect: g.postLoginRedirect };
|
|
6057
6221
|
}
|
|
6058
6222
|
return {
|
|
6059
|
-
async signIn(
|
|
6223
|
+
async signIn(m) {
|
|
6060
6224
|
if (typeof globalThis.location < "u" && typeof globalThis.location.assign == "function") {
|
|
6061
|
-
let
|
|
6062
|
-
if (!
|
|
6063
|
-
throw new
|
|
6225
|
+
let g = Fn(), w = m?.redirectUri ?? globalThis.location.origin, h = m?.scope ?? "openid profile email", C = T(), D = xe(g, 16), E = xe(g, 16), R = xe(g, 32), O = await $n(g, R), A = c();
|
|
6226
|
+
if (!A)
|
|
6227
|
+
throw new l(
|
|
6064
6228
|
"runtime_unsupported",
|
|
6065
6229
|
"auth.signIn requer sessionStorage para guardar a transa\xE7\xE3o PKCE entre o redirect e o callback."
|
|
6066
6230
|
);
|
|
6067
|
-
let
|
|
6068
|
-
state:
|
|
6069
|
-
nonce:
|
|
6070
|
-
codeVerifier:
|
|
6071
|
-
redirectUri:
|
|
6072
|
-
postLoginRedirect:
|
|
6231
|
+
let U = {
|
|
6232
|
+
state: D,
|
|
6233
|
+
nonce: E,
|
|
6234
|
+
codeVerifier: R,
|
|
6235
|
+
redirectUri: w,
|
|
6236
|
+
postLoginRedirect: m?.postLoginRedirect
|
|
6073
6237
|
};
|
|
6074
|
-
|
|
6075
|
-
let
|
|
6076
|
-
idpOrigin:
|
|
6077
|
-
clientId:
|
|
6078
|
-
redirectUri:
|
|
6079
|
-
scope:
|
|
6080
|
-
state:
|
|
6081
|
-
nonce:
|
|
6082
|
-
codeChallenge:
|
|
6238
|
+
A.setItem(se, JSON.stringify(U));
|
|
6239
|
+
let F = oe(n.baseUrl), j = Bn({
|
|
6240
|
+
idpOrigin: F,
|
|
6241
|
+
clientId: C,
|
|
6242
|
+
redirectUri: w,
|
|
6243
|
+
scope: h,
|
|
6244
|
+
state: D,
|
|
6245
|
+
nonce: E,
|
|
6246
|
+
codeChallenge: O
|
|
6083
6247
|
});
|
|
6084
|
-
globalThis.location.assign(
|
|
6248
|
+
globalThis.location.assign(j.toString());
|
|
6085
6249
|
return;
|
|
6086
6250
|
}
|
|
6087
|
-
throw new
|
|
6251
|
+
throw new l(
|
|
6088
6252
|
"invalid_config",
|
|
6089
6253
|
"auth.signIn requires a browser context or mocks. Use NEETRU_ENV=dev or pass mocks.auth."
|
|
6090
6254
|
);
|
|
6091
6255
|
},
|
|
6092
|
-
handleRedirectCallback:
|
|
6093
|
-
completeSignIn:
|
|
6256
|
+
handleRedirectCallback: P,
|
|
6257
|
+
completeSignIn: q,
|
|
6094
6258
|
// 3.7.0: exposto pro client.support renovar a sessão 1x num 401
|
|
6095
6259
|
// invalid_end_user_token (mesma função do refresh proativo, com dedup).
|
|
6096
|
-
refreshTokens:
|
|
6260
|
+
refreshTokens: b,
|
|
6097
6261
|
getIdToken() {
|
|
6098
|
-
let
|
|
6099
|
-
return
|
|
6262
|
+
let g = a()?.getItem(e) ?? null;
|
|
6263
|
+
return g && ie(g) ? g : null;
|
|
6100
6264
|
},
|
|
6101
6265
|
async signOut() {
|
|
6102
|
-
let
|
|
6103
|
-
|
|
6104
|
-
|
|
6105
|
-
for (let k = 0; k < 2; k++)
|
|
6266
|
+
let m = a(), g = m?.getItem(t) ?? null;
|
|
6267
|
+
if (m && (m.removeItem(e), m.removeItem(t)), p(), o = null, g) {
|
|
6268
|
+
let w = null;
|
|
6106
6269
|
try {
|
|
6107
|
-
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
body: E
|
|
6111
|
-
}), S = null;
|
|
6112
|
-
break;
|
|
6113
|
-
} catch (D) {
|
|
6114
|
-
S = D, k === 0 && await new Promise((I) => setTimeout(I, 500));
|
|
6270
|
+
w = T();
|
|
6271
|
+
} catch {
|
|
6272
|
+
w = null;
|
|
6115
6273
|
}
|
|
6116
|
-
|
|
6117
|
-
|
|
6118
|
-
|
|
6119
|
-
|
|
6274
|
+
!(w && await Vn(n.fetch, oe(n.baseUrl), w, [
|
|
6275
|
+
{ token: g, hint: "refresh_token" }
|
|
6276
|
+
])) && typeof console < "u" && console.warn(
|
|
6277
|
+
"[neetru-sdk] signOut: a revoga\xE7\xE3o do refresh_token no servidor n\xE3o foi confirmada. A sess\xE3o local foi encerrada, mas o refresh_token pode seguir v\xE1lido at\xE9 expirar."
|
|
6278
|
+
);
|
|
6279
|
+
}
|
|
6280
|
+
u();
|
|
6120
6281
|
},
|
|
6121
6282
|
getUser() {
|
|
6122
6283
|
return d();
|
|
6123
6284
|
},
|
|
6124
|
-
onAuthStateChanged(
|
|
6125
|
-
r.add(
|
|
6285
|
+
onAuthStateChanged(m) {
|
|
6286
|
+
r.add(m);
|
|
6126
6287
|
try {
|
|
6127
|
-
|
|
6288
|
+
m(d());
|
|
6128
6289
|
} catch {
|
|
6129
6290
|
}
|
|
6130
6291
|
return () => {
|
|
6131
|
-
r.delete(
|
|
6292
|
+
r.delete(m);
|
|
6132
6293
|
};
|
|
6133
6294
|
},
|
|
6134
|
-
async verifyToken(
|
|
6135
|
-
if (!
|
|
6136
|
-
throw new
|
|
6137
|
-
let
|
|
6138
|
-
if (!
|
|
6139
|
-
throw new
|
|
6295
|
+
async verifyToken(m, g) {
|
|
6296
|
+
if (!m || typeof m != "string")
|
|
6297
|
+
throw new l("token_invalid_signature", "token must be a non-empty string");
|
|
6298
|
+
let h = (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.baseUrl.replace(/^https?:\/\/api\./, "https://auth."), C = h, D = `${h}/api/v1/oauth/.well-known/jwks`, E = g?.jwksCacheTtlMs ?? Ue, R = g?.audience, O = lt(n.apiKey), A = R ?? n.oidcClientId ?? O?.keyId;
|
|
6299
|
+
if (!A)
|
|
6300
|
+
throw new l(
|
|
6140
6301
|
"invalid_config",
|
|
6141
6302
|
"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."
|
|
6142
6303
|
);
|
|
6143
|
-
let
|
|
6304
|
+
let U = Mo(D, E);
|
|
6144
6305
|
try {
|
|
6145
|
-
let { payload:
|
|
6146
|
-
issuer:
|
|
6147
|
-
audience:
|
|
6306
|
+
let { payload: F } = await jwtVerify(m, U, {
|
|
6307
|
+
issuer: C,
|
|
6308
|
+
audience: A,
|
|
6148
6309
|
algorithms: ["RS256"]
|
|
6149
6310
|
});
|
|
6150
|
-
return
|
|
6151
|
-
} catch (
|
|
6152
|
-
let
|
|
6153
|
-
throw
|
|
6311
|
+
return qo(F);
|
|
6312
|
+
} catch (F) {
|
|
6313
|
+
let j = F?.code ?? F?.name ?? "", L = F?.message ?? String(F);
|
|
6314
|
+
throw j === "ERR_JWT_EXPIRED" ? new l("token_expired", `Token expired: ${L}`) : j === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" ? new l("token_invalid_signature", `Token signature invalid: ${L}`) : j === "ERR_JWT_CLAIM_VALIDATION_FAILED" ? L.includes('"aud"') || L.includes("audience") ? new l("token_invalid_audience", `Token audience mismatch: ${L}`) : L.includes('"iss"') || L.includes("issuer") ? new l("token_invalid_issuer", `Token issuer mismatch: ${L}`) : new l("token_invalid_signature", `Token claim validation failed: ${L}`) : new l("token_invalid_signature", `Token verification failed: ${L}`);
|
|
6154
6315
|
}
|
|
6155
6316
|
}
|
|
6156
6317
|
};
|
|
6157
6318
|
}
|
|
6158
|
-
function
|
|
6319
|
+
function jo(n = {}) {
|
|
6159
6320
|
let e = n.fetch ?? globalThis.fetch;
|
|
6160
6321
|
if (typeof e != "function")
|
|
6161
|
-
throw new
|
|
6322
|
+
throw new l(
|
|
6162
6323
|
"invalid_config",
|
|
6163
6324
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createNeetruClient."
|
|
6164
6325
|
);
|
|
6165
|
-
let t = n.apiKey ??
|
|
6326
|
+
let t = n.apiKey ?? Lo();
|
|
6327
|
+
wt({ hasApiKey: !!t, strict: n.strictServerOnlyKey === true });
|
|
6328
|
+
let r = Uo(n.env), o = n.baseUrl ?? (r === "dev-local" ? mt : ft), i = pt(o, n.allowedHosts ?? []), s = Object.freeze({
|
|
6166
6329
|
apiKey: t,
|
|
6167
6330
|
oidcClientId: n.oidcClientId,
|
|
6168
6331
|
baseUrl: i,
|
|
@@ -6170,36 +6333,36 @@ function _o(n = {}) {
|
|
|
6170
6333
|
env: r,
|
|
6171
6334
|
productId: n.productId,
|
|
6172
6335
|
tenantId: n.tenantId
|
|
6173
|
-
}), a = r === "dev" && !
|
|
6174
|
-
getEndUserToken: () =>
|
|
6175
|
-
// `refreshTokens` é opcional
|
|
6336
|
+
}), a = r === "dev" && !Ce(), c = n.mocks?.auth ?? (a ? new Ae({ ...tt }) : Fo(s)), d = n.mocks?.usage ?? (a ? new Ee() : Ut(s)), u = n.endUserAuth ?? c, p = n.mocks?.support ?? (a ? new Pe() : Wt(s, {
|
|
6337
|
+
getEndUserToken: () => u.getIdToken(),
|
|
6338
|
+
// `refreshTokens` é opcional (MockAuth/mocks próprios omitem).
|
|
6176
6339
|
refreshEndUserToken: (() => {
|
|
6177
|
-
let
|
|
6178
|
-
return typeof
|
|
6340
|
+
let P = u.refreshTokens;
|
|
6341
|
+
return typeof P == "function" ? () => P.call(u) : void 0;
|
|
6179
6342
|
})()
|
|
6180
|
-
})),
|
|
6181
|
-
return Object.freeze({
|
|
6343
|
+
})), f = n.mocks?.entitlements ?? (a ? new De() : Dt()), _ = n.mocks?.db ?? (X() === "react-native" ? zn() : ln(s, n.db)), b = n.mocks?.webhooks ?? (a ? new Se() : mn(s)), S = n.mocks?.notifications ?? (a ? new Re() : yn(s)), x = n.mocks?.ai ?? (a ? new Ne() : qn(s)), T = Object.freeze({
|
|
6182
6344
|
config: s,
|
|
6183
|
-
auth:
|
|
6184
|
-
catalog:
|
|
6185
|
-
entitlements:
|
|
6186
|
-
telemetry:
|
|
6345
|
+
auth: c,
|
|
6346
|
+
catalog: Et(s),
|
|
6347
|
+
entitlements: f,
|
|
6348
|
+
telemetry: Ot(s),
|
|
6187
6349
|
usage: d,
|
|
6188
|
-
support:
|
|
6189
|
-
db:
|
|
6190
|
-
checkout:
|
|
6191
|
-
webhooks:
|
|
6192
|
-
notifications:
|
|
6193
|
-
ai:
|
|
6350
|
+
support: p,
|
|
6351
|
+
db: _,
|
|
6352
|
+
checkout: dn(s),
|
|
6353
|
+
webhooks: b,
|
|
6354
|
+
notifications: S,
|
|
6355
|
+
ai: x
|
|
6194
6356
|
});
|
|
6357
|
+
return n.endUserAuth && Kn(T, n.endUserAuth), T;
|
|
6195
6358
|
}
|
|
6196
|
-
var
|
|
6197
|
-
function
|
|
6359
|
+
var Oe = /* @__PURE__ */ Symbol.for("@neetru/sdk:serverClient");
|
|
6360
|
+
function oa(n = {}) {
|
|
6198
6361
|
let e = globalThis;
|
|
6199
|
-
return e[
|
|
6362
|
+
return e[Oe] || (e[Oe] = jo(n)), e[Oe];
|
|
6200
6363
|
}
|
|
6201
|
-
function
|
|
6202
|
-
delete globalThis[
|
|
6364
|
+
function ia() {
|
|
6365
|
+
delete globalThis[Oe];
|
|
6203
6366
|
}
|
|
6204
6367
|
|
|
6205
|
-
export {
|
|
6368
|
+
export { ra as __resetJwksCacheForTests, ia as __resetServerNeetruClientForTests, jo as createNeetruClient, oa as getServerNeetruClient };
|