UNPKG

mailbucket

Version:

An npm library that aggregates multiple APIs for creating and receiving temporary emails.

148 lines (147 loc) 6.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailTmProvider = void 0; const base_provider_1 = require("./base.provider"); function makeHash(size) { const charset = "abcdefghijklmnopqrstuvwxyz0123456789"; const select = () => charset.charAt(Math.floor(Math.random() * charset.length)); return Array.from({ length: size }, select).join(""); } class MailTmProvider { providerName; baseUrl; constructor(config) { this.baseUrl = config.baseUrl; this.providerName = config.providerName; } async getDomains() { return (0, base_provider_1.makeRequest)(`${this.baseUrl}/domains`, { method: 'GET' }); } async registerAccount(address, password) { return (0, base_provider_1.makeRequest)(`${this.baseUrl}/accounts`, { method: 'POST', body: { address, password } }, 201); } async getToken(address, password) { return (0, base_provider_1.makeRequest)(`${this.baseUrl}/token`, { method: 'POST', body: { address, password } }); } async createAccount() { const domainRes = await this.getDomains(); if (!domainRes.success || !domainRes.data || domainRes.data.length === 0) { return { success: false, message: `Failed to get domains from ${this.providerName}: ${domainRes.message}`, error: domainRes.error }; } const domain = domainRes.data[Math.floor(Math.random() * domainRes.data.length)].domain; const username = `${makeHash(10)}@${domain}`; const password = makeHash(12); const registerRes = await this.registerAccount(username, password); if (!registerRes.success || !registerRes.data) { return { success: false, message: `Failed to register account with ${this.providerName}: ${registerRes.message}`, error: registerRes.error }; } const tokenRes = await this.getToken(username, password); if (!tokenRes.success || !tokenRes.data) { return { success: false, message: `Failed to login/get token from ${this.providerName} after registration: ${tokenRes.message}`, error: tokenRes.error }; } return { success: true, data: { providerName: this.providerName, address: username, providerData: { token: tokenRes.data.token, accountId: registerRes.data.id, password: password } } }; } async getMessages(account) { if (account.providerName !== this.providerName) { return { success: false, message: `Invalid account type for ${this.providerName}` }; } const { token } = account.providerData; if (!token) { return { success: false, message: 'Missing token in account providerData.' }; } const response = await (0, base_provider_1.makeRequest)(`${this.baseUrl}/messages`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}` } }); if (!response.success || !response.data) { return response; } const messages = response.data.map(msg => ({ id: msg.id, from: `${msg.from.name} <${msg.from.address}>`, subject: msg.subject, intro: msg.intro, receivedAt: new Date(msg.createdAt), seen: msg.seen })); return { success: true, data: messages }; } async getMessage(account, messageId) { if (account.providerName !== this.providerName) { return { success: false, message: `Invalid account type for ${this.providerName}` }; } const { token } = account.providerData; if (!token || !messageId) { return { success: false, message: 'Missing token or messageId.' }; } const response = await (0, base_provider_1.makeRequest)(`${this.baseUrl}/messages/${messageId}`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}` } }); if (!response.success || !response.data) { return response; } const msg = response.data; const message = { id: msg.id, from: `${msg.from.name} <${msg.from.address}>`, subject: msg.subject, intro: msg.intro, receivedAt: new Date(msg.createdAt), seen: msg.seen, bodyHtml: msg.html?.join(''), bodyText: msg.text, attachments: msg.attachments?.map(att => ({ id: att.id, filename: att.filename, contentType: att.contentType, size: att.size, })) }; return { success: true, data: message }; } async deleteMessage(account, messageId) { if (account.providerName !== this.providerName) { return { success: false, message: `Invalid account type for ${this.providerName}` }; } const { token } = account.providerData; if (!token || !messageId) { return { success: false, message: 'Missing token or messageId.' }; } return (0, base_provider_1.makeRequest)(`${this.baseUrl}/messages/${messageId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }, 204); } async deleteAccount(account) { if (account.providerName !== this.providerName) { return { success: false, message: `Invalid account type for ${this.providerName}` }; } const { token, accountId } = account.providerData; if (!token || !accountId) { return { success: false, message: 'Missing token or accountId in account providerData.' }; } return (0, base_provider_1.makeRequest)(`${this.baseUrl}/accounts/${accountId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }, 204); } } exports.MailTmProvider = MailTmProvider;