File size: 1,648 Bytes
03cdf80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d85549c
 
 
03cdf80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const axios = require('axios');

const EMAIL_API_URL = 'https://email.agbala-itura.name.ng/api';

async function generateEmail(label = null, custom = null) {
  const payload = {};
  if (label) payload.label = label;
  if (custom) payload.custom = custom;
  
  const response = await axios.post(`${EMAIL_API_URL}/addresses`, payload);
  
  if (response.data.success) {
    return response.data.data;
  }
  throw new Error('Failed to generate email: ' + JSON.stringify(response.data));
}

async function listEmails(address) {
  const response = await axios.get(`${EMAIL_API_URL}/emails`, {
    params: { address }
  });
  
  if (response.data.success) {
    return response.data.data.items;
  }
  return [];
}

async function getEmail(id) {
  const response = await axios.get(`${EMAIL_API_URL}/emails/${id}`);
  
  if (response.data.success) {
    return response.data.data;
  }
  throw new Error('Email not found');
}

async function waitForEmail(address, timeout = 120000, interval = 5000) {
  const startTime = Date.now();
  
  while (Date.now() - startTime < timeout) {
    const emails = await listEmails(address);
    
    if (emails.length > 0) {
      // Fetch the full email content (list only returns metadata)
      const fullEmail = await getEmail(emails[0].id);
      return fullEmail;
    }
    
    await new Promise(r => setTimeout(r, interval));
  }
  
  throw new Error('Timeout waiting for email');
}

async function deleteAddress(address) {
  await axios.delete(`${EMAIL_API_URL}/addresses/${encodeURIComponent(address)}`);
}

module.exports = { 
  generateEmail, 
  listEmails, 
  getEmail, 
  waitForEmail, 
  deleteAddress 
};