Migrate 3 edge functions to wrapper

- Refactor validate-email, receive-inbound-email, and send-admin-email-reply to use createEdgeFunction wrapper with automatic error handling, tracing, and reduced boilerplate.
- enrich wrapper to support service-role usage and role-based authorization context for complex flows.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 20:09:42 +00:00
parent afe7a93f69
commit 4040fd783e
4 changed files with 563 additions and 596 deletions

View File

@@ -1,7 +1,7 @@
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
import { corsHeaders } from '../_shared/cors.ts';
import { startRequest, endRequest, edgeLogger } from "../_shared/logger.ts";
import { formatEdgeError } from "../_shared/errorFormatter.ts";
import { createEdgeFunction, type EdgeFunctionContext } from '../_shared/edgeFunctionWrapper.ts';
import { addSpanEvent } from '../_shared/logger.ts';
// Comprehensive list of disposable email domains
const DISPOSABLE_DOMAINS = new Set([
@@ -64,143 +64,91 @@ interface ValidationResult {
valid: boolean;
reason?: string;
suggestions?: string[];
requestId: string;
}
const handler = async (req: Request): Promise<Response> => {
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
const handler = async (req: Request, { span, requestId }: EdgeFunctionContext): Promise<Response> => {
const { email }: ValidateEmailRequest = await req.json();
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',
requestId: tracking.requestId
} as ValidationResult),
{
status: 400,
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}
// 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',
requestId: tracking.requestId
} as ValidationResult),
{
status: 400,
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
}
// Extract domain
const domain = email.split('@')[1].toLowerCase();
// Check if domain is disposable
if (DISPOSABLE_DOMAINS.has(domain)) {
edgeLogger.info('Blocked disposable email domain', {
domain,
requestId: tracking.requestId
});
endRequest(tracking, 400, 'Disposable email domain blocked');
return new Response(
JSON.stringify({
valid: false,
reason: 'Disposable email addresses are not allowed. Please use a permanent email address.',
suggestions: [
'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',
'X-Request-ID': tracking.requestId
}
}
);
}
// Email is valid
edgeLogger.info('Email validated successfully', {
email,
requestId: tracking.requestId
});
endRequest(tracking, 200);
return new Response(
JSON.stringify({
valid: true,
requestId: tracking.requestId
} as ValidationResult),
{
status: 200,
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
} catch (error) {
const errorMessage = formatEdgeError(error);
edgeLogger.error('Error in validate-email function', {
error: errorMessage,
requestId: tracking.requestId
});
endRequest(tracking, 500, error.message);
if (!email || typeof email !== 'string') {
addSpanEvent(span, 'validation_failed', { reason: 'missing_email' });
return new Response(
JSON.stringify({
valid: false,
reason: 'Internal server error during email validation',
requestId: tracking.requestId
reason: 'Email address is required',
requestId
} as ValidationResult),
{
status: 500,
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
status: 400,
headers: { 'Content-Type': 'application/json' }
}
);
}
// Basic email format validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
addSpanEvent(span, 'validation_failed', { reason: 'invalid_format' });
return new Response(
JSON.stringify({
valid: false,
reason: 'Invalid email format',
requestId
} as ValidationResult),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
);
}
// Extract domain
const domain = email.split('@')[1].toLowerCase();
addSpanEvent(span, 'domain_extracted', { domain });
// Check if domain is disposable
if (DISPOSABLE_DOMAINS.has(domain)) {
addSpanEvent(span, 'disposable_domain_blocked', { domain });
return new Response(
JSON.stringify({
valid: false,
reason: 'Disposable email addresses are not allowed. Please use a permanent email address.',
suggestions: [
'Use a personal email (Gmail, Outlook, Yahoo, etc.)',
'Use your work or school email address',
'Use an email from your own domain'
],
requestId
} as ValidationResult),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
);
}
// Email is valid
addSpanEvent(span, 'email_validated', { email });
return new Response(
JSON.stringify({
valid: true,
requestId
} as ValidationResult),
{
status: 200,
headers: { 'Content-Type': 'application/json' }
}
);
};
serve(handler);
serve(createEdgeFunction({
name: 'validate-email',
requireAuth: false,
corsHeaders,
logRequests: true,
logResponses: true,
}, handler));