Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit f8d7902

Browse files
authored
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.
2 parents f643224 + 028c40e commit f8d7902

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

src/RtsClient.js

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,80 @@
1-
const q = require('q');
2-
const request = q.nfbind(require('browser-request'));
1+
import 'whatwg-fetch';
2+
3+
function checkStatus(response) {
4+
if (!response.ok) {
5+
return response.text().then((text) => {
6+
throw new Error(text);
7+
});
8+
}
9+
return response;
10+
}
11+
12+
function parseJson(response) {
13+
return response.json();
14+
}
15+
16+
function encodeQueryParams(params) {
17+
return '?' + Object.keys(params).map((k) => {
18+
return k + '=' + encodeURIComponent(params[k]);
19+
}).join('&');
20+
}
21+
22+
const request = (url, opts) => {
23+
if (opts && opts.qs) {
24+
url += encodeQueryParams(opts.qs);
25+
delete opts.qs;
26+
}
27+
if (opts && opts.body) {
28+
if (!opts.headers) {
29+
opts.headers = {};
30+
}
31+
opts.body = JSON.stringify(opts.body);
32+
opts.headers['Content-Type'] = 'application/json';
33+
}
34+
return fetch(url, opts)
35+
.then(checkStatus)
36+
.then(parseJson);
37+
};
38+
339

440
export default class RtsClient {
541
constructor(url) {
642
this._url = url;
743
}
844

945
getTeamsConfig() {
10-
return request({
11-
url: this._url + '/teams',
12-
json: true,
13-
});
46+
return request(this._url + '/teams');
1447
}
1548

1649
/**
1750
* Track a referral with the Riot Team Server. This should be called once a referred
1851
* user has been successfully registered.
1952
* @param {string} referrer the user ID of one who referred the user to Riot.
20-
* @param {string} user_id the user ID of the user being referred.
21-
* @param {string} user_email the email address linked to `user_id`.
22-
* @returns {Promise} a promise that resolves to [$response, $body], where $response
23-
* is the response object created by the request lib and $body is the object parsed
24-
* from the JSON response body. $body should be { team_token: 'sometoken' } upon
53+
* @param {string} userId the user ID of the user being referred.
54+
* @param {string} userEmail the email address linked to `userId`.
55+
* @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon
2556
* success.
2657
*/
27-
trackReferral(referrer, user_id, user_email) {
28-
return request({
29-
url: this._url + '/register',
30-
json: true,
31-
body: {referrer, user_id, user_email},
32-
method: 'POST',
33-
});
58+
trackReferral(referrer, userId, userEmail) {
59+
return request(this._url + '/register',
60+
{
61+
body: {
62+
referrer: referrer,
63+
user_id: userId,
64+
user_email: userEmail,
65+
},
66+
method: 'POST',
67+
}
68+
);
3469
}
3570

36-
getTeam(team_token) {
37-
return request({
38-
url: this._url + '/teamConfiguration',
39-
json: true,
40-
qs: {team_token},
41-
});
71+
getTeam(teamToken) {
72+
return request(this._url + '/teamConfiguration',
73+
{
74+
qs: {
75+
team_token: teamToken,
76+
},
77+
}
78+
);
4279
}
4380
}

src/components/structures/login/Registration.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,22 @@ module.exports = React.createClass({
111111
teamServerBusy: true,
112112
});
113113
// GET team configurations including domains, names and icons
114-
this._rtsClient.getTeamsConfig().then((args) => {
115-
// args = [$request, $body]
114+
this._rtsClient.getTeamsConfig().then((data) => {
116115
const teamsConfig = {
117-
teams: args[1],
116+
teams: data,
118117
supportEmail: this.props.teamServerConfig.supportEmail,
119118
};
120119
console.log('Setting teams config to ', teamsConfig);
121120
this.setState({
122121
teamsConfig: teamsConfig,
122+
teamServerBusy: false,
123123
});
124124
}, (err) => {
125125
console.error('Error retrieving config for teams', err);
126-
}).finally(() => {
127126
this.setState({
128127
teamServerBusy: false,
129128
});
130-
}).done();
129+
});
131130
}
132131
},
133132

@@ -221,13 +220,12 @@ module.exports = React.createClass({
221220
self.props.referrer,
222221
response.user_id,
223222
self.state.formVals.email
224-
).then((args) => {
225-
const teamToken = args[1].team_token;
223+
).then((data) => {
224+
const teamToken = data.team_token;
226225
// Store for use /w welcome pages
227226
window.localStorage.setItem('mx_team_token', teamToken);
228227

229-
self._rtsClient.getTeam(teamToken).then((args) => {
230-
const team = args[1];
228+
self._rtsClient.getTeam(teamToken).then((team) => {
231229
console.log(
232230
`User successfully registered with team ${team.name}`
233231
);

0 commit comments

Comments
 (0)