From 824ad7d180acc3c07409dc59f10deffb20bd3369 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sat, 10 Mar 2018 23:45:56 +0100 Subject: [PATCH] refactor: middleware use es6 --- src/api/web/middleware.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/api/web/middleware.js b/src/api/web/middleware.js index 329a87800..ddf6b3e4a 100644 --- a/src/api/web/middleware.js +++ b/src/api/web/middleware.js @@ -1,16 +1,13 @@ -/* eslint prefer-rest-params: "off" */ - import crypto from 'crypto'; import _ from 'lodash'; import { validate_name as utilValidateName, validate_package as utilValidatePackage, - isObject} from '../../lib/utils'; + isObject, + ErrorCode} from '../../lib/utils'; -const createError = require('http-errors'); const Logger = require('../../lib/logger'); - export function match(regexp) { return function(req, res, next, value) { if (regexp.exec(value)) { @@ -34,7 +31,7 @@ export function validate_name(req, res, next, value, name) { } else if (utilValidateName(value)) { next(); } else { - next( createError[403]('invalid ' + name) ); + next( ErrorCode.get403('invalid ' + name)); } } @@ -45,14 +42,14 @@ export function validatePackage(req, res, next, value, name) { } else if (utilValidatePackage(value)) { next(); } else { - next( createError[403]('invalid ' + name) ); + next( ErrorCode.get403('invalid ' + name) ); } } export function media(expect) { return function(req, res, next) { if (req.headers['content-type'] !== expect) { - next( createError[415]('wrong content-type, expect: ' + expect + next( ErrorCode.getCode(415, 'wrong content-type, expect: ' + expect + ', got: '+req.headers['content-type']) ); } else { next(); @@ -70,7 +67,7 @@ export function encodeScopePackage(req, res, next) { export function expect_json(req, res, next) { if (!isObject(req.body)) { - return next( createError[400]('can\'t parse incoming json') ); + return next( ErrorCode.get400('can\'t parse incoming json') ); } next(); } @@ -83,7 +80,7 @@ export function anti_loop(config) { for (let i=0; i