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

parweb-react-gmail

v1.0.7

Published

Integration GMail API into your React Application

Downloads

3

Readme

react-gmail

React library for using gmail api : https://developers.google.com/gmail/api

install

You can use npm npm install --save react-gmail or yarn yarn add react-gmail

use

  • Repeat Step 1 from this link: https://developers.google.com/gmail/api/quickstart/js
  • You also need to create file gmail.config.json in root of your application with your config:
{
  "clientId": "<CLIENT_ID>",
  "apiKey": "<API_KEY>",
  "scope": "https://www.googleapis.com/auth/gmail.readonly",
  "discoveryDocs": ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"]
}
  • Import instance in your application import gmailApi from 'react-gmail'

For get more info about API please check documentation: https://developers.google.com/gmail/api/v1/reference

methods

getProfile

For getting profile info:

 /**
   * @param {string} userId
   * @returns {Promise} Object: { emailAddress, messagesTotal, threadsTotal , historyId }
   */
  getProfile(userId = "me").then(...)
getMessageIds

For getting message ids:

 /**
   * @param {boolean} [unread=false]
   * @param {number} [maxResults=10]
   * @param {string} [userId="me"]
   * @returns {Promise} Array: [ {id, threadId} ]
   */
  getMessageIds(unread = false, maxResults = 10, userId = "me").then(...)
getMessagesByIds

For getting message data by array of ids or by string id:

 /**
   * @param {[string] | string} id
   * @param {string} [userId="me"]
   * @returns {Promise} [{id, labelId, snippet, internalDate, payload}] | {...}
   */
  getMessagesFromIds(ids, userId = "me").then(...)
getMessages

For getting message data:

 /**
   * @param {boolean} [unread=false]
   * @param {number} [maxResults=10]
   * @param {string} [userId="me"]
   * @returns {Promise} [{id, labelIds, snippet, internalDate, payload}] | {...}
   */
  getMessages(unread = false, maxResults = 10, userId = "me").then(...)
getThreadsList

For getting threads list data (returned array of {id, snippet, historyId}):

/**
 * Get list of snippets from the last threads
 * @param {string} userId
 * @returns {Promise} [{id, snippet, historyId}]
 */
getThreadsList(userId = "me").then(...);
getThreads

For getting threads:

/**
 * @param {string | array} id
 * @param {Promise} userId
 */
getThreads(id, userId = "me").then(...);
listenSign

For listening your sign status

/**
 * Method for update your sign if it was changed
 * @param {*} callback function for updating sign status
 */
listenSign(callback);
getArrayOfIds

For converting object to array of ids (using for prepare array of ids for getMessages method):

/**
 * @param {object} data getMessageIds response
 */
getArrayOfIds(data);
normalizeData

For normalizing data (using with response of getMessages method):

/**
 * @param {array | object} data getMessages response
 * @returns {array | object}
 */
normalizeData(data);
getBody

For getting body from getMessages response (also decoding from base64):

/**
 * @param {array | object} data getMessages response
 * @returns {object} text, html
 */
getBody(data);
getMetaFromHeaders

For getting important info ("From", "Date", "Subject" headers object) from getMessage response:

/**
 * @param {object} data getMessageIds headers response
 */
getMetaFromHeaders(data);

examples

getMessages
import React from "react";
import gmailApi from "react-gmail";

class SomeComponent extends React.Component {
  state = {
    messages: []
  };

  getMessages = () => {
    gmailApi.getMessages(true, 5).then(res => {
      this.setState({ messages: gmailApi.normalizeData(res) });
    });
  };

  // Another way to get messages by ids
  // getMessages = () => {
  //   gmailApi.getMessageIds(false, 5).then(resIds => {
  //     gmailApi.getMessages(gmailApi.getArrayOfIds(resIds)).then(res => {
  //       this.setState({ messages: gmailApi.normalizeData(res) });
  //     });
  //   });
  // }

  render() {
    const { messages } = this.state;
    return (
      <div>
        <button onCLick={this.getMessages}>Get Messages</button>
        <ul>
          {messages.map(message => (
            <li key="message.id">
              <div>
                <span>
                  {message.subject}: {message.snippet}
                </span>
                <p>{message.date}</p>
              </div>
            </li>
          ))}
        </ul>
      </div>
    );
  }
}
getThreads

If you want to get just snippets from messages, the better way it's using getThreadsList method:

import React from "react";
import gmailApi from "react-gmail";

class SomeComponent extends React.Component {
  state = {
    messages: []
  };

  getMessages = () => {
    gmailApi.getThreadsList().then(res => {
      this.setState({ messages: res.result.threads });
    });
  };

  render() {
    const { messages } = this.state;
    return (
      <div>
        <button onCLick={this.getMessages}>Get Snippets from messages</button>
        <ul>
          {messages.map(message => (
            <li key="message.id">{message.snippet}</li>
          ))}
        </ul>
      </div>
    );
  }
}
getProfile
gmailApi.getProfile().then(resProfile => {
  this.setState({ profile: resProfile.result });
});
listenSign
import React from "react";
import gmailApi from "react-gmail";

class SomeComponent extends React.Component {
  state = {
    sign: gmailApi.sign
  };

  componentDidMount() {
    gmailApi.listenSign(this.signUpdate);
  }

  signUpdate = sign => {
    this.setState({ sign });
  };

  render() {
    return (
      <div>
        <p> Sign status: {this.state.sign} </p>
      </div>
    );
  }
}

customize

For customizing signIn & signOut you can use handleSignIn and handleSignOut methods:

import React from "react";
import gmailApi from "react-gmail";

class SomeComponent extends React.Component {
  state = {
    sign: gmailApi.sign
  };

  componentDidMount() {
    gmailApi.listenSign(this.signUpdate);
  }

  signUpdate = sign => {
    this.setState({ sign });
  };

  handleSignIn = () => {
    gmailApi.handleSignIn().then(() => {
      console.log("handleSignIn");
    });
  };

  handleSignOut = () => {
    gmailApi.handleSignOut().then(() => {
      console.log("handleSignOut");
    });
  };

  render() {
    return (
      <div>
        <button onCLick={this.handleSignIn}>SignIn Google</button>
        <button onCLick={this.handleSignOut}>SignOut Google</button>
        <p> Sign status: {this.state.sign} </p>
      </div>
    );
  }
}