// server/supabase/functions/_shared/json-response.ts // JSON response + auth-error mapping shared by the knowledge functions. import { corsHeaders } from './cors.ts' import type { AuthError } from './auth.ts' export function jsonResponse(status: number, body: Record): Response { return new Response(JSON.stringify(body), { status, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, }) } export function isAuthError(error: unknown): error is AuthError { return Boolean( error && typeof error === 'object' && 'status' in error && (error.status === 401 || error.status === 403) && 'message' in error && typeof error.message === 'string', ) } /** Map a thrown value to a response: auth errors keep their status, anything else is a 500. */ export function errorResponse(error: unknown): Response { // Same shape as auth.ts authErrorResponse, without loading the Supabase client. if (isAuthError(error)) return jsonResponse(error.status, { error: error.message }) return jsonResponse(500, { error: 'internal_error' }) }