Here’s a README.md
file for your msgraph-email-reader
package, explaining how to use it:
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.
- 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.
Install the package via npm:
npm install msgraph-email-reader
-
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.
Import the library into your Node.js or Cypress project:
import {
fetchAndSetAccessToken,
setAccessToken,
readEmails,
} from "msgraph-email-reader";
Retrieve and set the access token using your Azure AD credentials:
await fetchAndSetAccessToken(clientId, clientSecret, tenantId);
Fetch emails for a specific user based on search criteria:
const emails = await readEmails(emailID, searchText);
console.log("Fetched Emails:", emails);
import { fetchAndSetAccessToken, readEmails } from "msgraph-email-reader";
const clientId = "your-client-id";
const clientSecret = "your-client-secret";
const tenantId = "your-tenant-id";
const emailID = "user@example.com";
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);
}
})();
- 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 = "user@example.com";
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);
});
});
});
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.
-
Sets the access token for reuse.
-
Parameters:
-
token
: Access token.
-
Fetches emails for a user based on search criteria.
-
Parameters:
-
emailID
: Email ID of the user. -
searchText
: Search query for filtering emails.
-
-
axios
: HTTP client for making API requests.
-
Build: Compile TypeScript files to JavaScript.
npm run build
-
Test: Run tests using your preferred framework (e.g., Cypress).
This project is licensed under the ISC License.
Let me know if you need further adjustments!