This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MailBox.js
153 lines (132 loc) · 4.17 KB
/
MailBox.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
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
const crypto = require('crypto');
const fetch = require('node-fetch');
/**
* @type {string}
* @const
*/
const API_URL = 'http://api.temp-mail.ru';
/**
* Transforms response to JSON
*/
function transformResponse(response) {
return response.json();
}
/**
* Formats email message
*/
function formatMessage(message) {
return {
id: message.mail_id,
uid: message.mail_unique_id,
from: message.mail_from,
subject: message.mail_subject,
preview: message.mail_preview,
text: message.mail_preview.replace(/\s/g, ' ').trim(),
textOnly: message.mail_text_only,
html: message.mail_html,
timestamp: new Date(parseInt(message.mail_timestamp + '000'))
};
}
class MailBox {
/**
* @constructor
* @param {string} credentials Base64 encoded `username:password` combination used for authentication to the API
* @param {string|null} [address]
* @param {string} [apiUrl]
*/
constructor(credentials, address = null, apiUrl = API_URL) {
// this.credentials = credentials;
// this.params = {
// headers: {
// Authorization: 'Basic ' + this.credentials
// }
// };
this.address = address;
this.addressHash = address ? this.createAddressHash(address) : null;
this.apiUrl = apiUrl;
this.messages = [];
}
/**
* Checks used requests count and limits for a given account
* @returns {Promise.<Object, Error>}
*/
getAccountUsage() {
return fetch(`${this.apiUrl}/request/account/format/json/`).then(transformResponse);
}
/**
* Generates MD5 hash from email address
* @param {string} address
* @returns {string}
*/
createAddressHash(address) {
this.addressHash = crypto.createHash('md5').update(address).digest('hex');
return this.addressHash;
}
/**
* Generates random email address on one of the available domains
* @param {Array} domains
* @param {number} [len=7]
* @returns {string}
*/
generateRandomEmail(domains, len = 7) {
let name = Math.random().toString(36).substring(len);
let domain = domains[Math.floor(Math.random() * domains.length)];
this.address = name + domain;
return this.address;
}
/**
* Receives available domains
* @returns {Promise.<Array, Error>}
*/
getAvailableDomains() {
return fetch(`${this.apiUrl}/request/domains/format/json/`).then(transformResponse);
}
/**
* Generates email address on temp-mail.ru
* @param {number} [len]
* @returns {Promise.<String, Error>}
*/
getEmailAddress(len) {
return this.getAvailableDomains()
.then(availableDomains => this.generateRandomEmail(availableDomains, len));
}
/**
* Receives all messages from inbox at temp-mail.ru
* @param {string} [address]
* @returns {Promise.<(Object|Array), Error>}
*/
getMessages(address) {
address = address || this.address;
let url = `${this.apiUrl}/request/mail/id/${this.createAddressHash(address)}/format/json/`;
return fetch(url).then(transformResponse)
.then(response => {
this.messages = Array.isArray(response) ? response.map(formatMessage) : [];
return this.messages.length ? this.messages : response;
});
}
/**
* Deletes message with a given id
* @param {string} messageId
* @returns {Promise.<(Object|Array), Error>}
*/
deleteMessage(messageId) {
this.messages.forEach((message, index) => {
this.messages.splice(index, 1);
});
return fetch(`${this.apiUrl}/request/delete/id/${messageId}/format/json`).then(transformResponse);
}
/**
* Deletes all messages from inbox
* @returns {Promise.<Object, Error>}
*/
deleteAllMessages() {
return Promise.all(this.messages.map(message => {
return this.deleteMessage(message.id);
}))
.then(response => {
this.messages.length = 0;
return response;
});
}
}
module.exports = MailBox;