node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



This class creates a very easy and intuitive way to interact with APIs, most commonly those hosted on other domains.


var twitter = new APIConnect();

The route statuses/home_timeline will be automatically set up and accessible through a camelized method name in the api object. Calling the route is as simple as calling the method.

When making the call, the first object is params. This will most commonly be added to the end of the query string, but in cases where the route contains params such as tweets/:tweet_id, it will be placed here instead and removed from the query string.

The second object passed is an options hash. This will override any default params and also end up being passed to the AJAX lib, in this case jQuery. Any valid option for jQuery.ajax is allowed here. This means that all the standard callbacks will work:

twitter.getHomeTimeline({}, {
  success: function() {
    // Congratulations!
  error: function() {
    // Oh noeee... failure!

API calls will also pass back jQuery deferred objects, so these can be used as well:

twitter.getHomeTimeline().then(function() {
  // You're done!

Any route can be set up with any level of context. These contexts are always optional, and will only be added if they exist when passed as params:

twitter.getMembers({ user: 'bob', list_id: 5 });

To connect routes, the 4 main HTTP verbs, "GET", "POST", "PUT", and "DELETE" are supported, and map to the method names "get", "create", "update", and "destroy", respectively (note "del", which is a reserved keyword):':user/:list_id/members');
twitter.createMember({ user: 'bob', list_id: 5, member_name: 'harry' });
twitter.destroyMember({ user: 'bob', list_id: 5 });

"resource" serves as a shortcut to all 4 HTTP verbs:

twitter.getMember();     > GET
twitter.createMember();  > POST
twitter.updateMember();  > PUT
twitter.destroyMember(); > DELETE

If a resource is a collection, passing collection: true in the options for "resource" will create standard collection routes including an "index" method:

twitter.resource('status', { collection: true });
twitter.getStatus();              > GET
twitter.getStatus({ id: 3});      > GET
twitter.createStatus();           > POST
twitter.updateStatus({ id: 3 });  > PUT
twitter.destroyStatus({ id: 3 }); > DELETE

If a "collection" is omitted, it will attempt to be intelligently detected by the pluralization of the resource passed. In this case, pluralization of the methods will also use intelligent detection:

twitter.getMembers();              > GET
twitter.getMembers({ id: 3});      > GET
twitter.createMembers();           > POST
twitter.updateMembers({ id: 3 });  > PUT
twitter.destroyMembers({ id: 3 }); > DELETE

... more docs to come!