Skip to content

Commit

Permalink
refactor(services): discovery-8 minor config restructure (quipucords#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Jun 23, 2022
1 parent 6d89da0 commit 1224494
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 211 deletions.
9 changes: 9 additions & 0 deletions src/services/__tests__/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ServiceConfig should have specific properties and methods: specific props and methods 1`] = `
Array [
"serviceCall",
"serviceConfig",
"authHeader",
]
`;
12 changes: 6 additions & 6 deletions src/services/__tests__/config.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import cookies from 'js-cookie';
import * as service from '../config';
import { config } from '../config';

describe('ServiceConfig', () => {
it('should export a specific number of methods and classes', () => {
expect(Object.keys(service)).toHaveLength(2);
it('should have specific properties and methods', () => {
expect(Object.keys(config)).toMatchSnapshot('specific props and methods');
});

it('should export a default services config', () => {
expect(service.serviceConfig).toBeDefined();
expect(config.serviceConfig).toBeDefined();

cookies.set(process.env.REACT_APP_AUTH_TOKEN, 'spoof');

const configObject = service.serviceConfig(
const configObject = config.serviceConfig(
{
method: 'post',
timeout: 3
Expand All @@ -24,7 +24,7 @@ describe('ServiceConfig', () => {
});

it('should export a default services config without authorization', () => {
const configObject = service.serviceConfig({}, false);
const configObject = config.serviceConfig({}, false);

expect(configObject.headers[process.env.REACT_APP_AUTH_HEADER]).toBeUndefined();
});
Expand Down
37 changes: 34 additions & 3 deletions src/services/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import axios from 'axios';
import cookies from 'js-cookie';

/**
* Set Axios XHR default timeout.
*/
const globalXhrTimeout = Number.parseInt(process.env.REACT_APP_AJAX_TIMEOUT, 10) || 60000;

/**
* Return a formatted auth header.
*
* @return {{}}
*/
const authHeader = () => {
const authToken = cookies.get(process.env.REACT_APP_AUTH_TOKEN) || '';

Expand All @@ -12,10 +23,30 @@ const authHeader = () => {
};
};

const serviceConfig = (passedConfig = {}, auth = true) => ({
/**
* Apply custom service config.
*
* @param {object} passedConfig
* @param {object} options
* @param {boolean} options.auth
* @param {number} options.xhrTimeout
* @returns {{headers: *}}
*/
const serviceConfig = (passedConfig = {}, { auth = true, xhrTimeout = globalXhrTimeout } = {}) => ({
headers: auth ? authHeader() : {},
timeout: process.env.REACT_APP_AJAX_TIMEOUT,
timeout: xhrTimeout,
...passedConfig
});

export { serviceConfig as default, serviceConfig };
/**
* Use a global Axios configuration.
*
* @param {object} config
* @param {object} options
* @returns {Promise<*>}
*/
const serviceCall = async (config, options) => axios(serviceConfig(config, options));

const config = { serviceCall, globalXhrTimeout, serviceConfig, authHeader };

export { config as default, config, authHeader, globalXhrTimeout, serviceCall, serviceConfig };
51 changes: 21 additions & 30 deletions src/services/credentialsService.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
import axios from 'axios';
import serviceConfig from './config';
import { serviceCall } from './config';

const addCredential = (data = {}) =>
axios(
serviceConfig({
method: 'post',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}`,
data
})
);
serviceCall({
method: 'post',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}`,
data
});

const deleteCredential = id =>
axios(
serviceConfig({
method: 'delete',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}/`
})
);
serviceCall({
method: 'delete',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}/`
});

const deleteCredentials = (data = []) =>
Promise.all(data.map(id => deleteCredential(id))).then(success => new Promise(resolve => resolve({ data: success })));

const getCredentials = (id = '', params = {}) =>
axios(
serviceConfig(
{
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}`,
params
},
false
)
serviceCall(
{
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}`,
params
},
{ auth: false }
);

const getCredential = id => getCredentials(id);

const updateCredential = (id, data = {}) =>
axios(
serviceConfig({
method: 'put',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}/`,
data
})
);
serviceCall({
method: 'put',
url: `${process.env.REACT_APP_CREDENTIALS_SERVICE}${id}/`,
data
});

const credentialsService = {
addCredential,
Expand Down
15 changes: 6 additions & 9 deletions src/services/factsService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import axios from 'axios';
import serviceConfig from './config';
import { serviceCall } from './config';

const addFacts = (data = {}) =>
axios(
serviceConfig({
method: 'post',
url: `${process.env.REACT_APP_FACTS_SERVICE}`,
data
})
);
serviceCall({
method: 'post',
url: `${process.env.REACT_APP_FACTS_SERVICE}`,
data
});

const factsService = {
addFacts
Expand Down
29 changes: 29 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { credentialsService } from './credentialsService';
import { factsService } from './factsService';
import { reportsService } from './reportsService';
import { scansService } from './scansService';
import { sourcesService } from './sourcesService';
import { statusService } from './statusService';
import { userService } from './userService';

const services = {
credentials: credentialsService,
facts: factsService,
reports: reportsService,
scans: scansService,
sources: sourcesService,
status: statusService,
user: userService
};

export {
services as default,
services,
credentialsService,
factsService,
reportsService,
scansService,
sourcesService,
statusService,
userService
};
29 changes: 12 additions & 17 deletions src/services/reportsService.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import axios from 'axios';
import serviceConfig from './config';
import { serviceCall } from './config';
import helpers from '../common/helpers';

const getReportsDownload = id =>
axios(
serviceConfig(
{
url: `${process.env.REACT_APP_REPORTS_SERVICE}${id}/`,
responseType: 'blob'
},
false
)
serviceCall(
{
url: `${process.env.REACT_APP_REPORTS_SERVICE}${id}/`,
responseType: 'blob'
},
{ auth: false }
).then(
success =>
(helpers.TEST_MODE && success.data) ||
Expand All @@ -22,13 +19,11 @@ const getReportsDownload = id =>
);

const mergeReports = (data = {}) =>
axios(
serviceConfig({
method: 'put',
url: process.env.REACT_APP_REPORTS_SERVICE_MERGE,
data
})
);
serviceCall({
method: 'put',
url: process.env.REACT_APP_REPORTS_SERVICE_MERGE,
data
});

const reportsService = {
getReportsDownload,
Expand Down
Loading

0 comments on commit 1224494

Please sign in to comment.