Skip to content

Commit

Permalink
refactor(build,parseApi): minor syntax updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Oct 16, 2022
1 parent c1331fb commit 9c584de
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 83 deletions.
67 changes: 31 additions & 36 deletions src/api/buildApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ const buildResponse = (apiJson = []) => {
const appResponses = [];
let routesLoaded = 0;

apiJson.forEach(value => {
apiJson.forEach((value = {}) => {
const { error = {}, header, success = {}, type, url } = value;
try {
const successExamples = (value.success && value.success.examples) || [];
const errorExamples = (value.error && value.error.examples) || [];
const successExamples = success?.examples || [];
const errorExamples = error?.examples || [];
const mockSettings = parseCustomMockSettings(value);
const successObjects = parseStatus(successExamples, 'success', value.type, value.url);
const errorObjects = parseStatus(errorExamples, 'error', value.type, value.url);
const successObjects = parseStatus(successExamples, 'success', type, url);
const errorObjects = parseStatus(errorExamples, 'error', type, url);
const authExample = parseAuthExample(errorObjects);
const exampleObjects = successObjects.concat(errorObjects);

Expand All @@ -61,8 +62,8 @@ const buildResponse = (apiJson = []) => {
}

appResponses.push({
type: value.type,
url: value.url,
type,
url,
callback: (request, response) => {
if (mockSettings.reload) {
example = exampleResponse(mockSettings, exampleObjects, successObjects, errorObjects);
Expand All @@ -73,51 +74,45 @@ const buildResponse = (apiJson = []) => {

response.set('Cache-Control', 'no-cache');

if (httpStatus < 500) {
if (value.header && value.header.fields.Header && value.header.fields.Header.length) {
for (let i = 0; i < value.header.fields.Header.length; i++) {
const headerValue = value.header.fields.Header[i];
if (httpStatus < 500 && Array.isArray(header?.fields?.Header)) {
header?.fields?.Header?.forEach(headerValue => {
if (
!headerValue.optional &&
headerValue.field &&
/authorization/i.test(headerValue.field)
) {
const authorization = request.get('authorization');

if (
!headerValue.optional &&
headerValue.field &&
/authorization/i.test(headerValue.field)
) {
const authorization = request.get('authorization');
if (!authorization) {
const authObj = parseContentAndType(authExample.content, authExample.type);

if (!authorization) {
const authObj = parseContentAndType(authExample.content, authExample.type);
response.append('WWW-Authenticate', 'Spoof response');
response.status(401);
response.set('Content-Type', authObj.type);

response.append('WWW-Authenticate', 'Spoof response');
response.status(401);
response.set('Content-Type', authObj.type);

if (mockSettings.delay > 0) {
logger.info(`waiting\t:401 :${value.type} :${value.url}`);
}

setTimeout(() => {
logger.info(`response\t:401 :${value.type} :${value.url}`);
if (mockSettings.delay > 0) {
logger.info(`waiting\t:401 :${type} :${url}`);
}

response.end(authObj.content || 'Authorization Required');
}, mockSettings.delay || 0);
setTimeout(() => {
logger.info(`response\t:401 :${type} :${url}`);

return;
}
response.end(authObj.content || 'Authorization Required');
}, mockSettings.delay || 0);
}
}
}
});
}

response.set('Content-Type', type);
response.status(httpStatus);

if (mockSettings.delay > 0) {
logger.info(`waiting\t:${httpStatus} :${value.type} :${value.url}`);
logger.info(`waiting\t:${httpStatus} :${type} :${url}`);
}

setTimeout(() => {
logger.info(`response\t:${httpStatus} :${value.type} :${value.url}`);
logger.info(`response\t:${httpStatus} :${type} :${url}`);

response.send(content);
}, mockSettings.delay || 0);
Expand Down
88 changes: 43 additions & 45 deletions src/api/parseApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,53 +89,51 @@ const parseAuthExample = (errorObjects = []) => {
* @param {object} params.mock
* @returns {object}
*/
const parseCustomMockSettings = ({ mock = null }) => {
const parseCustomMockSettings = ({ mock = null } = {}) => {
const settings = {};

if (mock && mock.settings && mock.settings.length) {
mock.settings.forEach(val => {
const keys = Object.keys(val);
const key = keys[0] || '';

switch (key.toLowerCase()) {
case 'delay':
case 'delayresponse':
settings.delay = Number.parseInt(val[key], 10);

if (Number.isNaN(settings.delay)) {
settings.delay = 1000;
}

break;
case 'force':
case 'forcestatus':
case 'forcedstatus':
settings.forceStatus = Number.parseInt(val[key], 10);

if (Number.isNaN(settings.forceStatus)) {
settings.forceStatus = 200;
}

break;
case 'response':
settings.response = 'response';
break;
case 'random':
case 'randomresponse':
settings.response = 'response';
settings.reload = true;
break;
case 'randomsuccess':
settings.response = 'success';
settings.reload = true;
break;
case 'randomerror':
settings.response = 'error';
settings.reload = true;
break;
}
});
}
mock?.settings?.forEach(val => {
const keys = Object.keys(val);
const key = keys[0] || '';

switch (key.toLowerCase()) {
case 'delay':
case 'delayresponse':
settings.delay = Number.parseInt(val[key], 10);

if (Number.isNaN(settings.delay)) {
settings.delay = 1000;
}

break;
case 'force':
case 'forcestatus':
case 'forcedstatus':
settings.forceStatus = Number.parseInt(val[key], 10);

if (Number.isNaN(settings.forceStatus)) {
settings.forceStatus = 200;
}

break;
case 'response':
settings.response = 'response';
break;
case 'random':
case 'randomresponse':
settings.response = 'response';
settings.reload = true;
break;
case 'randomsuccess':
settings.response = 'success';
settings.reload = true;
break;
case 'randomerror':
settings.response = 'error';
settings.reload = true;
break;
}
});

return settings;
};
Expand Down
2 changes: 1 addition & 1 deletion src/docs/buildDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { logger } = require('../logger/configLogger');
* @param {string} params.apiJsonFile
* @returns {object}
*/
const buildDocs = ({ apiDocsConfig = null, apiJsonFile = null }) => {
const buildDocs = ({ apiDocsConfig = null, apiJsonFile = null } = {}) => {
let result;

if (apiDocsConfig) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const setupResponse = (apiJson = [], port) => {
* @param {string} params.docsPath
* @returns {*}
*/
const apiDocMock = ({ port = 8000, dataPath, docsPath = '.docs' }) => {
const apiDocMock = ({ port = 8000, dataPath, docsPath = '.docs' } = {}) => {
const apiJson = setupDocs(dataPath, docsPath);
let server = null;

Expand Down

0 comments on commit 9c584de

Please sign in to comment.