@ar-agents/mercadopago 0.11.0 → 0.12.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/dist/index.js CHANGED
@@ -226,6 +226,23 @@ var DEFAULT_BASE_URL = "https://api.mercadopago.com";
226
226
  function sleep(ms) {
227
227
  return new Promise((resolve) => setTimeout(resolve, ms));
228
228
  }
229
+ function randomUuid() {
230
+ const c = globalThis.crypto;
231
+ if (c?.randomUUID) {
232
+ return c.randomUUID();
233
+ }
234
+ if (c?.getRandomValues) {
235
+ const bytes = new Uint8Array(16);
236
+ c.getRandomValues(bytes);
237
+ bytes[6] = bytes[6] & 15 | 64;
238
+ bytes[8] = bytes[8] & 63 | 128;
239
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
240
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
241
+ }
242
+ throw new Error(
243
+ "MercadoPagoClient: no Web Crypto available for idempotency key generation. This shouldn't happen on Node 19+, Edge Runtime, or modern browsers."
244
+ );
245
+ }
229
246
  var MercadoPagoClient = class {
230
247
  accessToken;
231
248
  baseUrl;
@@ -269,8 +286,10 @@ var MercadoPagoClient = class {
269
286
  Authorization: `Bearer ${this.accessToken}`,
270
287
  "Content-Type": "application/json"
271
288
  };
272
- if (options?.idempotencyKey) {
273
- headers["X-Idempotency-Key"] = options.idempotencyKey;
289
+ const isMutatingPost = method === "POST";
290
+ const idempotencyKey = options?.idempotencyKey ?? (isMutatingPost ? randomUuid() : void 0);
291
+ if (idempotencyKey) {
292
+ headers["X-Idempotency-Key"] = idempotencyKey;
274
293
  }
275
294
  const trace = this.traceContext?.();
276
295
  if (trace?.traceId && trace?.spanId) {