@neetru/sdk 3.5.0 → 3.7.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 +71 -1
- package/dist/ai.cjs +254 -391
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +254 -391
- package/dist/auth.cjs +3560 -4993
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +3557 -4990
- package/dist/build-guard.cjs +101 -119
- package/dist/build-guard.mjs +91 -109
- package/dist/catalog.cjs +152 -224
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +152 -224
- package/dist/checkout.cjs +237 -320
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +236 -319
- package/dist/db-react.cjs +59 -87
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db-react.mjs +56 -84
- package/dist/db.cjs +2055 -3013
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +2068 -3026
- package/dist/entitlements.cjs +12 -20
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +12 -20
- package/dist/errors.cjs +9 -12
- package/dist/errors.d.cts +37 -2
- package/dist/errors.d.ts +37 -2
- package/dist/errors.mjs +8 -12
- package/dist/firestore-compat.cjs +241 -297
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/firestore-compat.mjs +215 -271
- package/dist/index.cjs +3627 -5135
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.mjs +3731 -5239
- package/dist/mobile.cjs +411 -0
- package/dist/mobile.d.cts +89 -0
- package/dist/mobile.d.ts +89 -0
- package/dist/mobile.mjs +409 -0
- package/dist/mocks.cjs +312 -447
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +306 -441
- package/dist/notifications.cjs +226 -314
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +225 -313
- package/dist/react.cjs +522 -693
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +512 -683
- package/dist/support.cjs +329 -325
- package/dist/support.d.cts +25 -3
- package/dist/support.d.ts +25 -3
- package/dist/support.mjs +328 -325
- package/dist/telemetry.cjs +197 -301
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +197 -301
- package/dist/{types-Du_iioJ7.d.cts → types-Bx4kTLmk.d.cts} +26 -0
- package/dist/{types-BPATkXYu.d.ts → types-Dx0IrOSF.d.ts} +26 -0
- package/dist/usage.cjs +163 -227
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +163 -227
- package/dist/webhooks.cjs +258 -359
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +255 -356
- package/package.json +9 -3
- package/dist/ai.cjs.map +0 -1
- package/dist/ai.mjs.map +0 -1
- package/dist/auth.cjs.map +0 -1
- package/dist/auth.mjs.map +0 -1
- package/dist/build-guard.cjs.map +0 -1
- package/dist/build-guard.mjs.map +0 -1
- package/dist/catalog.cjs.map +0 -1
- package/dist/catalog.mjs.map +0 -1
- package/dist/checkout.cjs.map +0 -1
- package/dist/checkout.mjs.map +0 -1
- package/dist/db-react.cjs.map +0 -1
- package/dist/db-react.mjs.map +0 -1
- package/dist/db.cjs.map +0 -1
- package/dist/db.mjs.map +0 -1
- package/dist/entitlements.cjs.map +0 -1
- package/dist/entitlements.mjs.map +0 -1
- package/dist/errors.cjs.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/firestore-compat.cjs.map +0 -1
- package/dist/firestore-compat.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/mocks.cjs.map +0 -1
- package/dist/mocks.mjs.map +0 -1
- package/dist/notifications.cjs.map +0 -1
- package/dist/notifications.mjs.map +0 -1
- package/dist/react.cjs.map +0 -1
- package/dist/react.mjs.map +0 -1
- package/dist/support.cjs.map +0 -1
- package/dist/support.mjs.map +0 -1
- package/dist/telemetry.cjs.map +0 -1
- package/dist/telemetry.mjs.map +0 -1
- package/dist/usage.cjs.map +0 -1
- package/dist/usage.mjs.map +0 -1
- package/dist/webhooks.cjs.map +0 -1
- package/dist/webhooks.mjs.map +0 -1
package/dist/telemetry.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
|
-
var
|
|
2
|
+
var a = class e extends Error {
|
|
3
3
|
code;
|
|
4
4
|
status;
|
|
5
5
|
requestId;
|
|
@@ -12,233 +12,184 @@ var NeetruError = class _NeetruError extends Error {
|
|
|
12
12
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
13
13
|
*/
|
|
14
14
|
reason;
|
|
15
|
-
constructor(
|
|
16
|
-
super(
|
|
17
|
-
this.name = "NeetruError";
|
|
18
|
-
this.code = code;
|
|
19
|
-
this.status = status;
|
|
20
|
-
this.requestId = requestId;
|
|
21
|
-
this.reason = reason;
|
|
22
|
-
Object.setPrototypeOf(this, _NeetruError.prototype);
|
|
15
|
+
constructor(r, n, d, m, c) {
|
|
16
|
+
super(n), this.name = "NeetruError", this.code = r, this.status = d, this.requestId = m, this.reason = c, Object.setPrototypeOf(this, e.prototype);
|
|
23
17
|
}
|
|
24
18
|
};
|
|
25
19
|
|
|
26
20
|
// src/idempotency.ts
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
-
throw new NeetruError(
|
|
21
|
+
function R() {
|
|
22
|
+
let e = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
23
|
+
if (e && typeof e.randomUUID == "function")
|
|
24
|
+
return e.randomUUID();
|
|
25
|
+
throw new a(
|
|
33
26
|
"runtime_unsupported",
|
|
34
27
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
35
28
|
);
|
|
36
29
|
}
|
|
37
30
|
|
|
38
31
|
// src/http.ts
|
|
39
|
-
var
|
|
32
|
+
var L = {
|
|
40
33
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
41
34
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
42
35
|
3: "2026.05.1"
|
|
43
36
|
};
|
|
44
|
-
function
|
|
45
|
-
return
|
|
37
|
+
function v(e) {
|
|
38
|
+
return e.split("+")[0] ?? e;
|
|
46
39
|
}
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const pa = partsA[i] ?? 0;
|
|
53
|
-
const pb = partsB[i] ?? 0;
|
|
54
|
-
if (pa !== pb) return pa - pb;
|
|
40
|
+
function O(e, r) {
|
|
41
|
+
let n = v(e).split(".").map(Number), d = v(r).split(".").map(Number), m = Math.max(n.length, d.length);
|
|
42
|
+
for (let c = 0; c < m; c++) {
|
|
43
|
+
let t = n[c] ?? 0, i = d[c] ?? 0;
|
|
44
|
+
if (t !== i) return t - i;
|
|
55
45
|
}
|
|
56
46
|
return 0;
|
|
57
47
|
}
|
|
58
|
-
function
|
|
48
|
+
function D() {
|
|
59
49
|
try {
|
|
60
|
-
return "3.
|
|
50
|
+
return "3.7.0";
|
|
61
51
|
} catch {
|
|
62
|
-
return
|
|
52
|
+
return;
|
|
63
53
|
}
|
|
64
54
|
}
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return 3;
|
|
55
|
+
function U() {
|
|
56
|
+
let e = D();
|
|
57
|
+
return e ? parseInt(e.split(".")[0] ?? "3", 10) : 3;
|
|
69
58
|
}
|
|
70
|
-
var
|
|
71
|
-
function
|
|
72
|
-
if (!
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
console.warn(
|
|
82
|
-
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${coreVersion}" \xE9 anterior ao m\xEDnimo recomendado "${minCore}" para SDK major ${sdkMajor}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
|
|
83
|
-
);
|
|
84
|
-
}
|
|
59
|
+
var A = /* @__PURE__ */ new Set();
|
|
60
|
+
function j(e) {
|
|
61
|
+
if (!e) return;
|
|
62
|
+
let r = U(), n = L[r];
|
|
63
|
+
if (!n) return;
|
|
64
|
+
let d = v(e);
|
|
65
|
+
if (O(d, n) < 0) {
|
|
66
|
+
let m = `${r}:${d}`;
|
|
67
|
+
A.has(m) || (A.add(m), console.warn(
|
|
68
|
+
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${e}" \xE9 anterior ao m\xEDnimo recomendado "${n}" para SDK major ${r}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
|
|
69
|
+
));
|
|
85
70
|
}
|
|
86
71
|
}
|
|
87
|
-
var
|
|
88
|
-
var DEFAULT_RETRIES_WRITE = 0;
|
|
89
|
-
var READ_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD"]);
|
|
90
|
-
var RETRYABLE_CODES = /* @__PURE__ */ new Set([
|
|
72
|
+
var $ = 2, K = 0, H = /* @__PURE__ */ new Set(["GET", "HEAD"]), F = /* @__PURE__ */ new Set([
|
|
91
73
|
"rate_limited",
|
|
92
74
|
"server_error",
|
|
93
75
|
"network_error"
|
|
94
76
|
]);
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return Math.max(50, Math.round(base + jitter));
|
|
77
|
+
function I(e) {
|
|
78
|
+
let r = 200 * Math.pow(4, e), n = r * 0.2 * (Math.random() * 2 - 1);
|
|
79
|
+
return Math.max(50, Math.round(r + n));
|
|
99
80
|
}
|
|
100
|
-
var
|
|
101
|
-
function
|
|
102
|
-
if (!
|
|
103
|
-
|
|
104
|
-
if (Number.isFinite(
|
|
105
|
-
|
|
106
|
-
if (Number.isFinite(
|
|
107
|
-
|
|
108
|
-
if (
|
|
81
|
+
var S = 6e4;
|
|
82
|
+
function V(e) {
|
|
83
|
+
if (!e) return null;
|
|
84
|
+
let r = Number(e);
|
|
85
|
+
if (Number.isFinite(r) && r >= 0) return Math.min(Math.round(r * 1e3), S);
|
|
86
|
+
let n = Date.parse(e);
|
|
87
|
+
if (Number.isFinite(n)) {
|
|
88
|
+
let d = n - Date.now();
|
|
89
|
+
if (d > 0) return Math.min(d, S);
|
|
109
90
|
}
|
|
110
91
|
return null;
|
|
111
92
|
}
|
|
112
|
-
function
|
|
113
|
-
return new Promise((
|
|
93
|
+
function q(e) {
|
|
94
|
+
return new Promise((r) => setTimeout(r, e));
|
|
114
95
|
}
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
if (status === 403) return "forbidden";
|
|
118
|
-
if (status === 404) return "not_found";
|
|
119
|
-
if (status === 409) return "conflict";
|
|
120
|
-
if (status === 422 || status === 400) return "validation_failed";
|
|
121
|
-
if (status === 429) return "rate_limited";
|
|
122
|
-
if (status >= 500) return "server_error";
|
|
123
|
-
return "unknown";
|
|
96
|
+
function G(e) {
|
|
97
|
+
return e === 401 ? "unauthorized" : e === 403 ? "forbidden" : e === 404 ? "not_found" : e === 409 ? "conflict" : e === 410 ? "gone" : e === 422 || e === 400 ? "validation_failed" : e === 429 ? "rate_limited" : e >= 500 ? "server_error" : "unknown";
|
|
124
98
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (v === void 0) continue;
|
|
132
|
-
url.searchParams.set(k, String(v));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return url.toString();
|
|
99
|
+
function z(e, r, n) {
|
|
100
|
+
let d = e.replace(/\/+$/, ""), m = r.startsWith("/") ? r : `/${r}`, c = new URL(`${d}${m}`);
|
|
101
|
+
if (n)
|
|
102
|
+
for (let [t, i] of Object.entries(n))
|
|
103
|
+
i !== void 0 && c.searchParams.set(t, String(i));
|
|
104
|
+
return c.toString();
|
|
136
105
|
}
|
|
137
|
-
async function
|
|
138
|
-
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
106
|
+
async function x(e) {
|
|
107
|
+
let r = await e.text();
|
|
108
|
+
if (r)
|
|
109
|
+
try {
|
|
110
|
+
return JSON.parse(r);
|
|
111
|
+
} catch {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
145
114
|
}
|
|
146
|
-
async function
|
|
147
|
-
|
|
148
|
-
const url = buildUrl(config.baseUrl, opts.path, opts.query);
|
|
149
|
-
const defaultRetries = READ_METHODS.has(method) ? DEFAULT_RETRIES_READ : DEFAULT_RETRIES_WRITE;
|
|
150
|
-
const maxRetries = opts.retries ?? defaultRetries;
|
|
151
|
-
const headers = {
|
|
115
|
+
async function _(e, r) {
|
|
116
|
+
let n = r.method ?? "GET", d = z(e.baseUrl, r.path, r.query), m = H.has(n) ? $ : K, c = r.retries ?? m, t = {
|
|
152
117
|
accept: "application/json",
|
|
153
|
-
...
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
if (opts.requireAuth) {
|
|
160
|
-
if (!config.apiKey) {
|
|
161
|
-
throw new NeetruError(
|
|
118
|
+
...r.headers
|
|
119
|
+
}, i = D();
|
|
120
|
+
if (i && (t["x-neetru-sdk-version"] = i), r.requireAuth) {
|
|
121
|
+
if (!e.apiKey)
|
|
122
|
+
throw new a(
|
|
162
123
|
"missing_api_key",
|
|
163
124
|
"This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
164
125
|
);
|
|
165
|
-
}
|
|
166
|
-
headers.authorization = `Bearer ${config.apiKey}`;
|
|
167
|
-
}
|
|
168
|
-
if (opts.idempotencyKey) {
|
|
169
|
-
const key = typeof opts.idempotencyKey === "string" ? opts.idempotencyKey : generateIdempotencyKey();
|
|
170
|
-
headers["idempotency-key"] = key;
|
|
126
|
+
t.authorization = `Bearer ${e.apiKey}`;
|
|
171
127
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
128
|
+
if (r.idempotencyKey) {
|
|
129
|
+
let f = typeof r.idempotencyKey == "string" ? r.idempotencyKey : R();
|
|
130
|
+
t["idempotency-key"] = f;
|
|
175
131
|
}
|
|
176
|
-
let
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
132
|
+
let l = r.body !== void 0 && n !== "GET" && n !== "DELETE" ? JSON.stringify(r.body) : void 0;
|
|
133
|
+
l !== void 0 && (t["content-type"] = "application/json");
|
|
134
|
+
let s = null, u = r.deadlineMs != null ? Date.now() + r.deadlineMs : null;
|
|
135
|
+
for (let f = 0; f <= c; f++) {
|
|
136
|
+
if (u != null && Date.now() >= u)
|
|
137
|
+
throw s ?? new a("network_error", "Deadline excedido antes de completar a request");
|
|
138
|
+
let y = { method: n, headers: t };
|
|
139
|
+
l !== void 0 && (y.body = l);
|
|
140
|
+
let M = u != null ? Math.max(1, Math.min(3e4, u - Date.now())) : 3e4;
|
|
141
|
+
y.signal = AbortSignal.timeout(M);
|
|
142
|
+
let p;
|
|
182
143
|
try {
|
|
183
|
-
|
|
184
|
-
} catch (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
144
|
+
p = await e.fetch(d, y);
|
|
145
|
+
} catch (o) {
|
|
146
|
+
let w = o instanceof DOMException && o.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${o instanceof Error ? o.message : "fetch failed"}`;
|
|
147
|
+
if (s = new a("network_error", w), f < c) {
|
|
148
|
+
let k = I(f);
|
|
149
|
+
if (u == null || Date.now() + k < u) {
|
|
150
|
+
await q(k);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
190
153
|
}
|
|
191
|
-
throw
|
|
154
|
+
throw s;
|
|
192
155
|
}
|
|
193
|
-
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const parsed = await safeJson(res);
|
|
198
|
-
return parsed;
|
|
156
|
+
let P = p.headers.get("x-request-id") ?? p.headers.get("x-correlation-id") ?? void 0;
|
|
157
|
+
if (p.ok) {
|
|
158
|
+
let o = p.headers.get("x-neetru-version");
|
|
159
|
+
return o && j(o), await x(p);
|
|
199
160
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
} else if (errField && typeof errField === "object") {
|
|
209
|
-
if (typeof errField.code === "string") code = errField.code;
|
|
210
|
-
if (typeof errField.message === "string") message = errField.message;
|
|
211
|
-
if (typeof errField.reason === "string") reason = errField.reason;
|
|
212
|
-
} else {
|
|
213
|
-
if (typeof body.code === "string") code = body.code;
|
|
214
|
-
if (typeof body.message === "string") message = body.message;
|
|
215
|
-
}
|
|
216
|
-
if (typeof body.reason === "string") reason = body.reason;
|
|
161
|
+
let g = await x(p), b = G(p.status), h = `HTTP ${p.status}`, T;
|
|
162
|
+
if (g && typeof g == "object") {
|
|
163
|
+
let o = g.error;
|
|
164
|
+
typeof o == "string" ? h = o : o && typeof o == "object" ? (typeof o.code == "string" && (b = o.code), typeof o.message == "string" && (h = o.message), typeof o.reason == "string" && (T = o.reason)) : (typeof g.code == "string" && (b = g.code), typeof g.message == "string" && (h = g.message)), typeof g.reason == "string" && (T = g.reason);
|
|
165
|
+
}
|
|
166
|
+
if (p.status === 410) {
|
|
167
|
+
let o = p.headers.get("x-neetru-replacement");
|
|
168
|
+
h === `HTTP ${p.status}` && (h = `Endpoint ${r.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), o && !h.includes(o) && (h += ` Substituto: ${o}.`);
|
|
217
169
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
continue;
|
|
170
|
+
let E = new a(b, h, p.status, P, T);
|
|
171
|
+
if (s = E, F.has(b) && f < c) {
|
|
172
|
+
let w = V(p.headers.get("retry-after")) ?? I(f);
|
|
173
|
+
if (u == null || Date.now() + w < u) {
|
|
174
|
+
await q(w);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
226
177
|
}
|
|
227
|
-
throw
|
|
178
|
+
throw E;
|
|
228
179
|
}
|
|
229
|
-
throw
|
|
180
|
+
throw s ?? new a("unknown", "unexpected httpRequest exit");
|
|
230
181
|
}
|
|
231
182
|
|
|
232
183
|
// src/telemetry.ts
|
|
233
|
-
var
|
|
234
|
-
function
|
|
235
|
-
return typeof globalThis
|
|
184
|
+
var N = ["debug", "info", "warn", "error", "fatal"];
|
|
185
|
+
function B() {
|
|
186
|
+
return typeof globalThis < "u" && typeof globalThis.window < "u";
|
|
236
187
|
}
|
|
237
|
-
function
|
|
238
|
-
return
|
|
188
|
+
function C() {
|
|
189
|
+
return B() ? { path: "/api/sdk/v1/telemetry/event/public", requireAuth: false } : { path: "/api/sdk/v1/telemetry/event", requireAuth: true };
|
|
239
190
|
}
|
|
240
|
-
function
|
|
241
|
-
switch (
|
|
191
|
+
function J(e) {
|
|
192
|
+
switch (e) {
|
|
242
193
|
case "debug":
|
|
243
194
|
return console.debug.bind(console);
|
|
244
195
|
case "info":
|
|
@@ -253,58 +204,41 @@ function consoleFor(level) {
|
|
|
253
204
|
return console.log.bind(console);
|
|
254
205
|
}
|
|
255
206
|
}
|
|
256
|
-
var
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (queue.length === 0) return;
|
|
264
|
-
const batch = queue.splice(0, queue.length);
|
|
265
|
-
const work = Promise.allSettled(
|
|
266
|
-
batch.map(async (ev) => {
|
|
207
|
+
var X = 500, Y = 50;
|
|
208
|
+
function se(e) {
|
|
209
|
+
let r = [], n = null, d = null;
|
|
210
|
+
async function m() {
|
|
211
|
+
if (r.length === 0) return;
|
|
212
|
+
let t = r.splice(0, r.length), i = Promise.allSettled(
|
|
213
|
+
t.map(async (l) => {
|
|
267
214
|
try {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
body.properties = ev.properties;
|
|
273
|
-
}
|
|
274
|
-
if (ev.timestamp) body.timestamp = ev.timestamp;
|
|
275
|
-
const transport = resolveEventTransport();
|
|
276
|
-
await httpRequest(config, {
|
|
215
|
+
let s = { name: l.name }, u = l.productSlug ?? e.productId;
|
|
216
|
+
u && (s.productSlug = u), l.properties && typeof l.properties == "object" && (s.properties = l.properties), l.timestamp && (s.timestamp = l.timestamp);
|
|
217
|
+
let f = C();
|
|
218
|
+
await _(e, {
|
|
277
219
|
method: "POST",
|
|
278
|
-
path:
|
|
279
|
-
body,
|
|
280
|
-
requireAuth:
|
|
220
|
+
path: f.path,
|
|
221
|
+
body: s,
|
|
222
|
+
requireAuth: f.requireAuth,
|
|
281
223
|
retries: 1,
|
|
282
224
|
// caça-bruxas: retry sem Idempotency-Key duplicava o evento se o
|
|
283
225
|
// servidor já tinha persistido antes da falha transiente. Mesmo
|
|
284
226
|
// padrão do usage.report (key estável por chamada, reusada no retry).
|
|
285
|
-
idempotencyKey:
|
|
227
|
+
idempotencyKey: !0
|
|
286
228
|
});
|
|
287
|
-
} catch (
|
|
288
|
-
|
|
289
|
-
console.warn("[neetru-sdk] telemetry.track flush failed for event", ev.name, err);
|
|
290
|
-
}
|
|
229
|
+
} catch (s) {
|
|
230
|
+
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event", l.name, s);
|
|
291
231
|
}
|
|
292
232
|
})
|
|
293
233
|
);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
});
|
|
297
|
-
await work;
|
|
298
|
-
flushTimer = null;
|
|
299
|
-
if (queue.length > 0) {
|
|
300
|
-
scheduleFlush();
|
|
301
|
-
}
|
|
234
|
+
d = i.then(() => {
|
|
235
|
+
d = null;
|
|
236
|
+
}), await i, n = null, r.length > 0 && c();
|
|
302
237
|
}
|
|
303
|
-
function
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
}, TRACK_FLUSH_MS);
|
|
238
|
+
function c() {
|
|
239
|
+
n || (n = setTimeout(() => {
|
|
240
|
+
m();
|
|
241
|
+
}, X));
|
|
308
242
|
}
|
|
309
243
|
return {
|
|
310
244
|
/**
|
|
@@ -319,34 +253,24 @@ function createTelemetryNamespace(config) {
|
|
|
319
253
|
* });
|
|
320
254
|
* ```
|
|
321
255
|
*/
|
|
322
|
-
async event(
|
|
323
|
-
if (!
|
|
324
|
-
throw new
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
const body = { name: input.name };
|
|
333
|
-
const productSlug = input.productSlug ?? config.productId;
|
|
334
|
-
if (productSlug) body.productSlug = productSlug;
|
|
335
|
-
if (input.properties && typeof input.properties === "object") {
|
|
336
|
-
body.properties = input.properties;
|
|
337
|
-
}
|
|
338
|
-
if (input.timestamp) body.timestamp = input.timestamp;
|
|
339
|
-
const transport = resolveEventTransport();
|
|
340
|
-
const raw = await httpRequest(config, {
|
|
256
|
+
async event(t) {
|
|
257
|
+
if (!t || typeof t != "object")
|
|
258
|
+
throw new a("validation_failed", "event input is required");
|
|
259
|
+
if (typeof t.name != "string" || t.name.length === 0)
|
|
260
|
+
throw new a("validation_failed", "event.name is required");
|
|
261
|
+
if (t.name.length > 128)
|
|
262
|
+
throw new a("validation_failed", "event.name max 128 chars");
|
|
263
|
+
let i = { name: t.name }, l = t.productSlug ?? e.productId;
|
|
264
|
+
l && (i.productSlug = l), t.properties && typeof t.properties == "object" && (i.properties = t.properties), t.timestamp && (i.timestamp = t.timestamp);
|
|
265
|
+
let s = C(), u = await _(e, {
|
|
341
266
|
method: "POST",
|
|
342
|
-
path:
|
|
343
|
-
body,
|
|
344
|
-
requireAuth:
|
|
267
|
+
path: s.path,
|
|
268
|
+
body: i,
|
|
269
|
+
requireAuth: s.requireAuth
|
|
345
270
|
});
|
|
346
|
-
if (!
|
|
347
|
-
throw new
|
|
348
|
-
}
|
|
349
|
-
return { ok: true, eventId: raw.eventId };
|
|
271
|
+
if (!u || u.ok !== true || typeof u.eventId != "string")
|
|
272
|
+
throw new a("invalid_response", "Telemetry response missing eventId");
|
|
273
|
+
return { ok: true, eventId: u.eventId };
|
|
350
274
|
},
|
|
351
275
|
/**
|
|
352
276
|
* Fire-and-forget: enqueue + flush 500ms debounce. Não retorna `eventId`
|
|
@@ -358,32 +282,15 @@ function createTelemetryNamespace(config) {
|
|
|
358
282
|
* // segue execução; flush async em background
|
|
359
283
|
* ```
|
|
360
284
|
*/
|
|
361
|
-
track(
|
|
362
|
-
|
|
363
|
-
if (typeof input.name !== "string" || input.name.length === 0) return;
|
|
364
|
-
if (input.name.length > 128) return;
|
|
365
|
-
queue.push(input);
|
|
366
|
-
if (queue.length >= TRACK_MAX_QUEUE) {
|
|
367
|
-
if (flushTimer) {
|
|
368
|
-
clearTimeout(flushTimer);
|
|
369
|
-
flushTimer = null;
|
|
370
|
-
}
|
|
371
|
-
void drainQueue();
|
|
372
|
-
} else {
|
|
373
|
-
scheduleFlush();
|
|
374
|
-
}
|
|
285
|
+
track(t) {
|
|
286
|
+
!t || typeof t != "object" || typeof t.name != "string" || t.name.length === 0 || t.name.length > 128 || (r.push(t), r.length >= Y ? (n && (clearTimeout(n), n = null), m()) : c());
|
|
375
287
|
},
|
|
376
288
|
/**
|
|
377
289
|
* Força flush imediato da queue de `track()`. Resolva antes de
|
|
378
290
|
* page unload / SSR boundary pra não perder eventos.
|
|
379
291
|
*/
|
|
380
292
|
async flush() {
|
|
381
|
-
|
|
382
|
-
clearTimeout(flushTimer);
|
|
383
|
-
flushTimer = null;
|
|
384
|
-
}
|
|
385
|
-
if (_inflightDrain) await _inflightDrain;
|
|
386
|
-
await drainQueue();
|
|
293
|
+
n && (clearTimeout(n), n = null), d && await d, await m();
|
|
387
294
|
},
|
|
388
295
|
/**
|
|
389
296
|
* Registra um log estruturado per-product (Sprint 6).
|
|
@@ -400,60 +307,49 @@ function createTelemetryNamespace(config) {
|
|
|
400
307
|
* });
|
|
401
308
|
* ```
|
|
402
309
|
*/
|
|
403
|
-
async log(
|
|
404
|
-
if (!
|
|
405
|
-
throw new
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
if (
|
|
411
|
-
throw new
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const fn = consoleFor(input.level);
|
|
418
|
-
fn(`[neetru-sdk] ${input.level}: ${input.message}`, input.metadata ?? {});
|
|
419
|
-
return { ok: true, mode: "mock" };
|
|
420
|
-
}
|
|
421
|
-
const body = {
|
|
422
|
-
level: input.level,
|
|
423
|
-
message: input.message
|
|
310
|
+
async log(t) {
|
|
311
|
+
if (!t || typeof t != "object")
|
|
312
|
+
throw new a("validation_failed", "log input is required");
|
|
313
|
+
if (!N.includes(t.level))
|
|
314
|
+
throw new a("validation_failed", `level must be one of ${N.join(", ")}`);
|
|
315
|
+
if (typeof t.message != "string" || t.message.length === 0)
|
|
316
|
+
throw new a("validation_failed", "message is required");
|
|
317
|
+
if (t.message.length > 4e3)
|
|
318
|
+
throw new a("validation_failed", "message max 4000 chars");
|
|
319
|
+
if (e.env === "dev")
|
|
320
|
+
return J(t.level)(`[neetru-sdk] ${t.level}: ${t.message}`, t.metadata ?? {}), { ok: true, mode: "mock" };
|
|
321
|
+
let i = {
|
|
322
|
+
level: t.level,
|
|
323
|
+
message: t.message
|
|
424
324
|
};
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
let
|
|
429
|
-
if (!
|
|
325
|
+
t.metadata && (i.metadata = t.metadata);
|
|
326
|
+
let l = t.productSlug ?? e.productId;
|
|
327
|
+
l && (i.productSlug = l);
|
|
328
|
+
let s = t.correlationId;
|
|
329
|
+
if (!s)
|
|
430
330
|
try {
|
|
431
|
-
|
|
432
|
-
|
|
331
|
+
let y = globalThis.NEETRU_CORRELATION_ID;
|
|
332
|
+
typeof y == "string" && y.length > 0 && (s = y);
|
|
433
333
|
} catch {
|
|
434
334
|
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const raw = await httpRequest(config, {
|
|
335
|
+
let u = {};
|
|
336
|
+
s && (u["x-correlation-id"] = s);
|
|
337
|
+
let f = await _(e, {
|
|
439
338
|
method: "POST",
|
|
440
339
|
path: "/api/sdk/v1/telemetry/log",
|
|
441
|
-
body,
|
|
340
|
+
body: i,
|
|
442
341
|
requireAuth: true,
|
|
443
|
-
headers
|
|
342
|
+
headers: u
|
|
444
343
|
});
|
|
445
|
-
if (!
|
|
446
|
-
throw new
|
|
447
|
-
}
|
|
344
|
+
if (!f || f.ok !== true)
|
|
345
|
+
throw new a("invalid_response", "Telemetry log response missing ok");
|
|
448
346
|
return {
|
|
449
347
|
ok: true,
|
|
450
|
-
logId: typeof
|
|
348
|
+
logId: typeof f.logId == "string" ? f.logId : void 0,
|
|
451
349
|
mode: "http"
|
|
452
350
|
};
|
|
453
351
|
}
|
|
454
352
|
};
|
|
455
353
|
}
|
|
456
354
|
|
|
457
|
-
export { createTelemetryNamespace };
|
|
458
|
-
//# sourceMappingURL=telemetry.mjs.map
|
|
459
|
-
//# sourceMappingURL=telemetry.mjs.map
|
|
355
|
+
export { se as createTelemetryNamespace };
|