Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ out
node_modules/
.npm
.eslintcache
.vscode
.vscode
.idea
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ const TwitchWebhook = require('twitch-webhook')
const twitchWebhook = new TwitchWebhook({
client_id: 'Your Twitch Client ID',
callback: 'Your Callback URL',
secret: 'It\'s a secret', // default: false
lease_seconds: 259200, // default: 864000 (maximum value)
access_token: 'Your access token', // default: null (ignored)
secret: 'It\'s a secret', // default: false
lease_seconds: 259200, // default: 864000 (maximum value)
listen: {
port: 8080, // default: 8443
host: '127.0.0.1', // default: 0.0.0.0
autoStart: false // default: true
port: 8080, // default: 8443
host: '127.0.0.1', // default: 0.0.0.0
autoStart: false // default: true
}
})

Expand Down
19 changes: 10 additions & 9 deletions examples/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const TwitchWebhook = require('twitch-webhook')

const twitchWebhook = new TwitchWebhook({
client_id: 'Your Twitch Client ID',
callback: 'Your Callback URL',
secret: 'It\'s a secret', // default: false
lease_seconds: 259200, // default: 864000 (maximum value)
listen: {
port: 8080, // default: 8443
host: '127.0.0.1', // default: 0.0.0.0
autoStart: false // default: true
}
client_id: 'Your Twitch Client ID',
callback: 'Your Callback URL',
access_token: 'Your access token', // default: null (ignored)
secret: 'It\'s a secret', // default: false
lease_seconds: 259200, // default: 864000 (maximum value)
listen: {
port: 8080, // default: 8443
host: '127.0.0.1', // default: 0.0.0.0
autoStart: false // default: true
}
})

// set listener for all topics
Expand Down
41 changes: 23 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ class TwitchWebhook extends EventEmitter {
*
* @param {Object} options - Options
* @param {string} options.client_id - Client ID required for Twitch API calls
* @param {string} options.callback - URL where notifications
* will be delivered.
* @param {string} [options.secret=false] - Secret used to sign
* notification payloads.
* @param {string} [options.access_token] - Access token used to increase rate limit
* @param {number} [options.lease_seconds=864000] - Number of seconds until
* the subscription expires.
* @param {boolean|Object} [options.listen] - Listen options
Expand All @@ -38,16 +37,9 @@ class TwitchWebhook extends EventEmitter {
throw new errors.FatalError('Twitch Client ID not provided!')
}

if (!options.callback) {
throw new errors.FatalError('Callback URL not provided!')
}

super()

this._options = options
if (options.callback.substr(-1) !== '/') {
this._options.callback += '/'
};

if (this._options.lease_seconds === undefined) {
this._options.lease_seconds = 864000
Expand Down Expand Up @@ -143,10 +135,17 @@ class TwitchWebhook extends EventEmitter {
* unsubscribe from.
* @param {string} topic - Topic name
* @param {Object} options - Topic options
* @param {string} callback - Callback url
* @throws {Promise<RequestDenied>} If the hub finds any errors in the request
* @return {Promise}
*/
_request (mode, topic, options) {
_request (mode, topic, callback, options) {
if (!callback) {
throw new errors.FatalError('Callback URL not provided!')
}
if (callback.substr(-1) !== '/') {
callback += '/'
}
if (!isAbsoluteUrl(topic)) {
topic = this._apiUrl + topic
}
Expand All @@ -156,11 +155,17 @@ class TwitchWebhook extends EventEmitter {

let requestOptions = {}
requestOptions.url = this._hubUrl
requestOptions.headers = {
'Client-ID': this._options.client_id
if (this._options.access_token !== undefined) {
requestOptions.headers = {
'Authorization': `Bearer ${this._options.access_token}`
}
} else {
requestOptions.headers = {
'Client-ID': this._options.client_id
}
}
requestOptions.qs = {
'hub.callback': this._options.callback,
'hub.callback': callback,
'hub.mode': mode,
'hub.topic': topic,
'hub.lease_seconds': this._options.lease_seconds
Expand Down Expand Up @@ -196,8 +201,8 @@ class TwitchWebhook extends EventEmitter {
* @throws {RequestDenied} If hub finds any errors in the request
* @return {Promise}
*/
subscribe (topic, options = {}) {
return this._request('subscribe', topic, options)
subscribe (topic, callback, options = {}) {
return this._request('subscribe', topic, callback, options)
}

/**
Expand All @@ -208,14 +213,14 @@ class TwitchWebhook extends EventEmitter {
* @throws {RequestDenied} If hub finds any errors in the request
* @return {Promise}
*/
unsubscribe (topic, options = {}) {
unsubscribe (topic, callback, options = {}) {
if (topic !== '*') {
return this._request('unsubscribe', topic, options)
return this._request('unsubscribe', topic, callback, options)
}

let poll = []
for (let topic of Object.keys(this._subscriptions)) {
poll.push(() => this._request('unsubscribe', topic))
poll.push(() => this._request('unsubscribe', topic, null, {}))
}
return Promise.all(poll)
}
Expand Down