@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/usage.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,236 +14,253 @@ 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, r, n, i, o, a) {
|
|
25
|
+
super(r), this.name = "NeetruError", this.code = t, this.status = n, this.requestId = i, this.reason = o, this.correlationId = a, 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 T() {
|
|
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 S = "x-correlation-id";
|
|
42
|
+
var j = /^[A-Za-z0-9._:\-]+$/;
|
|
43
|
+
function x(e) {
|
|
44
|
+
return typeof e == "string" && e.length > 0 && e.length <= 128 && j.test(e);
|
|
45
|
+
}
|
|
46
|
+
function F() {
|
|
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), r = false;
|
|
53
|
+
try {
|
|
54
|
+
e && typeof e.getRandomValues == "function" && (e.getRandomValues(t), r = !0);
|
|
55
|
+
} catch {
|
|
56
|
+
r = false;
|
|
57
|
+
}
|
|
58
|
+
if (!r)
|
|
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 n = Array.from(t, (i) => i.toString(16).padStart(2, "0")).join("");
|
|
62
|
+
return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
|
|
63
|
+
}
|
|
64
|
+
function D(e, t) {
|
|
65
|
+
let r, n = {};
|
|
66
|
+
for (let [o, a] of Object.entries(e)) {
|
|
67
|
+
if (o.toLowerCase() === S) {
|
|
68
|
+
x(a) && (r = a);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
n[o] = a;
|
|
72
|
+
}
|
|
73
|
+
let i = r ?? (x(t) ? t : void 0) ?? F();
|
|
74
|
+
return n[S] = i, { headers: n, correlationId: i };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/server-only-key.ts
|
|
78
|
+
function A() {
|
|
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 H = {
|
|
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 v(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 r = v(e).split(".").map(Number), n = v(t).split(".").map(Number), i = Math.max(r.length, n.length);
|
|
103
|
+
for (let o = 0; o < i; o++) {
|
|
104
|
+
let a = r[o] ?? 0, c = n[o] ?? 0;
|
|
105
|
+
if (a !== c) return a - c;
|
|
57
106
|
}
|
|
58
107
|
return 0;
|
|
59
108
|
}
|
|
60
|
-
function
|
|
109
|
+
function q() {
|
|
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 z() {
|
|
117
|
+
let e = q();
|
|
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 C = /* @__PURE__ */ new Set();
|
|
121
|
+
function B(e) {
|
|
122
|
+
if (!e) return;
|
|
123
|
+
let t = z(), r = H[t];
|
|
124
|
+
if (!r) return;
|
|
125
|
+
let n = v(e);
|
|
126
|
+
if (V(n, r) < 0) {
|
|
127
|
+
let i = `${t}:${n}`;
|
|
128
|
+
C.has(i) || (C.add(i), console.warn(
|
|
129
|
+
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${e}" \xE9 anterior ao m\xEDnimo recomendado "${r}" 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 A() ? "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 O(e) {
|
|
143
|
+
let t = 200 * Math.pow(4, e), r = t * 0.2 * (Math.random() * 2 - 1);
|
|
144
|
+
return Math.max(50, Math.round(t + r));
|
|
101
145
|
}
|
|
102
|
-
var
|
|
103
|
-
function
|
|
104
|
-
if (!
|
|
105
|
-
|
|
106
|
-
if (Number.isFinite(
|
|
107
|
-
|
|
108
|
-
if (Number.isFinite(
|
|
109
|
-
|
|
110
|
-
if (
|
|
146
|
+
var K = 6e4;
|
|
147
|
+
function Z(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), K);
|
|
151
|
+
let r = Date.parse(e);
|
|
152
|
+
if (Number.isFinite(r)) {
|
|
153
|
+
let n = r - Date.now();
|
|
154
|
+
if (n > 0) return Math.min(n, K);
|
|
111
155
|
}
|
|
112
156
|
return null;
|
|
113
157
|
}
|
|
114
|
-
function
|
|
115
|
-
return new Promise((
|
|
158
|
+
function M(e) {
|
|
159
|
+
return new Promise((t) => setTimeout(t, e));
|
|
160
|
+
}
|
|
161
|
+
function Q(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";
|
|
116
163
|
}
|
|
117
|
-
function
|
|
118
|
-
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (status === 429) return "rate_limited";
|
|
124
|
-
if (status >= 500) return "server_error";
|
|
125
|
-
return "unknown";
|
|
164
|
+
function ee(e, t, r) {
|
|
165
|
+
let n = e.replace(/\/+$/, ""), i = t.startsWith("/") ? t : `/${t}`, o = new URL(`${n}${i}`);
|
|
166
|
+
if (r)
|
|
167
|
+
for (let [a, c] of Object.entries(r))
|
|
168
|
+
c !== void 0 && o.searchParams.set(a, String(c));
|
|
169
|
+
return o.toString();
|
|
126
170
|
}
|
|
127
|
-
function
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
url.searchParams.set(k, String(v));
|
|
171
|
+
async function U(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 E(e, t) {
|
|
181
|
+
let r = t.method ?? "GET", n = ee(e.baseUrl, t.path, t.query), i = J.has(r) ? X : Y, o = t.retries ?? i, { headers: a, correlationId: c } = D(
|
|
182
|
+
{ accept: "application/json", ...t.headers },
|
|
183
|
+
t.correlationId
|
|
184
|
+
), R = q();
|
|
185
|
+
if (R && (a["x-neetru-sdk-version"] = R), t.requireAuth) {
|
|
186
|
+
if (!e.apiKey)
|
|
187
|
+
throw new d("missing_api_key", G());
|
|
188
|
+
a.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 p = typeof t.idempotencyKey == "string" ? t.idempotencyKey : T();
|
|
192
|
+
a["idempotency-key"] = p;
|
|
160
193
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
194
|
+
let b = t.body !== void 0 && r !== "GET" && r !== "DELETE" ? JSON.stringify(t.body) : void 0;
|
|
195
|
+
b !== void 0 && (a["content-type"] = "application/json");
|
|
196
|
+
let g = null, f = t.deadlineMs != null ? Date.now() + t.deadlineMs : null;
|
|
197
|
+
for (let p = 0; p <= o; p++) {
|
|
198
|
+
if (f != null && Date.now() >= f)
|
|
199
|
+
throw g ?? new d(
|
|
200
|
+
"network_error",
|
|
201
|
+
"Deadline excedido antes de completar a request",
|
|
202
|
+
void 0,
|
|
203
|
+
void 0,
|
|
204
|
+
void 0,
|
|
205
|
+
c
|
|
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 w = { method: r, headers: a };
|
|
208
|
+
b !== void 0 && (w.body = b);
|
|
209
|
+
let $ = f != null ? Math.max(1, Math.min(3e4, f - Date.now())) : 3e4;
|
|
210
|
+
w.signal = AbortSignal.timeout($);
|
|
211
|
+
let u;
|
|
184
212
|
try {
|
|
185
|
-
|
|
186
|
-
} catch (
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
213
|
+
u = await e.fetch(n, w);
|
|
214
|
+
} catch (s) {
|
|
215
|
+
let h = s instanceof DOMException && s.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${s instanceof Error ? s.message : "fetch failed"}`;
|
|
216
|
+
if (g = new d("network_error", h, void 0, void 0, void 0, c), p < o) {
|
|
217
|
+
let k = O(p);
|
|
218
|
+
if (f == null || Date.now() + k < f) {
|
|
219
|
+
await M(k);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
192
222
|
}
|
|
193
|
-
throw
|
|
223
|
+
throw g;
|
|
194
224
|
}
|
|
195
|
-
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const parsed = await safeJson(res);
|
|
200
|
-
return parsed;
|
|
225
|
+
let L = u.headers.get("x-request-id") ?? u.headers.get("x-correlation-id") ?? void 0;
|
|
226
|
+
if (u.ok) {
|
|
227
|
+
let s = u.headers.get("x-neetru-version");
|
|
228
|
+
return s && B(s), await U(u);
|
|
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 l = await U(u), _ = Q(u.status), m = `HTTP ${u.status}`, I;
|
|
231
|
+
if (l && typeof l == "object") {
|
|
232
|
+
let s = l.error;
|
|
233
|
+
typeof s == "string" ? m = s : s && typeof s == "object" ? (typeof s.code == "string" && (_ = s.code), typeof s.message == "string" && (m = s.message), typeof s.reason == "string" && (I = s.reason)) : (typeof l.code == "string" && (_ = l.code), typeof l.message == "string" && (m = l.message)), typeof l.reason == "string" && (I = l.reason);
|
|
219
234
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
235
|
+
if (u.status === 410) {
|
|
236
|
+
let s = u.headers.get("x-neetru-replacement");
|
|
237
|
+
m === `HTTP ${u.status}` && (m = `Endpoint ${t.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), s && !m.includes(s) && (m += ` Substituto: ${s}.`);
|
|
238
|
+
}
|
|
239
|
+
let N = new d(_, m, u.status, L, I, c);
|
|
240
|
+
if (g = N, W.has(_) && p < o) {
|
|
241
|
+
let h = Z(u.headers.get("retry-after")) ?? O(p);
|
|
242
|
+
if (f == null || Date.now() + h < f) {
|
|
243
|
+
await M(h);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
228
246
|
}
|
|
229
|
-
throw
|
|
247
|
+
throw N;
|
|
230
248
|
}
|
|
231
|
-
throw
|
|
249
|
+
throw g ?? new d("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, c);
|
|
232
250
|
}
|
|
233
251
|
|
|
234
252
|
// src/validators.ts
|
|
235
|
-
var
|
|
236
|
-
function
|
|
237
|
-
if (typeof
|
|
238
|
-
throw new
|
|
253
|
+
var P = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
254
|
+
function y(e, t) {
|
|
255
|
+
if (typeof t != "string" || !P.test(t))
|
|
256
|
+
throw new d(
|
|
239
257
|
"validation_failed",
|
|
240
|
-
`${
|
|
258
|
+
`${e} must match ${P.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
241
259
|
);
|
|
242
|
-
}
|
|
243
260
|
}
|
|
244
261
|
|
|
245
262
|
// src/usage.ts
|
|
246
|
-
function
|
|
263
|
+
function ye(e) {
|
|
247
264
|
return {
|
|
248
265
|
/**
|
|
249
266
|
* Reporta consumo metered de um resource. Hit no endpoint canônico
|
|
@@ -257,46 +274,39 @@ function createUsageNamespace(config) {
|
|
|
257
274
|
* await client.usage.report('api_call', 5, { productId: 'gestovendas', tenantId: 't-acme' });
|
|
258
275
|
* ```
|
|
259
276
|
*/
|
|
260
|
-
async report(
|
|
261
|
-
if (!
|
|
262
|
-
throw new
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const tenantId = options?.tenantId ?? config.tenantId;
|
|
269
|
-
if (!productId) {
|
|
270
|
-
throw new NeetruError(
|
|
277
|
+
async report(t, r = 1, n) {
|
|
278
|
+
if (!t || typeof t != "string")
|
|
279
|
+
throw new d("validation_failed", "resource is required");
|
|
280
|
+
if (!Number.isFinite(r) || r <= 0 || !Number.isInteger(r))
|
|
281
|
+
throw new d("validation_failed", "qty must be a positive integer");
|
|
282
|
+
let i = n?.productId ?? e.productId, o = n?.tenantId ?? e.tenantId;
|
|
283
|
+
if (!i)
|
|
284
|
+
throw new d(
|
|
271
285
|
"validation_failed",
|
|
272
286
|
"productId required (pass to options or set on createNeetruClient)"
|
|
273
287
|
);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
throw new NeetruError(
|
|
288
|
+
if (!o)
|
|
289
|
+
throw new d(
|
|
277
290
|
"validation_failed",
|
|
278
291
|
"tenantId required (pass to options or set on createNeetruClient)"
|
|
279
292
|
);
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
assertValidSdkId("tenantId", tenantId);
|
|
283
|
-
const raw = await httpRequest(config, {
|
|
293
|
+
y("productId", i), y("tenantId", o);
|
|
294
|
+
let a = await E(e, {
|
|
284
295
|
method: "POST",
|
|
285
296
|
path: "/api/sdk/v1/usage/record",
|
|
286
|
-
body: { productId, tenantId, resource, qty: Math.floor(
|
|
297
|
+
body: { productId: i, tenantId: o, resource: t, qty: Math.floor(r) },
|
|
287
298
|
requireAuth: true,
|
|
288
299
|
idempotencyKey: true
|
|
289
300
|
});
|
|
290
|
-
if (!
|
|
291
|
-
throw new
|
|
292
|
-
}
|
|
301
|
+
if (!a || a.ok !== true)
|
|
302
|
+
throw new d("invalid_response", "usage.report response missing ok");
|
|
293
303
|
return {
|
|
294
304
|
ok: true,
|
|
295
|
-
counterId:
|
|
296
|
-
value:
|
|
297
|
-
limit:
|
|
298
|
-
remaining:
|
|
299
|
-
status:
|
|
305
|
+
counterId: a.counterId,
|
|
306
|
+
value: a.value,
|
|
307
|
+
limit: a.limit,
|
|
308
|
+
remaining: a.remaining,
|
|
309
|
+
status: a.status
|
|
300
310
|
};
|
|
301
311
|
},
|
|
302
312
|
/**
|
|
@@ -305,43 +315,36 @@ function createUsageNamespace(config) {
|
|
|
305
315
|
* 'subscription_inactive' | 'feature_not_in_plan' | 'limit_exceeded'),
|
|
306
316
|
* `remaining`, `limit`, `planId`, `planFeatures`.
|
|
307
317
|
*/
|
|
308
|
-
async check(
|
|
309
|
-
if (!
|
|
310
|
-
throw new
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (!productId || !tenantId) {
|
|
315
|
-
throw new NeetruError(
|
|
318
|
+
async check(t, r) {
|
|
319
|
+
if (!t || typeof t != "string")
|
|
320
|
+
throw new d("validation_failed", "resource is required");
|
|
321
|
+
let n = r?.productId ?? e.productId, i = r?.tenantId ?? e.tenantId;
|
|
322
|
+
if (!n || !i)
|
|
323
|
+
throw new d(
|
|
316
324
|
"validation_failed",
|
|
317
325
|
"productId and tenantId required (pass to options or set on createNeetruClient)"
|
|
318
326
|
);
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
assertValidSdkId("tenantId", tenantId);
|
|
322
|
-
const raw = await httpRequest(config, {
|
|
327
|
+
y("productId", n), y("tenantId", i);
|
|
328
|
+
let o = await E(e, {
|
|
323
329
|
method: "GET",
|
|
324
330
|
path: "/api/sdk/v1/entitlements",
|
|
325
|
-
query: { productId, tenantId, feature:
|
|
331
|
+
query: { productId: n, tenantId: i, feature: t },
|
|
326
332
|
requireAuth: true
|
|
327
333
|
});
|
|
328
|
-
if (!
|
|
329
|
-
throw new
|
|
330
|
-
|
|
331
|
-
const behavior = raw.behavior ?? (raw.reason === "limit_exceeded" ? "readonly" : null);
|
|
334
|
+
if (!o || typeof o.allowed != "boolean")
|
|
335
|
+
throw new d("invalid_response", "usage.check response missing allowed");
|
|
336
|
+
let a = o.behavior ?? (o.reason === "limit_exceeded" ? "readonly" : null);
|
|
332
337
|
return {
|
|
333
|
-
allowed:
|
|
334
|
-
reason:
|
|
335
|
-
remaining:
|
|
336
|
-
limit:
|
|
337
|
-
planId:
|
|
338
|
-
planFeatures:
|
|
339
|
-
behavior
|
|
338
|
+
allowed: o.allowed,
|
|
339
|
+
reason: o.reason,
|
|
340
|
+
remaining: o.remaining,
|
|
341
|
+
limit: o.limit,
|
|
342
|
+
planId: o.planId,
|
|
343
|
+
planFeatures: o.planFeatures,
|
|
344
|
+
behavior: a
|
|
340
345
|
};
|
|
341
346
|
}
|
|
342
347
|
};
|
|
343
348
|
}
|
|
344
349
|
|
|
345
|
-
exports.createUsageNamespace =
|
|
346
|
-
//# sourceMappingURL=usage.cjs.map
|
|
347
|
-
//# sourceMappingURL=usage.cjs.map
|
|
350
|
+
exports.createUsageNamespace = ye;
|
package/dist/usage.d.cts
CHANGED
package/dist/usage.d.ts
CHANGED