@t2000/sdk 9.3.0 → 9.4.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/browser.cjs CHANGED
@@ -531,11 +531,18 @@ function preflightPay(input) {
531
531
  return PREFLIGHT_OK;
532
532
  }
533
533
  async function payWithMpp(args) {
534
- const { signer, client, options } = args;
534
+ const { signer, client } = args;
535
+ let options = args.options;
535
536
  const pf = preflightPay({ url: options.url, maxPrice: options.maxPrice });
536
537
  if (!pf.valid) throw new exports.T2000Error(pf.code, pf.error);
537
538
  const method = (options.method ?? "GET").toUpperCase();
538
539
  const canHaveBody = method !== "GET" && method !== "HEAD";
540
+ if (canHaveBody && typeof options.body === "string" && isJsonText(options.body) && !hasContentType(options.headers)) {
541
+ options = {
542
+ ...options,
543
+ headers: { ...options.headers ?? {}, "content-type": "application/json" }
544
+ };
545
+ }
539
546
  const reqInit = {
540
547
  method,
541
548
  headers: options.headers,
@@ -547,17 +554,36 @@ async function payWithMpp(args) {
547
554
  }
548
555
  const requirements = await pickSuiExactRequirements(probe, client.network);
549
556
  if (requirements) {
550
- return payViaX402({ signer, client, options, reqInit, requirements });
557
+ const result = await payViaX402({ signer, client, options, reqInit, requirements });
558
+ await reportDirectPayment(result, options.url);
559
+ return result;
551
560
  }
552
561
  const headerChallenge = await parseMppSuiChallenge(probe);
553
562
  if (headerChallenge) {
554
- return payViaMppHeader({ signer, client, options });
563
+ const result = await payViaMppHeader({ signer, client, options });
564
+ await reportDirectPayment(result, options.url);
565
+ return result;
555
566
  }
556
567
  throw new exports.T2000Error(
557
568
  "FACILITATOR_REJECTION",
558
569
  `Endpoint returned 402 without an x402 'exact' / sui:${client.network} requirement in the body or an MPP 'sui' challenge in WWW-Authenticate. Nothing this SDK can pay.`
559
570
  );
560
571
  }
572
+ var MPP_REPORT_URL = "https://mpp.t2000.ai/api/mpp/report";
573
+ var REPORT_TIMEOUT_MS = 2e3;
574
+ async function reportDirectPayment(result, url) {
575
+ if (!result.paid || !result.receipt?.reference) return;
576
+ try {
577
+ if (new URL(url).origin === new URL(MPP_REPORT_URL).origin) return;
578
+ await fetch(MPP_REPORT_URL, {
579
+ method: "POST",
580
+ headers: { "content-type": "application/json" },
581
+ body: JSON.stringify({ digest: result.receipt.reference, url }),
582
+ signal: AbortSignal.timeout(REPORT_TIMEOUT_MS)
583
+ });
584
+ } catch {
585
+ }
586
+ }
561
587
  async function parseMppSuiChallenge(response) {
562
588
  try {
563
589
  const { Challenge } = await import('mppx');
@@ -722,6 +748,18 @@ async function ensureAddressBalanceCovers(args) {
722
748
  const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
723
749
  return migration.gasCostSui;
724
750
  }
751
+ function isJsonText(text) {
752
+ try {
753
+ JSON.parse(text);
754
+ return true;
755
+ } catch {
756
+ return false;
757
+ }
758
+ }
759
+ function hasContentType(headers) {
760
+ if (!headers) return false;
761
+ return Object.keys(headers).some((k) => k.toLowerCase() === "content-type");
762
+ }
725
763
  async function finalize(response, opts) {
726
764
  const contentType = response.headers.get("content-type") ?? "";
727
765
  let body;