-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
77 lines (68 loc) · 2.28 KB
/
index.js
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
const { default: axios } = require("axios");
const nodify = require("./nodify");
/** @typedef {"1secmail.com" | "1secmail.net" | "1secmail.org" | "wwjmp.com" | "esiix.com" | "xojxe.com" | "yoggm.com"} Domain */
/** @typedef {{ from: string, timestamp: string, subject: string, message: string }} Message */
/** @typedef {{ address: string, messageCount: number, messages: Message[] }} EmailFetchResult */
class TempMail {
/** @type {string} */
mailingAddressLabel;
/** @type {Domain} */
domain;
/**
* create a new instance of TempMail
* @param {string} mailingAddressLabel mailing address label
* @param {Domain} [domain="1secmail.com"] domain of the mailing address
*/
constructor(mailingAddressLabel, domain = "1secmail.com") {
this.mailingAddressLabel = mailingAddressLabel;
this.domain = domain;
}
/**
* fetchs emails and returns it
* @returns {Promise<EmailFetchResult>}
*/
async #fetchEmails() {
const httpResponse = await axios.get(
`https://www.1secmail.com/api/v1/?action=getMessages&login=${this.mailingAddressLabel}&domain=${this.domain}`,
);
const data = httpResponse.data;
const messages = await Promise.all(
data.map(async (email) => {
const mailHttpResponse = await axios.get(
`https://www.1secmail.com/api/v1/?action=readMessage&login=${this.mailingAddressLabel}&domain=${this.domain}&id=${email.id}`,
);
const mailData = mailHttpResponse.data;
return {
from: mailData.from,
timestamp: mailData.date,
subject: mailData.subject,
message: mailData.body,
};
}),
);
const res = {
address: `${this.mailingAddressLabel}@${this.domain}`,
messageCount: data.length,
messages,
};
return res;
}
/**
* fetchs emails and calls the callback if not provided just returns a promise
* @param {import("./nodify").callback<EmailFetchResult, Error>} callback callback
* @returns {Promise<EmailFetchResult>}
*/
fetchEmails(callback) {
return nodify(this.#fetchEmails(), callback);
}
/**
* returns the mailing address label
* @returns {{ address: string }}
*/
getAddress() {
return {
address: `${this.mailingAddressLabel}@${this.domain}`,
};
}
}
module.exports = TempMail;