import type { LeadDatasetResponse } from './types.js'; import { requestApiWithAutoRefresh } from '@clawd/auth-runtime'; import type { EnvConfig } from '@clawd/auth-runtime'; /** * Fetch lead-dataset from a completed cold-outreach workflow. */ export async function fetchLeadDataset( config: EnvConfig, accessToken: string, workflowId: string, dryRun: boolean, ): Promise<{ data: LeadDatasetResponse | null; error: string }> { const url = `${config.authBase}/ecom/cold-outreach/${workflowId}/lead-dataset`; const result = await requestApiWithAutoRefresh( 'GET', url, dryRun, config, undefined, accessToken, ); if (result.status < 200 || result.status >= 300) { const msg = parseError(result.body); return { data: null, error: msg || `lead-dataset fetch failed: HTTP ${result.status}`, }; } try { const data = JSON.parse(result.body) as LeadDatasetResponse; if (!data.success) { return { data: null, error: data.message || 'lead-dataset returned success=false' }; } return { data, error: '' }; } catch { return { data: null, error: 'failed to parse lead-dataset response' }; } } function parseError(body: string): string { try { const data = JSON.parse(body); return data.message || data.error || ''; } catch { return ''; } }