Refactor: Continue implementation plan

This commit is contained in:
gpt-engineer-app[bot]
2025-10-21 12:56:53 +00:00
parent 74860c6774
commit 0d247d9fdd
10 changed files with 254 additions and 65 deletions

View File

@@ -1,8 +1,9 @@
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
import { startRequest, endRequest } from "../_shared/logger.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-request-id',
};
// Comprehensive list of disposable email domains
@@ -74,18 +75,27 @@ const handler = async (req: Request): Promise<Response> => {
return new Response(null, { headers: corsHeaders });
}
const tracking = startRequest('validate-email');
try {
const { email }: ValidateEmailRequest = await req.json();
if (!email || typeof email !== 'string') {
endRequest(tracking, 400, 'Email address is required');
return new Response(
JSON.stringify({
valid: false,
reason: 'Email address is required'
reason: 'Email address is required',
requestId: tracking.requestId
} as ValidationResult),
{
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}
@@ -93,14 +103,21 @@ const handler = async (req: Request): Promise<Response> => {
// Basic email format validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
endRequest(tracking, 400, 'Invalid email format');
return new Response(
JSON.stringify({
valid: false,
reason: 'Invalid email format'
reason: 'Invalid email format',
requestId: tracking.requestId
} as ValidationResult),
{
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}
@@ -111,6 +128,9 @@ const handler = async (req: Request): Promise<Response> => {
// Check if domain is disposable
if (DISPOSABLE_DOMAINS.has(domain)) {
console.log(`Blocked disposable email domain: ${domain}`);
endRequest(tracking, 400, 'Disposable email domain blocked');
return new Response(
JSON.stringify({
valid: false,
@@ -119,37 +139,58 @@ const handler = async (req: Request): Promise<Response> => {
'Use a personal email (Gmail, Outlook, Yahoo, etc.)',
'Use your work or school email address',
'Use an email from your own domain'
]
],
requestId: tracking.requestId
} as ValidationResult),
{
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}
// Email is valid
console.log(`Email validated successfully: ${email}`);
endRequest(tracking, 200);
return new Response(
JSON.stringify({
valid: true
valid: true,
requestId: tracking.requestId
} as ValidationResult),
{
status: 200,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
} catch (error: any) {
console.error('Error in validate-email function:', error);
endRequest(tracking, 500, error.message);
return new Response(
JSON.stringify({
valid: false,
reason: 'Internal server error during email validation'
reason: 'Internal server error during email validation',
requestId: tracking.requestId
} as ValidationResult),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}