Use whatwg-fetch instead of browser-request

This commit is contained in:
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');
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},
}
);
}
}

View file

@ -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}`
);