@@ -12,41 +12,54 @@ const { isConnected } = useWeb3Auth();
1212const { chainId, chainNamespace } = useChain ();
1313const { mutateAsync : switchChainAsync } = useSwitchChain ();
1414const { fetchWithPayment } = useX402Fetch ();
15+ const emit = defineEmits <{
16+ (e : " print-to-console" , title : string , payload ? : unknown ): void ;
17+ }>();
1518
1619const url = ref (DEFAULT_X402_URL );
1720const fetchLoading = ref (false );
18- const result = ref <string | null >(null );
19- const fetchError = ref <string | null >(null );
2021
2122const isOnBaseSepolia = ref (false );
2223
24+ const parseConsoleBody = (text : string ) => {
25+ try {
26+ return JSON .parse (text );
27+ } catch {
28+ return text ;
29+ }
30+ };
31+
2332watch (chainId , (id ) => {
2433 isOnBaseSepolia .value = id ?.toLowerCase () === BASE_SEPOLIA_CHAIN_ID .toLowerCase ();
2534}, { immediate: true });
2635
2736const onSwitchToBaseSepolia = async () => {
2837 fetchLoading .value = true ;
2938 try {
30- result .value = null ;
31- fetchError .value = null ;
3239 await switchChainAsync ({ chainId: parseInt (BASE_SEPOLIA_CHAIN_ID , 16 ) });
3340 } catch (err ) {
34- fetchError . value = err instanceof Error ? err .message : String (err );
41+ emit ( " print-to-console " , " x402 network error " , err instanceof Error ? err .message : String (err ) );
3542 } finally {
3643 fetchLoading .value = false ;
3744 }
3845};
3946
4047const onFetchWithPayment = async () => {
41- result .value = null ;
42- fetchError .value = null ;
4348 fetchLoading .value = true ;
4449 try {
4550 const response = await fetchWithPayment ({ url: url .value });
4651 const text = await response .text ();
47- result .value = text ;
52+ emit (" print-to-console" , " x402 response" , {
53+ url: url .value ,
54+ status: response .status ,
55+ ok: response .ok ,
56+ body: parseConsoleBody (text ),
57+ });
4858 } catch (err ) {
49- fetchError .value = err instanceof Error ? err .message : String (err );
59+ emit (" print-to-console" , " x402 error" , {
60+ url: url .value ,
61+ message: err instanceof Error ? err .message : String (err ),
62+ });
5063 } finally {
5164 fetchLoading .value = false ;
5265 }
@@ -117,16 +130,5 @@ const onFetchWithPayment = async () => {
117130 >
118131 Fetch with Payment
119132 </Button >
120-
121- <!-- Result -->
122- <div v-if =" result !== null || fetchError" class =" rounded-lg overflow-hidden text-xs" >
123- <div v-if =" fetchError" class =" bg-red-50 border border-red-200 px-3 py-2 text-red-600 break-words" >
124- <span class =" font-semibold" >Error: </span >{{ fetchError }}
125- </div >
126- <div v-else class =" bg-green-50 border border-green-200 px-3 py-2" >
127- <div class =" font-semibold text-green-700 mb-1" >Response</div >
128- <pre class =" text-gray-800 whitespace-pre-wrap break-words max-h-48 overflow-y-auto" >{{ result }}</pre >
129- </div >
130- </div >
131133 </Card >
132134</template >
0 commit comments