|
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 | + |
3 | 39 |
|
4 | 40 | export default class RtsClient {
|
5 | 41 | constructor(url) {
|
6 | 42 | this._url = url;
|
7 | 43 | }
|
8 | 44 |
|
9 | 45 | getTeamsConfig() {
|
10 |
| - return request({ |
11 |
| - url: this._url + '/teams', |
12 |
| - json: true, |
13 |
| - }); |
| 46 | + return request(this._url + '/teams'); |
14 | 47 | }
|
15 | 48 |
|
16 | 49 | /**
|
17 | 50 | * Track a referral with the Riot Team Server. This should be called once a referred
|
18 | 51 | * user has been successfully registered.
|
19 | 52 | * @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 |
25 | 56 | * success.
|
26 | 57 | */
|
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 | + ); |
34 | 69 | }
|
35 | 70 |
|
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 | + ); |
42 | 79 | }
|
43 | 80 | }
|
0 commit comments