lib/twitch.core.js
function api(options, callback) {
  if (!session) {
    throw new Error('You must call init() before api()');
  }

  var version = options.version || 'v3';

  var params = options.params || {};
  callback = callback || function() {};

  var authenticated = !!session.token,
    url = REDIRECT_PATH + (options.url || options.method || '');

  if (authenticated) params.oauth_token = session.token;


  var request_options = {
    host: REDIRECT_HOST,
    path: url + '?' + util.param(params),
    method: options.verb || 'GET',
    headers: {
      "Accept": "application/vnd.twitchtv." + version + "+json",
      "Client-ID": clientId
    }
  };

  var req = https.request(request_options, function(res) {
    log('Response status:', res.statusCode, res.statusMessage);
    res.setEncoding('utf8');

    var responseBody = "";
    res.on('data', function(data) {
      responseBody += data;
    });

    res.on('end', function() {
      var data = JSON.parse(responseBody);

      if (res.statusCode >= 200 && res.statusCode < 300) { // Status 2xx means success
        log('Response Data:', data);
        callback(null, data || null);
      }
      else {
        if (authenticated && res.statusCode === HTTP_CODES.unauthorized) {
          auth.logout(function() {
            callback(data, null);
          });
        }
        else {
          callback(data, null);
        }
      }
    });
  });

  req.on('error', function (e) {
    log('API Error:', e);
    callback(e, null);
  });

  req.end();
}
function api(options, callback) {
  if (!session) {
    throw new Error('You must call init() before api()');
  }

  var version = options.version || 'v3';

  var params = options.params || {};
  callback = callback || function() {};

  var authenticated = !!session.token,
    url = REDIRECT_PATH + (options.url || options.method || '');

  if (authenticated) params.oauth_token = session.token;


  var request_options = {
    host: REDIRECT_HOST,
    path: url + '?' + util.param(params),
    method: options.verb || 'GET',
    headers: {
      "Accept": "application/vnd.twitchtv." + version + "+json",
      "Client-ID": clientId
    }
  };

  var req = https.request(request_options, function(res) {
    log('Response status:', res.statusCode, res.statusMessage);
    res.setEncoding('utf8');

    var responseBody = "";
    res.on('data', function(data) {
      responseBody += data;
    });

    res.on('end', function() {
      var data = JSON.parse(responseBody);

      if (res.statusCode >= 200 && res.statusCode < 300) { // Status 2xx means success
        log('Response Data:', data);
        callback(null, data || null);
      }
      else {
        if (authenticated && res.statusCode === HTTP_CODES.unauthorized) {
          auth.logout(function() {
            callback(data, null);
          });
        }
        else {
          callback(data, null);
        }
      }
    });
  });

  req.on('error', function (e) {
    log('API Error:', e);
    callback(e, null);
  });

  req.end();
}
function api ( options , callback ) { if ( ! session ) { throw new Error ( 'You must call init() before api()' ) ; } var version = options . version || 'v3' ; var params = options . params || { } ; callback = callback || function ( ) { } ; var authenticated = ! ! session . token , url = REDIRECT_PATH + ( options . url || options . method || '' ) ; if ( authenticated ) params . oauth_token = session . token ; var request_options = { host : REDIRECT_HOST , path : url + '?' + util . param ( params ) , method : options . verb || 'GET' , headers : { "Accept" : "application/vnd.twitchtv." + version + "+json" , "Client-ID" : clientId } } ; var req = https . request ( request_options , function ( res ) { log ( 'Response status:' , res . statusCode , res . statusMessage ) ; res . setEncoding ( 'utf8' ) ; var responseBody = "" ; res . on ( 'data' , function ( data ) { responseBody += data ; } ) ; res . on ( 'end' , function ( ) { var data = JSON . parse ( responseBody ) ; if ( res . statusCode >= 200 && res . statusCode < 300 ) { // Status 2xx means success log ( 'Response Data:' , data ) ; callback ( null , data || null ) ; } else { if ( authenticated && res . statusCode === HTTP_CODES . unauthorized ) { auth . logout ( function ( ) { callback ( data , null ) ; } ) ; } else { callback ( data , null ) ; } } } ) ; } ) ; req . on ( 'error' , function ( e ) { log ( 'API Error:' , e ) ; callback ( e , null ) ; } ) ; req . end ( ) ; }
Performs asynchronous calls to the Twitch API.

`options` can have the following fields:

* `method` - The method of the API like `'channel'`.

* `params` - The parameters to augment the amount or type of data received.

* `verb` - The HTTP method like `'GET'`, `'PUT'` and `'DELETE'`

* `version` - The API version to use. Default is `'v3'`
@method api
@param {Object} options The object containing options
@param {function(err, data)} [callback]
Performs asynchronous calls to the Twitch API .