npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-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

21

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

  1. 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.
  2. Node.js
    Ensure Node.js version 14.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

  1. 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);
});
  1. 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!