@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.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
|
-
var
|
|
2
|
+
var d = class e extends Error {
|
|
3
3
|
code;
|
|
4
4
|
status;
|
|
5
5
|
requestId;
|
|
@@ -12,236 +12,253 @@ 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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
/**
|
|
16
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
17
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
18
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
19
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
20
|
+
*/
|
|
21
|
+
correlationId;
|
|
22
|
+
constructor(t, r, n, i, o, a) {
|
|
23
|
+
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);
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
26
|
|
|
26
27
|
// src/idempotency.ts
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
-
throw new NeetruError(
|
|
28
|
+
function T() {
|
|
29
|
+
let e = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
30
|
+
if (e && typeof e.randomUUID == "function")
|
|
31
|
+
return e.randomUUID();
|
|
32
|
+
throw new d(
|
|
33
33
|
"runtime_unsupported",
|
|
34
34
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
// src/correlation.ts
|
|
39
|
+
var S = "x-correlation-id";
|
|
40
|
+
var j = /^[A-Za-z0-9._:\-]+$/;
|
|
41
|
+
function x(e) {
|
|
42
|
+
return typeof e == "string" && e.length > 0 && e.length <= 128 && j.test(e);
|
|
43
|
+
}
|
|
44
|
+
function F() {
|
|
45
|
+
let e = globalThis.crypto;
|
|
46
|
+
try {
|
|
47
|
+
if (e && typeof e.randomUUID == "function") return e.randomUUID();
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
let t = new Uint8Array(16), r = false;
|
|
51
|
+
try {
|
|
52
|
+
e && typeof e.getRandomValues == "function" && (e.getRandomValues(t), r = !0);
|
|
53
|
+
} catch {
|
|
54
|
+
r = false;
|
|
55
|
+
}
|
|
56
|
+
if (!r)
|
|
57
|
+
for (let i = 0; i < t.length; i++) t[i] = Math.floor(Math.random() * 256);
|
|
58
|
+
t[6] = t[6] & 15 | 64, t[8] = t[8] & 63 | 128;
|
|
59
|
+
let n = Array.from(t, (i) => i.toString(16).padStart(2, "0")).join("");
|
|
60
|
+
return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
|
|
61
|
+
}
|
|
62
|
+
function D(e, t) {
|
|
63
|
+
let r, n = {};
|
|
64
|
+
for (let [o, a] of Object.entries(e)) {
|
|
65
|
+
if (o.toLowerCase() === S) {
|
|
66
|
+
x(a) && (r = a);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
n[o] = a;
|
|
70
|
+
}
|
|
71
|
+
let i = r ?? (x(t) ? t : void 0) ?? F();
|
|
72
|
+
return n[S] = i, { headers: n, correlationId: i };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/server-only-key.ts
|
|
76
|
+
function A() {
|
|
77
|
+
try {
|
|
78
|
+
let e = globalThis.navigator;
|
|
79
|
+
if (e && e.product === "ReactNative") return "react-native";
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
let e = globalThis;
|
|
84
|
+
if (typeof e.window < "u" && typeof e.document < "u") return "browser";
|
|
85
|
+
} catch {
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
38
90
|
// src/http.ts
|
|
39
|
-
var
|
|
91
|
+
var H = {
|
|
40
92
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
41
93
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
42
94
|
3: "2026.05.1"
|
|
43
95
|
};
|
|
44
|
-
function
|
|
45
|
-
return
|
|
96
|
+
function v(e) {
|
|
97
|
+
return e.split("+")[0] ?? e;
|
|
46
98
|
}
|
|
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;
|
|
99
|
+
function V(e, t) {
|
|
100
|
+
let r = v(e).split(".").map(Number), n = v(t).split(".").map(Number), i = Math.max(r.length, n.length);
|
|
101
|
+
for (let o = 0; o < i; o++) {
|
|
102
|
+
let a = r[o] ?? 0, c = n[o] ?? 0;
|
|
103
|
+
if (a !== c) return a - c;
|
|
55
104
|
}
|
|
56
105
|
return 0;
|
|
57
106
|
}
|
|
58
|
-
function
|
|
107
|
+
function q() {
|
|
59
108
|
try {
|
|
60
|
-
return "3.
|
|
109
|
+
return "3.8.0";
|
|
61
110
|
} catch {
|
|
62
|
-
return
|
|
111
|
+
return;
|
|
63
112
|
}
|
|
64
113
|
}
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return 3;
|
|
114
|
+
function z() {
|
|
115
|
+
let e = q();
|
|
116
|
+
return e ? parseInt(e.split(".")[0] ?? "3", 10) : 3;
|
|
69
117
|
}
|
|
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
|
-
}
|
|
118
|
+
var C = /* @__PURE__ */ new Set();
|
|
119
|
+
function B(e) {
|
|
120
|
+
if (!e) return;
|
|
121
|
+
let t = z(), r = H[t];
|
|
122
|
+
if (!r) return;
|
|
123
|
+
let n = v(e);
|
|
124
|
+
if (V(n, r) < 0) {
|
|
125
|
+
let i = `${t}:${n}`;
|
|
126
|
+
C.has(i) || (C.add(i), console.warn(
|
|
127
|
+
`[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.`
|
|
128
|
+
));
|
|
85
129
|
}
|
|
86
130
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
131
|
+
function G() {
|
|
132
|
+
let e = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
133
|
+
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;
|
|
134
|
+
}
|
|
135
|
+
var X = 2, Y = 0, J = /* @__PURE__ */ new Set(["GET", "HEAD"]), W = /* @__PURE__ */ new Set([
|
|
91
136
|
"rate_limited",
|
|
92
137
|
"server_error",
|
|
93
138
|
"network_error"
|
|
94
139
|
]);
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return Math.max(50, Math.round(base + jitter));
|
|
140
|
+
function O(e) {
|
|
141
|
+
let t = 200 * Math.pow(4, e), r = t * 0.2 * (Math.random() * 2 - 1);
|
|
142
|
+
return Math.max(50, Math.round(t + r));
|
|
99
143
|
}
|
|
100
|
-
var
|
|
101
|
-
function
|
|
102
|
-
if (!
|
|
103
|
-
|
|
104
|
-
if (Number.isFinite(
|
|
105
|
-
|
|
106
|
-
if (Number.isFinite(
|
|
107
|
-
|
|
108
|
-
if (
|
|
144
|
+
var K = 6e4;
|
|
145
|
+
function Z(e) {
|
|
146
|
+
if (!e) return null;
|
|
147
|
+
let t = Number(e);
|
|
148
|
+
if (Number.isFinite(t) && t >= 0) return Math.min(Math.round(t * 1e3), K);
|
|
149
|
+
let r = Date.parse(e);
|
|
150
|
+
if (Number.isFinite(r)) {
|
|
151
|
+
let n = r - Date.now();
|
|
152
|
+
if (n > 0) return Math.min(n, K);
|
|
109
153
|
}
|
|
110
154
|
return null;
|
|
111
155
|
}
|
|
112
|
-
function
|
|
113
|
-
return new Promise((
|
|
156
|
+
function M(e) {
|
|
157
|
+
return new Promise((t) => setTimeout(t, e));
|
|
158
|
+
}
|
|
159
|
+
function Q(e) {
|
|
160
|
+
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";
|
|
114
161
|
}
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (status === 429) return "rate_limited";
|
|
122
|
-
if (status >= 500) return "server_error";
|
|
123
|
-
return "unknown";
|
|
162
|
+
function ee(e, t, r) {
|
|
163
|
+
let n = e.replace(/\/+$/, ""), i = t.startsWith("/") ? t : `/${t}`, o = new URL(`${n}${i}`);
|
|
164
|
+
if (r)
|
|
165
|
+
for (let [a, c] of Object.entries(r))
|
|
166
|
+
c !== void 0 && o.searchParams.set(a, String(c));
|
|
167
|
+
return o.toString();
|
|
124
168
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
url.searchParams.set(k, String(v));
|
|
169
|
+
async function U(e) {
|
|
170
|
+
let t = await e.text();
|
|
171
|
+
if (t)
|
|
172
|
+
try {
|
|
173
|
+
return JSON.parse(t);
|
|
174
|
+
} catch {
|
|
175
|
+
return;
|
|
133
176
|
}
|
|
134
|
-
}
|
|
135
|
-
return url.toString();
|
|
136
177
|
}
|
|
137
|
-
async function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
178
|
+
async function E(e, t) {
|
|
179
|
+
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(
|
|
180
|
+
{ accept: "application/json", ...t.headers },
|
|
181
|
+
t.correlationId
|
|
182
|
+
), R = q();
|
|
183
|
+
if (R && (a["x-neetru-sdk-version"] = R), t.requireAuth) {
|
|
184
|
+
if (!e.apiKey)
|
|
185
|
+
throw new d("missing_api_key", G());
|
|
186
|
+
a.authorization = `Bearer ${e.apiKey}`;
|
|
144
187
|
}
|
|
145
|
-
|
|
146
|
-
|
|
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 = {
|
|
152
|
-
accept: "application/json",
|
|
153
|
-
...opts.headers
|
|
154
|
-
};
|
|
155
|
-
const sdkVersion = readInjectedSdkVersion();
|
|
156
|
-
if (sdkVersion) {
|
|
157
|
-
headers["x-neetru-sdk-version"] = sdkVersion;
|
|
188
|
+
if (t.idempotencyKey) {
|
|
189
|
+
let p = typeof t.idempotencyKey == "string" ? t.idempotencyKey : T();
|
|
190
|
+
a["idempotency-key"] = p;
|
|
158
191
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
192
|
+
let b = t.body !== void 0 && r !== "GET" && r !== "DELETE" ? JSON.stringify(t.body) : void 0;
|
|
193
|
+
b !== void 0 && (a["content-type"] = "application/json");
|
|
194
|
+
let g = null, f = t.deadlineMs != null ? Date.now() + t.deadlineMs : null;
|
|
195
|
+
for (let p = 0; p <= o; p++) {
|
|
196
|
+
if (f != null && Date.now() >= f)
|
|
197
|
+
throw g ?? new d(
|
|
198
|
+
"network_error",
|
|
199
|
+
"Deadline excedido antes de completar a request",
|
|
200
|
+
void 0,
|
|
201
|
+
void 0,
|
|
202
|
+
void 0,
|
|
203
|
+
c
|
|
164
204
|
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
headers["idempotency-key"] = key;
|
|
171
|
-
}
|
|
172
|
-
const bodyString = opts.body !== void 0 && method !== "GET" && method !== "DELETE" ? JSON.stringify(opts.body) : void 0;
|
|
173
|
-
if (bodyString !== void 0) {
|
|
174
|
-
headers["content-type"] = "application/json";
|
|
175
|
-
}
|
|
176
|
-
let lastError = null;
|
|
177
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
178
|
-
const init = { method, headers };
|
|
179
|
-
if (bodyString !== void 0) init.body = bodyString;
|
|
180
|
-
init.signal = AbortSignal.timeout(3e4);
|
|
181
|
-
let res;
|
|
205
|
+
let w = { method: r, headers: a };
|
|
206
|
+
b !== void 0 && (w.body = b);
|
|
207
|
+
let $ = f != null ? Math.max(1, Math.min(3e4, f - Date.now())) : 3e4;
|
|
208
|
+
w.signal = AbortSignal.timeout($);
|
|
209
|
+
let u;
|
|
182
210
|
try {
|
|
183
|
-
|
|
184
|
-
} catch (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
211
|
+
u = await e.fetch(n, w);
|
|
212
|
+
} catch (s) {
|
|
213
|
+
let h = s instanceof DOMException && s.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${s instanceof Error ? s.message : "fetch failed"}`;
|
|
214
|
+
if (g = new d("network_error", h, void 0, void 0, void 0, c), p < o) {
|
|
215
|
+
let k = O(p);
|
|
216
|
+
if (f == null || Date.now() + k < f) {
|
|
217
|
+
await M(k);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
190
220
|
}
|
|
191
|
-
throw
|
|
221
|
+
throw g;
|
|
192
222
|
}
|
|
193
|
-
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const parsed = await safeJson(res);
|
|
198
|
-
return parsed;
|
|
223
|
+
let L = u.headers.get("x-request-id") ?? u.headers.get("x-correlation-id") ?? void 0;
|
|
224
|
+
if (u.ok) {
|
|
225
|
+
let s = u.headers.get("x-neetru-version");
|
|
226
|
+
return s && B(s), await U(u);
|
|
199
227
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (body && typeof body === "object") {
|
|
205
|
-
const errField = body.error;
|
|
206
|
-
if (typeof errField === "string") {
|
|
207
|
-
message = errField;
|
|
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;
|
|
228
|
+
let l = await U(u), _ = Q(u.status), m = `HTTP ${u.status}`, I;
|
|
229
|
+
if (l && typeof l == "object") {
|
|
230
|
+
let s = l.error;
|
|
231
|
+
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);
|
|
217
232
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
233
|
+
if (u.status === 410) {
|
|
234
|
+
let s = u.headers.get("x-neetru-replacement");
|
|
235
|
+
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}.`);
|
|
236
|
+
}
|
|
237
|
+
let N = new d(_, m, u.status, L, I, c);
|
|
238
|
+
if (g = N, W.has(_) && p < o) {
|
|
239
|
+
let h = Z(u.headers.get("retry-after")) ?? O(p);
|
|
240
|
+
if (f == null || Date.now() + h < f) {
|
|
241
|
+
await M(h);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
226
244
|
}
|
|
227
|
-
throw
|
|
245
|
+
throw N;
|
|
228
246
|
}
|
|
229
|
-
throw
|
|
247
|
+
throw g ?? new d("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, c);
|
|
230
248
|
}
|
|
231
249
|
|
|
232
250
|
// src/validators.ts
|
|
233
|
-
var
|
|
234
|
-
function
|
|
235
|
-
if (typeof
|
|
236
|
-
throw new
|
|
251
|
+
var P = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
252
|
+
function y(e, t) {
|
|
253
|
+
if (typeof t != "string" || !P.test(t))
|
|
254
|
+
throw new d(
|
|
237
255
|
"validation_failed",
|
|
238
|
-
`${
|
|
256
|
+
`${e} must match ${P.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
239
257
|
);
|
|
240
|
-
}
|
|
241
258
|
}
|
|
242
259
|
|
|
243
260
|
// src/usage.ts
|
|
244
|
-
function
|
|
261
|
+
function ye(e) {
|
|
245
262
|
return {
|
|
246
263
|
/**
|
|
247
264
|
* Reporta consumo metered de um resource. Hit no endpoint canônico
|
|
@@ -255,46 +272,39 @@ function createUsageNamespace(config) {
|
|
|
255
272
|
* await client.usage.report('api_call', 5, { productId: 'gestovendas', tenantId: 't-acme' });
|
|
256
273
|
* ```
|
|
257
274
|
*/
|
|
258
|
-
async report(
|
|
259
|
-
if (!
|
|
260
|
-
throw new
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const tenantId = options?.tenantId ?? config.tenantId;
|
|
267
|
-
if (!productId) {
|
|
268
|
-
throw new NeetruError(
|
|
275
|
+
async report(t, r = 1, n) {
|
|
276
|
+
if (!t || typeof t != "string")
|
|
277
|
+
throw new d("validation_failed", "resource is required");
|
|
278
|
+
if (!Number.isFinite(r) || r <= 0 || !Number.isInteger(r))
|
|
279
|
+
throw new d("validation_failed", "qty must be a positive integer");
|
|
280
|
+
let i = n?.productId ?? e.productId, o = n?.tenantId ?? e.tenantId;
|
|
281
|
+
if (!i)
|
|
282
|
+
throw new d(
|
|
269
283
|
"validation_failed",
|
|
270
284
|
"productId required (pass to options or set on createNeetruClient)"
|
|
271
285
|
);
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
throw new NeetruError(
|
|
286
|
+
if (!o)
|
|
287
|
+
throw new d(
|
|
275
288
|
"validation_failed",
|
|
276
289
|
"tenantId required (pass to options or set on createNeetruClient)"
|
|
277
290
|
);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
assertValidSdkId("tenantId", tenantId);
|
|
281
|
-
const raw = await httpRequest(config, {
|
|
291
|
+
y("productId", i), y("tenantId", o);
|
|
292
|
+
let a = await E(e, {
|
|
282
293
|
method: "POST",
|
|
283
294
|
path: "/api/sdk/v1/usage/record",
|
|
284
|
-
body: { productId, tenantId, resource, qty: Math.floor(
|
|
295
|
+
body: { productId: i, tenantId: o, resource: t, qty: Math.floor(r) },
|
|
285
296
|
requireAuth: true,
|
|
286
297
|
idempotencyKey: true
|
|
287
298
|
});
|
|
288
|
-
if (!
|
|
289
|
-
throw new
|
|
290
|
-
}
|
|
299
|
+
if (!a || a.ok !== true)
|
|
300
|
+
throw new d("invalid_response", "usage.report response missing ok");
|
|
291
301
|
return {
|
|
292
302
|
ok: true,
|
|
293
|
-
counterId:
|
|
294
|
-
value:
|
|
295
|
-
limit:
|
|
296
|
-
remaining:
|
|
297
|
-
status:
|
|
303
|
+
counterId: a.counterId,
|
|
304
|
+
value: a.value,
|
|
305
|
+
limit: a.limit,
|
|
306
|
+
remaining: a.remaining,
|
|
307
|
+
status: a.status
|
|
298
308
|
};
|
|
299
309
|
},
|
|
300
310
|
/**
|
|
@@ -303,43 +313,36 @@ function createUsageNamespace(config) {
|
|
|
303
313
|
* 'subscription_inactive' | 'feature_not_in_plan' | 'limit_exceeded'),
|
|
304
314
|
* `remaining`, `limit`, `planId`, `planFeatures`.
|
|
305
315
|
*/
|
|
306
|
-
async check(
|
|
307
|
-
if (!
|
|
308
|
-
throw new
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (!productId || !tenantId) {
|
|
313
|
-
throw new NeetruError(
|
|
316
|
+
async check(t, r) {
|
|
317
|
+
if (!t || typeof t != "string")
|
|
318
|
+
throw new d("validation_failed", "resource is required");
|
|
319
|
+
let n = r?.productId ?? e.productId, i = r?.tenantId ?? e.tenantId;
|
|
320
|
+
if (!n || !i)
|
|
321
|
+
throw new d(
|
|
314
322
|
"validation_failed",
|
|
315
323
|
"productId and tenantId required (pass to options or set on createNeetruClient)"
|
|
316
324
|
);
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
assertValidSdkId("tenantId", tenantId);
|
|
320
|
-
const raw = await httpRequest(config, {
|
|
325
|
+
y("productId", n), y("tenantId", i);
|
|
326
|
+
let o = await E(e, {
|
|
321
327
|
method: "GET",
|
|
322
328
|
path: "/api/sdk/v1/entitlements",
|
|
323
|
-
query: { productId, tenantId, feature:
|
|
329
|
+
query: { productId: n, tenantId: i, feature: t },
|
|
324
330
|
requireAuth: true
|
|
325
331
|
});
|
|
326
|
-
if (!
|
|
327
|
-
throw new
|
|
328
|
-
|
|
329
|
-
const behavior = raw.behavior ?? (raw.reason === "limit_exceeded" ? "readonly" : null);
|
|
332
|
+
if (!o || typeof o.allowed != "boolean")
|
|
333
|
+
throw new d("invalid_response", "usage.check response missing allowed");
|
|
334
|
+
let a = o.behavior ?? (o.reason === "limit_exceeded" ? "readonly" : null);
|
|
330
335
|
return {
|
|
331
|
-
allowed:
|
|
332
|
-
reason:
|
|
333
|
-
remaining:
|
|
334
|
-
limit:
|
|
335
|
-
planId:
|
|
336
|
-
planFeatures:
|
|
337
|
-
behavior
|
|
336
|
+
allowed: o.allowed,
|
|
337
|
+
reason: o.reason,
|
|
338
|
+
remaining: o.remaining,
|
|
339
|
+
limit: o.limit,
|
|
340
|
+
planId: o.planId,
|
|
341
|
+
planFeatures: o.planFeatures,
|
|
342
|
+
behavior: a
|
|
338
343
|
};
|
|
339
344
|
}
|
|
340
345
|
};
|
|
341
346
|
}
|
|
342
347
|
|
|
343
|
-
export { createUsageNamespace };
|
|
344
|
-
//# sourceMappingURL=usage.mjs.map
|
|
345
|
-
//# sourceMappingURL=usage.mjs.map
|
|
348
|
+
export { ye as createUsageNamespace };
|