Skip to content

Commit 777c24f

Browse files
committed
Use Response.json where applicable
1 parent 5bf7b75 commit 777c24f

File tree

51 files changed

+81
-162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+81
-162
lines changed

src/content/docs/agents/api-reference/agents-api.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,9 @@ class MyAgent extends Agent<Env, MyState> {
148148
async onRequest(request: Request) {
149149
if (request.method === "POST") {
150150
await this.incrementCounter();
151-
return new Response(JSON.stringify(this.state), {
152-
headers: { "Content-Type": "application/json" }
153-
});
151+
return Response.json(this.state);
154152
}
155-
return new Response(JSON.stringify(this.state), {
156-
headers: { "Content-Type": "application/json" }
157-
});
153+
return Response.json(this.state);
158154
}
159155

160156
async incrementCounter() {

src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ export default {
9494
);
9595

9696
// Return the AI response as a JSON object
97-
return new Response(JSON.stringify(response), {
98-
headers: { "Content-Type": "application/json" },
99-
});
97+
return Response.json(response);
10098
},
10199
} satisfies ExportedHandler<Env>;
102100
```

src/content/docs/ai-gateway/observability/custom-metadata.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default {
8282
);
8383

8484
const response = chatCompletion.choices[0].message;
85-
return new Response(JSON.stringify(response));
85+
return Response.json(response);
8686
} catch (e) {
8787
console.log(e);
8888
return new Response(e);

src/content/docs/ai-gateway/tutorials/deploy-aig-worker.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default {
125125

126126
const response = chatCompletion.choices[0].message;
127127

128-
return new Response(JSON.stringify(response));
128+
return Response.json(response);
129129
} catch (e) {
130130
return new Response(e);
131131
}

src/content/docs/ai-gateway/usage/providers/bedrock.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default {
9696
response.headers.get("content-type")?.includes("application/json")
9797
) {
9898
const data = await response.json();
99-
return new Response(JSON.stringify(data));
99+
return Response.json(data);
100100
}
101101

102102
return new Response("Invalid response", { status: 500 });

src/content/docs/ai-gateway/usage/providers/deepseek.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ try {
7474

7575
const response = chatCompletion.choices[0].message;
7676

77-
return new Response(JSON.stringify(response));
77+
return Response.json(response);
7878
} catch (e) {
7979
return new Response(e);
8080
}

src/content/docs/ai-gateway/usage/providers/openrouter.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ try {
6565

6666
const response = chatCompletion.choices[0].message;
6767

68-
return new Response(JSON.stringify(response));
68+
return Response.json(response);
6969
} catch (e) {
7070
return new Response(e);
7171
}

src/content/docs/ai-gateway/usage/providers/workersai.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default {
106106
},
107107
},
108108
);
109-
return new Response(JSON.stringify(response));
109+
return Response.json(response);
110110
},
111111
} satisfies ExportedHandler<Env>;
112112
```

src/content/docs/cloudflare-one/identity/authorization-cookie/cors.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default {
204204

205205
return modifiedResponse;
206206
} catch (e) {
207-
return new Response(JSON.stringify({ error: e.message }), {
207+
return Response.json({ error: e.message }, {
208208
status: 500,
209209
});
210210
}

src/content/docs/cloudflare-one/tutorials/ai-wrapper-tenant-control.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ export default {
156156
},
157157
);
158158
} catch (error) {
159-
return new Response(JSON.stringify({ error: error.message }), {
160-
status: 500,
161-
headers: { "Content-Type": "application/json" },
159+
return Response.json({ error: error.message }, {
160+
status: 500
162161
});
163162
}
164163
}

0 commit comments

Comments
 (0)