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

js-gmail-api

v1.0.1

Published

Javascript functions to make easy the integration with the gmail api from google.

Downloads

61

Readme

js-gmail-api

Javascript functions to make easy the integration with the gmail api from google.

Table of Contents

Installing

Using npm:

$ npm install js-gmail-api

Using yarn:

$ yarn add js-gmail-api

Import

Once the package is installed, you can import the library using import or require approach:

import jsGmailAPI from "js-gmail-api";

OR:

const jsGmailAPI = require("js-gmail-api");

Functions

Note: Every functions takes gmail as the first parameter.

import { google } from "googleapis";

const auth = await authenticate({
  scopes: SCOPES,
  keyfilePath: CREDENTIALS_PATH,
});

async function main(auth) {
  const gmail = google.gmail({ version: "v1", auth });
  /** ... */
}

main(auth);

getAllEmails returns an array of all emails in your inbox, in an object containing the id of each email.

JavaScript

import { getAllEmails } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails = await getAllEmails(gmail);
  emails.forEach((email) => console.log(email.id));
}

TypeScript

import { getAllEmails } from "js-gmail-api";
import type { Messages } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails: Messages = await getAllEmails(gmail);
  emails.forEach((email) => console.log(email.id));
}

getUnreadEmails has the same return as getAllEmails but containing only unread emails.

JavaScript

import { getUnreadEmails } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails = await getUnreadEmails(gmail);
  emails.forEach((email) => console.log(email.id));
}

TypeScript

import { getUnreadEmails } from "js-gmail-api";
import type { Messages, Message } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails: Messages = await getUnreadEmails(gmail);
  emails.forEach((email: Message) => console.log(email.id));
}

getMessageById returns an array of object containing .

JavaScript

import { getAllEmails } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails = await getAllEmails(gmail);
  emails.forEach((email) => console.log(email.id));
}

TypeScript

import { getAllEmails } from "js-gmail-api";
import type { Messages } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails: Messages = await getAllEmails(gmail);
  emails.forEach((email) => console.log(email.id));
}

getMessageById returns an array of object containing .

JavaScript

import { getAllEmails } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails = await getAllEmails(gmail);
  emails.forEach(async (email) => {
    const message = await getFormattedMessageById(gmail, email.id);
    console.log(message);
  });
}

TypeScript

import { getAllEmails, getFormattedMessageById } from "js-gmail-api";
import type { Messages, FormattedMessage } from "js-gmail-api";

async function main(auth) {
  /** ... */

  const emails: Messages = await getAllEmails(gmail);
  emails.forEach(async (email) => {
    const message: FormattedMessage = await getFormattedMessageById(
      gmail,
      email.id
    );
    console.log(message);
  });
}

Example

If you are not familiarized with the gmail api, see the gmail api quickstart to get the initialization and authentication snippet.

import {
  getUserInfo,
  getFormattedMessageById,
  getUnreadEmails,
  markAsRead,
} from "js-gmail-api";
import { google } from "googleapis";

const auth = await authenticate({
  scopes: SCOPES,
  keyfilePath: CREDENTIALS_PATH,
});

const gmail = google.gmail({ version: "v1", auth }); // returns an object

const user = await getUserInfo(gmail); // { emailAddress,  messagesTotal }
console.log(">_ You are logged as", user.emailAddress);

const unreadEmails = await getUnreadEmails(gmail); // [{ id }]

// Check if there is any unread email
if (unreadEmails && unreadEmails.length > 0) {
  console.log(">_ There is no unread emails in your inbox.");
} else {
  console.log(`>_ There is ${unreadEmails.length} unread emails in your inbox`);
}

// Print all unread emails and mark as read
unreadEmails.forEach((email) => {
  getFormattedMessageById(gmail, email.id)
    .then((res) => res.data)
    .then((message) => {
      console.log(message); // message: { from, body }
      markAsRead(gmail, email.id);
    })
    .catch(console.error);
});