Failed Conditions
Push — master ( 35e909...51a126 )
by Yo
01:31
created

index.js ➔ ???   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 27.6718

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 60
rs 7.4661
ccs 15
cts 60
cp 0.25
crap 27.6718

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ... ➔ ??? 0 8 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"use strict";
2
3 2
const requestPromise = require('request-promise-native');
4 2
const logger = require('../logger/taskLogger')('Client');
5 2
const Response = require('../model/Response');
6 2
const pkg = require('../../package.json');
7
8 2
const defaultOptionList = {
9
    headers: {'User-Agent': pkg.name},
10
    json: true,
11
    simple: false,
12
    resolveWithFullResponse: true
13
};
14
15
/**
16
 * @param {Request} request
17
 *
18
 * @returns {Promise<Response|Error>}
19
 */
20 2
module.exports = request => {
21
    const optionList = Object.assign(
22
        {},
23
        defaultOptionList,
24
        {
25
            uri: request.getUri(),
26
            method: request.getMethod(),
27
            headers: Object.assign({}, defaultOptionList.headers, request.getHeaders()),
28
            json: request.isJson()
29
        }
30
    );
31
32 7
    if (request.getPayload() && Object.keys(request.getPayload()).length > 0) {
33 1
        optionList.body = request.getPayload();
34
    } else {
35 6
        optionList.body = optionList.json === true ? {} : '';
36
    }
37 7
    if (request.getQueryString() && Object.keys(request.getQueryString()).length > 0) {
38 1
        optionList.qs = request.getQueryString();
39
    } else {
40 6
        optionList.qs = optionList.json === true ? {} : null;
41
    }
42
43 7
    const transformResponseToResponseObject = response => {
44
        return new Response(
45
            response.body,
46
            response.statusCode,
47
            response.headers,
48
            request
49
        );
50
    };
51
52 7
    const transformErrorToResponseObject = error => {
53
        logger.debug('[Error] Conversion to Response.', {options: optionList});
54
55 2
        return Promise.resolve(new Response(
56
            optionList.json === true ? {
57
                error: error.error.code,
58
                message: error.message
59
            } : `${error.error.code} ${error.message}`,
60
            500,
61
            {},
62
            request
63
        ));
64
    };
65
66 7
    const logResponseObject = response => {
67
        logger.debug('[Reponse]', {response: response, options: optionList});
68
69 7
        return response;
70
    };
71
72 7
    logger.debug('[Request]', {options: optionList});
73
74 7
    const transform = requestPromise(optionList)
75
        .then(transformResponseToResponseObject, transformErrorToResponseObject);
76
77 7
    return transform
78
        .then(logResponseObject);
79
};
80