@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/checkout.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
|
-
var
|
|
2
|
+
var i = class t extends Error {
|
|
3
3
|
code;
|
|
4
4
|
status;
|
|
5
5
|
requestId;
|
|
@@ -12,424 +12,408 @@ 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(e, n, r, o, a, c) {
|
|
23
|
+
super(n), this.name = "NeetruError", this.code = e, this.status = r, this.requestId = o, this.reason = a, this.correlationId = c, Object.setPrototypeOf(this, t.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 E() {
|
|
29
|
+
let t = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
30
|
+
if (t && typeof t.randomUUID == "function")
|
|
31
|
+
return t.randomUUID();
|
|
32
|
+
throw new i(
|
|
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 A = "x-correlation-id";
|
|
40
|
+
var L = /^[A-Za-z0-9._:\-]+$/;
|
|
41
|
+
function x(t) {
|
|
42
|
+
return typeof t == "string" && t.length > 0 && t.length <= 128 && L.test(t);
|
|
43
|
+
}
|
|
44
|
+
function H() {
|
|
45
|
+
let t = globalThis.crypto;
|
|
46
|
+
try {
|
|
47
|
+
if (t && typeof t.randomUUID == "function") return t.randomUUID();
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
let e = new Uint8Array(16), n = false;
|
|
51
|
+
try {
|
|
52
|
+
t && typeof t.getRandomValues == "function" && (t.getRandomValues(e), n = !0);
|
|
53
|
+
} catch {
|
|
54
|
+
n = false;
|
|
55
|
+
}
|
|
56
|
+
if (!n)
|
|
57
|
+
for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
|
|
58
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
59
|
+
let r = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
60
|
+
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
61
|
+
}
|
|
62
|
+
function N(t, e) {
|
|
63
|
+
let n, r = {};
|
|
64
|
+
for (let [a, c] of Object.entries(t)) {
|
|
65
|
+
if (a.toLowerCase() === A) {
|
|
66
|
+
x(c) && (n = c);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
r[a] = c;
|
|
70
|
+
}
|
|
71
|
+
let o = n ?? (x(e) ? e : void 0) ?? H();
|
|
72
|
+
return r[A] = o, { headers: r, correlationId: o };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/server-only-key.ts
|
|
76
|
+
function D() {
|
|
77
|
+
try {
|
|
78
|
+
let t = globalThis.navigator;
|
|
79
|
+
if (t && t.product === "ReactNative") return "react-native";
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
let t = globalThis;
|
|
84
|
+
if (typeof t.window < "u" && typeof t.document < "u") return "browser";
|
|
85
|
+
} catch {
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
38
90
|
// src/http.ts
|
|
39
|
-
var
|
|
91
|
+
var B = {
|
|
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 b(t) {
|
|
97
|
+
return t.split("+")[0] ?? t;
|
|
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 G(t, e) {
|
|
100
|
+
let n = b(t).split(".").map(Number), r = b(e).split(".").map(Number), o = Math.max(n.length, r.length);
|
|
101
|
+
for (let a = 0; a < o; a++) {
|
|
102
|
+
let c = n[a] ?? 0, u = r[a] ?? 0;
|
|
103
|
+
if (c !== u) return c - u;
|
|
55
104
|
}
|
|
56
105
|
return 0;
|
|
57
106
|
}
|
|
58
|
-
function
|
|
107
|
+
function M() {
|
|
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 F() {
|
|
115
|
+
let t = M();
|
|
116
|
+
return t ? parseInt(t.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 U = /* @__PURE__ */ new Set();
|
|
119
|
+
function V(t) {
|
|
120
|
+
if (!t) return;
|
|
121
|
+
let e = F(), n = B[e];
|
|
122
|
+
if (!n) return;
|
|
123
|
+
let r = b(t);
|
|
124
|
+
if (G(r, n) < 0) {
|
|
125
|
+
let o = `${e}:${r}`;
|
|
126
|
+
U.has(o) || (U.add(o), console.warn(
|
|
127
|
+
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${t}" \xE9 anterior ao m\xEDnimo recomendado "${n}" para SDK major ${e}. 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 z() {
|
|
132
|
+
let t = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
133
|
+
return D() ? "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. (" + t + ")" : t;
|
|
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(t) {
|
|
141
|
+
let e = 200 * Math.pow(4, t), n = e * 0.2 * (Math.random() * 2 - 1);
|
|
142
|
+
return Math.max(50, Math.round(e + n));
|
|
99
143
|
}
|
|
100
|
-
var
|
|
101
|
-
function
|
|
102
|
-
if (!
|
|
103
|
-
|
|
104
|
-
if (Number.isFinite(
|
|
105
|
-
|
|
106
|
-
if (Number.isFinite(
|
|
107
|
-
|
|
108
|
-
if (
|
|
144
|
+
var q = 6e4;
|
|
145
|
+
function Z(t) {
|
|
146
|
+
if (!t) return null;
|
|
147
|
+
let e = Number(t);
|
|
148
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), q);
|
|
149
|
+
let n = Date.parse(t);
|
|
150
|
+
if (Number.isFinite(n)) {
|
|
151
|
+
let r = n - Date.now();
|
|
152
|
+
if (r > 0) return Math.min(r, q);
|
|
109
153
|
}
|
|
110
154
|
return null;
|
|
111
155
|
}
|
|
112
|
-
function
|
|
113
|
-
return new Promise((
|
|
156
|
+
function K(t) {
|
|
157
|
+
return new Promise((e) => setTimeout(e, t));
|
|
114
158
|
}
|
|
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";
|
|
159
|
+
function Q(t) {
|
|
160
|
+
return t === 401 ? "unauthorized" : t === 403 ? "forbidden" : t === 404 ? "not_found" : t === 409 ? "conflict" : t === 410 ? "gone" : t === 422 || t === 400 ? "validation_failed" : t === 429 ? "rate_limited" : t >= 500 ? "server_error" : "unknown";
|
|
124
161
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
162
|
+
function ee(t, e, n) {
|
|
163
|
+
let r = t.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, a = new URL(`${r}${o}`);
|
|
164
|
+
if (n)
|
|
165
|
+
for (let [c, u] of Object.entries(n))
|
|
166
|
+
u !== void 0 && a.searchParams.set(c, String(u));
|
|
167
|
+
return a.toString();
|
|
168
|
+
}
|
|
169
|
+
async function P(t) {
|
|
170
|
+
let e = await t.text();
|
|
171
|
+
if (e)
|
|
172
|
+
try {
|
|
173
|
+
return JSON.parse(e);
|
|
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 y(t, e) {
|
|
179
|
+
let n = e.method ?? "GET", r = ee(t.baseUrl, e.path, e.query), o = J.has(n) ? X : Y, a = e.retries ?? o, { headers: c, correlationId: u } = N(
|
|
180
|
+
{ accept: "application/json", ...e.headers },
|
|
181
|
+
e.correlationId
|
|
182
|
+
), C = M();
|
|
183
|
+
if (C && (c["x-neetru-sdk-version"] = C), e.requireAuth) {
|
|
184
|
+
if (!t.apiKey)
|
|
185
|
+
throw new i("missing_api_key", z());
|
|
186
|
+
c.authorization = `Bearer ${t.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 (e.idempotencyKey) {
|
|
189
|
+
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey : E();
|
|
190
|
+
c["idempotency-key"] = f;
|
|
158
191
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
192
|
+
let I = e.body !== void 0 && n !== "GET" && n !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
193
|
+
I !== void 0 && (c["content-type"] = "application/json");
|
|
194
|
+
let g = null, p = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
195
|
+
for (let f = 0; f <= a; f++) {
|
|
196
|
+
if (p != null && Date.now() >= p)
|
|
197
|
+
throw g ?? new i(
|
|
198
|
+
"network_error",
|
|
199
|
+
"Deadline excedido antes de completar a request",
|
|
200
|
+
void 0,
|
|
201
|
+
void 0,
|
|
202
|
+
void 0,
|
|
203
|
+
u
|
|
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 _ = { method: n, headers: c };
|
|
206
|
+
I !== void 0 && (_.body = I);
|
|
207
|
+
let $ = p != null ? Math.max(1, Math.min(3e4, p - Date.now())) : 3e4;
|
|
208
|
+
_.signal = AbortSignal.timeout($);
|
|
209
|
+
let d;
|
|
182
210
|
try {
|
|
183
|
-
|
|
184
|
-
} catch (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
211
|
+
d = await t.fetch(r, _);
|
|
212
|
+
} catch (s) {
|
|
213
|
+
let m = s instanceof DOMException && s.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${s instanceof Error ? s.message : "fetch failed"}`;
|
|
214
|
+
if (g = new i("network_error", m, void 0, void 0, void 0, u), f < a) {
|
|
215
|
+
let S = O(f);
|
|
216
|
+
if (p == null || Date.now() + S < p) {
|
|
217
|
+
await K(S);
|
|
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 j = d.headers.get("x-request-id") ?? d.headers.get("x-correlation-id") ?? void 0;
|
|
224
|
+
if (d.ok) {
|
|
225
|
+
let s = d.headers.get("x-neetru-version");
|
|
226
|
+
return s && V(s), await P(d);
|
|
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 P(d), k = Q(d.status), h = `HTTP ${d.status}`, w;
|
|
229
|
+
if (l && typeof l == "object") {
|
|
230
|
+
let s = l.error;
|
|
231
|
+
typeof s == "string" ? h = s : s && typeof s == "object" ? (typeof s.code == "string" && (k = s.code), typeof s.message == "string" && (h = s.message), typeof s.reason == "string" && (w = s.reason)) : (typeof l.code == "string" && (k = l.code), typeof l.message == "string" && (h = l.message)), typeof l.reason == "string" && (w = l.reason);
|
|
217
232
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (isRetryable && attempt < maxRetries) {
|
|
222
|
-
const retryAfter = parseRetryAfter(res.headers.get("retry-after"));
|
|
223
|
-
const delay = retryAfter ?? backoffMs(attempt);
|
|
224
|
-
await sleep(delay);
|
|
225
|
-
continue;
|
|
233
|
+
if (d.status === 410) {
|
|
234
|
+
let s = d.headers.get("x-neetru-replacement");
|
|
235
|
+
h === `HTTP ${d.status}` && (h = `Endpoint ${e.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}.`);
|
|
226
236
|
}
|
|
227
|
-
|
|
237
|
+
let v = new i(k, h, d.status, j, w, u);
|
|
238
|
+
if (g = v, W.has(k) && f < a) {
|
|
239
|
+
let m = Z(d.headers.get("retry-after")) ?? O(f);
|
|
240
|
+
if (p == null || Date.now() + m < p) {
|
|
241
|
+
await K(m);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
throw v;
|
|
228
246
|
}
|
|
229
|
-
throw
|
|
247
|
+
throw g ?? new i("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, u);
|
|
230
248
|
}
|
|
231
249
|
|
|
232
250
|
// src/checkout.ts
|
|
233
|
-
function
|
|
234
|
-
|
|
235
|
-
if (!
|
|
236
|
-
throw new
|
|
251
|
+
function T(t, e) {
|
|
252
|
+
let n = t?.accessToken || t?.idToken;
|
|
253
|
+
if (!n || typeof n != "string")
|
|
254
|
+
throw new i(
|
|
237
255
|
"validation_failed",
|
|
238
|
-
`checkout.${
|
|
256
|
+
`checkout.${e} requer o access_token OU o id_token OIDC do usu\xE1rio final (passe \`accessToken\` ou \`idToken\`). A chave de API do SDK (nrt_*) N\xC3O \xE9 aceita pelo endpoint de checkout \u2014 ele autentica a identidade do cliente que est\xE1 assinando (Bearer access_token/id_token OIDC ou cookie de sess\xE3o). No padr\xE3o BFF/cookie, passe o \`idToken\` que seu servidor guardou no cookie httpOnly.`
|
|
239
257
|
);
|
|
240
|
-
}
|
|
241
|
-
return { requireAuth: false, headers: { authorization: `Bearer ${token}` } };
|
|
258
|
+
return { requireAuth: false, headers: { authorization: `Bearer ${n}` } };
|
|
242
259
|
}
|
|
243
|
-
function
|
|
244
|
-
if (!
|
|
245
|
-
throw new
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (typeof r.redirectUrl !== "string" || !r.redirectUrl) {
|
|
252
|
-
throw new NeetruError("invalid_response", "checkout.start response missing redirectUrl");
|
|
253
|
-
}
|
|
260
|
+
function te(t) {
|
|
261
|
+
if (!t || typeof t != "object")
|
|
262
|
+
throw new i("invalid_response", "checkout.start response is not an object");
|
|
263
|
+
let e = t;
|
|
264
|
+
if (typeof e.intentId != "string" || !e.intentId)
|
|
265
|
+
throw new i("invalid_response", "checkout.start response missing intentId");
|
|
266
|
+
if (typeof e.redirectUrl != "string" || !e.redirectUrl)
|
|
267
|
+
throw new i("invalid_response", "checkout.start response missing redirectUrl");
|
|
254
268
|
return {
|
|
255
|
-
intentId:
|
|
256
|
-
redirectUrl:
|
|
257
|
-
status:
|
|
258
|
-
expiresAt: typeof
|
|
259
|
-
requiresKyc:
|
|
269
|
+
intentId: e.intentId,
|
|
270
|
+
redirectUrl: e.redirectUrl,
|
|
271
|
+
status: e.status ?? "pending",
|
|
272
|
+
expiresAt: typeof e.expiresAt == "string" ? e.expiresAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
273
|
+
requiresKyc: e.requiresKyc === true
|
|
260
274
|
};
|
|
261
275
|
}
|
|
262
|
-
function
|
|
263
|
-
if (!
|
|
264
|
-
throw new
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (
|
|
269
|
-
throw new
|
|
270
|
-
}
|
|
271
|
-
if (typeof intent.intentId !== "string") {
|
|
272
|
-
throw new NeetruError("invalid_response", "checkout.get response missing intentId");
|
|
273
|
-
}
|
|
276
|
+
function ne(t) {
|
|
277
|
+
if (!t || typeof t != "object")
|
|
278
|
+
throw new i("invalid_response", "checkout.get response is not an object");
|
|
279
|
+
let e = t, n = e.intent;
|
|
280
|
+
if (!n || typeof n != "object")
|
|
281
|
+
throw new i("invalid_response", "checkout.get response missing intent");
|
|
282
|
+
if (typeof n.intentId != "string")
|
|
283
|
+
throw new i("invalid_response", "checkout.get response missing intentId");
|
|
274
284
|
return {
|
|
275
|
-
intentId:
|
|
276
|
-
uid:
|
|
277
|
-
targetTenantId:
|
|
278
|
-
targetTenantType:
|
|
279
|
-
productId:
|
|
280
|
-
planId:
|
|
281
|
-
callbackUrl:
|
|
282
|
-
status:
|
|
283
|
-
stripeSessionId:
|
|
284
|
-
expiresAt:
|
|
285
|
-
isStale:
|
|
285
|
+
intentId: n.intentId,
|
|
286
|
+
uid: n.uid ?? "",
|
|
287
|
+
targetTenantId: n.targetTenantId ?? "",
|
|
288
|
+
targetTenantType: n.targetTenantType ?? "pf",
|
|
289
|
+
productId: n.productId ?? "",
|
|
290
|
+
planId: n.planId ?? "",
|
|
291
|
+
callbackUrl: n.callbackUrl ?? "",
|
|
292
|
+
status: n.status ?? "pending",
|
|
293
|
+
stripeSessionId: n.stripeSessionId ?? null,
|
|
294
|
+
expiresAt: n.expiresAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
295
|
+
isStale: e.isStale === true
|
|
286
296
|
};
|
|
287
297
|
}
|
|
288
|
-
function
|
|
298
|
+
function re() {
|
|
289
299
|
try {
|
|
290
|
-
return typeof globalThis
|
|
300
|
+
return typeof globalThis < "u" && typeof globalThis.window < "u" && typeof globalThis.location < "u" && typeof globalThis.location.assign == "function";
|
|
291
301
|
} catch {
|
|
292
302
|
return false;
|
|
293
303
|
}
|
|
294
304
|
}
|
|
295
|
-
function
|
|
305
|
+
function oe(t) {
|
|
296
306
|
try {
|
|
297
|
-
globalThis.location.assign(
|
|
307
|
+
globalThis.location.assign(t);
|
|
298
308
|
} catch {
|
|
299
309
|
}
|
|
300
310
|
}
|
|
301
|
-
function
|
|
311
|
+
function ie(t) {
|
|
302
312
|
return {
|
|
303
|
-
async start(
|
|
304
|
-
if (!
|
|
305
|
-
throw new
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
productId: input.productId,
|
|
315
|
-
planId: input.planId,
|
|
316
|
-
callbackUrl: input.callbackUrl
|
|
313
|
+
async start(e) {
|
|
314
|
+
if (!e?.productId)
|
|
315
|
+
throw new i("validation_failed", "checkout.start: productId is required");
|
|
316
|
+
if (!e?.planId)
|
|
317
|
+
throw new i("validation_failed", "checkout.start: planId is required");
|
|
318
|
+
if (!e?.callbackUrl)
|
|
319
|
+
throw new i("validation_failed", "checkout.start: callbackUrl is required");
|
|
320
|
+
let n = {
|
|
321
|
+
productId: e.productId,
|
|
322
|
+
planId: e.planId,
|
|
323
|
+
callbackUrl: e.callbackUrl
|
|
317
324
|
};
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
{ accessToken: input.accessToken, idToken: input.idToken },
|
|
325
|
+
e.tenantType && (n.targetTenantType = e.tenantType), e.tenantId && (n.targetTenantId = e.tenantId);
|
|
326
|
+
let { requireAuth: r, headers: o } = T(
|
|
327
|
+
{ accessToken: e.accessToken, idToken: e.idToken },
|
|
322
328
|
"start"
|
|
323
|
-
)
|
|
324
|
-
const raw = await httpRequest(config, {
|
|
329
|
+
), a = await y(t, {
|
|
325
330
|
method: "POST",
|
|
326
331
|
path: "/api/v1/checkout/intents",
|
|
327
|
-
body,
|
|
328
|
-
requireAuth,
|
|
329
|
-
headers,
|
|
332
|
+
body: n,
|
|
333
|
+
requireAuth: r,
|
|
334
|
+
headers: o,
|
|
330
335
|
idempotencyKey: true
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const shouldRedirect = input.autoRedirect !== false;
|
|
334
|
-
if (shouldRedirect && inBrowser()) {
|
|
335
|
-
performRedirect(result.redirectUrl);
|
|
336
|
-
}
|
|
337
|
-
return result;
|
|
336
|
+
}), c = te(a);
|
|
337
|
+
return e.autoRedirect !== false && re() && oe(c.redirectUrl), c;
|
|
338
338
|
},
|
|
339
|
-
async get(
|
|
340
|
-
if (!
|
|
341
|
-
throw new
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
{ accessToken: opts?.accessToken, idToken: opts?.idToken },
|
|
339
|
+
async get(e, n) {
|
|
340
|
+
if (!e || typeof e != "string")
|
|
341
|
+
throw new i("validation_failed", "checkout.get: intentId is required");
|
|
342
|
+
let { requireAuth: r, headers: o } = T(
|
|
343
|
+
{ accessToken: n?.accessToken, idToken: n?.idToken },
|
|
345
344
|
"get"
|
|
346
|
-
)
|
|
347
|
-
const raw = await httpRequest(config, {
|
|
345
|
+
), a = await y(t, {
|
|
348
346
|
method: "GET",
|
|
349
|
-
path: `/api/v1/checkout/intents/${encodeURIComponent(
|
|
350
|
-
requireAuth,
|
|
351
|
-
headers
|
|
347
|
+
path: `/api/v1/checkout/intents/${encodeURIComponent(e)}`,
|
|
348
|
+
requireAuth: r,
|
|
349
|
+
headers: o
|
|
352
350
|
});
|
|
353
|
-
return
|
|
351
|
+
return ne(a);
|
|
354
352
|
},
|
|
355
|
-
async cancel(
|
|
356
|
-
if (!
|
|
357
|
-
throw new
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
{ accessToken: opts?.accessToken, idToken: opts?.idToken },
|
|
353
|
+
async cancel(e, n) {
|
|
354
|
+
if (!e || typeof e != "string")
|
|
355
|
+
throw new i("validation_failed", "checkout.cancel: intentId is required");
|
|
356
|
+
let { requireAuth: r, headers: o } = T(
|
|
357
|
+
{ accessToken: n?.accessToken, idToken: n?.idToken },
|
|
361
358
|
"cancel"
|
|
362
359
|
);
|
|
363
|
-
const raw = await httpRequest(config, {
|
|
364
|
-
method: "DELETE",
|
|
365
|
-
path: `/api/v1/checkout/intents/${encodeURIComponent(intentId)}`,
|
|
366
|
-
requireAuth,
|
|
367
|
-
headers
|
|
368
|
-
});
|
|
369
360
|
return {
|
|
370
361
|
ok: true,
|
|
371
|
-
alreadyCancelled:
|
|
362
|
+
alreadyCancelled: (await y(t, {
|
|
363
|
+
method: "DELETE",
|
|
364
|
+
path: `/api/v1/checkout/intents/${encodeURIComponent(e)}`,
|
|
365
|
+
requireAuth: r,
|
|
366
|
+
headers: o
|
|
367
|
+
}))?.alreadyCancelled === true
|
|
372
368
|
};
|
|
373
369
|
}
|
|
374
370
|
};
|
|
375
371
|
}
|
|
376
|
-
var
|
|
372
|
+
var R = class {
|
|
377
373
|
intents = /* @__PURE__ */ new Map();
|
|
378
|
-
async start(
|
|
379
|
-
if (!
|
|
380
|
-
throw new
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const intentId = `chk_mock_${Math.random().toString(36).slice(2, 10)}`;
|
|
389
|
-
const expiresAt = new Date(Date.now() + 15 * 60 * 1e3).toISOString();
|
|
390
|
-
const redirectUrl = `https://localhost:9003/portal/checkout/${intentId}`;
|
|
391
|
-
this.intents.set(intentId, {
|
|
392
|
-
intentId,
|
|
374
|
+
async start(e) {
|
|
375
|
+
if (!e?.productId)
|
|
376
|
+
throw new i("validation_failed", "checkout.start: productId is required");
|
|
377
|
+
if (!e?.planId)
|
|
378
|
+
throw new i("validation_failed", "checkout.start: planId is required");
|
|
379
|
+
if (!e?.callbackUrl)
|
|
380
|
+
throw new i("validation_failed", "checkout.start: callbackUrl is required");
|
|
381
|
+
let n = `chk_mock_${Math.random().toString(36).slice(2, 10)}`, r = new Date(Date.now() + 900 * 1e3).toISOString(), o = `https://localhost:9003/portal/checkout/${n}`;
|
|
382
|
+
return this.intents.set(n, {
|
|
383
|
+
intentId: n,
|
|
393
384
|
uid: "dev-fixture-uid",
|
|
394
|
-
targetTenantId:
|
|
395
|
-
targetTenantType:
|
|
396
|
-
productId:
|
|
397
|
-
planId:
|
|
398
|
-
callbackUrl:
|
|
385
|
+
targetTenantId: e.tenantId ?? "dev-fixture-uid",
|
|
386
|
+
targetTenantType: e.tenantType ?? "pf",
|
|
387
|
+
productId: e.productId,
|
|
388
|
+
planId: e.planId,
|
|
389
|
+
callbackUrl: e.callbackUrl,
|
|
399
390
|
status: "pending",
|
|
400
391
|
stripeSessionId: null,
|
|
401
|
-
expiresAt
|
|
402
|
-
})
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
redirectUrl,
|
|
392
|
+
expiresAt: r
|
|
393
|
+
}), {
|
|
394
|
+
intentId: n,
|
|
395
|
+
redirectUrl: o,
|
|
406
396
|
status: "pending",
|
|
407
|
-
expiresAt,
|
|
397
|
+
expiresAt: r,
|
|
408
398
|
requiresKyc: false
|
|
409
399
|
};
|
|
410
400
|
}
|
|
411
|
-
async get(
|
|
412
|
-
|
|
413
|
-
if (!
|
|
414
|
-
throw new
|
|
415
|
-
}
|
|
416
|
-
return { ...found };
|
|
401
|
+
async get(e, n) {
|
|
402
|
+
let r = this.intents.get(e);
|
|
403
|
+
if (!r)
|
|
404
|
+
throw new i("not_found", `Mock intent ${e} not found`);
|
|
405
|
+
return { ...r };
|
|
417
406
|
}
|
|
418
|
-
async cancel(
|
|
419
|
-
|
|
420
|
-
if (!
|
|
421
|
-
throw new
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
this.intents.set(intentId, { ...found, status: "cancelled" });
|
|
425
|
-
return { ok: true, alreadyCancelled };
|
|
407
|
+
async cancel(e, n) {
|
|
408
|
+
let r = this.intents.get(e);
|
|
409
|
+
if (!r)
|
|
410
|
+
throw new i("not_found", `Mock intent ${e} not found`);
|
|
411
|
+
let o = r.status === "cancelled";
|
|
412
|
+
return this.intents.set(e, { ...r, status: "cancelled" }), { ok: true, alreadyCancelled: o };
|
|
426
413
|
}
|
|
427
414
|
};
|
|
428
|
-
function
|
|
429
|
-
|
|
430
|
-
return createHttpCheckoutNamespace(config);
|
|
415
|
+
function Ie(t) {
|
|
416
|
+
return t.env === "dev" ? new R() : ie(t);
|
|
431
417
|
}
|
|
432
418
|
|
|
433
|
-
export { MockCheckout, createCheckoutNamespace };
|
|
434
|
-
//# sourceMappingURL=checkout.mjs.map
|
|
435
|
-
//# sourceMappingURL=checkout.mjs.map
|
|
419
|
+
export { R as MockCheckout, Ie as createCheckoutNamespace };
|