From 0ae45a3ba5b53f4b27b18659448787491e78ee8a Mon Sep 17 00:00:00 2001 From: riteshshukla04 Date: Fri, 21 Nov 2025 20:19:36 +0530 Subject: [PATCH 1/2] fix: worklets not working for post request --- example/src/App.tsx | 68 +++++++++++++++++ package/src/fetch.ts | 174 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 237 insertions(+), 5 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 6d6b660..90a245d 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -181,6 +181,7 @@ export default function App() { Array<{ id: string; usd: number }> >([]); const [prefetchInfo, setPrefetchInfo] = React.useState(''); + const [postResult, setPostResult] = React.useState(''); const PREFETCH_URL = 'https://httpbin.org/uuid'; const PREFETCH_KEY = 'uuid'; @@ -233,6 +234,60 @@ export default function App() { } }, []); + const sendPostRequest = React.useCallback(async () => { + console.log('Sending POST request with worklet'); + const url = 'https://httpbin.org/post'; + const requestBody = { + message: 'Hello from Nitro Fetch!', + timestamp: Date.now(), + data: { userId: 123, action: 'test' }, + }; + + const mapper = (payload: { bodyString?: string; status: number }) => { + 'worklet'; + if (payload.status !== 200) { + return { success: false, error: `HTTP ${payload.status}` }; + } + const txt = payload.bodyString ?? ''; + const json = JSON.parse(txt) as { + json?: typeof requestBody; + data?: string; + }; + // Extract the parsed JSON from httpbin response + const sentData = json.json ?? (json.data ? JSON.parse(json.data) : null); + return { + success: true, + sent: sentData, + received: json, + }; + }; + + try { + setPostResult('Sending POST request...'); + const data = await nitroFetchOnWorklet( + url, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(requestBody), + }, + mapper, + { + preferBytes: false, + } + ); + console.log('POST request result:', data); + setPostResult( + `Success! Sent: ${JSON.stringify(data.sent, null, 2).substring(0, 100)}...` + ); + } catch (e: any) { + console.error('POST request error', e); + setPostResult(`Error: ${e?.message ?? String(e)}`); + } + }, []); + const run = React.useCallback(async () => { if (running) return; setRunning(true); @@ -304,6 +359,8 @@ export default function App() { loadPrices(); }} /> + +