gmail-inbox
Advanced tools
Comparing version 1.0.4 to 1.0.6
import { OAuth2Client } from 'google-auth-library'; | ||
export declare const authorizeAccount: (credentialsJsonPath: string, tokenPath: string) => OAuth2Client; | ||
export declare const authorizeAccount: (credentialsJsonPath: string, tokenPath: string) => Promise<OAuth2Client>; |
@@ -51,16 +51,24 @@ "use strict"; | ||
var scopes = ['https://www.googleapis.com/auth/gmail.readonly']; | ||
exports.authorizeAccount = function (credentialsJsonPath, tokenPath) { | ||
var credentials = getCredentials(credentialsJsonPath); | ||
var auth = new googleapis_1.google.auth.OAuth2({ | ||
// more info on the interface "OAuth2ClientOptions" in 'googleapis' package | ||
clientId: credentials.client_id, | ||
clientSecret: credentials.client_secret, | ||
redirectUri: credentials.redirect_uris[0], | ||
exports.authorizeAccount = function (credentialsJsonPath, tokenPath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var credentials, auth, token; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
credentials = getCredentials(credentialsJsonPath); | ||
auth = new googleapis_1.google.auth.OAuth2({ | ||
// more info on the interface "OAuth2ClientOptions" in 'googleapis' package | ||
clientId: credentials.client_id, | ||
clientSecret: credentials.client_secret, | ||
redirectUri: credentials.redirect_uris[0], | ||
}); | ||
return [4 /*yield*/, getToken(auth, tokenPath)]; | ||
case 1: | ||
token = _a.sent(); | ||
if (token) { | ||
auth.setCredentials(token); | ||
} | ||
return [2 /*return*/, auth]; | ||
} | ||
}); | ||
var token = getToken(auth, tokenPath); | ||
if (token) { | ||
auth.setCredentials(token); | ||
} | ||
return auth; | ||
}; | ||
}); }; | ||
var getCredentials = function (credentialsJsonPath) { | ||
@@ -89,12 +97,20 @@ var allCredentials; | ||
}; | ||
var getToken = function (oAuth2Client, tokenPath) { | ||
try { | ||
var credentialsString = fs_1.readFileSync(tokenPath, { encoding: 'utf8' }); | ||
return JSON.parse(credentialsString); | ||
} | ||
catch (e) { | ||
// means we got no valid token to use, so we request a new one | ||
return getNewToken(oAuth2Client, tokenPath); | ||
} | ||
}; | ||
var getToken = function (oAuth2Client, tokenPath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var credentialsString, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 1, , 3]); | ||
credentialsString = fs_1.readFileSync(tokenPath, { encoding: 'utf8' }); | ||
return [2 /*return*/, JSON.parse(credentialsString)]; | ||
case 1: | ||
e_1 = _a.sent(); | ||
return [4 /*yield*/, getNewToken(oAuth2Client, tokenPath)]; | ||
case 2: | ||
// means we got no valid token to use, so we request a new one | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var getNewToken = function (oAuth2Client, tokenPath) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -101,0 +117,0 @@ var authUrl, readLine; |
@@ -23,3 +23,5 @@ import { Label } from './Label.interface'; | ||
private gmailApi; | ||
private authenticated; | ||
constructor(credentialsJsonPath: string, tokenPath?: string); | ||
authenticateAccount(): Promise<void>; | ||
getAllLabels(): Promise<Label[]>; | ||
@@ -44,6 +46,6 @@ /** | ||
findMessages(searchQuery: SearchQuery | string | undefined): Promise<Message[]>; | ||
private authenticateAccount; | ||
private getMessageById; | ||
private guardAuthentication; | ||
private arrayToAdvancedSearchString; | ||
private mapSearchQueryToSearchString; | ||
} |
@@ -52,4 +52,20 @@ "use strict"; | ||
this.tokenPath = tokenPath; | ||
this.gmailApi = this.authenticateAccount(credentialsJsonPath, tokenPath); | ||
this.gmailApi = googleapis_1.google.gmail('v1'); | ||
this.authenticated = false; | ||
} | ||
Inbox.prototype.authenticateAccount = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var oAuthClient; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, GoogleAuthorizer_1.authorizeAccount(this.credentialsJsonPath, this.tokenPath)]; | ||
case 1: | ||
oAuthClient = _a.sent(); | ||
this.gmailApi = googleapis_1.google.gmail({ version: 'v1', auth: oAuthClient }); | ||
this.authenticated = true; | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Inbox.prototype.getAllLabels = function () { | ||
@@ -59,2 +75,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
this.guardAuthentication(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
@@ -84,7 +101,10 @@ _this.gmailApi.users.labels.list({ | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
this.guardAuthentication(); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.findMessages({ | ||
labels: ['inbox'], | ||
})]; | ||
case 1: | ||
case 2: | ||
messages = _a.sent(); | ||
@@ -97,8 +117,8 @@ if (messages && messages !== undefined) { | ||
} | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
console.log('gmail-inbox error:', e_1); | ||
return [2 /*return*/, []]; | ||
case 3: return [2 /*return*/]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -123,2 +143,3 @@ }); | ||
var _this = this; | ||
this.guardAuthentication(); | ||
return new Promise(function (resolve, reject) { | ||
@@ -171,6 +192,2 @@ var searchString; | ||
}; | ||
Inbox.prototype.authenticateAccount = function (credentialsJsonPath, tokenPath) { | ||
var oAuthClient = GoogleAuthorizer_1.authorizeAccount(credentialsJsonPath, tokenPath); | ||
return googleapis_1.google.gmail({ version: 'v1', auth: oAuthClient }); | ||
}; | ||
Inbox.prototype.getMessageById = function (messageId) { | ||
@@ -197,2 +214,7 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
Inbox.prototype.guardAuthentication = function () { | ||
if (!this.authenticated) { | ||
throw new Error('Please authenticate with Inbox.authenticate() before performing any action'); | ||
} | ||
}; | ||
Inbox.prototype.arrayToAdvancedSearchString = function (itemOrItems) { | ||
@@ -199,0 +221,0 @@ if (typeof itemOrItems === 'string') { |
{ | ||
"name": "gmail-inbox", | ||
"version": "1.0.4", | ||
"version": "1.0.6", | ||
"description": "Receive Gmail emails in code!", | ||
@@ -30,3 +30,3 @@ "main": "lib/Inbox.js", | ||
"@types/jest": "^24.0.25", | ||
"@types/node": "^13.1.1", | ||
"@types/node": "^13.1.2", | ||
"jest": "^24.9.0", | ||
@@ -33,0 +33,0 @@ "prettier": "^1.19.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34094
703