Best way to organize several modules into one


#1

I am trying to integrate social media APIs into my application as seperate modules. Each module will be implemented on its own, but I want another module that pulls in each of the social media integrations. I want each module to implement an interface like this

Social = (function(){
  var expose = {};

  expose.FACEBOOK = 'facebook';
  expose.YOUTUBE  = 'youtube';
  expose.TWITTER  = 'twitter';

  expose.like = function(type, data) {

  };

  expose.unlike = function(type, data) {

  };

  expose.post = function(type, data) {

  };

  expose.remove = function(type, data) {

  };

  expose.reply = function(type, data) {

  };

  return expose;
})();

I then want to be able to call something like

Social.post(Social.YOUTUBE, data) or Social.like(Social.FACEBOOK, data)
To which the correct integration is invoked. What is the best way to accomplish this, and can it be achieved without using a lot of switch statements?


#2

Let’s say you have an API wrapper for facebook: API.facebook.like(data)

If you have the wrappers for Twitter namespaced similarly: API.twitter.like(data)
Then you can start to programmatically call these functions:

Social.like = function(type, data) {
  API[type].like(data);
}
// Social.like(Social.FACEBOOK, data)
// Social.like(Social.TWITTER, data)