class CallPrepClient {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'https://rpiqzfzokrwxavztrpmp.supabase.co/functions/v1';
}
async research(email, options = {}) {
const res = await fetch(`${this.baseUrl}/research`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, ...options }),
});
const data = await res.json();
if (!res.ok) {
throw new CallPrepError(data.error, res.status, data);
}
return data;
}
async pollStatus(researchId, { intervalMs = 5000, timeoutMs = 180000 } = {}) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
await sleep(intervalMs);
const res = await fetch(`${this.baseUrl}/research-status/${researchId}`, {
headers: { 'Authorization': `Bearer ${this.apiKey}` },
});
const data = await res.json();
if (data.status === 'completed') return data;
if (data.status === 'failed') {
throw new CallPrepError('job_failed', 200, data);
}
}
throw new CallPrepError('polling_timeout', 0, { researchId });
}
}
class CallPrepError extends Error {
constructor(code, status, body) {
super(`CallPrep error: ${code}`);
this.code = code;
this.status = status;
this.body = body;
}
}
const sleep = ms => new Promise(r => setTimeout(r, ms));