msgraph-email-reader
v1.0.1
Published
Here’s a `README.md` file for your `msgraph-email-reader` package, explaining how to use it:
Downloads
11
Readme
Here’s a README.md
file for your msgraph-email-reader
package, explaining how to use it:
msgraph-email-reader
A lightweight TypeScript library for reading emails using Microsoft Graph API. This package supports fetching and managing access tokens and provides an easy-to-use method for retrieving emails from a user's mailbox.
Features
- Fetch an access token from Microsoft Identity Platform.
- Use the access token to retrieve emails from a user's mailbox via Microsoft Graph API.
- Designed for integration with Node.js or Cypress for end-to-end testing.
Installation
Install the package via npm:
npm install msgraph-email-reader
Prerequisites
Microsoft Azure AD App Registration
- Register an application in Azure AD.
- Obtain the following credentials:
- Client ID
- Client Secret
- Tenant ID
- Grant the application the
Mail.Read
permission.
Node.js
Ensure Node.js version14.17
or higher is installed.
Usage
1. Import the Package
Import the library into your Node.js or Cypress project:
import {
fetchAndSetAccessToken,
setAccessToken,
readEmails,
} from "msgraph-email-reader";
2. Fetch and Set Access Token
Retrieve and set the access token using your Azure AD credentials:
await fetchAndSetAccessToken(clientId, clientSecret, tenantId);
3. Read Emails
Fetch emails for a specific user based on search criteria:
const emails = await readEmails(emailID, searchText);
console.log("Fetched Emails:", emails);
Examples
Basic Example
import { fetchAndSetAccessToken, readEmails } from "msgraph-email-reader";
const clientId = "your-client-id";
const clientSecret = "your-client-secret";
const tenantId = "your-tenant-id";
const emailID = "[email protected]";
const searchText = "invoice";
(async () => {
try {
// Fetch and set access token
await fetchAndSetAccessToken(clientId, clientSecret, tenantId);
// Read emails
const emails = await readEmails(emailID, searchText);
console.log("Emails:", emails);
} catch (error) {
console.error("Error:", error.message);
}
})();
Cypress Integration
- Add the following commands in
cypress/support/commands.ts
:
import { fetchAndSetAccessToken, readEmails } from "msgraph-email-reader";
Cypress.Commands.add("setAccessToken", () => {
const clientId = Cypress.env("CLIENT_ID");
const clientSecret = Cypress.env("CLIENT_SECRET");
const tenantId = Cypress.env("TENANT_ID");
return fetchAndSetAccessToken(clientId, clientSecret, tenantId);
});
Cypress.Commands.add("readEmails", (emailID: string, searchText: string) => {
return readEmails(emailID, searchText);
});
- Use the custom commands in your test:
describe("Microsoft Graph API - Read Emails", () => {
before(() => {
cy.setAccessToken();
});
it("should fetch emails for a user", () => {
const emailID = "[email protected]";
const searchText = "invoice";
cy.readEmails(emailID, searchText).then((emails) => {
expect(emails).to.be.an("array").and.have.length.greaterThan(0);
cy.log("Fetched Emails:", emails);
});
});
});
API Reference
fetchAndSetAccessToken(clientId: string, clientSecret: string, tenantId: string): Promise<string>
Fetches and sets the access token. Must be called before fetching emails.
- Parameters:
clientId
: Azure AD application (client) ID.clientSecret
: Client secret for the Azure AD application.tenantId
: Azure AD tenant ID.
setAccessToken(token: string): void
Sets the access token for reuse.
- Parameters:
token
: Access token.
readEmails(emailID: string, searchText: string): Promise<any[]>
Fetches emails for a user based on search criteria.
- Parameters:
emailID
: Email ID of the user.searchText
: Search query for filtering emails.
Dependencies
axios
: HTTP client for making API requests.
Development
Scripts
Build: Compile TypeScript files to JavaScript.
npm run build
Test: Run tests using your preferred framework (e.g., Cypress).
License
This project is licensed under the ISC License.
Let me know if you need further adjustments!