UNPKG

@0xtld/tair-node

Version:

A Node.js package for Tair functionality with configuration, core, and helper modules.

194 lines (193 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EMailRestGateway = exports.MailRest = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const utils_1 = require("../utils"); const config_1 = require("../config"); var EMailRestGateway; (function (EMailRestGateway) { EMailRestGateway["MAIL_TM"] = "https://api.mail.tm"; EMailRestGateway["MAIL_GW"] = "https://api.mail.gw"; })(EMailRestGateway || (EMailRestGateway = {})); exports.EMailRestGateway = EMailRestGateway; /** * @description MailRest class */ class MailRest { constructor(configs) { /** * @description jwt token of the account */ this.token = null; this.domains = []; this.configs = Object.assign(Object.assign({}, configs), { maximumRetries: configs.maximumRetries || 20 }); this.axios = axios_1.default.create({ baseURL: this.configs.gateway, headers: { "Content-Type": "application/json", Accept: "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3", }, }); this.axios.interceptors.response.use((response) => { const data = response.data || {}; if (data["hydra:member"]) { return data["hydra:member"]; } return data; }, (error) => { return Promise.reject(error); }); } setAccount(account) { this.configs.account = account; } async setAccountAndLogin(account) { this.token = null; this.setAccount(account); await this.login(); } async getDomains() { const data = await this.axios.get("/domains"); this.domains = data; return this.domains; } /** * @description Create an account * @param account IMailRestAccount * @param configs { createAndSet?: boolean } * @returns Promise<IMailRestAccount> */ async createAccount(account, configs = { createAndSet: false }) { if (this.domains.length === 0) { await this.getDomains(); } const domain = utils_1.Utils.shuffleArray(this.domains)[0]; if (domain === undefined) { throw new Error("No domain available"); } if (utils_1.Utils.isEmail(account.address)) { account.address = account.address.split("@")[0] + "@" + domain.domain; } config_1.logger.debug(`[${MailRest.name}] Creating account with email: ${account.address}`); account = await this.axios.post("/accounts", account); config_1.logger.debug(`[${MailRest.name}] Account created successfully`); if (configs.createAndSet) { this.setAccount(account); } return account; } async beforeRequest() { if (this.token === null) { await this.login(); } } async login() { const body = { address: this.configs.account.address, password: this.configs.account.password, }; const data = await this.axios.post("/token", body, { headers: { "Content-Type": "application/json", Accept: "application/json", }, }); this.token = data.token; } /** * * @returns Promise<IMailRestMessage[]> */ async messages() { await this.beforeRequest(); const url = "messages?page=1"; return await this.axios.get(url, { headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${this.token}`, }, }); } async deleteMessage(id) { await this.beforeRequest(); const url = `messages/${id}`; return await this.axios.delete(url, { headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${this.token}`, }, }); } async getMessage(id) { await this.beforeRequest(); const url = `messages/${id}`; return await this.axios.get(url, { headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${this.token}`, }, }); } /** * @description Truncate all messages */ async truncateAllMessages() { const messages = await this.messages(); for (const message of messages) { await this.deleteMessage(message.id); config_1.logger.debug("Message deleted successfully", message.id); } } /** * @description Get OTP from email * @param args IMailRestArgsGetOtp * @returns Promise<string | null> * */ async getOTP(args) { const messages = await this.messages(); const { receiptEmail, extractOtp, configs } = args; let message = messages.find((message) => { if (Array.isArray(message.to)) { return message.to.find((to) => to.address === receiptEmail); } }); const { isGetDetail, isDelete } = configs; let retry = configs.retry; if (!message) { if (retry < this.configs.maximumRetries) { config_1.logger.debug(`[${MailRest.name}] Retrying getOTP ..... in ${retry + 1} seconds`); await utils_1.Utils.sleep(retry + 1); return await this.getOTP({ receiptEmail, extractOtp, configs: { retry: retry + 1, isGetDetail, isDelete }, }); } return null; } if (isGetDetail) { try { message = await this.getMessage(message.id); } catch (e) { console.error(`[${MailRest.name}] Error getting message`, e); } } if (isDelete) { try { await this.deleteMessage(message.id); config_1.logger.debug("Message deleted successfully", message.id); } catch (e) { console.error(`[${MailRest.name}] Error deleting message`, e); } } return extractOtp(message); } } exports.MailRest = MailRest;