Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
matteofigus committed Feb 11, 2016
1 parent e9f4867 commit be5c8b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
21 changes: 9 additions & 12 deletions client/src/components-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(config, renderTemplate){
getOCClientScript = new GetOCClientScript(cache),
getCompiledTemplate = new GetCompiledTemplate(cache),
buildHref = new HrefBuilder(config),
errorDescription = settings.messages.serverSideRenderingFail;
serverRenderingFail = settings.serverSideRenderingFail;

return function(components, options, callback){

Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = function(config, renderTemplate){
return cb();
} else if(!config.registries.serverRendering){
_.each(toDo, function(action){
action.result.error = errorDescription;
action.result.error = serverRenderingFail;
if(!!options.disableFailoverRendering){
action.result.html = '';
action.done = true;
Expand All @@ -76,7 +76,7 @@ module.exports = function(config, renderTemplate){
}
});

return cb(errorDescription);
return cb(serverRenderingFail);
}

makePostRequest(serverRendering.components, function(error, responses){
Expand All @@ -92,7 +92,7 @@ module.exports = function(config, renderTemplate){

if(action.render === 'server'){
if(response.status !== 200){
action.result.error = errorDescription;
action.result.error = serverRenderingFail;
if(!!options.disableFailoverRendering){
action.result.html = '';
action.done = true;
Expand All @@ -111,12 +111,9 @@ module.exports = function(config, renderTemplate){

var fetchTemplateAndRender = function(component, cb){

var isLocal = function(apiResponse){
return apiResponse.type === 'oc-component-local';
};

var data = component.data,
useCache = !isLocal(component);
isLocal = component.type === 'oc-component-local',
useCache = !isLocal;

getCompiledTemplate(component.template, useCache, options.timeout, function(err, template){
if(!!err){ return cb(err); }
Expand Down Expand Up @@ -152,7 +149,7 @@ module.exports = function(config, renderTemplate){
_.eachAsync(toRender, function(action, next){
fetchTemplateAndRender(action.apiResponse, function(err, html){
if(!!err){
action.result.error = errorDescription;
action.result.error = serverRenderingFail;
if(!!options.disableFailoverRendering){
action.result.html = '';
action.done = true;
Expand Down Expand Up @@ -186,13 +183,13 @@ module.exports = function(config, renderTemplate){
_.each(toDo, function(action){
if(action.render === 'client'){
if(!!clientErr || !clientJs){
action.result.error = 'Internal client error';
action.result.error = settings.genericError;
action.result.html = '';
} else {
var componentClientHref = buildHref.client(action.component);

if(!componentClientHref){
action.result.error = 'Client-side rendering failed.';
action.result.error = settings.clientSideRenderingFail;
action.result.html = '';
} else {
var unrenderedComponentTag = htmlRenderer.unrenderedComponent(componentClientHref, options);
Expand Down
11 changes: 6 additions & 5 deletions client/src/settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

module.exports = {
messages: {
componentMissing: 'Configuration is not valid - Component not found',
registryUrlMissing: 'Configuration is not valid - Registry location not found',
serverSideRenderingFail: 'Server-side rendering failed'
}
clientSideRenderingFail: 'Client-side rendering failed',
configurationNotValid: 'Configuration is not valid: ',
genericError: 'Internal client error',
registriesEmpty: 'registries must contain at least one endpoint',
registriesIsNotObject: 'registries must be an object',
serverSideRenderingFail: 'Server-side rendering failed'
};
7 changes: 4 additions & 3 deletions client/src/validator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict';

var settings = require('./settings');
var _ = require('./utils/helpers');

module.exports = {
validateConfiguration: function(conf){
var errorMessage = function(msg){
return {
isValid: false,
error: 'Configuration is not valid: ' + msg
error: settings.configurationNotValid + msg
};
};

if(!!conf.registries && _.isArray(conf.registries)){
return errorMessage('registries must be an object');
return errorMessage(settings.registriesIsNotObject);
} else if(!!conf.registries && !conf.registries.serverRendering && !conf.clientRendering){
return errorMessage('registries must contain at least one endpoint');
return errorMessage(settings.registriesEmpty);
}

return { isValid: true };
Expand Down

0 comments on commit be5c8b5

Please sign in to comment.