UNPKG

graph-api

Version:

Facebook Graph API SDK for NodeJS

232 lines (195 loc) 6.71 kB
/* dependencies */ // url.format for formating url's scheme const scheme = require('url') .format; // url.parse for getting query value const urlParse = require('url') .parse; //https for doing requests const https = require('https'); /* main */ class api { /* main options */ constructor(version) { try { if (typeof version === "string") { switch (version) { case "3.0": break; default: version = "3.0"; break; }; } else { throw new Error("Version must be a string!"); }; } catch (err) { console.log(err); process.exit(); } // returning values finally { this.version = version; }; }; /* user's options class */ options(platform, callback_uri, secret, identifier) { try { switch (platform) { case "self": if (typeof identifier === "undefined") { var identifier = "me"; }; if (typeof secret === "undefined") { throw new Error("To use platform 'self' you should specify an access token!"); }; break; case "app": if (typeof identifier === "undefined") { throw new Error("To use platform 'app' you should specify an appID!"); }; if (typeof secret === "undefined") { throw new Error("To use platform 'app' you should specify an app secret!"); }; break; default: throw new Error("Can't identify a valid platform!"); }; } catch (err) { console.log(err); process.exit(); } finally { this.platform = platform; this.identifier = identifier; this.secret = secret; this.callback_uri = callback_uri; }; }; /* class for setting an url callback */ get(query, callback, token) { if (this.platform === "app") { // setting url scheme for the request var url = scheme({ protocol: "https", hostname: "graph.facebook.com", pathname: "v" + this.version + "/" + "me", query: { fields: query.toString(), access_token: token, redirect_uri: this.callback_uri } }); } else { var url = scheme({ protocol: "https", hostname: "graph.facebook.com", pathname: "v" + this.version + "/" + this.identifier, query: { fields: query.toString(), access_token: this.secret, redirect_uri: this.callback_uri } }); } // requesting data https.request(url) .on('response', function(response) { // creating a var to store chunk data var data = ''; response.on("data", function(chunk) { // concatenating chunked data data += chunk; }); response.on('end', function() { // passing data to the callback callback(JSON.parse(data)); }); }) .end(); }; /* class to generate a dialog url */ dialog(scope, permissions) { // misusage filter try { // make sure we are running on app mode if (this.platform === "app") { // setting url scheme var url = scheme({ protocol: "https", hostname: "graph.facebook.com", pathname: "oauth/authorize", query: { client_id: this.identifier, redirect_uri: this.callback_uri, scope: scope, perms: permissions } }); } else { // throwing exceptions throw new Error("You can't use this function in self mode!"); }; } // handling errors catch (err) { console.log(err); process.exit(); } // returning values finally { return url; }; }; /* class to exchange the code for an access token */ token(url, callback) { // misusage filter try { // make sure we are running on app mode if (this.platform === "app") { // getting core from query let query = urlParse(url, true).query.code; // setting url scheme var url = scheme({ protocol: "https", hostname: "graph.facebook.com", pathname: "oauth/access_token", query: { client_id: this.identifier, client_secret: this.secret, redirect_uri: this.callback_uri, code: query } }); // requesting data https.request(url) .on('response', function(response) { // creating a var to store chunk data var data = ''; response.on("data", function(chunk) { // concatenating chunked data data += chunk; }); response.on('end', function() { // passing data to the callback callback(JSON.parse(data)); }); }) .end(); } else { // throwing exceptions throw new Error("You can't use this function in self mode!"); }; } // handling errors catch (err) { console.log(err); process.exit(); } }; // the bracket bellow is the end of main class, please don't delete it! }; /* export the api */ module.exports = new api();