diff --git a/README.md b/README.md index 433c55d..322c48b 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,9 @@ yarn add @samyca/mailflow ``` ## Usage + ### Creating an Account + ```js import { createAccount, createRandomAccount } from '@samyca/mailflow'; @@ -48,9 +50,13 @@ async function setupAccount() { console.log(account); } + +setupOtherAccount().then(); +setupAccount().then(); ``` ### Fetching and Parsing Emails + ```js import { createAccount } from '@samyca/mailflow'; @@ -64,8 +70,9 @@ async function getDataByCssQuery() { }); const lastEmail = await account.getLastMail({}); - const data = await lastEmail.getData('div.code'); - + const data = lastEmail.getData('div.code'); + console.log(data); } +getDataByCssQuery().then(); ``` diff --git a/package-lock.json b/package-lock.json index bded9a8..ef2ddf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@samyca/mailflow", - "version": "0.1.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@samyca/mailflow", - "version": "0.1.0", + "version": "1.0.1", "license": "MIT", "dependencies": { "cheerio": "^1.0.0-rc.12", diff --git a/package.json b/package.json index 66ce181..8221b42 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,18 @@ { "name": "@samyca/mailflow", - "version": "1.0.0", + "version": "1.0.1", "author": "Samyca ", "description": "A comprehensive tool for handling email flows in Node.js applications.", "homepage": "https://github.com/Samyca/mailflow#readme", - "keywords": ["email", "mail", "smtp", "imap", "mail parsing", "nodemailer", "mailflow"], + "keywords": [ + "email", + "mail", + "smtp", + "imap", + "mail parsing", + "nodemailer", + "mailflow" + ], "license": "MIT", "module": "dist/mailflow.esm.js", "main": "dist/index.js", @@ -14,7 +22,7 @@ ], "scripts": { "start": "tsdx watch", - "build": "tsdx build --format cjs,esm,umd", + "build": "tsdx build --format cjs,esm", "test": "tsdx test", "lint": "tsdx lint", "prepare": "husky", diff --git a/src/account.ts b/src/account.ts index bab57a5..6b8ba4b 100644 --- a/src/account.ts +++ b/src/account.ts @@ -101,12 +101,11 @@ export class Account { this.username = params.username; this.password = params.password; } - /** * Creates a nodemailer Transporter for SMTP operations. * @param params SMTP server parameters. */ - private _createTransport = (params: ServerParams) => { + private _createTransport(params: ServerParams) { this.transporter = nodemailer.createTransport({ host: params.host, port: params.port, @@ -116,13 +115,13 @@ export class Account { pass: params.password, }, }); - }; + } /** * Initializes the IMAP client for email retrieval. * @param params IMAP server parameters. */ - private _createClient = (params: ServerParams) => { + private _createClient(params: ServerParams) { this.client = new ImapFlow({ host: params.host, port: params.port, @@ -134,23 +133,23 @@ export class Account { pass: params.password, }, }); - }; + } /** * Parses the email from a raw source buffer. * @param source Raw source of the email. * @returns A Mail object parsed from the source. */ - private _parse = async (source: Buffer): Promise => { + private async _parse(source: Buffer): Promise { const parsedMail = await simpleParser(source); return new Mail(parsedMail); - }; + } /** * Retrieves all emails from the inbox. * @returns An array of FetchMessageObject representing each email. */ - private _getAllMails = async (): Promise => { + private async _getAllMails(): Promise { try { if (this.client instanceof ImapFlow) { const client = this.client; @@ -177,14 +176,14 @@ export class Account { } catch (error) { throw error; } - }; + } /** * Retrieves all emails that match the specified filter. * @param filter Criteria to filter emails. * @returns An array of Mail objects that match the filter criteria. */ - public getAllMails = async (filter: FilterParams): Promise => { + public async getAllMails(filter: FilterParams): Promise { try { const fetchMessage = await this._getAllMails(); @@ -199,21 +198,21 @@ export class Account { } catch (error) { throw error; } - }; + } /** * Retrieves the most recent email that matches the specified filter. * @param filter Criteria to filter emails. * @returns The most recent Mail object that matches the filter criteria. */ - public getLastMail = async (filter: FilterParams): Promise => { + public async getLastMail(filter: FilterParams): Promise { try { const mails = await this.getAllMails(filter); return mails[0]; } catch (error) { throw error; } - }; + } /** * Filters emails based on the specified criteria. @@ -221,7 +220,7 @@ export class Account { * @param mails Array of Mail objects to filter. * @returns An array of Mail objects that match the filter criteria. */ - private _leaked = (filter: FilterParams, mails: Mail[]): Mail[] => { + private _leaked(filter: FilterParams, mails: Mail[]): Mail[] { return mails.filter((mail: Mail) => { if (filter.from && mail.from?.text !== filter.from) { return false; @@ -249,7 +248,7 @@ export class Account { return true; }); - }; + } /** * Sends an email using the configured SMTP transporter. diff --git a/src/index.ts b/src/index.ts index 813e6f5..626fe51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,14 @@ -import { Account } from './account'; -import { AccountParams, FilterParams, ServerParams } from './types'; -import { createAccount, createRandomAccount, getDataByMail } from './utils'; -import { Mail } from './mail'; - -/** - * Re-exporting the imported modules for easier access - */ -export { Account, Mail }; -export { AccountParams, FilterParams, ServerParams }; -export { createAccount, createRandomAccount, getDataByMail }; +export { Account } from './account'; +export { Mail } from './mail'; +export { AccountParams, FilterParams, ServerParams } from './types'; +export { + createAccount, + createRandomAccount, + getDataByMail, + lastMailData, + accountLastMailData, + lastMail, + accountLastMail, + lastMailAndData, + accountLastMailAndData, +} from './utils'; diff --git a/src/utils.ts b/src/utils.ts index 42b2234..38ba8fc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import nodemailer from 'nodemailer'; import { Account } from './account'; import { Mail } from './mail'; -import { AccountParams } from './types'; +import { AccountParams, FilterParams } from './types'; /** * Creates and configures an Account instance with provided SMTP and IMAP settings. @@ -49,3 +49,92 @@ export async function createRandomAccount(): Promise { export function getDataByMail(mail: Mail, cssQuery: string) { return mail.getData(cssQuery); } + +/** + * Fetches the last email based on provided filter parameters. + * @param params Configuration parameters for the account including IMAP settings. + * @param filters Criteria to filter emails, such as sender, subject, and date range. + * @returns A Promise that resolves with the last Mail object that matches the filter criteria. + */ +export async function lastMail(params: AccountParams, filters: FilterParams): Promise { + const account = new Account({ + imapHost: params.imapHost, + imapPort: params.imapPort, + imapSecure: params.imapSecure, + username: params.username, + password: params.password, + smtpHost: '', + smtpPort: 0, + smtpSecure: false, + }); + + return await account.getLastMail(filters); +} + +/** + * Fetches the last email for a given account based on provided filter parameters. + * @param account An instance of the Account class. + * @param filters Criteria to filter emails, such as sender, subject, and date range. + * @returns A Promise that resolves with the last Mail object that matches the filter criteria. + */ +export async function accountLastMail(account: Account, filters: FilterParams): Promise { + return await account.getLastMail(filters); +} + +/** + * Fetches the last email and extracts data from it based on a CSS query. + * @param params Configuration parameters for the account including IMAP settings. + * @param filters Criteria to filter emails. + * @param cssQuery A CSS query to extract data from the email's HTML body. + * @returns A Promise that resolves with the extracted data as a string. + */ +export async function lastMailData(params: AccountParams, filters: FilterParams, cssQuery: string): Promise { + const mail = await lastMail(params, filters); + return mail.getData(cssQuery); +} + +/** + * Fetches the last email for a given account and extracts data from it based on a CSS query. + * @param account An instance of the Account class. + * @param filters Criteria to filter emails. + * @param cssQuery A CSS query to extract data from the email's HTML body. + * @returns A Promise that resolves with the extracted data as a string. + */ +export async function accountLastMailData(account: Account, filters: FilterParams, cssQuery: string): Promise { + const mail = await accountLastMail(account, filters); + return mail.getData(cssQuery); +} + +/** + * Fetches the last email based on provided filter parameters and extracts data from it using a CSS query. + * @param params Configuration parameters for the account including IMAP settings. + * @param filters Criteria to filter emails. + * @param cssQuery A CSS query to extract data from the email's HTML body. + * @returns A Promise that resolves with an object containing the last Mail and the extracted data. + */ +export async function lastMailAndData( + params: AccountParams, + filters: FilterParams, + cssQuery: string +): Promise<{ mail: Mail; data: string }> { + const mail = await lastMail(params, filters); + const data = mail.getData(cssQuery); // Ensure this is awaited if getData is an async operation + return { mail, data }; +} + +/** + * Fetches the last email for a given account based on provided filter parameters and extracts data from it using a CSS query. + * @param account An instance of the Account class. + * @param filters Criteria to filter emails. + * @param cssQuery A CSS query to extract data from the email's HTML body. + * @returns A Promise that resolves with an object containing the last Mail and the extracted data. + */ +export async function accountLastMailAndData( + account: Account, + filters: FilterParams, + cssQuery: string +): Promise<{ mail: Mail; data: string }> { + const mail = await accountLastMail(account, filters); + const data = mail.getData(cssQuery); // Ensure this is awaited if getData is an async operation + return { mail, data }; +} diff --git a/tsdx.config.js b/tsdx.config.js deleted file mode 100644 index 7031399..0000000 --- a/tsdx.config.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - rollup(config, options) { - - if (config.output instanceof Array) { - config.output = config.output?.filter( - (output) => !(output.format === 'cjs' && output.env === 'development') - ); - } - - - return config; - }, -};