-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathmoemail-utils.js
More file actions
312 lines (292 loc) · 8.97 KB
/
moemail-utils.js
File metadata and controls
312 lines (292 loc) · 8.97 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
(function moemailUtilsModule(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
return;
}
root.MoemailUtils = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createMoemailUtils() {
const DEFAULT_MAIL_PAGE_SIZE = 20;
function firstNonEmptyString(values) {
for (const value of values) {
if (value === undefined || value === null) continue;
const normalized = String(value).trim();
if (normalized) return normalized;
}
return '';
}
function normalizeMoemailBaseUrl(rawValue = '') {
const value = String(rawValue || '').trim();
if (!value) return '';
const candidate = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(value) ? value : `https://${value}`;
try {
const parsed = new URL(candidate);
parsed.hash = '';
parsed.search = '';
const pathname = parsed.pathname === '/' ? '' : parsed.pathname.replace(/\/+$/, '');
return `${parsed.origin}${pathname}`;
} catch {
return '';
}
}
function normalizeMoemailDomain(rawValue = '') {
let value = String(rawValue || '').trim().toLowerCase();
if (!value) return '';
value = value.replace(/^@+/, '').replace(/^https?:\/\//, '').replace(/\/.*$/, '');
return /^[a-z0-9.-]+\.[a-z]{2,}$/i.test(value) ? value : '';
}
function normalizeMoemailDomains(values) {
const domains = [];
const seen = new Set();
const source = Array.isArray(values)
? values
: (typeof values === 'string'
? values.split(/[\s,,;;]+/)
: (values && typeof values === 'object' ? Object.values(values) : []));
for (const value of source) {
if (typeof value === 'string' && /[\s,,;;]/.test(value)) {
for (const piece of value.split(/[\s,,;;]+/)) {
const normalizedPiece = normalizeMoemailDomain(piece);
if (!normalizedPiece || seen.has(normalizedPiece)) continue;
seen.add(normalizedPiece);
domains.push(normalizedPiece);
}
continue;
}
const candidate = typeof value === 'object' && value
? (value.domain || value.name || value.value || '')
: value;
const normalized = normalizeMoemailDomain(candidate);
if (!normalized || seen.has(normalized)) continue;
seen.add(normalized);
domains.push(normalized);
}
return domains;
}
function normalizeMoemailAddress(value = '') {
return String(value || '').trim().toLowerCase();
}
function buildMoemailHeaders(config = {}, options = {}) {
const headers = {};
const apiKey = firstNonEmptyString([
config.apiKey,
config.moemailApiKey,
options.apiKey,
]);
if (apiKey) {
headers['X-API-Key'] = apiKey;
}
if (options.json) {
headers['Content-Type'] = 'application/json';
}
if (options.acceptJson !== false) {
headers.Accept = 'application/json';
}
return headers;
}
function joinMoemailUrl(baseUrl, path) {
const normalizedBase = normalizeMoemailBaseUrl(baseUrl);
const normalizedPath = String(path || '').trim();
if (!normalizedBase || !normalizedPath) return normalizedBase || '';
return `${normalizedBase}${normalizedPath.startsWith('/') ? '' : '/'}${normalizedPath}`;
}
function getArrayCandidates(payload) {
if (Array.isArray(payload)) return payload;
if (!payload || typeof payload !== 'object') return [];
if (
payload.id !== undefined
&& (
payload.subject !== undefined
|| payload.from_address !== undefined
|| payload.email !== undefined
|| payload.address !== undefined
)
) {
return [payload];
}
const candidates = [
payload.data,
payload.items,
payload.rows,
payload.results,
payload.list,
payload.records,
payload.emails,
payload.messages,
payload.message ? [payload.message] : null,
payload?.data?.items,
payload?.data?.rows,
payload?.data?.results,
payload?.data?.list,
payload?.data?.records,
payload?.data?.emails,
payload?.data?.messages,
payload?.data?.message ? [payload.data.message] : null,
];
for (const candidate of candidates) {
if (Array.isArray(candidate)) return candidate;
}
return [];
}
function stripHtmlTags(value = '') {
return String(value || '')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<[^>]+>/g, ' ')
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/</gi, '<')
.replace(/>/gi, '>')
.replace(/\s+/g, ' ')
.trim();
}
function normalizeMoemailDate(value) {
if (value === undefined || value === null || value === '') return '';
if (typeof value === 'number' && Number.isFinite(value)) {
const numeric = value < 1e12 ? value * 1000 : value;
return new Date(numeric).toISOString();
}
const source = String(value).trim();
if (!source) return '';
const parsed = Date.parse(source);
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : source;
}
function extractMoemailMessageBody(row = {}) {
return firstNonEmptyString([
row.text,
row.text_content,
row.textBody,
row.plainText,
row.bodyText,
row.body?.text,
row.contentText,
stripHtmlTags(firstNonEmptyString([
row.html,
row.html_content,
row.htmlBody,
row.bodyHtml,
row.body?.html,
row.content,
])),
row.snippet,
row.preview,
]);
}
function normalizeMoemailMailbox(row = {}) {
if (!row || typeof row !== 'object') return null;
const address = normalizeMoemailAddress(firstNonEmptyString([
row.email,
row.address,
row.mailbox,
row.mailAddress,
row.account,
row.username && row.domain ? `${row.username}@${row.domain}` : '',
]));
if (!address) return null;
return {
id: firstNonEmptyString([row.id, row.emailId, row.mailboxId, row.addressId]),
address,
raw: row,
};
}
function normalizeMoemailMessage(row = {}) {
if (!row || typeof row !== 'object') return null;
const htmlContent = firstNonEmptyString([
row.html,
row.htmlBody,
row.bodyHtml,
row.body?.html,
row.content,
]);
const textContent = extractMoemailMessageBody(row);
const code = firstNonEmptyString([row.verificationCode, row.verification_code, row.code]);
const bodyPreview = [code, textContent || stripHtmlTags(htmlContent)].filter(Boolean).join(' ').trim();
return {
id: firstNonEmptyString([row.id, row.messageId, row.mailId]),
address: normalizeMoemailAddress(firstNonEmptyString([
row.email,
row.address,
row.to,
row.recipient,
row.mailbox,
])),
subject: firstNonEmptyString([row.subject, row.title]),
from: {
emailAddress: {
address: firstNonEmptyString([
row.from?.address,
row.from?.email,
row.from_address,
row.fromAddress,
row.from,
row.sender,
row.senderEmail,
row.mailFrom,
]),
},
},
bodyPreview,
raw: htmlContent || textContent || code || '',
receivedDateTime: normalizeMoemailDate(firstNonEmptyString([
row.createdAt,
row.created_at,
row.receivedAt,
row.received_at,
row.date,
row.timestamp,
])),
};
}
function normalizeMoemailMailboxes(payload) {
return getArrayCandidates(payload).map((row) => normalizeMoemailMailbox(row)).filter(Boolean);
}
function normalizeMoemailMessages(payload) {
return getArrayCandidates(payload).map((row) => normalizeMoemailMessage(row)).filter(Boolean);
}
function getMoemailAddressFromResponse(payload = {}) {
return firstNonEmptyString([
payload.email,
payload.address,
payload.data?.email,
payload.data?.address,
payload.data?.mailbox,
]);
}
function getMoemailEmailIdFromResponse(payload = {}) {
return firstNonEmptyString([
payload.id,
payload.emailId,
payload.data?.id,
payload.data?.emailId,
payload.data?.mailboxId,
]);
}
function getMoemailNextCursor(payload = {}) {
return firstNonEmptyString([
payload.nextCursor,
payload.cursor,
payload.next,
payload?.data?.nextCursor,
payload?.data?.cursor,
payload?.data?.next,
payload?.meta?.nextCursor,
payload?.meta?.cursor,
]);
}
return {
DEFAULT_MAIL_PAGE_SIZE,
buildMoemailHeaders,
extractMoemailMessageBody,
getMoemailAddressFromResponse,
getMoemailEmailIdFromResponse,
getMoemailNextCursor,
joinMoemailUrl,
normalizeMoemailAddress,
normalizeMoemailBaseUrl,
normalizeMoemailDate,
normalizeMoemailDomain,
normalizeMoemailDomains,
normalizeMoemailMailboxes,
normalizeMoemailMessage,
normalizeMoemailMessages,
};
});