gmail-tester
Version:
A simple NodeJS gmail client which checks the inbox for specific message existence
46 lines (41 loc) • 1.2 kB
JavaScript
const fs = require('fs');
const path = require('path');
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = "token.json";
/**
* Reads the token from the specified path or from default path.
* @param {String | Object | null} token_path Path to the token file or token object
* @returns {Object} token object
*/
function get(token_path) {
if (typeof token_path === 'object' && token_path !== null) {
return token_path;
}
try {
return JSON.parse(fs.readFileSync(
token_path || path.resolve(__dirname, TOKEN_PATH)
).toString());
} catch (error) {
throw new Error("No token found.");
}
}
/**
* Stores the token in the specified path or in default path.
* @param {Object} token Token
* @param {String | Object | null} token_path Path or token object (if object, no-op)
*/
function store(token, token_path) {
if (typeof token_path === 'object' && token_path !== null) {
return;
}
fs.writeFileSync(
token_path || path.resolve(__dirname, TOKEN_PATH),
JSON.stringify(token)
);
}
module.exports = {
get,
store
};