@aarconada/urserver
Version:
Basic Server definitions to develope REST API with a node + express Server
240 lines (220 loc) • 6.85 kB
JavaScript
/**
* Created by ubuntu on 16/08/18.
*/
;
const i18n = require('i18n');
const _ = require('lodash');
const response = require('./response');
function initializeConfiguration(defaultConfiguration, customConfiguration) {
const resultOptions = defaultConfiguration;
if(customConfiguration) {
Object.keys(resultOptions).forEach(currentProperty => {
if(customConfiguration[currentProperty]) {
resultOptions[currentProperty] = customConfiguration[currentProperty];
}
});
}
return resultOptions;
}
module.exports.documentation = function(margin, customResponse) {
var result = '';
if(customResponse === null) {
result += 'null'
} else if(customResponse instanceof Array) {
result += '[';
if(customResponse.length > 0) {
result += '<br/>';
}
var index = 0;
customResponse.forEach(currentItem => {
result += '<span style="margin-left:' + (margin + 1) + 'em"></span>';
result += module.exports.documentation((margin + 1), currentItem);
index++;
if(index<customResponse.length) {
result += '<br/>';
}
});
if(customResponse.length > 0) {
result += ',<br/><span style="margin-left:' + (margin + 1) + 'em">...</span><br/>';
result += '<span style="margin-left:' + margin + 'em">]</span>';
} else {
result += '...]';
}
} else {
result += '{<br/>';
var index = 0;
Object.keys(customResponse).sort((a, b) => {
return a > b ? 1 : 0;
}).forEach(property => {
index++;
result += '<span style="margin-left:' + (margin + 1) + 'em">' + property + ' : ';
if(customResponse[property] === null) {
if(index < Object.keys(customResponse).length) {
result += 'null,<br/>';
} else {
result += 'null<br/>';
}
} else if(customResponse[property] instanceof Array) {
result += module.exports.documentation((margin + 1), customResponse[property]);
if(index < Object.keys(customResponse).length) {
result += ',<br/>';
} else {
result += '<br/>';
}
} else if (typeof customResponse[property] === 'object') {
//result += '<br/>';
result += module.exports.documentation((margin + 1), customResponse[property]);
if(index < Object.keys(customResponse).length) {
result += ',<br/>';
} else {
result += '<br/>';
}
} else {
if (typeof customResponse[property] === 'string') {
if(index < Object.keys(customResponse).length) {
result += '"' + customResponse[property] + '",<br/>';
} else {
result += '"' + customResponse[property] + '"<br/>';
}
} else {
if(index < Object.keys(customResponse).length) {
result += customResponse[property] + ',<br/>';
} else {
result += customResponse[property] + '<br/>';
}
}
}
result += '</span>';
});
result += '<span style="margin-left:' + margin + 'em">}</span>';
}
return result;
};
module.exports.sendCustomResponse = function(res, httpstatus, customResponse, data) {
return res.status(httpstatus).send({
code : customResponse.code,
message : i18n.__(customResponse.message),
data : data
});
};
module.exports.sendCustomErrorMessage = function(res, customResponse) {
return this.sendCustomResponse(res, 200, customResponse, null);
};
module.exports.sendHTTPErrorMessage = function(res, httpstatus, errormessage) {
return res.status(httpstatus).send(errormessage);
};
module.exports.sendOk = function(res, data) {
return this.sendCustomResponse(res, 200, response.success, data);
};
module.exports.sendHtml = function(res, HTMLCode) {
return res.status(200).send(HTMLCode);
};
module.exports.initializeConfiguration = initializeConfiguration;
module.exports.method = {
UNDEFINED : {
name : 'Undefined'
},
DELETE : {
name : 'Delete'
},
GET : {
name : 'Get'
},
POST : {
name : 'Post'
},
PUT : {
name : 'Put'
},
OPTIONS : {
name : 'Options'
}
};
module.exports.parameterType = {
QUERY : {
name: 'Query'
},
BODY : {
name: 'Body'
},
FILE : {
name: 'File'
},
PARAM : {
name: 'Param'
},
HEADER : {
name: 'Header'
}
};
module.exports.dataType = {
STRING : {
name: 'String'
},
INTEGER : {
name: 'Integer'
},
DATE : {
name: 'Date'
},
FLOAT : {
name: 'Float'
},
BOOLEAN : {
name: 'Boolean'
},
JSON : {
name: 'JSON'
}
};
module.exports.valueInList = function(list, key) {
const result = Object.keys(list).find(currentKey => currentKey === key);
return result && result !== null;
};
module.exports.throwError = function(ex, previousEx) {
if(previousEx && previousEx.code && previousEx.message) {
throw previousEx;
} else {
throw ex;
}
};
module.exports.isJSON = function(value) {
try {
JSON.parse(value);
} catch(e) {
return false;
}
return true;
};
module.exports.isBoolean = function(value) {
return !_.isUndefined(value) && (
_.isBoolean(value) ||
value === 'true' ||
value === 'false' ||
+value === 1 ||
+value === 0);
};
module.exports.parseBoolean = function(value) {
if(_.isBoolean(value))
return Boolean(value);
else
return value === 'true' || +value === 1;
};
const contains = function(container, filter) {
if (!container) return false;
const filterKeys = Object.keys(filter);
for (var i = 0; i < filterKeys.length; i++) {
var currentKey = filterKeys[i];
if (typeof filter[currentKey] === 'object') {
if (!contains(container[currentKey], filter[currentKey]))
return false;
} else {
if (container[currentKey] !== filter[currentKey])
return false;
}
}
return true;
};
module.exports.contains = contains;
module.exports.log = function() {
};