Spaces:
Running
Running
File size: 4,436 Bytes
bd28470 | 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import axios from "axios";
import { getEnv } from "../../shared/config/env";
import { withRetry, isCircuitOpen, recordFailure, recordSuccess } from "../../shared/utils/retry";
import { hunterLimiter } from "../../shared/utils/rate-limiter";
import { logger } from "../../shared/utils/logger";
const PROVIDER = "hunter";
export interface HunterEmailResult {
email: string | null;
score: number; // Hunter confidence 0-100
source: "hunter";
firstName: string | null;
lastName: string | null;
}
/**
* Finds a professional email address using Hunter.io.
* Falls through to pattern generation if not found.
*/
export async function findEmail(
domain: string,
firstName: string,
lastName: string
): Promise<HunterEmailResult | null> {
if (isCircuitOpen(PROVIDER)) return null;
await hunterLimiter.consume(PROVIDER);
try {
const result = await withRetry(
() => callHunterEmailFinder(domain, firstName, lastName),
{ provider: PROVIDER }
);
recordSuccess(PROVIDER);
return result;
} catch (err) {
recordFailure(PROVIDER);
logger.warn({ domain, err }, "Hunter email find failed β will try pattern generation");
return null;
}
}
/**
* Searches all known emails for a domain (domain search).
*/
export async function searchDomain(domain: string): Promise<HunterEmailResult[]> {
if (isCircuitOpen(PROVIDER)) return [];
await hunterLimiter.consume(PROVIDER);
try {
const result = await withRetry(
() => callHunterDomainSearch(domain),
{ provider: PROVIDER }
);
recordSuccess(PROVIDER);
return result;
} catch (err) {
recordFailure(PROVIDER);
logger.warn({ domain, err }, "Hunter domain search failed");
return [];
}
}
async function callHunterEmailFinder(
domain: string,
firstName: string,
lastName: string
): Promise<HunterEmailResult | null> {
const env = getEnv();
const response = await axios.get("https://api.hunter.io/v2/email-finder", {
params: {
domain,
first_name: firstName,
last_name: lastName,
api_key: env.HUNTER_API_KEY,
},
timeout: 8_000,
});
const data = response.data?.data;
if (!data?.email) return null;
return {
email: data.email,
score: data.score ?? 0,
source: "hunter",
firstName: data.first_name ?? null,
lastName: data.last_name ?? null,
};
}
async function callHunterDomainSearch(domain: string): Promise<HunterEmailResult[]> {
const env = getEnv();
const response = await axios.get("https://api.hunter.io/v2/domain-search", {
params: { domain, api_key: env.HUNTER_API_KEY, limit: 10 },
timeout: 8_000,
});
const emails = response.data?.data?.emails ?? [];
return emails
.filter((e: { type: string }) => e.type === "professional")
.map((e: { value: string; confidence: number; first_name: string; last_name: string }) => ({
email: e.value,
score: e.confidence,
source: "hunter" as const,
firstName: e.first_name ?? null,
lastName: e.last_name ?? null,
}));
}
// βββ Aliases for contact-enricher.ts compatibility ββββββββββ
export type HunterContact = {
value: string; // email
first_name: string | null;
last_name: string | null;
position: string | null;
seniority: string | null;
confidence: number;
};
/**
* Search for contacts at a domain β used by contact-enricher.
* Maps Hunter's domain-search response to HunterContact format.
*/
export async function searchHunterContacts(domain: string): Promise<HunterContact[]> {
if (isCircuitOpen(PROVIDER)) return [];
await hunterLimiter.consume(PROVIDER);
try {
const env = getEnv();
const response = await axios.get("https://api.hunter.io/v2/domain-search", {
params: { domain, api_key: env.HUNTER_API_KEY, limit: 10 },
timeout: 8_000,
});
recordSuccess(PROVIDER);
const emails = response.data?.data?.emails ?? [];
return emails.map((e: Record<string, unknown>) => ({
value: (e.value as string) ?? "",
first_name: (e.first_name as string) ?? null,
last_name: (e.last_name as string) ?? null,
position: (e.position as string) ?? null,
seniority: (e.seniority as string) ?? null,
confidence: (e.confidence as number) ?? 0,
}));
} catch (err) {
recordFailure(PROVIDER);
logger.warn({ domain, err }, "Hunter domain search failed");
return [];
}
}
|