Skip to content

Commit 4067334

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

File tree

23 files changed

+211
-247
lines changed

23 files changed

+211
-247
lines changed

__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/prod/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/services.api.md

Lines changed: 6 additions & 18 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 20 additions & 24 deletions
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

Lines changed: 3 additions & 3 deletions
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,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy