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.
- package/README.md +8 -8
- package/bin/express-sweet-cli.js +4 -4
- package/package.json +1 -1
- package/templates/cjs/config/config.js +6 -6
- package/templates/cjs/errors/NotFoundError.js +14 -0
- package/templates/cjs/models/UserModel.js +6 -6
- package/templates/cjs/public/build/editPersonal.js +105 -105
- package/templates/cjs/public/build/login.js +7 -7
- package/templates/cjs/public/build/users.js +198 -189
- package/templates/cjs/routes/api/users.js +27 -27
- package/templates/client/src/api/UserApi.js +1 -1
- package/templates/client/src/editPersonal.js +3 -3
- package/templates/client/src/login.js +2 -2
- package/templates/client/src/modals/UserModal.js +5 -5
- package/templates/client/src/shared/Api.js +4 -4
- package/templates/client/src/shared/reloadDataTableWithDelay.js +12 -0
- package/templates/client/src/users.js +4 -8
- package/templates/esm/config/config.js +6 -6
- package/templates/esm/errors/NotFoundError.js +14 -0
- package/templates/esm/models/UserModel.js +6 -6
- package/templates/esm/public/build/editPersonal.js +105 -105
- package/templates/esm/public/build/login.js +7 -7
- package/templates/esm/public/build/users.js +198 -189
- package/templates/esm/routes/api/users.js +27 -27
- package/templates/cjs/exceptions/UserNotFound.js +0 -6
- package/templates/esm/exceptions/UserNotFound.js +0 -6
|
@@ -2,7 +2,7 @@ const express = require('express');
|
|
|
2
2
|
const expressExtension = require('express-sweet');
|
|
3
3
|
const {query, body, validationResult} = require('express-validator');
|
|
4
4
|
const UserModel = require('../../models/UserModel');
|
|
5
|
-
const
|
|
5
|
+
const NotFoundError = require('../../errors/NotFoundError');
|
|
6
6
|
const CustomValidation = require('../../shared/CustomValidation');
|
|
7
7
|
|
|
8
8
|
const router = express.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
|
|
15
|
-
if (!
|
|
16
|
-
return void res.status(400).json({errors:
|
|
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
|
|
35
|
-
if (!
|
|
36
|
-
return void res.status(400).json({errors:
|
|
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
|
|
47
|
-
if (!
|
|
48
|
-
return void res.status(400).json({errors:
|
|
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
|
|
60
|
-
if (!
|
|
61
|
-
return void res.status(400).json({errors:
|
|
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
|
|
79
|
-
if (!
|
|
80
|
-
return void res.status(400).json({errors:
|
|
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 (
|
|
84
|
-
if (
|
|
85
|
-
res.json({error:
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof NotFoundError)
|
|
85
|
+
res.json({error: error.name});
|
|
86
86
|
else
|
|
87
|
-
next(
|
|
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
|
|
104
|
-
if (!
|
|
105
|
-
return void res.status(400).json({errors:
|
|
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 (
|
|
109
|
-
if (
|
|
110
|
-
res.json({error:
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (error instanceof NotFoundError)
|
|
110
|
+
res.json({error: error.name});
|
|
111
111
|
else
|
|
112
|
-
next(
|
|
112
|
+
next(error);
|
|
113
113
|
}
|
|
114
114
|
});
|
|
115
115
|
|
|
@@ -50,7 +50,7 @@ function initForm() {
|
|
|
50
50
|
const {data} = await userApi.updateProfile(new FormData(validation.form));
|
|
51
51
|
validation.offIndicator();
|
|
52
52
|
if (data.error)
|
|
53
|
-
if (data.error === '
|
|
53
|
+
if (data.error === 'NotFoundError') {
|
|
54
54
|
await components.Dialog.warning('The account you are using has been deleted and you will be forcibly logged out.');
|
|
55
55
|
return void userApi.logout();
|
|
56
56
|
} else
|
|
@@ -61,10 +61,10 @@ function initForm() {
|
|
|
61
61
|
await components.Dialog.info('Email address has been changed. Please re-login.');
|
|
62
62
|
return void userApi.logout();
|
|
63
63
|
}
|
|
64
|
-
} catch (
|
|
64
|
+
} catch (error) {
|
|
65
65
|
validation.offIndicator();
|
|
66
66
|
components.Dialog.unknownError();
|
|
67
|
-
throw
|
|
67
|
+
throw error;
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
ref.personalForm
|
|
@@ -28,10 +28,10 @@ function initForm() {
|
|
|
28
28
|
if (!data)
|
|
29
29
|
return void validation.setError('email', 'userNotFound');
|
|
30
30
|
location.href = '/';
|
|
31
|
-
} catch (
|
|
31
|
+
} catch (error) {
|
|
32
32
|
validation.offIndicator();
|
|
33
33
|
components.Dialog.unknownError();
|
|
34
|
-
throw
|
|
34
|
+
throw error;
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
37
|
}
|
|
@@ -250,10 +250,10 @@ export default class extends components.Modal {
|
|
|
250
250
|
this.#validator.offIndicator();
|
|
251
251
|
components.Toast.success('User created.');
|
|
252
252
|
super.hide(true);
|
|
253
|
-
} catch (
|
|
253
|
+
} catch (error) {
|
|
254
254
|
this.#validator.offIndicator();
|
|
255
255
|
components.Dialog.unknownError();
|
|
256
|
-
throw
|
|
256
|
+
throw error;
|
|
257
257
|
}
|
|
258
258
|
});
|
|
259
259
|
}
|
|
@@ -269,16 +269,16 @@ export default class extends components.Modal {
|
|
|
269
269
|
// User update request.
|
|
270
270
|
const {data} = await this.#userApi.updateUser(this.#userId, new FormData(this.#validator.form));
|
|
271
271
|
this.#validator.offIndicator();
|
|
272
|
-
if (data?.error === '
|
|
272
|
+
if (data?.error === 'NotFoundError') {
|
|
273
273
|
await components.Dialog.warning('This user has been deleted.');
|
|
274
274
|
return void super.hide(true);
|
|
275
275
|
}
|
|
276
276
|
components.Toast.success('User updated.');
|
|
277
277
|
super.hide(true);
|
|
278
|
-
} catch (
|
|
278
|
+
} catch (error) {
|
|
279
279
|
this.#validator.offIndicator();
|
|
280
280
|
components.Dialog.unknownError();
|
|
281
|
-
throw
|
|
281
|
+
throw error;
|
|
282
282
|
}
|
|
283
283
|
});
|
|
284
284
|
}
|
|
@@ -35,13 +35,13 @@ export default class extends components.Api {
|
|
|
35
35
|
/**
|
|
36
36
|
* Handles API errors.
|
|
37
37
|
* @param {number} code The HTTP status code.
|
|
38
|
-
* @param {Error}
|
|
38
|
+
* @param {Error} error The error object.
|
|
39
39
|
* @return {void}
|
|
40
40
|
*/
|
|
41
|
-
errorHook(code,
|
|
41
|
+
errorHook(code, error) {
|
|
42
42
|
// Check if the error object and request properties exist to avoid runtime errors
|
|
43
|
-
if (
|
|
44
|
-
const {pathname} = new URL(
|
|
43
|
+
if (error && error.request && error.request.responseURL) {
|
|
44
|
+
const {pathname} = new URL(error.request.responseURL);
|
|
45
45
|
if (pathname !== '/api/users/login' && code === 401) {
|
|
46
46
|
// Redirect to the login page if an authentication error occurs on a non-login request.
|
|
47
47
|
location.replace('/');
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reloads a DataTable after a specified delay.
|
|
3
|
+
* @param {object} dataTable The DataTable instance to reload.
|
|
4
|
+
* @param {number} delay The delay in milliseconds before reloading.
|
|
5
|
+
*/
|
|
6
|
+
export default (dataTable, delay = 500) => {
|
|
7
|
+
let timer;
|
|
8
|
+
return function() { // Preserve the timer using a closure
|
|
9
|
+
clearTimeout(timer);
|
|
10
|
+
timer = setTimeout(() => dataTable.reload(), delay);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -2,6 +2,7 @@ import '~/users.css';
|
|
|
2
2
|
import hbs from 'handlebars-extd';
|
|
3
3
|
import {components} from 'metronic-extension';
|
|
4
4
|
import Datatable from '~/shared/Datatable';
|
|
5
|
+
import reloadDataTableWithDelay from '~/shared/reloadDataTableWithDelay';
|
|
5
6
|
import UserApi from '~/api/UserApi';
|
|
6
7
|
import UserModal from '~/modals/UserModal';
|
|
7
8
|
|
|
@@ -87,22 +88,17 @@ function initForm() {
|
|
|
87
88
|
throw Error('Unknown error');
|
|
88
89
|
components.Toast.success(`${name} was deleted.`);
|
|
89
90
|
userTable.reload();
|
|
90
|
-
} catch (
|
|
91
|
+
} catch (error) {
|
|
91
92
|
components.Dialog.unknownError();
|
|
92
|
-
throw
|
|
93
|
+
throw error;
|
|
93
94
|
}
|
|
94
95
|
})
|
|
95
|
-
.on('input', '[data-on-search-change]' ,
|
|
96
|
-
if (searchTimer)
|
|
97
|
-
clearTimeout(searchTimer);
|
|
98
|
-
searchTimer = setTimeout(() => userTable.reload(), 300);
|
|
99
|
-
});
|
|
96
|
+
.on('input', '[data-on-search-change]' , reloadDataTableWithDelay(userTable));
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
const userApi = new UserApi();
|
|
103
100
|
const userModal = new UserModal();
|
|
104
101
|
const ref = components.selectRef('#kt_app_content_container');
|
|
105
102
|
let userTable;
|
|
106
|
-
let searchTimer;
|
|
107
103
|
initTable();
|
|
108
104
|
initForm();
|
|
@@ -65,11 +65,11 @@ export default {
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Hooks the default behavior on request errors.
|
|
68
|
-
* If unset, simply returns an error HTTP status. (<code>res.status(
|
|
69
|
-
* @type {(
|
|
68
|
+
* If unset, simply returns an error HTTP status. (<code>res.status(error.status||500).end();</code>)
|
|
69
|
+
* @type {(error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
|
|
70
70
|
* @example
|
|
71
|
-
* hook_handle_error: (
|
|
72
|
-
* if (
|
|
71
|
+
* hook_handle_error: (error, req, res, next) => {
|
|
72
|
+
* if (error.status === 404)
|
|
73
73
|
* // If the URL cannot be found, a 404 error screen (views/errors/404.hbs) is displayed.
|
|
74
74
|
* res.render('errors/404');
|
|
75
75
|
* else
|
|
@@ -77,8 +77,8 @@ export default {
|
|
|
77
77
|
* res.render('error/500');
|
|
78
78
|
* },
|
|
79
79
|
*/
|
|
80
|
-
hook_handle_error: (
|
|
81
|
-
if (
|
|
80
|
+
hook_handle_error: (error, req, res, next) => {
|
|
81
|
+
if (error.status === 404)
|
|
82
82
|
res.status(404).render('errors/404', {isErrorPage: true});
|
|
83
83
|
else
|
|
84
84
|
res.status(500).render('errors/500', {isErrorPage: true});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for "Not Found" errors.
|
|
3
|
+
*/
|
|
4
|
+
export default class extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new NotFoundError instance.
|
|
7
|
+
* @param {string} [message='The requested record could not be found.'] - The error message.
|
|
8
|
+
*/
|
|
9
|
+
constructor(message = 'The requested record could not be found.') {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'NotFoundError';
|
|
12
|
+
this.statusCode = 404; // HTTP status code for "Not Found"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as expressExtension from 'express-sweet';
|
|
2
2
|
import {Media} from 'nodejs-shared';
|
|
3
|
-
import
|
|
3
|
+
import NotFoundError from '../errors/NotFoundError.js';
|
|
4
4
|
import ProfileModel from './ProfileModel.js';
|
|
5
5
|
import CommentModel from './CommentModel.js';
|
|
6
6
|
import BookModel from './BookModel.js';
|
|
@@ -105,10 +105,10 @@ export default class extends expressExtension.database.Model {
|
|
|
105
105
|
}, {transaction});
|
|
106
106
|
await this.#updateUserIcon(user, set.icon, transaction);
|
|
107
107
|
await transaction.commit();
|
|
108
|
-
} catch (
|
|
108
|
+
} catch (error) {
|
|
109
109
|
if (transaction)
|
|
110
110
|
await transaction.rollback();
|
|
111
|
-
throw
|
|
111
|
+
throw error;
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -123,7 +123,7 @@ export default class extends expressExtension.database.Model {
|
|
|
123
123
|
}, set);
|
|
124
124
|
const user = await this.getUser(userId);
|
|
125
125
|
if (!user)
|
|
126
|
-
throw new
|
|
126
|
+
throw new NotFoundError();
|
|
127
127
|
transaction = await super.begin();
|
|
128
128
|
user.email = set.email;
|
|
129
129
|
user.name = set.name;
|
|
@@ -133,10 +133,10 @@ export default class extends expressExtension.database.Model {
|
|
|
133
133
|
await user.save({transaction});
|
|
134
134
|
await this.#updateUserIcon(user, set.icon, transaction);
|
|
135
135
|
await transaction.commit();
|
|
136
|
-
} catch (
|
|
136
|
+
} catch (error) {
|
|
137
137
|
if (transaction)
|
|
138
138
|
await transaction.rollback();
|
|
139
|
-
throw
|
|
139
|
+
throw error;
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|