UNPKG

savedemails

Version:

Simple email collection for your applications

49 lines (48 loc) 1.28 kB
// src/index.ts async function saveEmail(email, apiKey, options = {}) { const baseUrl = options.baseUrl || "https://savedemails.app"; if (!email) { throw new Error("Email is required"); } if (!apiKey) { throw new Error("API key is required"); } if (!isValidEmail(email)) { throw new Error("Invalid email format"); } try { const response = await fetch(`${baseUrl}/api/emails`, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey }, body: JSON.stringify({ email, source: options.source, metadata: options.metadata }) }); if (!response.ok) { const error = await response.json().catch(() => ({ error: "Request failed" })); return { success: false, error: error.error || `Request failed with status ${response.status}` }; } const data = await response.json(); return { success: true, id: data.email?.id }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Failed to save email" }; } } function isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } export { saveEmail };