Skip to content

Commit 4067334

Browse files
authored
Merge pull request #41 from topcoder-platform/code-quality-fixes
Code quality improvements
2 parents ff9341b + 34456c3 commit 4067334

23 files changed

+211
-247
lines changed

__tests__/__snapshots__/index.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Object {
237237
"services": Object {
238238
"api": Object {
239239
"default": [Function],
240+
"getApi": [Function],
240241
"getApiV2": [Function],
241242
"getApiV3": [Function],
242243
"getApiV4": [Function],

__tests__/services/api.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jest.mock(
55
);
66

77
const { config } = require('topcoder-react-utils');
8-
const { getApiV2, getApiV3 } = require('../../src/services/api');
8+
const { getApi } = require('../../src/services/api');
99

1010
describe('Test api', () => {
1111
const ENDPOINT = '/ENDPOINT';
@@ -79,43 +79,43 @@ describe('Test api', () => {
7979

8080
let api;
8181
test('API v2 service works without auth token', () => {
82-
api = getApiV2();
82+
api = getApi('V2');
8383
return testApi(api, config.API.V2);
8484
});
8585

8686
test('API v2 service works with auth token', () => {
87-
api = getApiV2('TOKEN');
87+
api = getApi('V2', 'TOKEN');
8888
return testApi(api, config.API.V2, 'TOKEN');
8989
});
9090

9191
test(
9292
'API v2 service from the previous call is re-used, if token is the same',
93-
() => expect(getApiV2('TOKEN')).toBe(api),
93+
() => expect(getApi('V2', 'TOKEN')).toBe(api),
9494
);
9595

9696
test('New API v2 service is created if token is new', () => {
97-
const api2 = getApiV2('TOKEN2');
97+
const api2 = getApi('V2', 'TOKEN2');
9898
expect(api2).not.toBe(api);
9999
return testApi(api2, config.API.V2, 'TOKEN2');
100100
});
101101

102102
test('API v3 service works without auth token', () => {
103-
api = getApiV3();
103+
api = getApi('V3');
104104
return testApi(api, config.API.V3);
105105
});
106106

107107
test('API v3 service works with auth token', () => {
108-
api = getApiV3('TOKEN');
108+
api = getApi('V3', 'TOKEN');
109109
return testApi(api, config.API.V3, 'TOKEN');
110110
});
111111

112112
test(
113113
'API v3 service from the previous call is re-used, if token is the same',
114-
() => expect(getApiV3('TOKEN')).toBe(api),
114+
() => expect(getApi('V3', 'TOKEN')).toBe(api),
115115
);
116116

117117
test('New API v3 service is created if token is new', () => {
118-
const api2 = getApiV3('TOKEN2');
118+
const api2 = getApi('V3', 'TOKEN2');
119119
expect(api2).not.toBe(api);
120120
return testApi(api2, config.API.V3, 'TOKEN2');
121121
});

dist/dev/index.js

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/prod/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/services.api.md

+6-18
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ This module provides a service for conventient access to Topcoder APIs.
77
* [services.api](#module_services.api)
88
* _static_
99
* [.default](#module_services.api.default)
10-
* [.getApiV2(token)](#module_services.api.getApiV2) ⇒ <code>Api</code>
11-
* [.getApiV3(token)](#module_services.api.getApiV3) ⇒ <code>Api</code>
10+
* [.getApi(version, token)](#module_services.api.getApi) ⇒ <code>Api</code>
1211
* _inner_
1312
* [~Api](#module_services.api..Api)
1413
* [new Api(base, token)](#new_module_services.api..Api_new)
@@ -30,30 +29,19 @@ The default export from the module is
3029
[Api](#module_services.api..Api) class.
3130

3231
**Kind**: static property of [<code>services.api</code>](#module_services.api)
33-
<a name="module_services.api.getApiV2"></a>
32+
<a name="module_services.api.getApi"></a>
3433

35-
### services.api.getApiV2(token) ⇒ <code>Api</code>
36-
Returns a new or existing Api object for Topcoder API v2.
34+
### services.api.getApi(version, token) ⇒ <code>Api</code>
35+
Returns a new or existing Api object for Topcoder API.
3736

3837
**Kind**: static method of [<code>services.api</code>](#module_services.api)
39-
**Returns**: <code>Api</code> - API v2 service object.
38+
**Returns**: <code>Api</code> - API service object.
4039

4140
| Param | Type | Description |
4241
| --- | --- | --- |
42+
| version | <code>String</code> | The version of the API. |
4343
| token | <code>String</code> | Optional. Auth token for Topcoder API v2. |
4444

45-
<a name="module_services.api.getApiV3"></a>
46-
47-
### services.api.getApiV3(token) ⇒ <code>Api</code>
48-
Returns a new or existing Api object for Topcoder API v3
49-
50-
**Kind**: static method of [<code>services.api</code>](#module_services.api)
51-
**Returns**: <code>Api</code> - API v3 service object.
52-
53-
| Param | Type | Description |
54-
| --- | --- | --- |
55-
| token | <code>String</code> | Optional. Auth token for Topcoder API v3. |
56-
5745
<a name="module_services.api..Api"></a>
5846

5947
### services.api~Api

src/actions/auth.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { createActions } from 'redux-actions';
77
import { decodeToken } from 'tc-accounts';
8-
import { getApiV3 } from '../services/api';
8+
import { getApi } from '../services/api';
99

1010
/**
1111
* @static
@@ -16,7 +16,7 @@ import { getApiV3 } from '../services/api';
1616
function loadProfileDone(userTokenV3) {
1717
if (!userTokenV3) return Promise.resolve(null);
1818
const user = decodeToken(userTokenV3);
19-
const api = getApiV3(userTokenV3);
19+
const api = getApi('V3', userTokenV3);
2020
return Promise.all([
2121
api.get(`/members/${user.handle}`)
2222
.then(res => res.json()).then(res => (res.result.status === 200 ? res.result.content : {})),

src/actions/challenge.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import _ from 'lodash';
77
import { config } from 'topcoder-react-utils';
88
import { createActions } from 'redux-actions';
99
import { getService as getChallengesService } from '../services/challenges';
10-
import { getApiV2 } from '../services/api';
10+
import { getApi } from '../services/api';
1111

1212
/**
1313
* @static
@@ -70,7 +70,7 @@ function getSubmissionsInit(challengeId) {
7070
* @return {Action}
7171
*/
7272
function getSubmissionsDone(challengeId, tokenV2) {
73-
return getApiV2(tokenV2)
73+
return getApi('V2', tokenV2)
7474
.fetch(`/challenges/submissions/${challengeId}/mySubmissions`)
7575
.then(response => response.json())
7676
.then(response => ({
@@ -169,7 +169,7 @@ function loadResultsInit(challengeId) {
169169
* @return {Action}
170170
*/
171171
function loadResultsDone(auth, challengeId, type) {
172-
return getApiV2(auth.tokenV2)
172+
return getApi('V2', auth.tokenV2)
173173
.fetch(`/${type}/challenges/result/${challengeId}`)
174174
.then(response => response.json())
175175
.then(response => ({
@@ -194,7 +194,7 @@ function fetchCheckpointsInit() {}
194194
*/
195195
function fetchCheckpointsDone(tokenV2, challengeId) {
196196
const endpoint = `/design/challenges/checkpoint/${challengeId}`;
197-
return getApiV2(tokenV2).fetch(endpoint)
197+
return getApi('V2', tokenV2).fetch(endpoint)
198198
.then((response) => {
199199
if (response.status !== 200) {
200200
throw response.status;

src/actions/smp.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import _ from 'lodash';
77
import { createActions } from 'redux-actions';
8-
import { getApiV3 } from '../services/api';
8+
import { getApi } from '../services/api';
99

1010
/**
1111
* @static
@@ -22,7 +22,7 @@ function deleteSubmissionInit() {}
2222
* @return {Action}
2323
*/
2424
function deleteSubmissionDone(tokenV3, submissionId) {
25-
return getApiV3(tokenV3).delete(`/submissions/${submissionId}`)
25+
return getApi('V3', tokenV3).delete(`/submissions/${submissionId}`)
2626
.then(() => submissionId);
2727
}
2828

src/services/__mocks__/api.js

+20-24
Original file line numberDiff line numberDiff line change
@@ -103,36 +103,32 @@ export default class Api {
103103
}
104104
}
105105

106-
/**
107-
* Topcoder API v2.
106+
/*
107+
* Topcoder API
108108
*/
109+
const lastApiInstances = {};
109110

110111
/**
111-
* Returns a new or existing Api object for Topcoder API v2.
112-
* @param {String} token Optional. Auth token for Topcoder API v2.
113-
* @return {Api} API v2 service object.
112+
* Returns a new or existing Api object for Topcoder API.
113+
* @param {String} version The API version.
114+
* @param {String} token Optional. Auth token for Topcoder API.
115+
* @return {Api} API service object.
114116
*/
115-
let lastApiV2 = null;
116-
export function getApiV2(token) {
117-
if (!lastApiV2 || lastApiV2.private.token !== token) {
118-
lastApiV2 = new Api(config.API.V2, token);
117+
export function getApi(version, token) {
118+
if (!version || !config.API[version]) {
119+
throw new Error(`${version} is not a valid API version`);
120+
}
121+
if (!lastApiInstances[version] || lastApiInstances[version].private.token !== token) {
122+
lastApiInstances[version] = new Api(config.API[version], token);
119123
}
120-
return lastApiV2;
124+
return lastApiInstances[version];
121125
}
122126

123127
/**
124-
* Topcoder API v3.
128+
* Keep the old API factories for backwards compatibility
129+
* DO NOT USE THEM FOR NEW IMPLEMENTATIONS.
130+
* USE THE getApi(version, token) FACTORY.
125131
*/
126-
127-
/**
128-
* Returns a new or existing Api object for Topcoder API v3
129-
* @param {String} token Optional. Auth token for Topcoder API v3.
130-
* @return {Api} API v3 service object.
131-
*/
132-
let lastApiV3 = null;
133-
export function getApiV3(token) {
134-
if (!lastApiV3 || lastApiV3.private.token !== token) {
135-
lastApiV3 = new Api(config.API.V3, token);
136-
}
137-
return lastApiV3;
138-
}
132+
export const getApiV2 = token => getApi('V2', token);
133+
export const getApiV3 = token => getApi('V3', token);
134+
export const getApiV4 = token => getApi('V4', token);

src/services/__mocks__/challenges.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import _ from 'lodash';
77
import { COMPETITION_TRACKS } from '../../utils/tc';
8-
import { getApiV2, getApiV3 } from './api';
8+
import { getApi } from './api';
99

1010
import sampleApiV3Response from './data/challenges-v3.json';
1111
import sampleApiV3ResponseSingle from './data/challenge-v3.json';
@@ -215,8 +215,8 @@ class ChallengesService {
215215
};
216216

217217
this.private = {
218-
api: getApiV3(tokenV3),
219-
apiV2: getApiV2(tokenV2),
218+
api: getApi('V3', tokenV3),
219+
apiV2: getApi('V2', tokenV2),
220220
getChallenges,
221221
tokenV2,
222222
tokenV3,

src/services/__mocks__/groups.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import _ from 'lodash';
1919
import { config } from 'topcoder-react-utils';
20-
import { getApiV3 } from './api';
20+
import { getApi } from './api';
2121
import logger from '../../utils/logger';
2222

2323
/* The value of USER_GROUP_MAXAGE constant converted to [ms]. */
@@ -172,7 +172,7 @@ class GroupService {
172172
*/
173173
constructor(tokenV3) {
174174
this.private = {
175-
api: getApiV3(tokenV3),
175+
api: getApi('V3', tokenV3),
176176
tokenV3,
177177
};
178178
}

src/services/api.js

+18-35
Original file line numberDiff line numberDiff line change
@@ -236,51 +236,34 @@ class Api {
236236
export default Api;
237237

238238
/*
239-
* Topcoder API v2.
239+
* Topcoder API
240240
*/
241+
const lastApiInstances = {};
241242

242-
let lastApiV2 = null;
243243
/**
244-
* Returns a new or existing Api object for Topcoder API v2.
245-
* @param {String} token Optional. Auth token for Topcoder API v2.
246-
* @return {Api} API v2 service object.
244+
* Returns a new or existing Api object for Topcoder API.
245+
* @param {String} version The API version.
246+
* @param {String} token Optional. Auth token for Topcoder API.
247+
* @return {Api} API service object.
247248
*/
248-
export function getApiV2(token) {
249-
if (!lastApiV2 || lastApiV2.private.token !== token) {
250-
lastApiV2 = new Api(config.API.V2, token);
249+
export function getApi(version, token) {
250+
if (!version || !config.API[version]) {
251+
throw new Error(`${version} is not a valid API version`);
251252
}
252-
return lastApiV2;
253-
}
254-
255-
/*
256-
* Topcoder API v3.
257-
*/
258-
259-
let lastApiV3 = null;
260-
/**
261-
* Returns a new or existing Api object for Topcoder API v3
262-
* @param {String} token Optional. Auth token for Topcoder API v3.
263-
* @return {Api} API v3 service object.
264-
*/
265-
export function getApiV3(token) {
266-
if (!lastApiV3 || lastApiV3.private.token !== token) {
267-
lastApiV3 = new Api(config.API.V3, token);
253+
if (!lastApiInstances[version] || lastApiInstances[version].private.token !== token) {
254+
lastApiInstances[version] = new Api(config.API[version], token);
268255
}
269-
return lastApiV3;
256+
return lastApiInstances[version];
270257
}
271258

272-
let lastApiV4 = null;
273259
/**
274-
* Returns a new or existing Api object for Topcoder API V4
275-
* @param {String} token Optional. Auth token for Topcoder API V4.
276-
* @return {Api} API V4 service object.
260+
* Keep the old API factories for backwards compatibility
261+
* DO NOT USE THEM FOR NEW IMPLEMENTATIONS.
262+
* USE THE getApi(version, token) FACTORY.
277263
*/
278-
export function getApiV4(token) {
279-
if (!lastApiV4 || lastApiV4.private.token !== token) {
280-
lastApiV4 = new Api(config.API.V4, token);
281-
}
282-
return lastApiV4;
283-
}
264+
export const getApiV2 = token => getApi('V2', token);
265+
export const getApiV3 = token => getApi('V3', token);
266+
export const getApiV4 = token => getApi('V4', token);
284267

285268
/**
286269
* Gets a valid TC M2M token, either requesting one from TC Auth0 API, or

src/services/billing.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @module "services.billing"
33
* @desc Access to Topcoder billing accounts.
44
*/
5-
import { getApiV3 } from './api';
5+
import { getApi } from './api';
66

77
/**
88
* @static
@@ -21,7 +21,7 @@ class Billing {
2121
*/
2222
constructor(tokenV3) {
2323
this.private = {
24-
api: getApiV3(tokenV3),
24+
api: getApi('V3', tokenV3),
2525
tokenV3,
2626
};
2727
}

0 commit comments

Comments
 (0)