Spaces:
Runtime error
Runtime error
| 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 | |
| }; | |