Merge pull request #662 from matrix-org/luke/rts-handle-errors
Throw errors on not 'ok' status codes from RTS, swap to `whatwg-fetch` for sending requests to RTS.pull/21833/head
commit
f8d7902d2e
|
@ -1,5 +1,41 @@
|
|||
const q = require('q');
|
||||
const request = q.nfbind(require('browser-request'));
|
||||
import 'whatwg-fetch';
|
||||
|
||||
function checkStatus(response) {
|
||||
if (!response.ok) {
|
||||
return response.text().then((text) => {
|
||||
throw new Error(text);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
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 = {};
|
||||
}
|
||||
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) {
|
||||
|
@ -7,37 +43,38 @@ export default class RtsClient {
|
|||
}
|
||||
|
||||
getTeamsConfig() {
|
||||
return request({
|
||||
url: this._url + '/teams',
|
||||
json: true,
|
||||
});
|
||||
return request(this._url + '/teams');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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`.
|
||||
* @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
|
||||
* @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) {
|
||||
return request({
|
||||
url: this._url + '/register',
|
||||
json: true,
|
||||
body: {referrer, user_id, user_email},
|
||||
method: 'POST',
|
||||
});
|
||||
trackReferral(referrer, userId, userEmail) {
|
||||
return request(this._url + '/register',
|
||||
{
|
||||
body: {
|
||||
referrer: referrer,
|
||||
user_id: userId,
|
||||
user_email: userEmail,
|
||||
},
|
||||
method: 'POST',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getTeam(team_token) {
|
||||
return request({
|
||||
url: this._url + '/teamConfiguration',
|
||||
json: true,
|
||||
qs: {team_token},
|
||||
});
|
||||
getTeam(teamToken) {
|
||||
return request(this._url + '/teamConfiguration',
|
||||
{
|
||||
qs: {
|
||||
team_token: teamToken,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}`
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue