diff --git a/src/content/docs/agents/api-reference/agents-api.mdx b/src/content/docs/agents/api-reference/agents-api.mdx index c991b8302e3eab2..692ed27d0e1d671 100644 --- a/src/content/docs/agents/api-reference/agents-api.mdx +++ b/src/content/docs/agents/api-reference/agents-api.mdx @@ -148,13 +148,9 @@ class MyAgent extends Agent { async onRequest(request: Request) { if (request.method === "POST") { await this.incrementCounter(); - return new Response(JSON.stringify(this.state), { - headers: { "Content-Type": "application/json" } - }); + return Response.json(this.state); } - return new Response(JSON.stringify(this.state), { - headers: { "Content-Type": "application/json" } - }); + return Response.json(this.state); } async incrementCounter() { diff --git a/src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx b/src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx index 987177cef75544f..8b524c9e6b9c7c8 100644 --- a/src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx +++ b/src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx @@ -94,9 +94,7 @@ export default { ); // Return the AI response as a JSON object - return new Response(JSON.stringify(response), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(response); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/ai-gateway/observability/custom-metadata.mdx b/src/content/docs/ai-gateway/observability/custom-metadata.mdx index c0c61da02988754..a046b6f6c134493 100644 --- a/src/content/docs/ai-gateway/observability/custom-metadata.mdx +++ b/src/content/docs/ai-gateway/observability/custom-metadata.mdx @@ -82,7 +82,7 @@ export default { ); const response = chatCompletion.choices[0].message; - return new Response(JSON.stringify(response)); + return Response.json(response); } catch (e) { console.log(e); return new Response(e); diff --git a/src/content/docs/ai-gateway/tutorials/deploy-aig-worker.mdx b/src/content/docs/ai-gateway/tutorials/deploy-aig-worker.mdx index 126411f5ebfb349..3c2fecedb90aa07 100644 --- a/src/content/docs/ai-gateway/tutorials/deploy-aig-worker.mdx +++ b/src/content/docs/ai-gateway/tutorials/deploy-aig-worker.mdx @@ -125,7 +125,7 @@ export default { const response = chatCompletion.choices[0].message; - return new Response(JSON.stringify(response)); + return Response.json(response); } catch (e) { return new Response(e); } diff --git a/src/content/docs/ai-gateway/usage/providers/bedrock.mdx b/src/content/docs/ai-gateway/usage/providers/bedrock.mdx index f305aa4f0687208..918f5100bcad9db 100644 --- a/src/content/docs/ai-gateway/usage/providers/bedrock.mdx +++ b/src/content/docs/ai-gateway/usage/providers/bedrock.mdx @@ -96,7 +96,7 @@ export default { response.headers.get("content-type")?.includes("application/json") ) { const data = await response.json(); - return new Response(JSON.stringify(data)); + return Response.json(data); } return new Response("Invalid response", { status: 500 }); diff --git a/src/content/docs/ai-gateway/usage/providers/deepseek.mdx b/src/content/docs/ai-gateway/usage/providers/deepseek.mdx index 5991e50966fc887..93dedb06e75c5c4 100644 --- a/src/content/docs/ai-gateway/usage/providers/deepseek.mdx +++ b/src/content/docs/ai-gateway/usage/providers/deepseek.mdx @@ -74,7 +74,7 @@ try { const response = chatCompletion.choices[0].message; - return new Response(JSON.stringify(response)); + return Response.json(response); } catch (e) { return new Response(e); } diff --git a/src/content/docs/ai-gateway/usage/providers/openrouter.mdx b/src/content/docs/ai-gateway/usage/providers/openrouter.mdx index 8ab1a8f15b896f3..edcd90390fcd2df 100644 --- a/src/content/docs/ai-gateway/usage/providers/openrouter.mdx +++ b/src/content/docs/ai-gateway/usage/providers/openrouter.mdx @@ -65,7 +65,7 @@ try { const response = chatCompletion.choices[0].message; - return new Response(JSON.stringify(response)); + return Response.json(response); } catch (e) { return new Response(e); } diff --git a/src/content/docs/ai-gateway/usage/providers/workersai.mdx b/src/content/docs/ai-gateway/usage/providers/workersai.mdx index e409d530fa9f880..71b2506842a2cf3 100644 --- a/src/content/docs/ai-gateway/usage/providers/workersai.mdx +++ b/src/content/docs/ai-gateway/usage/providers/workersai.mdx @@ -106,7 +106,7 @@ export default { }, }, ); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/cloudflare-one/identity/authorization-cookie/cors.mdx b/src/content/docs/cloudflare-one/identity/authorization-cookie/cors.mdx index 02c4c441f374041..9283760901f4800 100644 --- a/src/content/docs/cloudflare-one/identity/authorization-cookie/cors.mdx +++ b/src/content/docs/cloudflare-one/identity/authorization-cookie/cors.mdx @@ -204,7 +204,7 @@ export default { return modifiedResponse; } catch (e) { - return new Response(JSON.stringify({ error: e.message }), { + return Response.json({ error: e.message }, { status: 500, }); } diff --git a/src/content/docs/cloudflare-one/tutorials/ai-wrapper-tenant-control.mdx b/src/content/docs/cloudflare-one/tutorials/ai-wrapper-tenant-control.mdx index 93a1c5923b003af..d32c46aae938a38 100644 --- a/src/content/docs/cloudflare-one/tutorials/ai-wrapper-tenant-control.mdx +++ b/src/content/docs/cloudflare-one/tutorials/ai-wrapper-tenant-control.mdx @@ -156,9 +156,8 @@ export default { }, ); } catch (error) { - return new Response(JSON.stringify({ error: error.message }), { - status: 500, - headers: { "Content-Type": "application/json" }, + return Response.json({ error: error.message }, { + status: 500 }); } } diff --git a/src/content/docs/d1/best-practices/read-replication.mdx b/src/content/docs/d1/best-practices/read-replication.mdx index c5c1f4fcbb8c5ad..c62318a1b63c7cf 100644 --- a/src/content/docs/d1/best-practices/read-replication.mdx +++ b/src/content/docs/d1/best-practices/read-replication.mdx @@ -215,12 +215,12 @@ export default { if (path === '/bills' && method === 'GET') { // List bills const result = await listBillStatements('1', env.DB); - return new Response(JSON.stringify(result), { status: 200 }); + return Response.json(result, { status: 200 }); } if (path === '/bill' && method === 'GET') { // Get bill const result = await getBillStatement('1', '1', 'first-unconstrained', env.DB); - return new Response(JSON.stringify(result), { status: 200 }); + return Response.json(result, { status: 200 }); } // Fetch using bookmark from cookie @@ -234,7 +234,7 @@ export default { ?.split('=')[1] ?? 'first-unconstrained'; console.log('bookmark', bookmark); const result = await getBillStatement('1', '1', bookmark, env.DB); - return new Response(JSON.stringify(result), { + return Response.json(result, { status: 200, headers: { 'Set-Cookie': `X-D1-Bookmark=${result.bookmark}; Path=/; SameSite=Strict`, @@ -253,7 +253,7 @@ export default { .run(); const bookmark = session.getBookmark() ?? 'first-unconstrained'; - return new Response(JSON.stringify(results), { + return Response.json(results, { status: 201, headers: { // Set bookmark cookie diff --git a/src/content/docs/d1/examples/d1-and-sveltekit.mdx b/src/content/docs/d1/examples/d1-and-sveltekit.mdx index d422dd2df567e4b..f09422bc6da2f4d 100644 --- a/src/content/docs/d1/examples/d1-and-sveltekit.mdx +++ b/src/content/docs/d1/examples/d1-and-sveltekit.mdx @@ -39,7 +39,7 @@ export async function GET({ request, platform }) { let result = await platform.env.DB.prepare( "SELECT * FROM users LIMIT 5", ).run(); - return new Response(JSON.stringify(result)); + return Response.json(result); } ``` diff --git a/src/content/docs/d1/get-started.mdx b/src/content/docs/d1/get-started.mdx index bbb7618ee01635f..1ce2dd218fa43aa 100644 --- a/src/content/docs/d1/get-started.mdx +++ b/src/content/docs/d1/get-started.mdx @@ -416,9 +416,7 @@ You can query your D1 database using your Worker. ) .bind("Bs Beverages") .run(); - return new Response(JSON.stringify(results), { - headers: { 'Content-Type': 'application/json' } - }); + return Response.json(results); } return new Response( diff --git a/src/content/docs/durable-objects/examples/alarms-api.mdx b/src/content/docs/durable-objects/examples/alarms-api.mdx index 85a6e88db9791cd..2ce79b28ff1348a 100644 --- a/src/content/docs/durable-objects/examples/alarms-api.mdx +++ b/src/content/docs/durable-objects/examples/alarms-api.mdx @@ -50,11 +50,7 @@ export class Batcher extends DurableObject { // Add the request to the batch. await this.storage.put(this.count, await request.text()); - return new Response(JSON.stringify({ queued: this.count }), { - headers: { - "content-type": "application/json;charset=UTF-8", - }, - }); + return Response.json({ queued: this.count }); } async alarm() { diff --git a/src/content/docs/durable-objects/tutorials/build-a-seat-booking-app.mdx b/src/content/docs/durable-objects/tutorials/build-a-seat-booking-app.mdx index 8efd192e3184d1d..45abf978cf1b54e 100644 --- a/src/content/docs/durable-objects/tutorials/build-a-seat-booking-app.mdx +++ b/src/content/docs/durable-objects/tutorials/build-a-seat-booking-app.mdx @@ -591,16 +591,14 @@ export default { ... if (request.method === "GET" && url.pathname === "/seats") { - return new Response(JSON.stringify(await stub.getSeats()), { - headers: { 'Content-Type': 'application/json' }, - }); + return Response.json(await stub.getSeats()); } else if (request.method === "POST" && url.pathname === "/book-seat") { const { seatNumber, name } = (await request.json()) as { seatNumber: string; name: string; }; const result = await stub.assignSeat(seatNumber, name); - return new Response(JSON.stringify(result)); + return Response.json(result); } else if (request.headers.get("Upgrade") === "websocket") { return stub.fetch(request); } diff --git a/src/content/docs/hyperdrive/get-started.mdx b/src/content/docs/hyperdrive/get-started.mdx index b700d597f0ade40..0c7ba3406f5e588 100644 --- a/src/content/docs/hyperdrive/get-started.mdx +++ b/src/content/docs/hyperdrive/get-started.mdx @@ -315,9 +315,8 @@ export default { ctx.waitUntil(connection.end()); // Return result rows as JSON - return new Response(JSON.stringify({ results, fields }), { + return Response.json({ results, fields }, { headers: { - 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, }); diff --git a/src/content/docs/hyperdrive/index.mdx b/src/content/docs/hyperdrive/index.mdx index b980519ce6b3a7f..29d607643191499 100644 --- a/src/content/docs/hyperdrive/index.mdx +++ b/src/content/docs/hyperdrive/index.mdx @@ -112,9 +112,8 @@ export default { const [results, fields] = await connection.query('SHOW tables;'); - return new Response(JSON.stringify({ results, fields }), { + return Response.json({ results, fields }, { headers: { - 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '\*', }, }); diff --git a/src/content/docs/hyperdrive/tutorials/serverless-timeseries-api-with-timescale.mdx b/src/content/docs/hyperdrive/tutorials/serverless-timeseries-api-with-timescale.mdx index bd4dc32710b6f5d..f9a88976b552ab0 100644 --- a/src/content/docs/hyperdrive/tutorials/serverless-timeseries-api-with-timescale.mdx +++ b/src/content/docs/hyperdrive/tutorials/serverless-timeseries-api-with-timescale.mdx @@ -200,9 +200,7 @@ export default { ]); // Collect the raw row count inserted to return - const resp = new Response(JSON.stringify(insertResult.rowCount), { - headers: { "Content-Type": "application/json" }, - }); + const resp = Response.json(insertResult.rowCount); return resp; @@ -217,9 +215,7 @@ export default { ); // Return the result as JSON - const resp = new Response(JSON.stringify(result.rows), { - headers: { "Content-Type": "application/json" }, - }); + const resp = Response.json(result.rows); return resp; } diff --git a/src/content/docs/kv/api/list-keys.mdx b/src/content/docs/kv/api/list-keys.mdx index eb912d649bba819..d1b798cead42aee 100644 --- a/src/content/docs/kv/api/list-keys.mdx +++ b/src/content/docs/kv/api/list-keys.mdx @@ -25,7 +25,7 @@ export default { try { const value = await env.NAMESPACE.list(); - return new Response(JSON.stringify(value.keys), { + return Response.json(value.keys, { status: 200 }); } diff --git a/src/content/docs/kv/api/write-key-value-pairs.mdx b/src/content/docs/kv/api/write-key-value-pairs.mdx index aff47d9f69e848a..3b5bd96e7cdf52a 100644 --- a/src/content/docs/kv/api/write-key-value-pairs.mdx +++ b/src/content/docs/kv/api/write-key-value-pairs.mdx @@ -192,9 +192,7 @@ export default { // ... // ] - return new Response(JSON.stringify(results), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(results); }, }; ``` @@ -223,9 +221,7 @@ export default { ), ); - return new Response(JSON.stringify(results), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(results); }, }; diff --git a/src/content/docs/kv/examples/cache-data-with-workers-kv.mdx b/src/content/docs/kv/examples/cache-data-with-workers-kv.mdx index 814657a6641ff45..11e37bc94e68dae 100644 --- a/src/content/docs/kv/examples/cache-data-with-workers-kv.mdx +++ b/src/content/docs/kv/examples/cache-data-with-workers-kv.mdx @@ -66,11 +66,9 @@ export default { } // Return the appropriate response format - return new Response(JSON.stringify({ + return Response.json({ data, fromCache - }), { - headers: { 'Content-Type': 'application/json' } }); } diff --git a/src/content/docs/pages/functions/routing.mdx b/src/content/docs/pages/functions/routing.mdx index 49e621c7e3fe19c..a2f0a6700e38af9 100644 --- a/src/content/docs/pages/functions/routing.mdx +++ b/src/content/docs/pages/functions/routing.mdx @@ -120,7 +120,7 @@ For files which match against multiple URL segments (use a double set of bracket ```js export function onRequest(context) { - return new Response(JSON.stringify(context.params.catchall)); + return Response.json(context.params.catchall); } ``` diff --git a/src/content/docs/pub-sub/learning/integrate-workers.mdx b/src/content/docs/pub-sub/learning/integrate-workers.mdx index 138355bd4f5e3a1..5394b05b42ab913 100644 --- a/src/content/docs/pub-sub/learning/integrate-workers.mdx +++ b/src/content/docs/pub-sub/learning/integrate-workers.mdx @@ -193,7 +193,7 @@ const worker = { // Re-serialize the messages and return a HTTP 200. // The Content-Type is optional, but must either by // "application/octet-stream" or left empty. - return new Response(JSON.stringify(outgoingMessages), { status: 200 }); + return Response.json(outgoingMessages, { status: 200 }); } return new Response("not a valid Broker request", { status: 403 }); diff --git a/src/content/docs/r2/api/workers/workers-multipart-usage.mdx b/src/content/docs/r2/api/workers/workers-multipart-usage.mdx index 40ddfd812632822..dd877491f61f9d9 100644 --- a/src/content/docs/r2/api/workers/workers-multipart-usage.mdx +++ b/src/content/docs/r2/api/workers/workers-multipart-usage.mdx @@ -119,7 +119,7 @@ export default { try { const uploadedPart: R2UploadedPart = await multipartUpload.uploadPart(partNumber, request.body); - return new Response(JSON.stringify(uploadedPart)); + return Response.json(uploadedPart); } catch (error: any) { return new Response(error.message, { status: 400 }); } diff --git a/src/content/docs/secrets-store/integrations/workers.mdx b/src/content/docs/secrets-store/integrations/workers.mdx index feec3287ca2d7fb..3778130edf2c649 100644 --- a/src/content/docs/secrets-store/integrations/workers.mdx +++ b/src/content/docs/secrets-store/integrations/workers.mdx @@ -165,9 +165,7 @@ export default { } const data = await response.json(); - return new Response(JSON.stringify(data), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(data); }, }; ``` \ No newline at end of file diff --git a/src/content/docs/ssl/client-certificates/configure-your-mobile-app-or-iot-device.mdx b/src/content/docs/ssl/client-certificates/configure-your-mobile-app-or-iot-device.mdx index 884d9eba5aa9b04..f964af8bf7cdef4 100644 --- a/src/content/docs/ssl/client-certificates/configure-your-mobile-app-or-iot-device.mdx +++ b/src/content/docs/ssl/client-certificates/configure-your-mobile-app-or-iot-device.mdx @@ -69,18 +69,13 @@ async function getTemperatures(request) { const cache = await getCache(cacheKey); if (!cache) { - return new Response(JSON.stringify(defaultData), { + return Response.json(defaultData, { status: 200, - headers: { "content-type": "application/json" }, }); } else { data = JSON.parse(cache); - const retval = JSON.stringify( - data.temperatures.sort(compareTimestamps).splice(0, 5), - ); - return new Response(retval, { + return Response.json(data.temperatures.sort(compareTimestamps).splice(0, 5), { status: 200, - headers: { "content-type": "application/json" }, }); } } diff --git a/src/content/docs/turnstile/tutorials/integrating-turnstile-waf-and-bot-management.mdx b/src/content/docs/turnstile/tutorials/integrating-turnstile-waf-and-bot-management.mdx index 5156a648cb8a4a5..50062cc6e906e3e 100644 --- a/src/content/docs/turnstile/tutorials/integrating-turnstile-waf-and-bot-management.mdx +++ b/src/content/docs/turnstile/tutorials/integrating-turnstile-waf-and-bot-management.mdx @@ -43,7 +43,7 @@ If your site is on Cloudflare's network and subscribed to an Enterprise plan, yo 1. In the Cloudflare dashboard, go to the **WAF** page. - + 2. Create a new custom WAF rule by selecting **Edit expression**: - Field: "Bot Score" @@ -60,7 +60,7 @@ Turnstile can be used on any site, regardless of whether it is on Cloudflare's n 1. In the Cloudflare dashboard, go to the **Turnstile** page. - + 2. Select **Add widget** and fill out the necessary information. 3. Add your domain to the Turnstile configuration. @@ -145,14 +145,12 @@ export async function onRequestPost(context) { const isValidLogin = await checkCredentials(username, password); if (isValidLogin) { - return new Response(JSON.stringify({ message: "Login successful" }), { + return Response.json({ message: "Login successful" }, { status: 200, - headers: { "Content-Type": "application/json" }, }); } else { - return new Response(JSON.stringify({ error: "Invalid credentials" }), { + return Response.json({ error: "Invalid credentials" }, { status: 401, - headers: { "Content-Type": "application/json" }, }); } } diff --git a/src/content/docs/workers-ai/features/function-calling/embedded/examples/fetch.mdx b/src/content/docs/workers-ai/features/function-calling/embedded/examples/fetch.mdx index 29753293f23a815..6340f5c7fcb9930 100644 --- a/src/content/docs/workers-ai/features/function-calling/embedded/examples/fetch.mdx +++ b/src/content/docs/workers-ai/features/function-calling/embedded/examples/fetch.mdx @@ -70,7 +70,7 @@ export default { ], } ); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; diff --git a/src/content/docs/workers-ai/features/function-calling/embedded/examples/kv.mdx b/src/content/docs/workers-ai/features/function-calling/embedded/examples/kv.mdx index f70be9e51f5b064..87b6dc11e7c81c9 100644 --- a/src/content/docs/workers-ai/features/function-calling/embedded/examples/kv.mdx +++ b/src/content/docs/workers-ai/features/function-calling/embedded/examples/kv.mdx @@ -74,7 +74,7 @@ export default { ], }, ); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/workers-ai/features/function-calling/embedded/examples/openapi.mdx b/src/content/docs/workers-ai/features/function-calling/embedded/examples/openapi.mdx index 11cac1eb205fd44..1ee7b36623f3528 100644 --- a/src/content/docs/workers-ai/features/function-calling/embedded/examples/openapi.mdx +++ b/src/content/docs/workers-ai/features/function-calling/embedded/examples/openapi.mdx @@ -59,7 +59,7 @@ export default { } ); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; diff --git a/src/content/docs/workers-ai/features/function-calling/embedded/get-started.mdx b/src/content/docs/workers-ai/features/function-calling/embedded/get-started.mdx index 5329e9a31511ce5..f22bdfca327b27f 100644 --- a/src/content/docs/workers-ai/features/function-calling/embedded/get-started.mdx +++ b/src/content/docs/workers-ai/features/function-calling/embedded/get-started.mdx @@ -74,7 +74,7 @@ export default { ], }, ); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/workers-ai/features/function-calling/index.mdx b/src/content/docs/workers-ai/features/function-calling/index.mdx index fdea2cd6ae542b5..7ef8b380aca6ff7 100644 --- a/src/content/docs/workers-ai/features/function-calling/index.mdx +++ b/src/content/docs/workers-ai/features/function-calling/index.mdx @@ -73,7 +73,7 @@ export default { return response; }); - return new Response(JSON.stringify(response)); + return Response.json(response); }, }; ``` @@ -162,7 +162,7 @@ export default { ], }, ); - return new Response(JSON.stringify(finalResponse)); + return Response.json(finalResponse); }, }; ``` diff --git a/src/content/docs/workers-ai/features/function-calling/traditional.mdx b/src/content/docs/workers-ai/features/function-calling/traditional.mdx index 1b977b25304abdd..b51bf9832adbbe3 100644 --- a/src/content/docs/workers-ai/features/function-calling/traditional.mdx +++ b/src/content/docs/workers-ai/features/function-calling/traditional.mdx @@ -44,7 +44,7 @@ const response = await env.AI.run("@hf/nousresearch/hermes-2-pro-mistral-7b", { ], }); -return new Response(JSON.stringify(response.tool_calls)); +return Response.json(response.tool_calls); ``` The LLM will then return a JSON object with the required arguments and the name of the tool that was called. You can then pass this JSON object to make an API call. diff --git a/src/content/docs/workers-ai/get-started/workers-wrangler.mdx b/src/content/docs/workers-ai/get-started/workers-wrangler.mdx index 4e5c99820dc73a3..7aa9b86bb0dad6c 100644 --- a/src/content/docs/workers-ai/get-started/workers-wrangler.mdx +++ b/src/content/docs/workers-ai/get-started/workers-wrangler.mdx @@ -88,7 +88,7 @@ export default { prompt: "What is the origin of the phrase Hello, World", }); - return new Response(JSON.stringify(response)); + return Response.json(response); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai.mdx b/src/content/docs/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai.mdx index 789e040a69aecad..4b7ede682365194 100644 --- a/src/content/docs/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai.mdx +++ b/src/content/docs/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai.mdx @@ -112,7 +112,7 @@ export default { messages: [{ role: "user", content: `What is the square root of 9?` }], }); - return new Response(JSON.stringify(answer)); + return Response.json(answer); }, }; ``` diff --git a/src/content/docs/workers-ai/guides/tutorials/using-bigquery-with-workers-ai.mdx b/src/content/docs/workers-ai/guides/tutorials/using-bigquery-with-workers-ai.mdx index 038d940f21802d9..fbd64ce3cc79a09 100644 --- a/src/content/docs/workers-ai/guides/tutorials/using-bigquery-with-workers-ai.mdx +++ b/src/content/docs/workers-ai/guides/tutorials/using-bigquery-with-workers-ai.mdx @@ -652,9 +652,7 @@ export default { const response = { data: formattedResults }; - return new Response(JSON.stringify(response), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(response); }, }; ``` diff --git a/src/content/docs/workers/configuration/secrets.mdx b/src/content/docs/workers/configuration/secrets.mdx index 5bbb777454ec658..ae357cabcc27201 100644 --- a/src/content/docs/workers/configuration/secrets.mdx +++ b/src/content/docs/workers/configuration/secrets.mdx @@ -24,9 +24,7 @@ export default { const result = await sql`SELECT * FROM products;`; - return new Response(JSON.stringify(result), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(result); }, }; ``` diff --git a/src/content/docs/workers/databases/third-party-integrations/neon.mdx b/src/content/docs/workers/databases/third-party-integrations/neon.mdx index 4194fb7581a765f..7ee02deb4430dd4 100644 --- a/src/content/docs/workers/databases/third-party-integrations/neon.mdx +++ b/src/content/docs/workers/databases/third-party-integrations/neon.mdx @@ -82,7 +82,7 @@ To connect to Neon using `@neondatabase/serverless`, follow these steps: await client.connect(); const { rows } = await client.query("SELECT * FROM elements"); - return new Response(JSON.stringify(rows)); + return Response.json(rows); }, }; ``` diff --git a/src/content/docs/workers/databases/third-party-integrations/planetscale.mdx b/src/content/docs/workers/databases/third-party-integrations/planetscale.mdx index 07ac435297b3d23..20631bdb416c20e 100644 --- a/src/content/docs/workers/databases/third-party-integrations/planetscale.mdx +++ b/src/content/docs/workers/databases/third-party-integrations/planetscale.mdx @@ -91,11 +91,8 @@ To set up an integration with PlanetScale: const conn = connect(config); const data = await conn.execute("SELECT * FROM products;"); - return new Response(JSON.stringify(data.rows), { + return Response.json(data.rows, { status: 200, - headers: { - "Content-Type": "application/json", - }, }); }, }; diff --git a/src/content/docs/workers/databases/third-party-integrations/supabase.mdx b/src/content/docs/workers/databases/third-party-integrations/supabase.mdx index b861125f59e07df..3756bd323e840c6 100644 --- a/src/content/docs/workers/databases/third-party-integrations/supabase.mdx +++ b/src/content/docs/workers/databases/third-party-integrations/supabase.mdx @@ -9,9 +9,9 @@ import { Render, PackageManagers, Tabs, TabItem } from "~/components"; :::note -The Supabase client (`@supabase/supabase-js`) provides access to Supabase's various features, including database access. If you need access to all of the Supabase client functionality, use the Supabase client. +The Supabase client (`@supabase/supabase-js`) provides access to Supabase's various features, including database access. If you need access to all of the Supabase client functionality, use the Supabase client. -If you want to connect directly to the Supabase Postgres database, connect using [Hyperdrive](/hyperdrive). Hyperdrive can provide lower latencies because it performs the database connection setup and connection pooling across Cloudflare's network. Hyperdrive supports native database drivers, libraries, and ORMs, and is included in all [Workers plans](/hyperdrive/platform/pricing/). Learn more about Hyperdrive in [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/). +If you want to connect directly to the Supabase Postgres database, connect using [Hyperdrive](/hyperdrive). Hyperdrive can provide lower latencies because it performs the database connection setup and connection pooling across Cloudflare's network. Hyperdrive supports native database drivers, libraries, and ORMs, and is included in all [Workers plans](/hyperdrive/platform/pricing/). Learn more about Hyperdrive in [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/). ::: @@ -72,11 +72,7 @@ To set up an integration with Supabase: const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY); const { data, error } = await supabase.from("countries").select("*"); if (error) throw error; - return new Response(JSON.stringify(data), { - headers: { - "Content-Type": "application/json", - }, - }); + return Response.json(data); }, }; ``` diff --git a/src/content/docs/workers/databases/third-party-integrations/turso.mdx b/src/content/docs/workers/databases/third-party-integrations/turso.mdx index 9865a4cac3a419d..d4c66a96de9ac23 100644 --- a/src/content/docs/workers/databases/third-party-integrations/turso.mdx +++ b/src/content/docs/workers/databases/third-party-integrations/turso.mdx @@ -89,7 +89,7 @@ To set up an integration with Turso: ```sh # Get your database URL turso db show my-db --url - + # Create an authentication token turso db tokens create my-db ``` @@ -100,8 +100,8 @@ To set up an integration with Turso: # Add the database URL as a secret npx wrangler secret put TURSO_URL # When prompted, paste your database URL - - # Add the authentication token as a secret + + # Add the authentication token as a secret npx wrangler secret put TURSO_AUTH_TOKEN # When prompted, paste your authentication token ``` @@ -126,14 +126,12 @@ To set up an integration with Turso: try { const res = await client.execute("SELECT * FROM elements"); - return new Response(JSON.stringify(res), { + return Response.json(res, { status: 200, - headers: { "Content-Type": "application/json" }, }); } catch (error) { console.error("Error executing SQL query:", error); - return new Response( - JSON.stringify({ error: "Internal Server Error" }), + return new Response({ error: "Internal Server Error" }, { status: 500, }, diff --git a/src/content/docs/workers/examples/aggregate-requests.mdx b/src/content/docs/workers/examples/aggregate-requests.mdx index cd2b48b2d719779..ba148df76f32297 100644 --- a/src/content/docs/workers/examples/aggregate-requests.mdx +++ b/src/content/docs/workers/examples/aggregate-requests.mdx @@ -35,10 +35,7 @@ export default { const responses = await Promise.all([fetch(url1), fetch(url2)]); const results = await Promise.all(responses.map((r) => r.json())); - const options = { - headers: { "content-type": "application/json;charset=UTF-8" }, - }; - return new Response(JSON.stringify(results), options); + return Response.json(results); }, }; ``` @@ -56,10 +53,7 @@ export default { const responses = await Promise.all([fetch(url1), fetch(url2)]); const results = await Promise.all(responses.map((r) => r.json())); - const options = { - headers: { "content-type": "application/json;charset=UTF-8" }, - }; - return new Response(JSON.stringify(results), options); + return Response.json(results); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/workers/examples/cache-tags.mdx b/src/content/docs/workers/examples/cache-tags.mdx index 567bb217e885fdd..0fa7d164d9c762d 100644 --- a/src/content/docs/workers/examples/cache-tags.mdx +++ b/src/content/docs/workers/examples/cache-tags.mdx @@ -35,7 +35,7 @@ export default { const errorObject = { error: "URL cannot be empty", }; - return new Response(JSON.stringify(errorObject), { status: 400 }); + return Response.json(errorObject, { status: 400 }); } const init = { cf: { @@ -50,15 +50,13 @@ export default { cache: cacheStatus, lastModified: lastModified, }; - return new Response(JSON.stringify(response), { - status: result.status, - }); + return Response.json(response, { status: result.status }); }) .catch((err) => { const errorObject = { error: err.message, }; - return new Response(JSON.stringify(errorObject), { status: 500 }); + return Response.json(errorObject, { status: 500 }); }); }, }; @@ -78,7 +76,7 @@ export default { const errorObject = { error: "URL cannot be empty", }; - return new Response(JSON.stringify(errorObject), { status: 400 }); + return Response.json(errorObject, { status: 400 }); } const init = { cf: { @@ -93,15 +91,13 @@ export default { cache: cacheStatus, lastModified: lastModified, }; - return new Response(JSON.stringify(response), { - status: result.status, - }); + return Response.json(response, { status: result.status }); }) .catch((err) => { const errorObject = { error: err.message, }; - return new Response(JSON.stringify(errorObject), { status: 500 }); + return Response.json(errorObject, { status: 500 }); }); }, } satisfies ExportedHandler; diff --git a/src/content/docs/workers/examples/modify-request-property.mdx b/src/content/docs/workers/examples/modify-request-property.mdx index 5b4085012082c02..a37c866f84863ce 100644 --- a/src/content/docs/workers/examples/modify-request-property.mdx +++ b/src/content/docs/workers/examples/modify-request-property.mdx @@ -75,9 +75,7 @@ export default { try { return await fetch(newRequest); } catch (e) { - return new Response(JSON.stringify({ error: e.message }), { - status: 500, - }); + return Response.json({ error: e.message }, { status: 500 }); } }, }; @@ -134,9 +132,7 @@ export default { try { return await fetch(newRequest); } catch (e) { - return new Response(JSON.stringify({ error: e.message }), { - status: 500, - }); + return Response.json({ error: e.message }, { status: 500 }); } }, } satisfies ExportedHandler; diff --git a/src/content/docs/workers/examples/modify-response.mdx b/src/content/docs/workers/examples/modify-response.mdx index faa92f674d2c907..97035d0a468d698 100644 --- a/src/content/docs/workers/examples/modify-response.mdx +++ b/src/content/docs/workers/examples/modify-response.mdx @@ -189,7 +189,7 @@ app.get('*', async (c) => { }; // Create a new custom response with modified status, headers, and body - const response = new Response(JSON.stringify(modifiedBody), { + const response = Response.json(modifiedBody, { status: 500, statusText: "some message", headers: originalResponse.headers, diff --git a/src/content/docs/workers/static-assets/index.mdx b/src/content/docs/workers/static-assets/index.mdx index 23d615a6ac7e9cd..6bd0a5551581c94 100644 --- a/src/content/docs/workers/static-assets/index.mdx +++ b/src/content/docs/workers/static-assets/index.mdx @@ -85,9 +85,7 @@ export default { const url = new URL(request.url); if (url.pathname.startsWith("/api/")) { - return new Response(JSON.stringify({ name: "Cloudflare" }), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json({ name: "Cloudflare" }); } return env.ASSETS.fetch(request); diff --git a/src/content/docs/workers/static-assets/routing/single-page-application.mdx b/src/content/docs/workers/static-assets/routing/single-page-application.mdx index 87ff725b0dcc4a2..1b61a29c2acb876 100644 --- a/src/content/docs/workers/static-assets/routing/single-page-application.mdx +++ b/src/content/docs/workers/static-assets/routing/single-page-application.mdx @@ -76,9 +76,7 @@ export default { const url = new URL(request.url); if (url.pathname === "/api/name") { - return new Response(JSON.stringify({ name: "Cloudflare" }), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json({ name: "Cloudflare" }); } return new Response(null, { status: 404 }); diff --git a/src/content/docs/workers/tutorials/build-a-jamstack-app.mdx b/src/content/docs/workers/tutorials/build-a-jamstack-app.mdx index 9a8d66aaf84d015..e9400258497eeca 100644 --- a/src/content/docs/workers/tutorials/build-a-jamstack-app.mdx +++ b/src/content/docs/workers/tutorials/build-a-jamstack-app.mdx @@ -150,7 +150,7 @@ export default { data = JSON.parse(cache); } - return new Response(JSON.stringify(data)); + return Response.json(data); }, }; ``` diff --git a/src/content/docs/workers/tutorials/postgres.mdx b/src/content/docs/workers/tutorials/postgres.mdx index 1bdd73dc12178ba..e2ad2ba65ed6f81 100644 --- a/src/content/docs/workers/tutorials/postgres.mdx +++ b/src/content/docs/workers/tutorials/postgres.mdx @@ -214,11 +214,7 @@ export default { const result = await sql.query("SELECT * FROM products"); // Return the result as JSON - return new Response(JSON.stringify(result.rows), { - headers: { - "Content-Type": "application/json", - }, - }); + return Response.json(result.rows); }, } satisfies ExportedHandler; ``` @@ -280,20 +276,14 @@ export default { ); // Return the inserted row as JSON - return new Response(JSON.stringify(insertResult.rows), { - headers: { "Content-Type": "application/json" }, - }); + return Response.json(insertResult.rows); } // Query the products table const result = await sql.query("SELECT * FROM products"); // Return the result as JSON - return new Response(JSON.stringify(result.rows), { - headers: { - "Content-Type": "application/json", - }, - }); + return Response.json(result.rows); }, } satisfies ExportedHandler; ``` diff --git a/src/content/docs/zaraz/advanced/context-enricher.mdx b/src/content/docs/zaraz/advanced/context-enricher.mdx index 67f4223e1693d54..01f9939da567eaf 100644 --- a/src/content/docs/zaraz/advanced/context-enricher.mdx +++ b/src/content/docs/zaraz/advanced/context-enricher.mdx @@ -36,7 +36,7 @@ export default { system.device.location.country = 'PI'; */ - return new Response(JSON.stringify({ system, client })); + return Response.json({ system, client }); }, }; ``` @@ -85,7 +85,7 @@ export default { const newContext = getWeatherForLocation({ system, client }); // Return as JSON - return new Response(JSON.stringify(newContext)); + return Response.json(newContext); }, }; ``` @@ -126,7 +126,7 @@ export default { const newContext = redactEmailAddressesFromObject({ system, client }); // Return as JSON - return new Response(JSON.stringify(newContext)); + return Response.json(newContext); }, }; ``` diff --git a/src/content/partials/hyperdrive/use-mysql-to-make-query.mdx b/src/content/partials/hyperdrive/use-mysql-to-make-query.mdx index 1a696537f6de6d0..432df0d4e49e9b8 100644 --- a/src/content/partials/hyperdrive/use-mysql-to-make-query.mdx +++ b/src/content/partials/hyperdrive/use-mysql-to-make-query.mdx @@ -44,11 +44,7 @@ export default { }); // Return result as JSON - return new Response(JSON.stringify(result), { - headers: { - "Content-Type": "application/json", - }, - }); + return Response.json(result); }, } satisfies ExportedHandler; ```