File size: 1,927 Bytes
5812e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// tempmail.js
const fetch = require('node-fetch');

class TempMailCore {
    constructor(baseUrl = 'https://api.internal.temp-mail.io/api/v3') {
        this.baseUrl = baseUrl;
    }

    // ambil domain
    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 (e) {
            return { success: false, error: e.message };
        }
    }

    // buat email
    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 })
            });
            if (!response.ok) throw new Error(`HTTP ${response.status}`);
            const data = await response.json();
            return {
                success: true,
                email: data.email,
                token: data.token
            };
        } catch (e) {
            return { success: false, error: e.message };
        }
    }

    // cek inbox
    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
            };
        } catch (e) {
            return {
                success: false,
                email: emailAddress,
                error: e.message
            };
        }
    }
}

module.exports = TempMailCore;