From 4c4cc585c74730c7e591688d1e1785a85d030883 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 31 Jan 2017 13:40:01 +0000 Subject: [PATCH 1/4] Throw errors on !==200 status codes from RTS --- src/RtsClient.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index 0067d0ae10..fe1f0538ca 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -1,5 +1,25 @@ const q = require('q'); -const request = q.nfbind(require('browser-request')); +const request = (opts) => { + const expectingJSONOnSucess = opts.json; + if (opts.json) { + opts.json = false; + } + return q.nfbind(require('browser-request'))(opts).then((args) => { + const response = args[0]; + let body = args[1]; + + // Do not expect JSON on error status code, throw error instead + if (response.statusCode !== 200) { + throw new Error(body); + } + + if (expectingJSONOnSucess) { + body = JSON.parse(body); + } + + return [response, body]; + }); +}; export default class RtsClient { constructor(url) { From 2f188770e56a557871d5f482b40ea31ef5031750 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 31 Jan 2017 15:59:38 +0000 Subject: [PATCH 2/4] Typo --- src/RtsClient.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index fe1f0538ca..0115edacb4 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -1,6 +1,6 @@ const q = require('q'); const request = (opts) => { - const expectingJSONOnSucess = opts.json; + const expectingJSONOnSuccess = opts.json; if (opts.json) { opts.json = false; } @@ -13,7 +13,7 @@ const request = (opts) => { throw new Error(body); } - if (expectingJSONOnSucess) { + if (expectingJSONOnSuccess) { body = JSON.parse(body); } From fa1981ce0915f707d928f4b26de17ef226efcccf Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 1 Feb 2017 10:39:52 +0000 Subject: [PATCH 3/4] Use whatwg-fetch instead of browser-request --- src/RtsClient.js | 83 +++++++++++-------- .../structures/login/Registration.js | 16 ++-- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index 0115edacb4..f1cbc8e6f1 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -1,36 +1,49 @@ -const q = require('q'); -const request = (opts) => { - const expectingJSONOnSuccess = opts.json; - if (opts.json) { - opts.json = false; +import 'whatwg-fetch'; + +function checkStatus(response) { + if (!response.ok) { + return response.text().then((text) => { + throw new Error(text); + }); } - return q.nfbind(require('browser-request'))(opts).then((args) => { - const response = args[0]; - let body = args[1]; + return response; +} - // Do not expect JSON on error status code, throw error instead - if (response.statusCode !== 200) { - throw new Error(body); +function parseJson(response) { + return response.json(); +} + +function encodeQueryParams(params) { + return '?' + Object.keys(params).map((k) => { + return k + '=' + encodeURIComponent(params[k]); + }).join('&'); +} + +const request = (url, opts) => { + if (opts && opts.qs) { + url += encodeQueryParams(opts.qs); + delete opts.qs; + } + if (opts && opts.body) { + if (!opts.headers) { + opts.headers = {}; } - - if (expectingJSONOnSuccess) { - body = JSON.parse(body); - } - - return [response, body]; - }); + opts.body = JSON.stringify(opts.body); + opts.headers['Content-Type'] = 'application/json'; + } + return fetch(url, opts) + .then(checkStatus) + .then(parseJson); }; + export default class RtsClient { constructor(url) { this._url = url; } getTeamsConfig() { - return request({ - url: this._url + '/teams', - json: true, - }); + return request(this._url + '/teams'); } /** @@ -39,25 +52,23 @@ export default class RtsClient { * @param {string} referrer the user ID of one who referred the user to Riot. * @param {string} user_id the user ID of the user being referred. * @param {string} user_email the email address linked to `user_id`. - * @returns {Promise} a promise that resolves to [$response, $body], where $response - * is the response object created by the request lib and $body is the object parsed - * from the JSON response body. $body should be { team_token: 'sometoken' } upon + * @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon * success. */ trackReferral(referrer, user_id, user_email) { - return request({ - url: this._url + '/register', - json: true, - body: {referrer, user_id, user_email}, - method: 'POST', - }); + return request(this._url + '/register', + { + body: {referrer, user_id, user_email}, + method: 'POST', + } + ); } getTeam(team_token) { - return request({ - url: this._url + '/teamConfiguration', - json: true, - qs: {team_token}, - }); + return request(this._url + '/teamConfiguration', + { + qs: {team_token}, + } + ); } } diff --git a/src/components/structures/login/Registration.js b/src/components/structures/login/Registration.js index 82cdbef9ca..3fa2723ad3 100644 --- a/src/components/structures/login/Registration.js +++ b/src/components/structures/login/Registration.js @@ -111,23 +111,22 @@ module.exports = React.createClass({ teamServerBusy: true, }); // GET team configurations including domains, names and icons - this._rtsClient.getTeamsConfig().then((args) => { - // args = [$request, $body] + this._rtsClient.getTeamsConfig().then((data) => { const teamsConfig = { - teams: args[1], + teams: data, supportEmail: this.props.teamServerConfig.supportEmail, }; console.log('Setting teams config to ', teamsConfig); this.setState({ teamsConfig: teamsConfig, + teamServerBusy: false, }); }, (err) => { console.error('Error retrieving config for teams', err); - }).finally(() => { this.setState({ teamServerBusy: false, }); - }).done(); + }); } }, @@ -221,13 +220,12 @@ module.exports = React.createClass({ self.props.referrer, response.user_id, self.state.formVals.email - ).then((args) => { - const teamToken = args[1].team_token; + ).then((data) => { + const teamToken = data.team_token; // Store for use /w welcome pages window.localStorage.setItem('mx_team_token', teamToken); - self._rtsClient.getTeam(teamToken).then((args) => { - const team = args[1]; + self._rtsClient.getTeam(teamToken).then((team) => { console.log( `User successfully registered with team ${team.name}` ); From 028c40e293d7fe241f55b4bc1361ccd72f5e6929 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 1 Feb 2017 11:16:14 +0000 Subject: [PATCH 4/4] Linting --- src/RtsClient.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index f1cbc8e6f1..ae62fb8b22 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -50,24 +50,30 @@ export default class RtsClient { * Track a referral with the Riot Team Server. This should be called once a referred * user has been successfully registered. * @param {string} referrer the user ID of one who referred the user to Riot. - * @param {string} user_id the user ID of the user being referred. - * @param {string} user_email the email address linked to `user_id`. + * @param {string} userId the user ID of the user being referred. + * @param {string} userEmail the email address linked to `userId`. * @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon * success. */ - trackReferral(referrer, user_id, user_email) { + trackReferral(referrer, userId, userEmail) { return request(this._url + '/register', { - body: {referrer, user_id, user_email}, + body: { + referrer: referrer, + user_id: userId, + user_email: userEmail, + }, method: 'POST', } ); } - getTeam(team_token) { + getTeam(teamToken) { return request(this._url + '/teamConfiguration', { - qs: {team_token}, + qs: { + team_token: teamToken, + }, } ); }