File size: 6,845 Bytes
8a8fe1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from 'axios';
import {md5, randomStr} from "./index";
import {CreateAxiosProxy} from "./proxyAgent";

export enum TempEmailType {
    // need credit card https://rapidapi.com/Privatix/api/temp-mail
    TempEmail = 'temp-email',
    // not need credit card , hard limit 100/day https://rapidapi.com/calvinloveland335703-0p6BxLYIH8f/api/temp-mail44
    TempEmail44 = 'temp-email44',
    // not need credit card and not need credit rapid_api_key
    TempMailLOL = 'tempmail-lol',
}

export function CreateEmail(tempMailType: TempEmailType, options?: BaseOptions): BaseEmail {
    switch (tempMailType) {
        case TempEmailType.TempEmail44:
            return new TempMail44(options);
        case TempEmailType.TempEmail:
            return new TempMail(options);
        case TempEmailType.TempMailLOL:
            return new TempMailLOL(options);
        default:
            throw new Error('not support TempEmailType')
    }
}

export interface BaseMailMessage {
    // main content of email
    content: string;
}

export interface TempMailMessage extends BaseMailMessage {
    _id: {
        oid: string;
    };
    createdAt: {
        milliseconds: number;
    };
    mail_id: string;
    mail_address_id: string;
    mail_from: string;
    mail_subject: string;
    mail_preview: string;
    mail_text_only: string;
    mail_text: string;
    mail_html: string;
    mail_timestamp: number;
    mail_attachments_count: number;
    mail_attachments: {
        attachment: any[];
    };
}

interface BaseOptions {
}

abstract class BaseEmail {
    protected constructor(options?: BaseOptions) {
    }

    public abstract getMailAddress(): Promise<string>

    public abstract waitMails(): Promise<BaseMailMessage[]>
}

export interface TempMailOptions extends BaseOptions {
    apikey?: string;
}

class TempMail extends BaseEmail {
    private readonly client: AxiosInstance;
    private address: string | undefined;
    private mailID: string = '';

    constructor(options?: TempMailOptions) {
        super(options)
        const apikey = options?.apikey || process.env.rapid_api_key;
        if (!apikey) {
            throw new Error('Need apikey for TempMail')
        }
        this.client = CreateAxiosProxy({
            baseURL: 'https://privatix-temp-mail-v1.p.rapidapi.com/request/',
            headers: {
                'X-RapidAPI-Key': apikey,
                'X-RapidAPI-Host': 'privatix-temp-mail-v1.p.rapidapi.com'
            }
        } as CreateAxiosDefaults);
    }

    public async getMailAddress(): Promise<string> {
        this.address = `${randomStr()}${await this.randomDomain()}`;
        this.mailID = md5(this.address);
        return this.address;
    }

    public async waitMails(): Promise<TempMailMessage[]> {
        const mailID = this.mailID;
        return new Promise(resolve => {
            let time = 0;
            const itl = setInterval(async () => {
                const response = await this.client.get(`/mail/id/${mailID}`);
                if (response.data && response.data.length > 0) {
                    resolve(response.data.map((item: any) => ({...item, content: item.mail_html})));
                    clearInterval(itl);
                    return;
                }
                if (time > 5) {
                    resolve([]);
                    clearInterval(itl);
                    return;
                }
                time++;
            }, 5000);
        });
    }

    async getDomainsList(): Promise<string[]> {
        const res = await this.client.get(`/domains/`);
        return res.data;
    }

    async randomDomain(): Promise<string> {
        const domainList = await this.getDomainsList();
        return domainList[Math.floor(Math.random() * domainList.length)];
    }
}

class TempMail44 extends BaseEmail {
    private readonly client: AxiosInstance;
    private address: string = '';

    constructor(options?: TempMailOptions) {
        super(options)
        const apikey = options?.apikey || process.env.rapid_api_key;
        if (!apikey) {
            throw new Error('Need apikey for TempMail')
        }
        this.client = CreateAxiosProxy({
            baseURL: 'https://temp-mail44.p.rapidapi.com/api/v3/email/',
            headers: {
                'X-RapidAPI-Key': apikey,
                'X-RapidAPI-Host': 'temp-mail44.p.rapidapi.com'
            }
        } as CreateAxiosDefaults);
    }

    public async getMailAddress(): Promise<string> {
        const response = await this.client.post('/new', {}, {
            headers: {
                'content-type': 'application/json',
            }
        } as AxiosRequestConfig);
        this.address = response.data.email;
        return this.address;
    }

    public async waitMails(): Promise<TempMailMessage[]> {
        return new Promise(resolve => {
            let time = 0;
            const itl = setInterval(async () => {
                const response = await this.client.get(`/${this.address}/messages`);
                if (response.data && response.data.length > 0) {
                    resolve(response.data.map((item: any) => ({...item, content: item.body_html})));
                    clearInterval(itl);
                    return;
                }
                if (time > 5) {
                    resolve([]);
                    clearInterval(itl);
                    return;
                }
                time++;
            }, 5000);
        });
    }
}

class TempMailLOL extends BaseEmail {
    private readonly client: AxiosInstance;
    private address: string = '';
    private token: string = '';

    constructor(options?: TempMailOptions) {
        super(options)
        this.client = CreateAxiosProxy({
            baseURL: 'https://api.tempmail.lol'
        } as CreateAxiosDefaults);
    }

    public async getMailAddress(): Promise<string> {
        const response = await this.client.get('/generate');
        this.address = response.data.address;
        this.token = response.data.token;
        return this.address;
    }

    public async waitMails(): Promise<TempMailMessage[]> {
        return new Promise(resolve => {
            let time = 0;
            const itl = setInterval(async () => {
                const response = await this.client.get(`/auth/${this.token}`);

                if (response.data && response.data.email.length > 0) {
                    resolve(response.data.email.map((item: any) => ({...item, content: item.html})));
                    clearInterval(itl);
                    return;
                }
                if (time > 5) {
                    resolve([]);
                    clearInterval(itl);
                    return;
                }
                time++;
            }, 5000);
        });
    }
}