@vitrindigital/node 0.3.0 → 0.3.1
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/README.md +8 -0
- package/dist/index.cjs +6 -1
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -170,6 +170,14 @@ Inclua `idempotencyKey` em POSTs sensíveis. Se o request chegar duas vezes
|
|
|
170
170
|
(retry de rede, deploy etc), a Vitrin reconhece pela chave e devolve a mesma
|
|
171
171
|
resposta — sem cobrar duas vezes.
|
|
172
172
|
|
|
173
|
+
Vale em `charges.create()` e `charges.createWithSavedCard()`. A chave é escopada
|
|
174
|
+
por organização (a sua nunca colide com a de outro lojista) e aceita até 128
|
|
175
|
+
caracteres.
|
|
176
|
+
|
|
177
|
+
Use um identificador **seu** e estável — número do pedido, id do carrinho. A
|
|
178
|
+
proteção vem de a chave se repetir no retry; um UUID novo a cada tentativa não
|
|
179
|
+
protege de nada.
|
|
180
|
+
|
|
173
181
|
```ts
|
|
174
182
|
await vitrin.charges.create({
|
|
175
183
|
customer_id: 'cus_1',
|
package/dist/index.cjs
CHANGED
|
@@ -109,7 +109,7 @@ var VitrinClient = class {
|
|
|
109
109
|
lastError = new VitrinNetworkError(
|
|
110
110
|
netErr.name === "AbortError" ? `Timeout ap\xF3s ${this.timeout}ms` : netErr.message || "Falha de rede"
|
|
111
111
|
);
|
|
112
|
-
if (attempt < this.maxRetries) {
|
|
112
|
+
if (attempt < this.maxRetries && canRetryAfterNetworkError(method, idempotencyKey)) {
|
|
113
113
|
await sleep(backoffMs(attempt));
|
|
114
114
|
continue;
|
|
115
115
|
}
|
|
@@ -191,6 +191,11 @@ function parseRetryAfter(header) {
|
|
|
191
191
|
if (!Number.isNaN(seconds)) return seconds * 1e3;
|
|
192
192
|
return null;
|
|
193
193
|
}
|
|
194
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "PUT", "DELETE", "OPTIONS"]);
|
|
195
|
+
function canRetryAfterNetworkError(method, idempotencyKey) {
|
|
196
|
+
if (IDEMPOTENT_METHODS.has(method.toUpperCase())) return true;
|
|
197
|
+
return Boolean(idempotencyKey);
|
|
198
|
+
}
|
|
194
199
|
function backoffMs(attempt) {
|
|
195
200
|
const base = 250 * Math.pow(2, attempt);
|
|
196
201
|
return base + Math.floor(Math.random() * base * 0.5);
|
package/dist/index.d.cts
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
* - Serializar/deserializar JSON
|
|
7
7
|
* - Mapear erros HTTP pra `VitrinError`*
|
|
8
8
|
* - Retry automático em 429 e 5xx (até 3 tentativas, backoff exponencial)
|
|
9
|
+
* - Retry em timeout/erro de rede SÓ quando é seguro: método idempotente
|
|
10
|
+
* (GET/HEAD/PUT/DELETE) ou POST/PATCH com Idempotency-Key. Sem isso o
|
|
11
|
+
* retry de um POST cria uma segunda cobrança — o timeout não diz que o
|
|
12
|
+
* servidor deixou de processar, só que a resposta se perdeu.
|
|
9
13
|
* - Idempotency-Key opcional pra POSTs (evita duplo-débito quando o cliente
|
|
10
14
|
* retenta uma chamada que já chegou no servidor)
|
|
11
15
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
* - Serializar/deserializar JSON
|
|
7
7
|
* - Mapear erros HTTP pra `VitrinError`*
|
|
8
8
|
* - Retry automático em 429 e 5xx (até 3 tentativas, backoff exponencial)
|
|
9
|
+
* - Retry em timeout/erro de rede SÓ quando é seguro: método idempotente
|
|
10
|
+
* (GET/HEAD/PUT/DELETE) ou POST/PATCH com Idempotency-Key. Sem isso o
|
|
11
|
+
* retry de um POST cria uma segunda cobrança — o timeout não diz que o
|
|
12
|
+
* servidor deixou de processar, só que a resposta se perdeu.
|
|
9
13
|
* - Idempotency-Key opcional pra POSTs (evita duplo-débito quando o cliente
|
|
10
14
|
* retenta uma chamada que já chegou no servidor)
|
|
11
15
|
*/
|
package/dist/index.js
CHANGED
|
@@ -75,7 +75,7 @@ var VitrinClient = class {
|
|
|
75
75
|
lastError = new VitrinNetworkError(
|
|
76
76
|
netErr.name === "AbortError" ? `Timeout ap\xF3s ${this.timeout}ms` : netErr.message || "Falha de rede"
|
|
77
77
|
);
|
|
78
|
-
if (attempt < this.maxRetries) {
|
|
78
|
+
if (attempt < this.maxRetries && canRetryAfterNetworkError(method, idempotencyKey)) {
|
|
79
79
|
await sleep(backoffMs(attempt));
|
|
80
80
|
continue;
|
|
81
81
|
}
|
|
@@ -157,6 +157,11 @@ function parseRetryAfter(header) {
|
|
|
157
157
|
if (!Number.isNaN(seconds)) return seconds * 1e3;
|
|
158
158
|
return null;
|
|
159
159
|
}
|
|
160
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "PUT", "DELETE", "OPTIONS"]);
|
|
161
|
+
function canRetryAfterNetworkError(method, idempotencyKey) {
|
|
162
|
+
if (IDEMPOTENT_METHODS.has(method.toUpperCase())) return true;
|
|
163
|
+
return Boolean(idempotencyKey);
|
|
164
|
+
}
|
|
160
165
|
function backoffMs(attempt) {
|
|
161
166
|
const base = 250 * Math.pow(2, attempt);
|
|
162
167
|
return base + Math.floor(Math.random() * base * 0.5);
|