diff --git a/lib/graph.js b/lib/graph.js index 835136b..4eb13c9 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -32,7 +32,7 @@ function extend(target) { var accessToken = null , appSecret = null , graphUrl = 'https://graph.facebook.com' - , graphVersion = '2.9' // default to the oldest version + , graphVersion = '2.10' // default to the oldest version , oauthDialogUrl = "https://www.facebook.com/v2.0/dialog/oauth?" // oldest version for auth , oauthDialogUrlMobile = "https://m.facebook.com/v2.0/dialog/oauth?" // oldest version for auth , requestOptions = {}; @@ -60,6 +60,8 @@ function Graph(method, url, postData, callback) { postData = {}; } + let httpMethod = method; + url = this.prepareUrl(url); this.callback = callback || noop; this.postData = postData; @@ -67,8 +69,11 @@ function Graph(method, url, postData, callback) { this.options = extend({}, requestOptions); this.options.encoding = this.options.encoding || 'utf-8'; + if(httpMethod==="MULTIPART") + httpMethod = "POST"; + // these particular set of options should be immutable - this.options.method = method; + this.options.method = httpMethod; this.options.uri = url; this.options.followRedirect = false; @@ -196,12 +201,13 @@ Graph.prototype.get = function () { } self.end(body); + }).on('error', function(err) { self.callback({ message: 'Error processing https request' , exception: err }, null); - }); + }; @@ -237,6 +243,62 @@ Graph.prototype.post = function() { }; + +/** + * https.multipart request wrapper + */ + +Graph.prototype.multipart = function() { + + var self = this + , postData = this.postData; + + this.options.formData = postData; + + return request(this.options, function (err, res, body) { + if (err) { + self.callback({ + message: 'Error processing https request' + , exception: err + }, null); + + return; + } + + self.end(body); + }); + +}; + + +/** + * Publish to the facebook graph + * access token will be needed for posts + * Ex: + * + * var wallPost = { message: "heyooo budday" }; + * graph.multipart(friendID + "/feed", wallPost, callback); + * + * @param {string} url + * @param {object} postData + * @param {function} callback + */ + +exports.multipart = function (url, postData, callback) { + + if (typeof url !== 'string') { + return callback({ message: 'Graph api url must be a string' }, null); + } + + if (typeof postData === 'function') { + callback = postData; + postData = url.indexOf('access_token') !== -1 ? {} : {access_token: accessToken}; + } + + + return new Graph('MULTIPART', url, postData, callback); +}; + /** * Accepts an url an returns facebook * json data to the callback provided