Skip to content

Commit

Permalink
add(utils): add more utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
darienmh committed Feb 29, 2024
1 parent 06c6f46 commit d53d00d
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 47 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ yarn add @samyca/mailflow
```

## Usage

### Creating an Account

```js
import { createAccount, createRandomAccount } from '@samyca/mailflow';

Expand All @@ -48,9 +50,13 @@ async function setupAccount() {

console.log(account);
}

setupOtherAccount().then();
setupAccount().then();
```

### Fetching and Parsing Emails

```js
import { createAccount } from '@samyca/mailflow';

Expand All @@ -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();
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
"name": "@samyca/mailflow",
"version": "1.0.0",
"version": "1.0.1",
"author": "Samyca <contact@samyca.com>",
"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",
Expand All @@ -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",
Expand Down
29 changes: 14 additions & 15 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<Mail> => {
private async _parse(source: Buffer): Promise<Mail> {
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<FetchMessageObject[]> => {
private async _getAllMails(): Promise<FetchMessageObject[]> {
try {
if (this.client instanceof ImapFlow) {
const client = this.client;
Expand All @@ -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<Mail[]> => {
public async getAllMails(filter: FilterParams): Promise<Mail[]> {
try {
const fetchMessage = await this._getAllMails();

Expand All @@ -199,29 +198,29 @@ 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<Mail> => {
public async getLastMail(filter: FilterParams): Promise<Mail> {
try {
const mails = await this.getAllMails(filter);
return mails[0];
} catch (error) {
throw error;
}
};
}

/**
* Filters emails based on the specified criteria.
* @param filter Criteria to filter emails by.
* @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;
Expand Down Expand Up @@ -249,7 +248,7 @@ export class Account {

return true;
});
};
}

/**
* Sends an email using the configured SMTP transporter.
Expand Down
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
91 changes: 90 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -49,3 +49,92 @@ export async function createRandomAccount(): Promise<Account> {
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<Mail> {
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<Mail> {
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<string> {
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<string> {
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 };
}
13 changes: 0 additions & 13 deletions tsdx.config.js

This file was deleted.

0 comments on commit d53d00d

Please sign in to comment.