express-sweet-generator 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import * as expressExtension from 'express-sweet';
2
2
  import {Router} from 'express';
3
3
  import {query, body, validationResult} from 'express-validator';
4
4
  import UserModel from '../../models/UserModel.js';
5
- import UserNotFound from '../../exceptions/UserNotFound.js';
5
+ import NotFoundError from '../../errors/NotFoundError.js';
6
6
  import * as CustomValidation from '../../shared/CustomValidation.js';
7
7
 
8
8
  const router = Router();
@@ -11,9 +11,9 @@ router.post('/login', [
11
11
  body('email').trim().not().isEmpty().isEmail(),
12
12
  body('password').trim().not().isEmpty()
13
13
  ], async (req, res, next) => {
14
- const errs = validationResult(req);
15
- if (!errs.isEmpty())
16
- return void res.status(400).json({errors: errs.array()});
14
+ const result = validationResult(req);
15
+ if (!result.isEmpty())
16
+ return void res.status(400).json({errors: result.array()});
17
17
  const isAuthenticated = await expressExtension.services.Authentication.authenticate(req, res, next);
18
18
  res.json(isAuthenticated);
19
19
  });
@@ -31,9 +31,9 @@ router.get('/', [
31
31
  query('dir').optional({nullable: true, checkFalsy: true}).isIn(['asc', 'desc']),
32
32
  query('search').trim().optional({nullable: true, checkFalsy: true}).trim()
33
33
  ], async (req, res) => {
34
- const errs = validationResult(req);
35
- if (!errs.isEmpty())
36
- return void res.status(400).json({errors: errs.array()});
34
+ const result = validationResult(req);
35
+ if (!result.isEmpty())
36
+ return void res.status(400).json({errors: result.array()});
37
37
  const data = await UserModel.paginate(req.query);
38
38
  data.draw = req.query.draw;
39
39
  res.json(data);
@@ -43,9 +43,9 @@ router.get('/email-exists', [
43
43
  query('user.email').trim().not().isEmpty(),
44
44
  query('excludeUserId').optional({nullable: true, checkFalsy: true}).isInt({min: 1})
45
45
  ], async (req, res) => {
46
- const errs = validationResult(req);
47
- if (!errs.isEmpty())
48
- return void res.status(400).json({errors: errs.array()});
46
+ const result = validationResult(req);
47
+ if (!result.isEmpty())
48
+ return void res.status(400).json({errors: result.array()});
49
49
  const emailExists = await UserModel.emailExists(req.query.user.email, req.query.excludeUserId || null);
50
50
  res.json({valid: !emailExists});
51
51
  });
@@ -56,9 +56,9 @@ router.post('/', [
56
56
  body('user.password').trim().not().isEmpty().isLength({max: 128}),
57
57
  body('user.icon').not().isEmpty().custom(CustomValidation.isImageDataUrl)
58
58
  ], async (req, res) => {
59
- const errs = validationResult(req);
60
- if (!errs.isEmpty())
61
- return void res.status(400).json({errors: errs.array()});
59
+ const result = validationResult(req);
60
+ if (!result.isEmpty())
61
+ return void res.status(400).json({errors: result.array()});
62
62
  await UserModel.createUser(req.body.user);
63
63
  res.json(true);
64
64
  });
@@ -75,16 +75,16 @@ router.put('/:userId(\\d+)', [
75
75
  body('user.icon').not().isEmpty().custom(CustomValidation.isImageDataUrl)
76
76
  ], async (req, res, next) => {
77
77
  try {
78
- const errs = validationResult(req);
79
- if (!errs.isEmpty())
80
- return void res.status(400).json({errors: errs.array()});
78
+ const result = validationResult(req);
79
+ if (!result.isEmpty())
80
+ return void res.status(400).json({errors: result.array()});
81
81
  await UserModel.updateUser(req.params.userId, req.body.user);
82
82
  res.json(true);
83
- } catch (err) {
84
- if (err instanceof UserNotFound)
85
- res.json({error: err.name});
83
+ } catch (error) {
84
+ if (error instanceof NotFoundError)
85
+ res.json({error: error.name});
86
86
  else
87
- next(err);
87
+ next(error);
88
88
  }
89
89
  });
90
90
 
@@ -100,16 +100,16 @@ router.put('/profile', [
100
100
  body('user.icon').not().isEmpty().custom(CustomValidation.isImageDataUrl)
101
101
  ], async (req, res, next) => {
102
102
  try {
103
- const errs = validationResult(req);
104
- if (!errs.isEmpty())
105
- return void res.status(400).json({errors: errs.array()});
103
+ const result = validationResult(req);
104
+ if (!result.isEmpty())
105
+ return void res.status(400).json({errors: result.array()});
106
106
  await UserModel.updateUser(req.user.id, req.body.user);
107
107
  res.json(true);
108
- } catch (err) {
109
- if (err instanceof UserNotFound)
110
- res.json({error: err.name});
108
+ } catch (error) {
109
+ if (error instanceof NotFoundError)
110
+ res.json({error: error.name});
111
111
  else
112
- next(err);
112
+ next(error);
113
113
  }
114
114
  });
115
115
 
@@ -1,6 +0,0 @@
1
- module.exports = class extends Error {
2
- constructor() {
3
- super('User not found');
4
- this.name = 'UserNotFound';
5
- }
6
- }
@@ -1,6 +0,0 @@
1
- export default class extends Error {
2
- constructor() {
3
- super('User not found');
4
- this.name = 'UserNotFound';
5
- }
6
- }