Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tempmail.js
- const fetch = require('node-fetch');
- class TempMailCore {
- constructor(baseUrl = 'https://api.internal.temp-mail.io/api/v3') {
- this.baseUrl = baseUrl;
- }
- async listDomains() {
- try {
- const response = await fetch(`${this.baseUrl}/domains`);
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
- const data = await response.json();
- return {
- success: true,
- domains: data.domains
- };
- } catch (error) {
- return {
- success: false,
- error: error.message
- };
- }
- }
- async generateEmail(username, domain) {
- try {
- const response = await fetch(`${this.baseUrl}/email/new`, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- name: username,
- domain: domain
- })
- });
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
- const data = await response.json();
- return {
- success: true,
- email: data.email,
- token: data.token
- };
- } catch (error) {
- return {
- success: false,
- error: error.message
- };
- }
- }
- async checkEmail(emailAddress) {
- try {
- const response = await fetch(`${this.baseUrl}/email/${emailAddress}/messages`);
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
- const messages = await response.json();
- return {
- success: true,
- email: emailAddress,
- messages: messages
- };
- } catch (error) {
- return {
- success: false,
- email: emailAddress,
- error: error.message
- };
- }
- }
- async pollEmail(emailAddress, intervalSeconds = 3, maxAttempts = 25) {
- const results = [];
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
- const checkResult = await this.checkEmail(emailAddress);
- results.push({
- attempt: attempt,
- timestamp: new Date().toISOString(),
- ...checkResult
- });
- if (attempt < maxAttempts) {
- await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
- }
- }
- return {
- success: true,
- email: emailAddress,
- polling_results: results
- };
- }
- }
- module.exports = TempMailCore;
Advertisement
Add Comment
Please, Sign In to add comment