Skip to content

Commit 4836ada

Browse files
committed
chore: use Response.json in edge functions, drop two dead declarations
Response.json sets the JSON content type itself, so three hand-rolled wrappers that stringified a body and spread in a Content-Type header can delegate to it. Call sites are unchanged. Two behavioural differences exist in principle, and neither is reachable here: - Response.json(undefined) throws a TypeError, where the old form produced an empty body. Both remaining wrappers are typed `unknown`, so this was worth checking: every one of the 12 call sites in import-source and the 3 in announcements passes an object literal, never a possibly-undefined value. - Response.json defers to a caller-supplied content-type, where the old form always overrode it. No getCorsHeaders in this repo returns one. Verified under Deno 2.9.6 that the content type is exactly "application/json" with no charset parameter, matching the old form byte for byte. Also deletes two declarations with no references: - _shared/cors.ts, a wildcard Access-Control-Allow-Origin header set with no importer, no import-map alias, and no config.toml entry. Every function defines its own getCorsHeaders origin allowlist instead, so this file was a misleading leftover. _shared/native-cors.test.ts is unaffected: it iterates a hardcoded list of function names and reads ../<name>/index.ts as text, so it never enumerates _shared or asserts on this file. - the local Source type in ai/index.ts, never exported and never used. Verified on main fe679fd, from a clean npm ci: docs:check, lint, typecheck, 956 unit tests, 121 edge tests, deno check on the 4 touched function entrypoints, build, and 126 Chromium e2e tests pass.
1 parent fe679fd commit 4836ada

5 files changed

Lines changed: 3 additions & 24 deletions

File tree

‎supabase/functions/_shared/cors.ts‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎supabase/functions/ai-stream/index.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ function getProviderForModel(model: string): "openai" | "google" | "anthropic" |
8181
}
8282

8383
function jsonResponse(body: Record<string, unknown>, status: number, corsHeaders: Record<string, string>) {
84-
return new Response(JSON.stringify(body), {
85-
status,
86-
headers: { ...corsHeaders, "Content-Type": "application/json" },
87-
});
84+
return Response.json(body, { status, headers: corsHeaders });
8885
}
8986

9087
function sse(event: string, payload: Record<string, unknown>) {

‎supabase/functions/ai/index.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ function getCorsHeaders(origin: string | null) {
2727

2828
type Feature = "generate" | "attack" | "brainstorm" | "factoids" | "ask" | "sparring" | "crossfire";
2929

30-
type Source = {
31-
title: string;
32-
content: string;
33-
origin?: string;
34-
type: string;
35-
};
36-
3730
// Generate unique request ID for tracing
3831
function generateRequestId(): string {
3932
return `edge_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;

‎supabase/functions/announcements/index.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ function getCorsHeaders(origin: string | null) {
2323
}
2424

2525
function json(data: unknown, status: number, corsHeaders: Record<string, string>) {
26-
return new Response(JSON.stringify(data), {
27-
status,
28-
headers: { ...corsHeaders, "Content-Type": "application/json" },
29-
});
26+
return Response.json(data, { status, headers: corsHeaders });
3027
}
3128

3229
Deno.serve(async (req) => {

‎supabase/functions/import-source/index.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
3333
const supabase = createClient(supabaseUrl, supabaseServiceKey);
3434

3535
function jsonResponse(body: unknown, status: number, headers: Record<string, string>) {
36-
return new Response(JSON.stringify(body), {
37-
status,
38-
headers: { ...headers, "Content-Type": "application/json" },
39-
});
36+
return Response.json(body, { status, headers });
4037
}
4138

4239
async function isUserBanned(userId: string): Promise<boolean> {

0 commit comments

Comments
 (0)