google-contacts
Advanced tools
Comparing version 0.1.2 to 0.1.5
344
index.js
@@ -20,25 +20,24 @@ /** | ||
var EventEmitter = require('events').EventEmitter, | ||
_ = require('underscore'), | ||
qs = require('querystring'), | ||
util = require('util'), | ||
url = require('url'), | ||
https = require('https'), | ||
debug = require('debug')('google-contacts'), | ||
querystring = require('querystring'); | ||
_ = require('lodash'), | ||
qs = require('querystring'), | ||
util = require('util'), | ||
url = require('url'), | ||
https = require('https'), | ||
debug = require('debug')('google-contacts'); | ||
var GoogleContacts = function (params) { | ||
if (typeof params === 'string') { | ||
params = { token: params } | ||
} | ||
if (!params) { | ||
params = {}; | ||
} | ||
if (typeof params === 'string') { | ||
params = {token: params} | ||
} | ||
if (!params) { | ||
params = {}; | ||
} | ||
this.contacts = []; | ||
this.consumerKey = params.consumerKey ? params.consumerKey : null; | ||
this.consumerSecret = params.consumerSecret ? params.consumerSecret : null; | ||
this.token = params.token ? params.token : null; | ||
this.refreshToken = params.refreshToken ? params.refreshToken : null; | ||
this.contacts = []; | ||
this.consumerKey = params.consumerKey ? params.consumerKey : null; | ||
this.consumerSecret = params.consumerSecret ? params.consumerSecret : null; | ||
this.token = params.token ? params.token : null; | ||
this.refreshToken = params.refreshToken ? params.refreshToken : null; | ||
this.params = _.extend({thin:true},params); | ||
this.params = _.defaults(params, {thin: true}); | ||
}; | ||
@@ -50,182 +49,203 @@ | ||
GoogleContacts.prototype._get = function (params, cb) { | ||
var self = this; | ||
if (typeof params === 'function') { | ||
cb = params; | ||
params = {}; | ||
} | ||
if (typeof params === 'function') { | ||
cb = params; | ||
params = {}; | ||
} | ||
var req = { | ||
host: 'www.google.com', | ||
port: 443, | ||
path: this._buildPath(params), | ||
method: 'GET', | ||
headers: { | ||
'Authorization': 'OAuth ' + this.token, | ||
'GData-Version': 3 | ||
} | ||
}; | ||
var req = { | ||
host: 'www.google.com', | ||
port: 443, | ||
path: this._buildPath(params), | ||
method: 'GET', | ||
headers: { | ||
'Authorization': 'OAuth ' + this.token | ||
} | ||
}; | ||
debug(req); | ||
debug(req); | ||
https.request(req, function (res) { | ||
var data = ''; | ||
https.request(req, function (res) { | ||
var data = ''; | ||
res.on('data', function (chunk) { | ||
debug('got ' + chunk.length + ' bytes'); | ||
data += chunk.toString('utf-8'); | ||
}); | ||
res.on('data', function (chunk) { | ||
debug('got ' + chunk.length + ' bytes'); | ||
data += chunk.toString('utf-8'); | ||
}); | ||
res.on('error', function (err) { | ||
cb(err); | ||
}); | ||
res.on('error', function (err) { | ||
cb(err); | ||
}); | ||
res.on('end', function () { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
var error = new Error('Bad client request status: ' + res.statusCode); | ||
return cb(error); | ||
} | ||
try { | ||
debug(data); | ||
cb(null, JSON.parse(data)); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
}); | ||
}) | ||
.on('error', cb) | ||
.end(); | ||
res.on('end', function () { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
var error = new Error('Bad client request status: ' + res.statusCode); | ||
return cb(error); | ||
} | ||
try { | ||
debug(data); | ||
cb(null, JSON.parse(data)); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
}); | ||
}) | ||
.on('error', cb) | ||
.end(); | ||
}; | ||
GoogleContacts.prototype.getContacts = function (cb, params) { | ||
var self = this; | ||
var self = this; | ||
this._get(_.extend({ type: 'contacts' },params,this.params), receivedContacts); | ||
function receivedContacts(err, data) { | ||
if (err) return cb(err); | ||
this._get(_.extend({type: 'contacts'}, params, this.params), receivedContacts); | ||
function receivedContacts(err, data) { | ||
if (err) return cb(err); | ||
if(!data.feed.entry) { | ||
return cb(null, []); | ||
} | ||
var feed = _.get(data, 'feed', []); | ||
var entry = _.get(data, 'feed.entry', []); | ||
if (!entry.length) { | ||
return cb(null, entry); | ||
} | ||
self._saveContactsFromFeed(data.feed); | ||
self._saveContactsFromFeed(feed); | ||
var next = false; | ||
data.feed.link.forEach(function (link) { | ||
if (link.rel === 'next') { | ||
next = true; | ||
var path = url.parse(link.href).path; | ||
self._get({ path: path }, receivedContacts); | ||
} | ||
}); | ||
if (!next) { | ||
cb(null, self.contacts); | ||
var next = false; | ||
_.each(feed.link, function (link) { | ||
if (link.rel === 'next') { | ||
next = true; | ||
var path = url.parse(link.href).path; | ||
self._get({path: path}, receivedContacts); | ||
} | ||
}); | ||
if (!next) { | ||
cb(null, self.contacts); | ||
} | ||
} | ||
}; | ||
}; | ||
GoogleContacts.prototype._saveContactsFromFeed = function (feed) { | ||
var self = this; | ||
feed.entry.forEach(function (entry) { | ||
var el; | ||
try { | ||
if(self.params.thin){ | ||
el = { | ||
name: entry.title['$t'], | ||
email: entry['gd$email'][0].address // only save first email | ||
}; | ||
}else{ | ||
el = entry; | ||
} | ||
self.contacts.push(el); | ||
GoogleContacts.prototype.getContact = function (cb, params) { | ||
var self = this; | ||
if(!_.has(params, 'id')){ | ||
return cb("No id found in params"); | ||
} | ||
catch (e) { | ||
// property not available... | ||
this._get(_.extend({type: 'contacts'}, this.params, params), receivedContact); | ||
function receivedContact(err, contact) { | ||
if (err) return cb(err); | ||
cb(null, contact); | ||
} | ||
}); | ||
} | ||
}; | ||
GoogleContacts.prototype._saveContactsFromFeed = function (feed) { | ||
var self = this; | ||
_.each(feed.entry, function (entry) { | ||
var el, url; | ||
if (self.params.thin) { | ||
url = _.get(entry, 'id.$t', ''); | ||
el = { | ||
name: _.get(entry, 'title.$t'), | ||
email: _.get(entry, 'gd$email.0.address'), // only save first email | ||
phoneNumber: _.get(entry, 'gd$phoneNumber.0.uri', '').replace('tel:', ''), | ||
id: url.substring(_.lastIndexOf(url, '/') + 1) | ||
}; | ||
} else { | ||
el = entry; | ||
} | ||
self.contacts.push(el); | ||
}); | ||
}; | ||
GoogleContacts.prototype._buildPath = function (params) { | ||
if (params.path) return params.path; | ||
if (params.path) return params.path; | ||
params = _.extend({},params,this.params); | ||
params.type = params.type || 'contacts'; | ||
params.alt = params.alt || 'json'; | ||
params.projection = params.projection || (params.thin?'thin':'full'); | ||
params.email = params.email || 'default'; | ||
params['max-results'] = params['max-results'] || 2000; | ||
params = _.extend({}, params, this.params); | ||
params.type = params.type || 'contacts'; | ||
params.alt = params.alt || 'json'; | ||
params.projection = params.projection || (params.thin ? 'thin' : 'full'); | ||
params.email = params.email || 'default'; | ||
params['max-results'] = params['max-results'] || 10000; | ||
var query = { | ||
alt: params.alt, | ||
'max-results': params['max-results'] | ||
}; | ||
if(params['updated-min']) | ||
query['updated-min'] = params['updated-min']; | ||
var query = { | ||
alt: params.alt | ||
}; | ||
var path = '/m8/feeds/'; | ||
path += params.type + '/'; | ||
path += params.email + '/'; | ||
path += params.projection; | ||
path += '?' + qs.stringify(query); | ||
if(!params.id) query['max-results'] = params['max-results']; | ||
return path; | ||
if (params['updated-min']) | ||
query['updated-min'] = params['updated-min']; | ||
if (params.q || params.query) | ||
query.q = params.q || params.query; | ||
var path = '/m8/feeds/'; | ||
path += params.type + '/'; | ||
path += params.email + '/'; | ||
path += params.projection; | ||
if(params.id) path += '/'+ params.id; | ||
path += '?' + qs.stringify(query); | ||
return path; | ||
}; | ||
GoogleContacts.prototype.refreshAccessToken = function (refreshToken, cb) { | ||
if (typeof params === 'function') { | ||
cb = params; | ||
params = {}; | ||
} | ||
GoogleContacts.prototype.refreshAccessToken = function (refreshToken, params, cb) { | ||
if (typeof params === 'function') { | ||
cb = params; | ||
params = {}; | ||
} | ||
var data = { | ||
refresh_token: refreshToken, | ||
client_id: this.consumerKey, | ||
client_secret: this.consumerSecret, | ||
grant_type: 'refresh_token' | ||
var data = { | ||
refresh_token: refreshToken, | ||
client_id: this.consumerKey, | ||
client_secret: this.consumerSecret, | ||
grant_type: 'refresh_token' | ||
} | ||
}; | ||
var body = qs.stringify(data); | ||
var body = qs.stringify(data); | ||
var opts = { | ||
host: 'accounts.google.com', | ||
port: 443, | ||
path: '/o/oauth2/token', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': body.length | ||
} | ||
}; | ||
var opts = { | ||
host: 'accounts.google.com', | ||
port: 443, | ||
path: '/o/oauth2/token', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': body.length | ||
} | ||
}; | ||
var req = https.request(opts, function (res) { | ||
var data = ''; | ||
res.on('end', function () { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
var error = new Error('Bad client request status: ' + res.statusCode); | ||
return cb(error); | ||
} | ||
try { | ||
data = JSON.parse(data); | ||
cb(null, data.access_token); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
}); | ||
var req = https.request(opts, function (res) { | ||
var data = ''; | ||
res.on('end', function () { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
var error = new Error('Bad client request status: ' + res.statusCode); | ||
return cb(error); | ||
} | ||
try { | ||
data = JSON.parse(data); | ||
cb(null, data.access_token); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
}); | ||
res.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
res.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
res.on('error', cb); | ||
res.on('error', cb); | ||
}).on('error', cb); | ||
}).on('error', cb); | ||
req.write(body); | ||
req.end(); | ||
} | ||
req.write(body); | ||
req.end(); | ||
}; | ||
exports.GoogleContacts = GoogleContacts; |
{ | ||
"name": "google-contacts", | ||
"version": "0.1.2", | ||
"version": "0.1.5", | ||
"description": "API wrapper for Google Contacts", | ||
@@ -23,9 +23,6 @@ "main": "index.js", | ||
"debug": "^2.2.0", | ||
"underscore": "" | ||
"lodash": "^4.5.1" | ||
}, | ||
"author": "Ajnasz <ajnasz@ajnasz.hu>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"inireader": "^0.3.3" | ||
} | ||
"license": "MIT" | ||
} |
@@ -6,5 +6,7 @@ /*jslint indent:2*/ | ||
var contactsTested = false; | ||
var contactTested = false; | ||
var c = new GoogleContacts({ | ||
token: process.env.GOOGLE_TOKEN, | ||
id: process.env.GOOGLE_CONTACT_ID | ||
}); | ||
@@ -19,6 +21,13 @@ | ||
c.getContact(function (err, contact) { | ||
if (err) throw err; | ||
assert.ok(typeof contact === 'object', 'Contact is not an object'); | ||
console.log(contact); | ||
contactTested = true; | ||
}); | ||
process.on('exit', function () { | ||
if (!contactsTested) { | ||
if (!contactsTested || !contactTested) { | ||
throw new Error('contact test failed'); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
10196
0
233
1
2
+ Addedlodash@^4.5.1
+ Addedlodash@4.17.21(transitive)
- Removedunderscore@
- Removedunderscore@1.13.7(transitive)