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