All files / lib contact.ts

100% Statements 39/39
100% Branches 27/27
100% Functions 12/12
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100              5x                                       5x     5x     19x 19x 19x 19x   19x 16x 1x   19x 17x 6x   25x   19x 16x 1x 15x 1x   19x       7x         8x 6x                                       8x 8x 8x 8x 3x     5x                
/**
 * Contact form logic, kept free of React and of the EmailJS SDK so it can be unit tested.
 *
 * Mail is sent with EmailJS from the browser. The recipient address lives in the EmailJS
 * template ("To Email"), not in the page, so it can't be changed by a visitor.
 */
 
export const topics = [
  { value: "hiring", label: "A role I'm hiring for" },
  { value: "project", label: "A project for Deleptual" },
  { value: "other", label: "Something else" },
] as const;
 
export type Topic = (typeof topics)[number]["value"];
 
export interface ContactInput {
  name: string;
  email: string;
  company: string;
  topic: string;
  message: string;
  /** Honeypot: hidden from people, filled in by naive bots. */
  website: string;
}
 
export type FieldErrors = Partial<Record<"name" | "email" | "topic" | "message", string>>;
 
export const LIMITS = { name: 100, email: 254, company: 120, message: 5000, minMessage: 10 };
 
// Deliberately loose: the real check is whether a reply arrives.
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
 
export function validateContact(input: ContactInput): FieldErrors {
  const errors: FieldErrors = {};
  const name = input.name.trim();
  const email = input.email.trim();
  const message = input.message.trim();
 
  if (!name) errors.name = "Enter your name.";
  else if (name.length > LIMITS.name)
    errors.name = `Keep your name under ${LIMITS.name} characters.`;
 
  if (!email) errors.email = "Enter your email address so I can reply.";
  else if (email.length > LIMITS.email || !EMAIL.test(email))
    errors.email = "Enter an email address like name@example.com.";
 
  if (!topics.some((t) => t.value === input.topic)) errors.topic = "Choose what this is about.";
 
  if (!message) errors.message = "Write a message.";
  else if (message.length < LIMITS.minMessage)
    errors.message = `Write at least ${LIMITS.minMessage} characters.`;
  else if (message.length > LIMITS.message)
    errors.message = `Keep your message under ${LIMITS.message.toLocaleString("en-CA")} characters.`;
 
  return errors;
}
 
export function isSpam(input: ContactInput): boolean {
  return input.website.trim().length > 0;
}
 
/** The variables the EmailJS template uses: {{from_name}}, {{reply_to}} and so on. */
export function toTemplateParams(input: ContactInput): Record<string, string> {
  const topic = topics.find((t) => t.value === input.topic)?.label ?? input.topic;
  return {
    from_name: input.name.trim(),
    reply_to: input.email.trim(),
    company: input.company.trim() || "(not given)",
    topic,
    message: input.message.trim(),
  };
}
 
export interface EmailJsConfig {
  serviceId: string;
  templateId: string;
  publicKey: string;
}
 
/**
 * Reads the EmailJS settings. They are public by design (EmailJS restricts use by
 * allowed origin), and Next.js inlines NEXT_PUBLIC_ values at build time.
 */
export function readEmailJsConfig(env: Record<string, string | undefined>): EmailJsConfig | null {
  const serviceId = env.NEXT_PUBLIC_EMAILJS_SERVICE_ID?.trim();
  const templateId = env.NEXT_PUBLIC_EMAILJS_TEMPLATE_ID?.trim();
  const publicKey = env.NEXT_PUBLIC_EMAILJS_PUBLIC_KEY?.trim();
  if (!serviceId || !templateId || !publicKey) return null;
  return { serviceId, templateId, publicKey };
}
 
export const emptyContact: ContactInput = {
  name: "",
  email: "",
  company: "",
  topic: "",
  message: "",
  website: "",
};