Use whatwg-fetch instead of browser-request

pull/21833/head
Luke Barnard 2017-02-01 10:39:52 +00:00
parent 2f188770e5
commit fa1981ce09
2 changed files with 54 additions and 45 deletions

View File

@ -1,36 +1,49 @@
const q = require('q'); import 'whatwg-fetch';
const request = (opts) => {
const expectingJSONOnSuccess = opts.json; function checkStatus(response) {
if (opts.json) { if (!response.ok) {
opts.json = false; return response.text().then((text) => {
throw new Error(text);
});
} }
return q.nfbind(require('browser-request'))(opts).then((args) => { return response;
const response = args[0]; }
let body = args[1];
// Do not expect JSON on error status code, throw error instead function parseJson(response) {
if (response.statusCode !== 200) { return response.json();
throw new Error(body); }
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 = {};
} }
opts.body = JSON.stringify(opts.body);
if (expectingJSONOnSuccess) { opts.headers['Content-Type'] = 'application/json';
body = JSON.parse(body); }
} return fetch(url, opts)
.then(checkStatus)
return [response, body]; .then(parseJson);
});
}; };
export default class RtsClient { export default class RtsClient {
constructor(url) { constructor(url) {
this._url = url; this._url = url;
} }
getTeamsConfig() { getTeamsConfig() {
return request({ return request(this._url + '/teams');
url: this._url + '/teams',
json: true,
});
} }
/** /**
@ -39,25 +52,23 @@ export default class RtsClient {
* @param {string} referrer the user ID of one who referred the user to Riot. * @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_id the user ID of the user being referred.
* @param {string} user_email the email address linked to `user_id`. * @param {string} user_email the email address linked to `user_id`.
* @returns {Promise} a promise that resolves to [$response, $body], where $response * @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon
* 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
* success. * success.
*/ */
trackReferral(referrer, user_id, user_email) { trackReferral(referrer, user_id, user_email) {
return request({ return request(this._url + '/register',
url: this._url + '/register', {
json: true, body: {referrer, user_id, user_email},
body: {referrer, user_id, user_email}, method: 'POST',
method: 'POST', }
}); );
} }
getTeam(team_token) { getTeam(team_token) {
return request({ return request(this._url + '/teamConfiguration',
url: this._url + '/teamConfiguration', {
json: true, qs: {team_token},
qs: {team_token}, }
}); );
} }
} }

View File

@ -111,23 +111,22 @@ module.exports = React.createClass({
teamServerBusy: true, teamServerBusy: true,
}); });
// GET team configurations including domains, names and icons // GET team configurations including domains, names and icons
this._rtsClient.getTeamsConfig().then((args) => { this._rtsClient.getTeamsConfig().then((data) => {
// args = [$request, $body]
const teamsConfig = { const teamsConfig = {
teams: args[1], teams: data,
supportEmail: this.props.teamServerConfig.supportEmail, supportEmail: this.props.teamServerConfig.supportEmail,
}; };
console.log('Setting teams config to ', teamsConfig); console.log('Setting teams config to ', teamsConfig);
this.setState({ this.setState({
teamsConfig: teamsConfig, teamsConfig: teamsConfig,
teamServerBusy: false,
}); });
}, (err) => { }, (err) => {
console.error('Error retrieving config for teams', err); console.error('Error retrieving config for teams', err);
}).finally(() => {
this.setState({ this.setState({
teamServerBusy: false, teamServerBusy: false,
}); });
}).done(); });
} }
}, },
@ -221,13 +220,12 @@ module.exports = React.createClass({
self.props.referrer, self.props.referrer,
response.user_id, response.user_id,
self.state.formVals.email self.state.formVals.email
).then((args) => { ).then((data) => {
const teamToken = args[1].team_token; const teamToken = data.team_token;
// Store for use /w welcome pages // Store for use /w welcome pages
window.localStorage.setItem('mx_team_token', teamToken); window.localStorage.setItem('mx_team_token', teamToken);
self._rtsClient.getTeam(teamToken).then((args) => { self._rtsClient.getTeam(teamToken).then((team) => {
const team = args[1];
console.log( console.log(
`User successfully registered with team ${team.name}` `User successfully registered with team ${team.name}`
); );