Passed
Push — master ( 316078...d4e34d )
by Dmytro
02:03 queued 11s
created

src/controllers/express/middlewares.js   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 34
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 24
mnd 1
bc 1
fnc 4
dl 0
loc 34
bpm 0.25
cpm 1.25
noi 0
c 0
b 0
f 0
rs 10
1
import cors       from 'cors';
2
import bodyParser from 'body-parser';
3
4
export default {
5
    json : bodyParser.json({
6
        limit  : 1024 * 1024,
7
        type   : [ 'txt', 'json' ],
8
        verify : (req, res, buf) => {
9
            try {
10
                JSON.parse(buf);
11
            } catch {
12
                res.send({
13
                    status : 0,
14
                    error  : {
15
                        code    : 'FORMAT_ERROR',
16
                        message : 'BROKEN_JSON'
17
                    }
18
                });
19
                throw new Error('BROKEN_JSON');
20
            }
21
        }
22
    }),
23
    arrays(req, res, next) {
24
        const keys = Object.keys(req.query);
25
26
        keys
27
            .filter(key => req.query[key].includes(','))
28
            // eslint-disable-next-line no-param-reassign
29
            .forEach(key => req.query[key] = req.query[key].split(','));
30
31
        return next();
32
    },
33
    urlencoded : bodyParser.urlencoded({ extended: true }),
34
    cors       : cors({ origin: '*' })
35
};
36