Commit 1a68e066 authored by nanahira's avatar nanahira

Merge branch 'master' of github.com:moecube/challonge

parents bace6141 fb21f6d8
### 2.2.0
- Add timeout support (#39)
### 2.1.2
- Force escaping of all url params (#34)
......
......@@ -294,6 +294,39 @@ const client = challonge.createClient({
<tr>
<td class="name"><code>timeout</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
&lt;optional><br>
</td>
<td class="description last">Duration in ms to wait for a timeout, passed to https request as timeout option</td>
</tr>
<tr>
<td class="name"><code>massageProperties</code></td>
......@@ -383,7 +416,7 @@ const client = challonge.createClient({
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -945,7 +945,7 @@
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -1645,7 +1645,7 @@
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -2356,7 +2356,7 @@
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -48,6 +48,7 @@ const util = require('../util');
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] - Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {number} [options.timeout] Duration in ms to wait for a timeout, passed to https request as timeout option
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @description
* Constructor function for the Client base responsible for communicating with Challonge API
......@@ -141,6 +142,11 @@ Client.prototype.makeRequest = function(obj) {
}
};
const timeout = this.options.get('timeout');
if (timeout) {
options.timeout = timeout;
}
const req = https.request(options, (res) => {
// store the chunked data as it comes back
let resData = '';
......@@ -165,6 +171,13 @@ Client.prototype.makeRequest = function(obj) {
});
});
req.on('timeout', () => {
errorHandler.handle({
timeout: true
}, '', callback, self.options.get('format'));
req.destroy();
});
req.end();
};
</code></pre>
......@@ -179,7 +192,7 @@ Client.prototype.makeRequest = function(obj) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -96,6 +96,17 @@ exports.handle = function(res, resData, callback, format) {
return;
}
if (res.timeout) {
err = {
error: true,
errors: [],
statusCode: 'timeout',
text: ''
};
callback(err, res);
return;
}
// not an api-documented error
err = {
error: true,
......@@ -120,7 +131,7 @@ exports.handle = function(res, resData, callback, format) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -160,7 +160,7 @@ Matches.prototype.update = function(obj) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -249,7 +249,7 @@ Participants.prototype.randomize = function(obj) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -361,7 +361,7 @@ Tournaments.prototype.abortCheckIn = function(obj) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -49,6 +49,7 @@ endpoints.forEach(endpointName => {
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] - Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {number} [options.timeout] Duration in ms to wait for a timeout, passed to https request as timeout option
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @returns {object} new api client instance
* @description
......@@ -89,7 +90,7 @@ exports.createClient = function createClient(options) {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -353,6 +353,39 @@ const client = challonge.createClient({
<tr>
<td class="name"><code>timeout</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
&lt;optional><br>
</td>
<td class="description last">Duration in ms to wait for a timeout, passed to https request as timeout option</td>
</tr>
<tr>
<td class="name"><code>massageProperties</code></td>
......@@ -450,7 +483,7 @@ const client = challonge.createClient({
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -103,7 +103,7 @@ client.tournaments.create({
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -366,7 +366,7 @@
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -857,7 +857,7 @@
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -107,7 +107,7 @@ module.exports = serialize;
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -121,7 +121,7 @@ module.exports = {
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jul 09 2018 17:18:03 GMT-0400 (EDT) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Feb 18 2019 15:34:43 GMT-0500 (EST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
......
......@@ -9,6 +9,7 @@ const util = require('../util');
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {number} [options.timeout] Duration in ms to wait for a timeout, passed to https request as timeout option
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @param {string} [options.apiHostname] If you need to change the domain that the library makes requests to, pass the string domain. This gets passed as the hostname to the config for the https library (so you cant add a path fragment on the end, just the domain). Defaults to 'api.challonge.com'
* @description
......@@ -106,6 +107,11 @@ Client.prototype.makeRequest = function(obj) {
}
};
const timeout = this.options.get('timeout');
if (timeout) {
options.timeout = timeout;
}
const req = https.request(options, (res) => {
// store the chunked data as it comes back
let resData = '';
......@@ -135,5 +141,12 @@ Client.prototype.makeRequest = function(obj) {
callback(e, null);
});
req.on('timeout', () => {
errorHandler.handle({
timeout: true
}, '', callback, self.options.get('format'));
req.destroy();
});
req.end();
};
......@@ -178,6 +178,13 @@ describe('Client Class', () => {
expect(opts.cache_bust).toBeDefined();
});
it('should add a timeout if one is passed', () => {
client.options.timeout = 20000;
client.makeRequest({});
expect(httpsMock.opts.timeout).toEqual(20000);
});
it('should delete the callback, path, and method from the object', () => {
client.makeRequest({
callback: '',
......@@ -249,6 +256,17 @@ describe('Client Class', () => {
});
});
it('should call the error handler on a timeout', () => {
spyOn(errorHandler, 'handle');
client.makeRequest({});
httpsMock.reqListeners.timeout[0]();
expect(errorHandler.handle).toHaveBeenCalledWith({
timeout: true
}, '', undefined, 'json');
});
it('should call the error handler on anything but a 200 response', () => {
spyOn(errorHandler, 'handle');
......
......@@ -57,6 +57,17 @@ exports.handle = function(res, resData, callback, format) {
return;
}
if (res.timeout) {
err = {
error: true,
errors: [],
statusCode: 'timeout',
text: ''
};
callback(err, res);
return;
}
// not an api-documented error
err = {
error: true,
......
......@@ -46,6 +46,23 @@ describe('error handler', () => {
});
it('should throw the correct error on a timeout', () => {
function cb(err, res) {
expect(err).toEqual({
error: true,
errors: [],
statusCode: 'timeout',
text: ''
});
}
HandleFn({
timeout: true
}, '', cb, 'json');
});
it('should not parse resData if not json type and passed as a 422', () => {
function cb(err, res) {
......
......@@ -10,6 +10,7 @@ endpoints.forEach(endpointName => {
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] - Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {number} [options.timeout] Duration in ms to wait for a timeout, passed to https request as timeout option
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @returns {object} new api client instance
* @description
......
......@@ -3,7 +3,7 @@
"description": "Wrapper for the challong api",
"author": "Nanahira <78877@qq.com>",
"main": "./lib/challonge.js",
"version": "2.1.2",
"version": "2.2.0",
"contributors": [
{
"name": "Ricardo Reis",
......
......@@ -6,6 +6,7 @@ mockery.enable({
const httpsMock = {
reset: function() {
this.reqListeners = {};
this.listeners = {};
this.opts = {};
this.res.statusCode = '';
......@@ -13,6 +14,7 @@ const httpsMock = {
opts: {},
listeners: {
},
reqListeners: {},
res: {
on: (method, cb) => {
if (!httpsMock.listeners[method]) {
......@@ -27,6 +29,14 @@ const httpsMock = {
return {
end: () => {
},
destroy: () => {
},
on: (method, cb) => {
if (!httpsMock.reqListeners[method]) {
httpsMock.reqListeners[method] = [];
}
httpsMock.reqListeners[method].push(cb);
}
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment