|
| 1 | +import { KV_KEYS } from '@/configs/keys' |
| 2 | +import { ERROR_CODES } from '@/configs/logs' |
| 3 | +import { kv } from '@vercel/kv' |
| 4 | + |
| 5 | +/** |
| 6 | + * Response type from the ZeroBounce email validation API |
| 7 | + */ |
| 8 | +export type EmailValidationResponse = { |
| 9 | + address: string |
| 10 | + status: string |
| 11 | + sub_status: string |
| 12 | + free_email: boolean |
| 13 | + account: string |
| 14 | + domain: string |
| 15 | + mx_found: boolean |
| 16 | + did_you_mean: string | null |
| 17 | + domain_age_days: string | null |
| 18 | + active_in_days: string | null |
| 19 | + smtp_provider: string | null |
| 20 | + mx_record: string | null |
| 21 | + firstname: string | null |
| 22 | + lastname: string | null |
| 23 | + gender: string | null |
| 24 | + country: string | null |
| 25 | + region: string | null |
| 26 | + city: string | null |
| 27 | + zipcode: string | null |
| 28 | + processed_at: string |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Validates an email address using the ZeroBounce API |
| 33 | + * |
| 34 | + * This function checks if an email is deliverable and safe to use by querying |
| 35 | + * the ZeroBounce validation service. It handles various email statuses including |
| 36 | + * invalid addresses, spam traps, and abusive accounts. |
| 37 | + * |
| 38 | + * @param email - The email address to validate |
| 39 | + * @returns An object containing validation result and response data, or null |
| 40 | + * - Object with `{ valid: boolean, data: EmailValidationResponse }` when validation succeeds |
| 41 | + * - `null` if validation couldn't be performed (API key missing or error occurred) |
| 42 | + * This allows for graceful degradation when email validation is unavailable |
| 43 | + * |
| 44 | + * @example |
| 45 | + * const result = await validateEmail("[email protected]"); |
| 46 | + * if (result === null) { |
| 47 | + * // Validation service unavailable |
| 48 | + * } else if (result.valid) { |
| 49 | + * // Email is valid |
| 50 | + * } else { |
| 51 | + * // Email is invalid |
| 52 | + * } |
| 53 | + */ |
| 54 | +export async function validateEmail( |
| 55 | + email: string |
| 56 | +): Promise<{ valid: boolean; data: EmailValidationResponse } | null> { |
| 57 | + if (!process.env.ZEROBOUNCE_API_KEY) { |
| 58 | + return null |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const response = await fetch( |
| 63 | + `https://api.zerobounce.net/v2/validate?api_key=${process.env.ZEROBOUNCE_API_KEY}&email=${email}&ip_address=` |
| 64 | + ) |
| 65 | + // Parse the JSON response from the ZeroBounce API |
| 66 | + const responseData = await response.json() |
| 67 | + |
| 68 | + // Convert the mx_found string value to a boolean if it's 'true' or 'false' |
| 69 | + // Otherwise keep the original value (could be null or another value) |
| 70 | + const data = { |
| 71 | + ...responseData, |
| 72 | + mx_found: |
| 73 | + responseData.mx_found === 'true' |
| 74 | + ? true |
| 75 | + : responseData.mx_found === 'false' |
| 76 | + ? false |
| 77 | + : responseData.mx_found, |
| 78 | + } as EmailValidationResponse |
| 79 | + |
| 80 | + switch (data.status) { |
| 81 | + case 'invalid': |
| 82 | + case 'spamtrap': |
| 83 | + case 'abuse': |
| 84 | + case 'do_not_mail': |
| 85 | + return { valid: false, data } |
| 86 | + default: |
| 87 | + return { valid: true, data } |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + console.error(ERROR_CODES.EMAIL_VALIDATION, error) |
| 91 | + return null |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export const shouldWarnAboutAlternateEmail = async ( |
| 96 | + validationResult: EmailValidationResponse |
| 97 | +): Promise<boolean> => { |
| 98 | + if (validationResult.sub_status === 'alternate') { |
| 99 | + const warnedAlternateEmail = await kv.get( |
| 100 | + KV_KEYS.WARNED_ALTERNATE_EMAIL(validationResult.address) |
| 101 | + ) |
| 102 | + |
| 103 | + if (!warnedAlternateEmail) { |
| 104 | + await kv.set( |
| 105 | + KV_KEYS.WARNED_ALTERNATE_EMAIL(validationResult.address), |
| 106 | + true |
| 107 | + ) |
| 108 | + |
| 109 | + return true |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return false |
| 114 | +} |
0 commit comments