reducing the cyclomatic complexity
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
7684947e41
commit
72642eb14b
|
|
@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js");
|
||||||
|
|
||||||
const JSONParser = require("../../formats/json/parser.js");
|
const JSONParser = require("../../formats/json/parser.js");
|
||||||
|
|
||||||
|
const {
|
||||||
|
isDefinedOrThrow,
|
||||||
|
isStringOrObjectOrThrow
|
||||||
|
} = require("../../utils/fun.js");
|
||||||
|
|
||||||
const parserByType = {};
|
const parserByType = {};
|
||||||
parserByType[Constants.MIME_JSON] = new JSONParser();
|
parserByType[Constants.MIME_JSON] = new JSONParser();
|
||||||
|
|
||||||
|
|
@ -48,20 +53,18 @@ setterByHeader[Constants.HEADER_CONTENT_TYPE] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function validateArgs(payload, attributes) {
|
function validateArgs(payload, attributes) {
|
||||||
if(!payload){
|
|
||||||
throw {message: "payload is null or undefined"};
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!attributes) {
|
Array.of(payload)
|
||||||
throw {message: "attributes is null or undefined"};
|
.filter(p => isDefinedOrThrow(p,
|
||||||
}
|
{message: "payload is null or undefined"}))
|
||||||
|
.filter(p => isStringOrObjectOrThrow(p,
|
||||||
|
{message: "payload must be an object or a string"}))
|
||||||
|
.shift();
|
||||||
|
|
||||||
if((typeof payload) !== "object" && (typeof payload) !== "string"){
|
Array.of(attributes)
|
||||||
throw {
|
.filter(a => isDefinedOrThrow(a,
|
||||||
message: "payload must be an object or a string",
|
{message: "attributes is null or undefined"}))
|
||||||
errors: [typeof payload]
|
.shift();
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Receiver(configuration) {
|
function Receiver(configuration) {
|
||||||
|
|
@ -128,7 +131,7 @@ Receiver.prototype.parse = function(payload, headers) {
|
||||||
parserByType[sanityHeaders[Constants.HEADER_CONTENT_TYPE]]
|
parserByType[sanityHeaders[Constants.HEADER_CONTENT_TYPE]]
|
||||||
.parse(payload);
|
.parse(payload);
|
||||||
|
|
||||||
// Every unprocessed header should be an extension
|
// Every unprocessed header can be an extension
|
||||||
Array.from(Object.keys(sanityHeaders))
|
Array.from(Object.keys(sanityHeaders))
|
||||||
.filter(value => !processedHeaders.includes(value))
|
.filter(value => !processedHeaders.includes(value))
|
||||||
.filter(value =>
|
.filter(value =>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,11 @@ const Spec02 = require("../../specs/spec_0_2.js");
|
||||||
|
|
||||||
var JSONParser = require("../../formats/json/parser.js");
|
var JSONParser = require("../../formats/json/parser.js");
|
||||||
|
|
||||||
|
const {
|
||||||
|
isDefinedOrThrow,
|
||||||
|
isStringOrObjectOrThrow
|
||||||
|
} = require("../../utils/fun.js");
|
||||||
|
|
||||||
const spec02 = new Spec02();
|
const spec02 = new Spec02();
|
||||||
const jsonParserSpec02 = new JSONParser();
|
const jsonParserSpec02 = new JSONParser();
|
||||||
|
|
||||||
|
|
@ -50,20 +55,18 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_02.DATA] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function validateArgs(payload, attributes) {
|
function validateArgs(payload, attributes) {
|
||||||
if(!payload){
|
|
||||||
throw {message: "payload is null or undefined"};
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!attributes) {
|
Array.of(payload)
|
||||||
throw {message: "attributes is null or undefined"};
|
.filter(p => isDefinedOrThrow(p,
|
||||||
}
|
{message: "payload is null or undefined"}))
|
||||||
|
.filter(p => isStringOrObjectOrThrow(p,
|
||||||
|
{message: "payload must be an object or string"}))
|
||||||
|
.shift();
|
||||||
|
|
||||||
if((typeof payload) !== "object" && (typeof payload) !== "string"){
|
Array.of(attributes)
|
||||||
throw {
|
.filter(a => isDefinedOrThrow(a,
|
||||||
message: "payload must be an object or string",
|
{message: "attributes is null or undefined"}))
|
||||||
errors: [typeof payload]
|
.shift();
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Receiver(configuration) {
|
function Receiver(configuration) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue