Node API abstraction
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md
common.js
package.json

README.md

common-api

Node API abstraction library.

Usage

Without API Key

var MyAPI = function() {
    // api specific setup
};

MyAPI.prototype = new API({
    hostname: 'api.domain.com',
    format:   API.FORMAT.JSON,
    // prefix for all api reqeusts
    base:     '/v1'
});

MyAPI.prototype.getWidgets = function() {
    // this.call returns a promise.
    //
    // The constructed URL for this call would be:
    //     http://api.domain.com/v1/widgets
    //
    return this.call('widgets').then(function(widgets) {
        // do some transformations on the api response

        return widgets;
    };
};

With API Key in URL

var MyAPI = function(api_key) {
    // setup
    this.api_key = api_key;
};

MyAPI.prototype = new API({
    hostname: 'api.domain.com',
    // use https on port 443
    secure:   true,
    format:   API.FORMAT.JSON,
    base:     '/json/{api_key}',

    // urlTransform is called after building the request URL.
    urlTransform: function(url) { return url.replace('{api_key}', this.api_key); }
});