Skip to content

Code quality improvements #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Object {
"services": Object {
"api": Object {
"default": [Function],
"getApi": [Function],
"getApiV2": [Function],
"getApiV3": [Function],
"getApiV4": [Function],
Expand Down
18 changes: 9 additions & 9 deletions __tests__/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock(
);

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

describe('Test api', () => {
const ENDPOINT = '/ENDPOINT';
Expand Down Expand Up @@ -79,43 +79,43 @@ describe('Test api', () => {

let api;
test('API v2 service works without auth token', () => {
api = getApiV2();
api = getApi('V2');
return testApi(api, config.API.V2);
});

test('API v2 service works with auth token', () => {
api = getApiV2('TOKEN');
api = getApi('V2', 'TOKEN');
return testApi(api, config.API.V2, 'TOKEN');
});

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

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

test('API v3 service works without auth token', () => {
api = getApiV3();
api = getApi('V3');
return testApi(api, config.API.V3);
});

test('API v3 service works with auth token', () => {
api = getApiV3('TOKEN');
api = getApi('V3', 'TOKEN');
return testApi(api, config.API.V3, 'TOKEN');
});

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

test('New API v3 service is created if token is new', () => {
const api2 = getApiV3('TOKEN2');
const api2 = getApi('V3', 'TOKEN2');
expect(api2).not.toBe(api);
return testApi(api2, config.API.V3, 'TOKEN2');
});
Expand Down
30 changes: 15 additions & 15 deletions dist/dev/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/prod/index.js

Large diffs are not rendered by default.

24 changes: 6 additions & 18 deletions docs/services.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ This module provides a service for conventient access to Topcoder APIs.
* [services.api](#module_services.api)
* _static_
* [.default](#module_services.api.default)
* [.getApiV2(token)](#module_services.api.getApiV2) ⇒ <code>Api</code>
* [.getApiV3(token)](#module_services.api.getApiV3) ⇒ <code>Api</code>
* [.getApi(version, token)](#module_services.api.getApi) ⇒ <code>Api</code>
* _inner_
* [~Api](#module_services.api..Api)
* [new Api(base, token)](#new_module_services.api..Api_new)
Expand All @@ -30,30 +29,19 @@ The default export from the module is
[Api](#module_services.api..Api) class.

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

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

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

| Param | Type | Description |
| --- | --- | --- |
| version | <code>String</code> | The version of the API. |
| token | <code>String</code> | Optional. Auth token for Topcoder API v2. |

<a name="module_services.api.getApiV3"></a>

### services.api.getApiV3(token) ⇒ <code>Api</code>
Returns a new or existing Api object for Topcoder API v3

**Kind**: static method of [<code>services.api</code>](#module_services.api)
**Returns**: <code>Api</code> - API v3 service object.

| Param | Type | Description |
| --- | --- | --- |
| token | <code>String</code> | Optional. Auth token for Topcoder API v3. |

<a name="module_services.api..Api"></a>

### services.api~Api
Expand Down
4 changes: 2 additions & 2 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { createActions } from 'redux-actions';
import { decodeToken } from 'tc-accounts';
import { getApiV3 } from '../services/api';
import { getApi } from '../services/api';

/**
* @static
Expand All @@ -16,7 +16,7 @@ import { getApiV3 } from '../services/api';
function loadProfileDone(userTokenV3) {
if (!userTokenV3) return Promise.resolve(null);
const user = decodeToken(userTokenV3);
const api = getApiV3(userTokenV3);
const api = getApi('V3', userTokenV3);
return Promise.all([
api.get(`/members/${user.handle}`)
.then(res => res.json()).then(res => (res.result.status === 200 ? res.result.content : {})),
Expand Down
8 changes: 4 additions & 4 deletions src/actions/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ from 'lodash';
import { config } from 'topcoder-react-utils';
import { createActions } from 'redux-actions';
import { getService as getChallengesService } from '../services/challenges';
import { getApiV2 } from '../services/api';
import { getApi } from '../services/api';

/**
* @static
Expand Down Expand Up @@ -70,7 +70,7 @@ function getSubmissionsInit(challengeId) {
* @return {Action}
*/
function getSubmissionsDone(challengeId, tokenV2) {
return getApiV2(tokenV2)
return getApi('V2', tokenV2)
.fetch(`/challenges/submissions/${challengeId}/mySubmissions`)
.then(response => response.json())
.then(response => ({
Expand Down Expand Up @@ -169,7 +169,7 @@ function loadResultsInit(challengeId) {
* @return {Action}
*/
function loadResultsDone(auth, challengeId, type) {
return getApiV2(auth.tokenV2)
return getApi('V2', auth.tokenV2)
.fetch(`/${type}/challenges/result/${challengeId}`)
.then(response => response.json())
.then(response => ({
Expand All @@ -194,7 +194,7 @@ function fetchCheckpointsInit() {}
*/
function fetchCheckpointsDone(tokenV2, challengeId) {
const endpoint = `/design/challenges/checkpoint/${challengeId}`;
return getApiV2(tokenV2).fetch(endpoint)
return getApi('V2', tokenV2).fetch(endpoint)
.then((response) => {
if (response.status !== 200) {
throw response.status;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/smp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import _ from 'lodash';
import { createActions } from 'redux-actions';
import { getApiV3 } from '../services/api';
import { getApi } from '../services/api';

/**
* @static
Expand All @@ -22,7 +22,7 @@ function deleteSubmissionInit() {}
* @return {Action}
*/
function deleteSubmissionDone(tokenV3, submissionId) {
return getApiV3(tokenV3).delete(`/submissions/${submissionId}`)
return getApi('V3', tokenV3).delete(`/submissions/${submissionId}`)
.then(() => submissionId);
}

Expand Down
44 changes: 20 additions & 24 deletions src/services/__mocks__/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,32 @@ export default class Api {
}
}

/**
* Topcoder API v2.
/*
* Topcoder API
*/
const lastApiInstances = {};

/**
* Returns a new or existing Api object for Topcoder API v2.
* @param {String} token Optional. Auth token for Topcoder API v2.
* @return {Api} API v2 service object.
* Returns a new or existing Api object for Topcoder API.
* @param {String} version The API version.
* @param {String} token Optional. Auth token for Topcoder API.
* @return {Api} API service object.
*/
let lastApiV2 = null;
export function getApiV2(token) {
if (!lastApiV2 || lastApiV2.private.token !== token) {
lastApiV2 = new Api(config.API.V2, token);
export function getApi(version, token) {
if (!version || !config.API[version]) {
throw new Error(`${version} is not a valid API version`);
}
if (!lastApiInstances[version] || lastApiInstances[version].private.token !== token) {
lastApiInstances[version] = new Api(config.API[version], token);
}
return lastApiV2;
return lastApiInstances[version];
}

/**
* Topcoder API v3.
* Keep the old API factories for backwards compatibility
* DO NOT USE THEM FOR NEW IMPLEMENTATIONS.
* USE THE getApi(version, token) FACTORY.
*/

/**
* Returns a new or existing Api object for Topcoder API v3
* @param {String} token Optional. Auth token for Topcoder API v3.
* @return {Api} API v3 service object.
*/
let lastApiV3 = null;
export function getApiV3(token) {
if (!lastApiV3 || lastApiV3.private.token !== token) {
lastApiV3 = new Api(config.API.V3, token);
}
return lastApiV3;
}
export const getApiV2 = token => getApi('V2', token);
export const getApiV3 = token => getApi('V3', token);
export const getApiV4 = token => getApi('V4', token);
6 changes: 3 additions & 3 deletions src/services/__mocks__/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import _ from 'lodash';
import { COMPETITION_TRACKS } from '../../utils/tc';
import { getApiV2, getApiV3 } from './api';
import { getApi } from './api';

import sampleApiV3Response from './data/challenges-v3.json';
import sampleApiV3ResponseSingle from './data/challenge-v3.json';
Expand Down Expand Up @@ -215,8 +215,8 @@ class ChallengesService {
};

this.private = {
api: getApiV3(tokenV3),
apiV2: getApiV2(tokenV2),
api: getApi('V3', tokenV3),
apiV2: getApi('V2', tokenV2),
getChallenges,
tokenV2,
tokenV3,
Expand Down
4 changes: 2 additions & 2 deletions src/services/__mocks__/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import _ from 'lodash';
import { config } from 'topcoder-react-utils';
import { getApiV3 } from './api';
import { getApi } from './api';
import logger from '../../utils/logger';

/* The value of USER_GROUP_MAXAGE constant converted to [ms]. */
Expand Down Expand Up @@ -172,7 +172,7 @@ class GroupService {
*/
constructor(tokenV3) {
this.private = {
api: getApiV3(tokenV3),
api: getApi('V3', tokenV3),
tokenV3,
};
}
Expand Down
53 changes: 18 additions & 35 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,51 +236,34 @@ class Api {
export default Api;

/*
* Topcoder API v2.
* Topcoder API
*/
const lastApiInstances = {};

let lastApiV2 = null;
/**
* Returns a new or existing Api object for Topcoder API v2.
* @param {String} token Optional. Auth token for Topcoder API v2.
* @return {Api} API v2 service object.
* Returns a new or existing Api object for Topcoder API.
* @param {String} version The API version.
* @param {String} token Optional. Auth token for Topcoder API.
* @return {Api} API service object.
*/
export function getApiV2(token) {
if (!lastApiV2 || lastApiV2.private.token !== token) {
lastApiV2 = new Api(config.API.V2, token);
export function getApi(version, token) {
if (!version || !config.API[version]) {
throw new Error(`${version} is not a valid API version`);
}
return lastApiV2;
}

/*
* Topcoder API v3.
*/

let lastApiV3 = null;
/**
* Returns a new or existing Api object for Topcoder API v3
* @param {String} token Optional. Auth token for Topcoder API v3.
* @return {Api} API v3 service object.
*/
export function getApiV3(token) {
if (!lastApiV3 || lastApiV3.private.token !== token) {
lastApiV3 = new Api(config.API.V3, token);
if (!lastApiInstances[version] || lastApiInstances[version].private.token !== token) {
lastApiInstances[version] = new Api(config.API[version], token);
}
return lastApiV3;
return lastApiInstances[version];
}

let lastApiV4 = null;
/**
* Returns a new or existing Api object for Topcoder API V4
* @param {String} token Optional. Auth token for Topcoder API V4.
* @return {Api} API V4 service object.
* Keep the old API factories for backwards compatibility
* DO NOT USE THEM FOR NEW IMPLEMENTATIONS.
* USE THE getApi(version, token) FACTORY.
*/
export function getApiV4(token) {
if (!lastApiV4 || lastApiV4.private.token !== token) {
lastApiV4 = new Api(config.API.V4, token);
}
return lastApiV4;
}
export const getApiV2 = token => getApi('V2', token);
export const getApiV3 = token => getApi('V3', token);
export const getApiV4 = token => getApi('V4', token);

/**
* Gets a valid TC M2M token, either requesting one from TC Auth0 API, or
Expand Down
4 changes: 2 additions & 2 deletions src/services/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module "services.billing"
* @desc Access to Topcoder billing accounts.
*/
import { getApiV3 } from './api';
import { getApi } from './api';

/**
* @static
Expand All @@ -21,7 +21,7 @@ class Billing {
*/
constructor(tokenV3) {
this.private = {
api: getApiV3(tokenV3),
api: getApi('V3', tokenV3),
tokenV3,
};
}
Expand Down
Loading