Skip to content

Instantly share code, notes, and snippets.

@kevinohara80
Last active January 2, 2016 14:48
Show Gist options
  • Save kevinohara80/8318819 to your computer and use it in GitHub Desktop.
Save kevinohara80/8318819 to your computer and use it in GitHub Desktop.
nforce plugin system example
// example nforce plugin
module.exports = function(nforce) {
// throws if the plugin already exists
var plugin = nforce.plugin('myplugin');
// simple example
plugin.fn('foo', function() {
return 'bar'
});
// using a callback
plugin.fn('getApiVersion', function(cb) {
if(!this.apiVersion) {
return cb(new Error('No API version specified'));
} else {
cb(null, 'The current ApiVersion is ' + this.apiVersion);
}
});
// using exposed util functions
plugin.fn('doValidateOAuth', function(oauth) {
return plugin.util.validateOAuth(oauth);
});
}
var nforce = require('nforce');
var sfuser = process.env.SFUSER;
var sfpass = process.env.SFPASS;
var clientId = process.env.CLIENT_ID;
var clientSecret = process.env.CLIENT_SECRET;
// load the plugin
require('myplugin')(nforce);
var org = nforce.createConnection({
clientId: clientId,
clientSecret: clientSecret,
redirectUri: 'http://localhost:3000/oauth/_callback',
plugins: ['myplugin'] // make sure you enable it when creating a connection
});
console.log(org.myplugin.foo()); // => 'bar'
org.myplugin.getApiVersion(function(err, msg) {
if(err) throw err;
console.log(msg); // => current api version
});
console.log(org.myplugin.doValidateOAuth({ invalid: 'oauth' })); // => 'false'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment