jcc-express-mvc 1.0.0
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 +0 -0
- package/index.js +1 -0
- package/package.json +22 -0
- package/src/Auth/index.js +74 -0
- package/src/Auth/protectedRoute.js +30 -0
- package/src/Router/Router.js +42 -0
- package/src/Router/Routes.js +28 -0
- package/src/app/index.js +62 -0
- package/src/command-line-interface/commands.js +119 -0
- package/src/command-line-interface/dbCli.js +59 -0
- package/src/command-line-interface/getCommands.js +86 -0
- package/src/command-line-interface/getDbCliCommand.js +18 -0
- package/src/dbConnection/index.js +14 -0
- package/src/error/AppError.js +8 -0
- package/src/error/ErrorHandler.js +71 -0
- package/src/error/public/pageNotFound.html +28 -0
- package/src/files/controller.js +67 -0
- package/src/files/model.js +14 -0
- package/src/files/request.js +28 -0
- package/src/files/resoucesController.js +72 -0
- package/src/helpers.js +70 -0
- package/src/index.js +42 -0
- package/src/middlware/index.js +60 -0
- package/src/middlware/isLogin.js +19 -0
- package/src/passport/config.js +38 -0
- package/src/passport/index.js +0 -0
- package/src/request/FormRequest.js +35 -0
- package/src/request/index.js +22 -0
- package/src/response/index.js +32 -0
- package/src/socket/index.js +18 -0
- package/src/utils/index.js +79 -0
- package/src/validation/index.js +71 -0
- package/src/validation/method.js +168 -0
- package/src/validation/rules.js +92 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const getControllerName = (name) => {
|
|
2
|
+
return `class ${name}{
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @return Express Request Response
|
|
7
|
+
*/
|
|
8
|
+
create(req,res,next)
|
|
9
|
+
{
|
|
10
|
+
//
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @return Express Request Response
|
|
15
|
+
*/
|
|
16
|
+
index(req,res,next)
|
|
17
|
+
{
|
|
18
|
+
//
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
*
|
|
24
|
+
* @return Express Request Response
|
|
25
|
+
*/
|
|
26
|
+
store(req,res,next)
|
|
27
|
+
{
|
|
28
|
+
//
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param id
|
|
34
|
+
* @return Express Request Response
|
|
35
|
+
*/
|
|
36
|
+
show(req,res,next)
|
|
37
|
+
{
|
|
38
|
+
//
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
*
|
|
44
|
+
* @param id
|
|
45
|
+
* @return Express Request Response
|
|
46
|
+
*/
|
|
47
|
+
update(req,res,next)
|
|
48
|
+
{
|
|
49
|
+
//
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param id
|
|
55
|
+
* @return Express Response
|
|
56
|
+
*/
|
|
57
|
+
destroy(req,res,next)
|
|
58
|
+
{
|
|
59
|
+
//
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
module.exports = new ${name}()
|
|
65
|
+
`;
|
|
66
|
+
};
|
|
67
|
+
module.exports = getControllerName;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const getModelName = (name) => {
|
|
2
|
+
return `const mongoose = require("mongoose")
|
|
3
|
+
|
|
4
|
+
const ${name}Schema = new mongoose.Schema({
|
|
5
|
+
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
timestamps:true
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
module.exports = mongoose.model('${name}',${name}Schema)
|
|
12
|
+
`;
|
|
13
|
+
};
|
|
14
|
+
module.exports = getModelName;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const request = (fileName) => {
|
|
2
|
+
return `
|
|
3
|
+
const { getModel, FormRequest } = require("gambtech-lib");
|
|
4
|
+
|
|
5
|
+
class ${fileName} extends FormRequest {
|
|
6
|
+
constructor(req) {
|
|
7
|
+
super(req);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async rules ()
|
|
12
|
+
{
|
|
13
|
+
await this.apiValidate({
|
|
14
|
+
//
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async save()
|
|
19
|
+
{
|
|
20
|
+
await this.rules()
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = ${fileName}
|
|
25
|
+
`;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = request;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const resourceController = (controller, model) => {
|
|
2
|
+
return `const {getModel }= require("express-mvc")
|
|
3
|
+
const ${model} = getModel('${model}')
|
|
4
|
+
class ${controller} {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @return Express Request Response
|
|
9
|
+
*/
|
|
10
|
+
async create(req,res,next)
|
|
11
|
+
{
|
|
12
|
+
return res.render();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @return Express Request Response
|
|
18
|
+
*/
|
|
19
|
+
async index(req,res,next)
|
|
20
|
+
{
|
|
21
|
+
return res.json({message: await ${model}.find({}),success:true})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
*
|
|
27
|
+
* @return Express Request Response
|
|
28
|
+
*/
|
|
29
|
+
async store(req,res,next)
|
|
30
|
+
{
|
|
31
|
+
const data = {...req.body}
|
|
32
|
+
return res.json({message: await ${model}.create(data)})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param id
|
|
38
|
+
* @return Express Request Response
|
|
39
|
+
*/
|
|
40
|
+
async show(req,res,next)
|
|
41
|
+
{
|
|
42
|
+
return res.json({message: await ${model}.findById(req.params.id)})
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
*
|
|
49
|
+
* @param id
|
|
50
|
+
* @return Express Request Response
|
|
51
|
+
*/
|
|
52
|
+
async update(req,res,next)
|
|
53
|
+
{
|
|
54
|
+
const data = {...req.body}
|
|
55
|
+
return res.json({message: await ${model}.findByIdAndUpdate(req.params.id,data)});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @param id
|
|
61
|
+
* @return Express Response
|
|
62
|
+
*/
|
|
63
|
+
async destroy(req,res,next)
|
|
64
|
+
{
|
|
65
|
+
return res.json({message:await ${model}.findByIdAndRemove(req.params.id)})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
module.exports = new ${controller}()
|
|
70
|
+
`;
|
|
71
|
+
};
|
|
72
|
+
module.exports = resourceController;
|
package/src/helpers.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const bcryptjs = require("bcryptjs");
|
|
2
|
+
const getRootPath = require("app-root-path").path;
|
|
3
|
+
const jwt = require("jsonwebtoken");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const AppError = require("./error/AppError");
|
|
6
|
+
|
|
7
|
+
exports.rootPath = (file) =>
|
|
8
|
+
require(`${path.resolve(`${getRootPath}/${file}`)}`);
|
|
9
|
+
|
|
10
|
+
exports.getController = (file) =>
|
|
11
|
+
require(`${getRootPath}/app/Controllers/${file}`);
|
|
12
|
+
|
|
13
|
+
exports.getApiController = (file) =>
|
|
14
|
+
require(`${getRootPath}/app/Controllers/Api/${file}`);
|
|
15
|
+
|
|
16
|
+
exports.getModel = (file) => require(`${getRootPath}/app/Models/${file}`);
|
|
17
|
+
|
|
18
|
+
exports.getMiddleware = (file) =>
|
|
19
|
+
require(`${getRootPath}/app/Middlewares/${file}`);
|
|
20
|
+
|
|
21
|
+
exports.getRequest = (file) => require(`${getRootPath}/app/Request/${file}`);
|
|
22
|
+
|
|
23
|
+
exports.bcrypt = async (string) => {
|
|
24
|
+
const salt = await bcryptjs.genSalt(10);
|
|
25
|
+
return bcryptjs.hash(`${string}`, salt);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.verifyHash = async (string, hash) => {
|
|
29
|
+
return bcryptjs.compare(`${string}`, hash);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
exports.jwtSign = (data) => {
|
|
33
|
+
try {
|
|
34
|
+
return jwt.sign(data, process.env.JWT_SECRET);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.log(error.message);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.jwtVerify = (token) => {
|
|
41
|
+
try {
|
|
42
|
+
return jwt.verify(token, process.env.JWT_SECRET);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
new AppError(error, 500);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.saveImage = (req, fieldName, folder = "image") => {
|
|
49
|
+
let file = req.files[fieldName];
|
|
50
|
+
const name = file.name.split(".")[0];
|
|
51
|
+
const randomNum = Math.floor(Math.random() * 1000);
|
|
52
|
+
const extension = path.extname(req.files[fieldName].name).toLocaleLowerCase();
|
|
53
|
+
const fileName = `${name}-${randomNum + extension}`;
|
|
54
|
+
file.mv(
|
|
55
|
+
`${require("app-root-path").path}/public/${folder}/${fileName}`,
|
|
56
|
+
(err) => {
|
|
57
|
+
if (err) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
return fileName;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
exports.capitalize = (str) => {
|
|
66
|
+
return `${str[0].toUpperCase() + str.slice(1)}`;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
exports.asyncHandler = (func) => (req, res, next) =>
|
|
70
|
+
Promise.resolve(func(req, res, next)).catch(next);
|
package/src/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const App = require("./app");
|
|
2
|
+
const { authenticated, protectedApi } = require("./Auth/protectedRoute");
|
|
3
|
+
const Auth = require("./Auth");
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
bcrypt,
|
|
7
|
+
getApiController,
|
|
8
|
+
getModel,
|
|
9
|
+
jwtSign,
|
|
10
|
+
jwtVerify,
|
|
11
|
+
getMiddleware,
|
|
12
|
+
getRequest,
|
|
13
|
+
verifyHash,
|
|
14
|
+
saveImage,
|
|
15
|
+
getController,
|
|
16
|
+
rootPath,
|
|
17
|
+
} = require("./helpers");
|
|
18
|
+
|
|
19
|
+
const getDbCli = require("./command-line-interface/getDbCliCommand");
|
|
20
|
+
|
|
21
|
+
const app = new App();
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
server: app.server(),
|
|
25
|
+
Route: app.webRoute(),
|
|
26
|
+
jccCommadLine: app.commandLineArgv,
|
|
27
|
+
bcrypt,
|
|
28
|
+
getModel,
|
|
29
|
+
getController,
|
|
30
|
+
getApiController,
|
|
31
|
+
jwtSign,
|
|
32
|
+
jwtVerify,
|
|
33
|
+
getMiddleware,
|
|
34
|
+
getRequest,
|
|
35
|
+
verifyHash,
|
|
36
|
+
saveImage,
|
|
37
|
+
rootPath,
|
|
38
|
+
authenticated,
|
|
39
|
+
apiAuthenticated: protectedApi,
|
|
40
|
+
Auth,
|
|
41
|
+
getDbCli,
|
|
42
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const passport = require("passport");
|
|
2
|
+
const { getMiddleware, rootPath } = require("../helpers");
|
|
3
|
+
const Response = require("../response");
|
|
4
|
+
const Request = require("../request");
|
|
5
|
+
const middlewares = getMiddleware("app");
|
|
6
|
+
const templateEngine = rootPath("/app/Config/engine");
|
|
7
|
+
const isLogIn = require("./isLogin");
|
|
8
|
+
|
|
9
|
+
// console.log(middlewares);
|
|
10
|
+
class Middleware {
|
|
11
|
+
constructor(express, app) {
|
|
12
|
+
this.express = express;
|
|
13
|
+
this.app = app;
|
|
14
|
+
this.handler();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
expressBodyParser() {
|
|
18
|
+
this.app.use(this.express.json());
|
|
19
|
+
return this.app.use(this.express.urlencoded({ extended: false }));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
staticFiles() {
|
|
23
|
+
this.app.use(
|
|
24
|
+
this.express.static(`${require("app-root-path").path}/resources`)
|
|
25
|
+
);
|
|
26
|
+
return this.app.use(
|
|
27
|
+
this.express.static(`${require("app-root-path").path}/public`)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
registeredMiddleware() {
|
|
32
|
+
return middlewares.map((middleware) => this.app.use(middleware));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
passport() {
|
|
36
|
+
require("../passport/config")(passport);
|
|
37
|
+
this.app.use(passport.initialize());
|
|
38
|
+
return this.app.use(passport.session());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
handler() {
|
|
42
|
+
try {
|
|
43
|
+
templateEngine(this.app);
|
|
44
|
+
this.expressBodyParser();
|
|
45
|
+
this.staticFiles();
|
|
46
|
+
this.registeredMiddleware();
|
|
47
|
+
this.passport();
|
|
48
|
+
this.app.use((req, res, next) => {
|
|
49
|
+
new Response(req, res);
|
|
50
|
+
new Request(req, res);
|
|
51
|
+
next();
|
|
52
|
+
});
|
|
53
|
+
new isLogIn(this.app);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.log({ middleware: error?.message });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = Middleware;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class isLogIn {
|
|
2
|
+
constructor(app) {
|
|
3
|
+
this.handler(app);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
handler(app) {
|
|
7
|
+
return app.use((req, res, next) => {
|
|
8
|
+
if (
|
|
9
|
+
(req.url === "/login" || req.url == "/register") &&
|
|
10
|
+
req.isAuthenticated()
|
|
11
|
+
) {
|
|
12
|
+
return res.redirectBack();
|
|
13
|
+
}
|
|
14
|
+
return next();
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = isLogIn;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const LocalStrategy = require("passport-local").Strategy;
|
|
2
|
+
|
|
3
|
+
const { getModel, verifyHash } = require("../helpers");
|
|
4
|
+
const User = getModel("User");
|
|
5
|
+
|
|
6
|
+
module.exports = (passport) => {
|
|
7
|
+
passport.use(
|
|
8
|
+
new LocalStrategy(
|
|
9
|
+
{ usernameField: "email" },
|
|
10
|
+
async (email, password, done) => {
|
|
11
|
+
try {
|
|
12
|
+
const user = await User.findOne({ email }).select("+password");
|
|
13
|
+
if (!user) {
|
|
14
|
+
return done(null, false, { message: "Invalid credentials" });
|
|
15
|
+
}
|
|
16
|
+
const isMatch = await verifyHash(password, user.password);
|
|
17
|
+
if (isMatch) {
|
|
18
|
+
// console.log("Match");
|
|
19
|
+
return done(null, user);
|
|
20
|
+
}
|
|
21
|
+
return done(null, false, { message: "Invalid credentials" });
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.log({ passportError: error.message });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
passport.serializeUser((user, done) => done(null, user.id));
|
|
30
|
+
passport.deserializeUser(async (id, done) => {
|
|
31
|
+
try {
|
|
32
|
+
const user = await User.findById(id);
|
|
33
|
+
done(null, user);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.log({ passportError: error.message });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const { getModel } = require("../helpers");
|
|
2
|
+
|
|
3
|
+
class FormRequest {
|
|
4
|
+
constructor(req) {
|
|
5
|
+
this.req = req;
|
|
6
|
+
const formdata = req.body;
|
|
7
|
+
for (let data in formdata) {
|
|
8
|
+
this[data] = formdata[data] || null;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
query(name) {
|
|
13
|
+
return this.req.query[name];
|
|
14
|
+
}
|
|
15
|
+
route(name) {
|
|
16
|
+
return this.req.params[name];
|
|
17
|
+
}
|
|
18
|
+
request() {
|
|
19
|
+
return this.req.body;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async validate(formData) {
|
|
23
|
+
return this.req.validate(formData);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async apiValidate(formData) {
|
|
27
|
+
return this.req.apiValidate(formData);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async findById(model, id) {
|
|
31
|
+
return getModel(model).findById(id);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = FormRequest;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const validate = require("../validation");
|
|
2
|
+
class Request {
|
|
3
|
+
constructor(request, response) {
|
|
4
|
+
this.validationErrors(request, response);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
validationErrors(request, response) {
|
|
8
|
+
const errors = {};
|
|
9
|
+
response.locals.error = errors;
|
|
10
|
+
response.locals.errors = request.flash("validationErrors")[0];
|
|
11
|
+
response.locals.old = request.flash("old")[0];
|
|
12
|
+
request.validate = async (formData) =>
|
|
13
|
+
validate("validationErrors", request, formData);
|
|
14
|
+
|
|
15
|
+
request.apiValidate = async (formdata = {}) =>
|
|
16
|
+
validate("apiValidationErrors", request, formdata);
|
|
17
|
+
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = Request;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const reqUrls = [];
|
|
2
|
+
class Response {
|
|
3
|
+
constructor(request, response) {
|
|
4
|
+
this.request = request;
|
|
5
|
+
this.response = response;
|
|
6
|
+
this.visitedUrls();
|
|
7
|
+
response.redirectBack = () => this.redirectBack();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
visitedUrls() {
|
|
11
|
+
if (
|
|
12
|
+
this.request.originalUrl !== "/favicon.ico" &&
|
|
13
|
+
this.request.originalUrl !== "/js/app.js"
|
|
14
|
+
) {
|
|
15
|
+
if (reqUrls.length > 1) {
|
|
16
|
+
reqUrls.pop();
|
|
17
|
+
}
|
|
18
|
+
reqUrls.unshift(this.request.originalUrl);
|
|
19
|
+
}
|
|
20
|
+
this.request.previsiousUrls = reqUrls;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
redirectBack() {
|
|
25
|
+
// console.log(this.request.previsiousUrls);
|
|
26
|
+
return reqUrls.length > 0 && reqUrls[1]
|
|
27
|
+
? this.response.redirect(reqUrls[1])
|
|
28
|
+
: this.response.redirect(reqUrls[0]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = Response;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const socket = require("socket.io");
|
|
2
|
+
const socketCors = require(`${
|
|
3
|
+
require("app-root-path").path
|
|
4
|
+
}/app/Config/cors/socket`);
|
|
5
|
+
const SocketFile = require(`${require("app-root-path").path}/socket`);
|
|
6
|
+
class SocketIo {
|
|
7
|
+
constructor(server) {
|
|
8
|
+
const io = socket(server, {
|
|
9
|
+
origin: socketCors.origin,
|
|
10
|
+
credentials: socketCors.credential,
|
|
11
|
+
methods: ["GET", "POST"],
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
SocketFile(io);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = SocketIo;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const { asyncHandler } = require("../helpers");
|
|
2
|
+
class Util {
|
|
3
|
+
//
|
|
4
|
+
constructor(app, prefix, middlewares, controller) {
|
|
5
|
+
this.app = app;
|
|
6
|
+
this.prefix = prefix;
|
|
7
|
+
this.controller = controller;
|
|
8
|
+
this.middlewares = middlewares;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
setGroupMiddleware(middlewares) {
|
|
12
|
+
this.middlewares = middlewares;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param {*} path
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
resolvePath(path) {
|
|
21
|
+
return this.prefix ? `${this.prefix}${path}` : path;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
setGroupController(controller) {
|
|
25
|
+
this.controller = controller;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
classController(callback) {
|
|
29
|
+
return typeof this.controller === "object" && typeof callback === "string";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
isMiddleware() {
|
|
33
|
+
return (
|
|
34
|
+
typeof this.middlewares === "function" ||
|
|
35
|
+
this.middlewares instanceof Array
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async validateMiddleware(middlewares) {
|
|
40
|
+
if (typeof middlewares === "function") {
|
|
41
|
+
return asyncHandler(middlewares);
|
|
42
|
+
}
|
|
43
|
+
if (middlewares instanceof Array) {
|
|
44
|
+
return middlewares.map((middleware) => asyncHandler(middleware));
|
|
45
|
+
}
|
|
46
|
+
return (req, res, next) => {
|
|
47
|
+
next();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async routeHelper(httpMethod, path, callback) {
|
|
52
|
+
// check to see if class controller is added in route
|
|
53
|
+
try {
|
|
54
|
+
if (this.classController(callback)) {
|
|
55
|
+
return this.isMiddleware()
|
|
56
|
+
? this.app[httpMethod](this.resolvePath(path), [
|
|
57
|
+
this.middlewares,
|
|
58
|
+
asyncHandler(this.controller[callback]),
|
|
59
|
+
])
|
|
60
|
+
: this.app[httpMethod](
|
|
61
|
+
this.resolvePath(path),
|
|
62
|
+
asyncHandler(this.controller[callback])
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
//
|
|
66
|
+
// console.log(this.middlewares);
|
|
67
|
+
return this.isMiddleware()
|
|
68
|
+
? this.app[httpMethod](this.resolvePath(path), [
|
|
69
|
+
this.middlewares,
|
|
70
|
+
asyncHandler(callback),
|
|
71
|
+
])
|
|
72
|
+
: this.app[httpMethod](this.resolvePath(path), asyncHandler(callback));
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.log({ errMsg: error?.message, stack: error?.stack });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = Util;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const validationMethod = require("./method");
|
|
2
|
+
const { rules } = require("./rules");
|
|
3
|
+
|
|
4
|
+
// class Validation {
|
|
5
|
+
const run = async (req, formData) => {
|
|
6
|
+
const errors = {};
|
|
7
|
+
const validateData = {};
|
|
8
|
+
const { extractValue, secondValue } = validationMethod;
|
|
9
|
+
|
|
10
|
+
for (let data in formData) {
|
|
11
|
+
for (let item of formData[data]) {
|
|
12
|
+
if (item.includes("same:")) {
|
|
13
|
+
const result = await rules(
|
|
14
|
+
extractValue(item, 0),
|
|
15
|
+
[`${data}`, `${extractValue(item, 1)}`],
|
|
16
|
+
[`${req.body[data]}`, `${req.body[extractValue(item, 1)]}`]
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (result) {
|
|
20
|
+
errors[data] = result;
|
|
21
|
+
}
|
|
22
|
+
validateData[data] = req.body[data];
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (secondValue(item)) {
|
|
27
|
+
let result = await rules(
|
|
28
|
+
extractValue(item, 0),
|
|
29
|
+
data,
|
|
30
|
+
req.body[data],
|
|
31
|
+
extractValue(item, 1)
|
|
32
|
+
);
|
|
33
|
+
if (result) {
|
|
34
|
+
errors[data] = result;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
validateData[data] = req.body[data];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!secondValue(item)) {
|
|
42
|
+
let result = await rules(item, data, req.body[data]);
|
|
43
|
+
if (result) {
|
|
44
|
+
errors[data] = result;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
validateData[data] = req.body[data];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
req.flash("old", req.body);
|
|
53
|
+
req.flash("validationErrors", errors);
|
|
54
|
+
return { errors, validateData };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const validate = async (errorType, req, formData) => {
|
|
58
|
+
const validation = await run(req, formData);
|
|
59
|
+
|
|
60
|
+
const errors = validation.errors;
|
|
61
|
+
|
|
62
|
+
if (errors && Object.keys(errors).length > 0) {
|
|
63
|
+
let err = new Error("validation", errors);
|
|
64
|
+
err[errorType] = errors;
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
return validation.validateData;
|
|
68
|
+
};
|
|
69
|
+
// }
|
|
70
|
+
|
|
71
|
+
module.exports = validate;
|