|
1 | 1 | # thirdweb |
2 | 2 |
|
| 3 | +## 5.113.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- [#8444](https://github.com/thirdweb-dev/js/pull/8444) [`9809d5c`](https://github.com/thirdweb-dev/js/commit/9809d5cf66baa520a9413986eb5bd2900de6f337) Thanks [@joaquim-verges](https://github.com/joaquim-verges)! - # New `useFetchWithPayment()` React Hook |
| 8 | + |
| 9 | + Added a new React hook that wraps the native fetch API to automatically handle 402 Payment Required responses using the x402 payment protocol. |
| 10 | + |
| 11 | + ## Features |
| 12 | + - **Automatic Payment Handling**: Automatically detects 402 responses, creates payment headers, and retries requests |
| 13 | + - **Built-in UI**: Shows an error modal with retry and fund wallet options when payment fails |
| 14 | + - **Sign In Flow**: Prompts users to connect their wallet if not connected, then automatically retries the payment |
| 15 | + - **Insufficient Funds Flow**: Integrates BuyWidget to help users top up their wallet directly in the modal |
| 16 | + - **Customizable**: Supports theming, custom payment selectors, BuyWidget customization, and ConnectModal customization |
| 17 | + - **Opt-out Modal**: Can disable the modal to handle errors manually |
| 18 | + |
| 19 | + ## Basic Usage |
| 20 | + |
| 21 | + The hook automatically parses JSON responses by default. |
| 22 | + |
| 23 | + ```tsx |
| 24 | + import { useFetchWithPayment } from "thirdweb/react"; |
| 25 | + import { createThirdwebClient } from "thirdweb"; |
| 26 | + |
| 27 | + const client = createThirdwebClient({ clientId: "your-client-id" }); |
| 28 | + |
| 29 | + function MyComponent() { |
| 30 | + const { fetchWithPayment, isPending } = useFetchWithPayment(client); |
| 31 | + |
| 32 | + const handleApiCall = async () => { |
| 33 | + // Response is automatically parsed as JSON by default |
| 34 | + const data = await fetchWithPayment( |
| 35 | + "https://api.example.com/paid-endpoint", |
| 36 | + ); |
| 37 | + console.log(data); |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <button onClick={handleApiCall} disabled={isPending}> |
| 42 | + {isPending ? "Loading..." : "Make Paid API Call"} |
| 43 | + </button> |
| 44 | + ); |
| 45 | + } |
| 46 | + ``` |
| 47 | + |
| 48 | + ## Customize Response Parsing |
| 49 | + |
| 50 | + By default, responses are parsed as JSON. You can customize this with the `parseAs` option: |
| 51 | + |
| 52 | + ```tsx |
| 53 | + // Parse as text instead of JSON |
| 54 | + const { fetchWithPayment } = useFetchWithPayment(client, { |
| 55 | + parseAs: "text", |
| 56 | + }); |
| 57 | + |
| 58 | + // Or get the raw Response object |
| 59 | + const { fetchWithPayment } = useFetchWithPayment(client, { |
| 60 | + parseAs: "raw", |
| 61 | + }); |
| 62 | + ``` |
| 63 | + |
| 64 | + ## Customize Theme & Payment Options |
| 65 | + |
| 66 | + ```tsx |
| 67 | + const { fetchWithPayment } = useFetchWithPayment(client, { |
| 68 | + maxValue: 5000000n, // 5 USDC in base units |
| 69 | + theme: "light", |
| 70 | + paymentRequirementsSelector: (requirements) => { |
| 71 | + // Custom logic to select preferred payment method |
| 72 | + return requirements[0]; |
| 73 | + }, |
| 74 | + }); |
| 75 | + ``` |
| 76 | + |
| 77 | + ## Customize Fund Wallet Widget |
| 78 | + |
| 79 | + ```tsx |
| 80 | + const { fetchWithPayment } = useFetchWithPayment(client, { |
| 81 | + fundWalletOptions: { |
| 82 | + title: "Add Funds", |
| 83 | + description: "You need more tokens to complete this payment", |
| 84 | + buttonLabel: "Get Tokens", |
| 85 | + }, |
| 86 | + }); |
| 87 | + ``` |
| 88 | + |
| 89 | + ## Customize Connect Modal |
| 90 | + |
| 91 | + ```tsx |
| 92 | + const { fetchWithPayment } = useFetchWithPayment(client, { |
| 93 | + connectOptions: { |
| 94 | + wallets: [inAppWallet(), createWallet("io.metamask")], |
| 95 | + title: "Sign in to continue", |
| 96 | + showThirdwebBranding: false, |
| 97 | + }, |
| 98 | + }); |
| 99 | + ``` |
| 100 | + |
| 101 | + ## Disable Modal (Handle Errors Manually) |
| 102 | + |
| 103 | + ```tsx |
| 104 | + const { fetchWithPayment, error } = useFetchWithPayment(client, { |
| 105 | + showErrorModal: false, |
| 106 | + }); |
| 107 | + |
| 108 | + // Handle the error manually |
| 109 | + if (error) { |
| 110 | + console.error("Payment failed:", error); |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +### Patch Changes |
| 115 | + |
| 116 | +- [#8453](https://github.com/thirdweb-dev/js/pull/8453) [`52aba0f`](https://github.com/thirdweb-dev/js/commit/52aba0ffb814904414cdc8e76407afd71272d88e) Thanks [@joaquim-verges](https://github.com/joaquim-verges)! - Dont attempt chain switching for cb wallet if already connected to the right chain |
| 117 | + |
3 | 118 | ## 5.112.4 |
4 | 119 |
|
5 | 120 | ### Patch Changes |
|
0 commit comments