Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to send JSON with request, added clientId option #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function call(opt, db, callback) {
var channel = typeof opt.channel !== 'undefined' ? opt.channel : 'no_channel_specified';
var method = typeof opt.method !== 'undefined' ? opt.method : 'GET';
var options = typeof opt.options !== 'undefined' ? opt.options : {};
var json = typeof opt.json !== 'undefined' ? opt.json : null;
var clientId = typeof opt.clientId !== 'undefined' ? opt.clientId : '';
var path = typeof opt.path === 'string' ? opt.path : '';
var requestOptions = {};
var token = '';
Expand Down Expand Up @@ -81,11 +83,21 @@ function call(opt, db, callback) {
url: 'https://api.twitch.tv/kraken' + path + (options ? '?' + options : ''),
headers: {
'Accept': 'application/vnd.twitchtv.v3+json',
'Client-ID': ''
'Client-ID': clientId
},
method: method
};

// Add JSON to send
if (json !== null) {
/*
* Automatically sends the correct Content-Type header and interprets
* body as JSON. Also parses the response as JSON.
*/
requestOptions.json = true;
requestOptions.body = json;
}

if (token !== '') { requestOptions.headers['Authorization'] = 'OAuth ' + token; }

cb(null);
Expand All @@ -98,7 +110,12 @@ function call(opt, db, callback) {
return callback.call(this, error);
}

try { body = JSON.parse(body); }
try {
// If JSON was send, the response will be parsed as JSON already
if (json === null) {
body = JSON.parse(body);
}
}
catch (error) {
if (typeof db === 'function') { return db.call(this, error); }
return callback.call(this, error);
Expand All @@ -110,4 +127,4 @@ function call(opt, db, callback) {
});
}

exports.call = call;
exports.call = call;