Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -60,15 +60,20 @@ function Graph(method, url, postData, callback) {
postData = {};
}

let httpMethod = method;

url = this.prepareUrl(url);
this.callback = callback || noop;
this.postData = postData;

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;

Expand Down Expand Up @@ -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);
});

};


Expand Down Expand Up @@ -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
Expand Down