@madhusha_99/exp-gen 1.2.0 → 1.2.1

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.
Files changed (86) hide show
  1. package/package.json +1 -1
  2. package/LICENSE +0 -21
  3. package/README.md +0 -97
  4. package/bin/copyDir.js +0 -37
  5. package/bin/copyDir.js.map +0 -1
  6. package/bin/copyTemplates.js +0 -44
  7. package/bin/copyTemplates.js.map +0 -1
  8. package/bin/installDependencies.js +0 -36
  9. package/bin/installDependencies.js.map +0 -1
  10. package/bin/scaffoldProject.js +0 -45
  11. package/bin/scaffoldProject.js.map +0 -1
  12. package/bin/script.js +0 -138
  13. package/bin/script.js.map +0 -1
  14. package/bin/type/projectConfig.js +0 -2
  15. package/bin/type/projectConfig.js.map +0 -1
  16. package/src/templates/js/mongodb/app.js +0 -42
  17. package/src/templates/js/mongodb/configs/db.js +0 -14
  18. package/src/templates/js/mongodb/controllers/auth.controller.js +0 -71
  19. package/src/templates/js/mongodb/controllers/user.controller.js +0 -27
  20. package/src/templates/js/mongodb/default.env +0 -4
  21. package/src/templates/js/mongodb/middlewares/auth.middleware.js +0 -64
  22. package/src/templates/js/mongodb/models/user.model.js +0 -13
  23. package/src/templates/js/mongodb/package.json +0 -29
  24. package/src/templates/js/mongodb/repositories/user.repository.js +0 -21
  25. package/src/templates/js/mongodb/routes/auth.route.js +0 -9
  26. package/src/templates/js/mongodb/routes/user.route.js +0 -24
  27. package/src/templates/js/mongodb/services/user.service.js +0 -50
  28. package/src/templates/js/mongodb/utils/asyncHandler.js +0 -5
  29. package/src/templates/js/mongodb/utils/response.js +0 -13
  30. package/src/templates/js/mysql/app.js +0 -42
  31. package/src/templates/js/mysql/configs/config.js +0 -17
  32. package/src/templates/js/mysql/configs/db.js +0 -50
  33. package/src/templates/js/mysql/controllers/auth.controller.js +0 -83
  34. package/src/templates/js/mysql/controllers/user.controller.js +0 -27
  35. package/src/templates/js/mysql/default.env +0 -9
  36. package/src/templates/js/mysql/middlewares/auth.middleware.js +0 -64
  37. package/src/templates/js/mysql/models/user.model.js +0 -22
  38. package/src/templates/js/mysql/package-lock.json +0 -4386
  39. package/src/templates/js/mysql/package.json +0 -29
  40. package/src/templates/js/mysql/repositories/user.repository.js +0 -84
  41. package/src/templates/js/mysql/routes/auth.route.js +0 -9
  42. package/src/templates/js/mysql/routes/user.route.js +0 -24
  43. package/src/templates/js/mysql/services/user.service.js +0 -50
  44. package/src/templates/js/mysql/utils/asyncHandler.js +0 -5
  45. package/src/templates/js/mysql/utils/response.js +0 -13
  46. package/src/templates/js/none/app.js +0 -32
  47. package/src/templates/js/none/default.env +0 -4
  48. package/src/templates/js/none/package.json +0 -23
  49. package/src/templates/js/none/routes/api.route.js +0 -9
  50. package/src/templates/js/postgresql/default.env +0 -10
  51. package/src/templates/js/postgresql/package.json +0 -29
  52. package/src/templates/js/postgresql/src/app.js +0 -42
  53. package/src/templates/js/postgresql/src/configs/db.js +0 -29
  54. package/src/templates/js/postgresql/src/controllers/auth.controller.js +0 -72
  55. package/src/templates/js/postgresql/src/controllers/user.controller.js +0 -27
  56. package/src/templates/js/postgresql/src/middlewares/auth.middleware.js +0 -64
  57. package/src/templates/js/postgresql/src/repositories/user.repository.js +0 -46
  58. package/src/templates/js/postgresql/src/routes/auth.route.js +0 -9
  59. package/src/templates/js/postgresql/src/routes/user.route.js +0 -24
  60. package/src/templates/js/postgresql/src/services/user.service.js +0 -50
  61. package/src/templates/js/postgresql/src/utils/asyncHandler.js +0 -5
  62. package/src/templates/js/postgresql/src/utils/response.js +0 -13
  63. package/src/templates/js/sqlite/.gitkeep +0 -0
  64. package/src/templates/ts/mongodb/default.env +0 -4
  65. package/src/templates/ts/mongodb/package.json +0 -34
  66. package/src/templates/ts/mongodb/src/app.ts +0 -45
  67. package/src/templates/ts/mongodb/src/configs/db.ts +0 -11
  68. package/src/templates/ts/mongodb/src/controllers/auth.controller.ts +0 -78
  69. package/src/templates/ts/mongodb/src/controllers/user.controller.ts +0 -31
  70. package/src/templates/ts/mongodb/src/dtos/user.dto.ts +0 -13
  71. package/src/templates/ts/mongodb/src/interfaces/IUser.ts +0 -8
  72. package/src/templates/ts/mongodb/src/interfaces/express.d.ts +0 -9
  73. package/src/templates/ts/mongodb/src/middlewares/auth.middleware.ts +0 -82
  74. package/src/templates/ts/mongodb/src/models/user.model.ts +0 -14
  75. package/src/templates/ts/mongodb/src/repositories/user.repository.ts +0 -22
  76. package/src/templates/ts/mongodb/src/routes/auth.route.ts +0 -9
  77. package/src/templates/ts/mongodb/src/routes/user.route.ts +0 -25
  78. package/src/templates/ts/mongodb/src/services/user.service.ts +0 -52
  79. package/src/templates/ts/mongodb/src/utils/asyncHandler.ts +0 -14
  80. package/src/templates/ts/mongodb/src/utils/response.ts +0 -18
  81. package/src/templates/ts/mongodb/tsconfig.json +0 -16
  82. package/src/templates/ts/none/default.env +0 -4
  83. package/src/templates/ts/none/package.json +0 -34
  84. package/src/templates/ts/none/src/app.ts +0 -40
  85. package/src/templates/ts/none/src/routes/api.route.ts +0 -10
  86. package/src/templates/ts/none/tsconfig.json +0 -16
@@ -1,64 +0,0 @@
1
- /**
2
- * Auth middleware to protect routes using JWT
3
- *
4
- * How to use:
5
- * 1. Generate a JWT token after user login using `jwt.sign(payload, JWT_SECRET, { expiresIn })`.
6
- * 2. Send the token in the request headers: Authorization: Bearer <token>
7
- * 3. Add this middleware to routes you want to protect:
8
- * router.get('/protected', protect, protectedController)
9
- * 4. Inside your controller, `req.user` will have the decoded user info
10
- */
11
-
12
- import jwt from "jsonwebtoken";
13
- import { errorResponse } from "../utils/response.js";
14
-
15
- export const protect = (req, res, next) => {
16
- try {
17
- const authHeader = req.headers.authorization;
18
-
19
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
20
- return res
21
- .status(401)
22
- .json(
23
- errorResponse(
24
- { status: 401 },
25
- "Authorization token missing or malformed"
26
- )
27
- );
28
- }
29
-
30
- // Get token from headers
31
- const token = authHeader.split(" ")[1];
32
-
33
- if (!token) {
34
- return res
35
- .status(401)
36
- .json(errorResponse({ status: 401 }, "Not authorized, token missing"));
37
- }
38
-
39
- // Verify token
40
- let decoded;
41
- try {
42
- decoded = jwt.verify(token, process.env.JWT_SECRET);
43
- } catch (err) {
44
- if (err.name === "TokenExpiredError") {
45
- return res
46
- .status(401)
47
- .json(errorResponse({ status: 401 }, "Token expired"));
48
- }
49
- return res
50
- .status(401)
51
- .json(errorResponse({ status: 401 }, "Unauthorized: Invalid token"));
52
- }
53
-
54
- // Attach decoded user info to request
55
- req.user = decoded;
56
-
57
- next();
58
- } catch (err) {
59
- // Catch any unexpected errors
60
- return res
61
- .status(500)
62
- .json(errorResponse({ status: 500 }, "Server error: " + err.message));
63
- }
64
- };
@@ -1,13 +0,0 @@
1
- import mongoose from "mongoose";
2
-
3
- const userSchema = new mongoose.Schema(
4
- {
5
- name: { type: String, required: true },
6
- email: { type: String, required: true, unique: true },
7
- password: { type: String, required: true },
8
- age: { type: Number, default: 0 },
9
- },
10
- { timestamps: true }
11
- );
12
-
13
- export default mongoose.model("User", userSchema);
@@ -1,29 +0,0 @@
1
- {
2
- "name": "js-mongodb",
3
- "version": "1.0.0",
4
- "description": "Modern Express API with MongoDB, ES Modules, and async CRUD",
5
- "type": "module",
6
- "main": "app.js",
7
- "scripts": {
8
- "dev": "nodemon app.js",
9
- "start": "node app.js",
10
- "lint": "eslint .",
11
- "format": "prettier --write ."
12
- },
13
- "dependencies": {
14
- "bcryptjs": "^3.0.3",
15
- "cors": "^2.8.5",
16
- "dotenv": "^16.4.5",
17
- "express": "^4.19.2",
18
- "http-errors": "^2.0.0",
19
- "jsonwebtoken": "^9.0.3",
20
- "mongoose": "^7.5.0",
21
- "morgan": "^1.10.0"
22
- },
23
- "devDependencies": {
24
- "eslint": "^9.0.0",
25
- "eslint-plugin-import": "^2.29.1",
26
- "nodemon": "^3.1.14",
27
- "prettier": "^3.2.5"
28
- }
29
- }
@@ -1,21 +0,0 @@
1
- import User from "../models/user.model.js";
2
-
3
- export const getAllUsers = async () => {
4
- return await User.find({});
5
- };
6
-
7
- export const getUserById = async (id) => {
8
- return await User.findById(id);
9
- };
10
-
11
- export const createUser = async (data) => {
12
- return await User.create(data);
13
- };
14
-
15
- export const updateUser = async (id, data) => {
16
- return await User.findByIdAndUpdate(id, data, { new: true });
17
- };
18
-
19
- export const deleteUser = async (id) => {
20
- return await User.findByIdAndDelete(id);
21
- };
@@ -1,9 +0,0 @@
1
- import { Router } from "express";
2
- import { login, signup } from "../controllers/auth.controller.js";
3
-
4
- const router = Router();
5
-
6
- router.post("/login", login);
7
- router.post("/signup", signup);
8
-
9
- export default router;
@@ -1,24 +0,0 @@
1
- import { Router } from "express";
2
- import {
3
- getUsers,
4
- getUser,
5
- createUser,
6
- updateUser,
7
- deleteUser,
8
- } from "../controllers/user.controller.js";
9
- import { protect } from "../middlewares/auth.middleware.js";
10
- import { successResponse } from "../utils/response.js";
11
-
12
- const router = Router();
13
-
14
- router.get("/protected", protect, (req, res) => {
15
- res.json(successResponse({ user: req.user }, "Protected route accessed"));
16
- });
17
-
18
- router.get("/users", getUsers);
19
- router.get("/users/:id", protect, getUser);
20
- router.post("/users", createUser);
21
- router.put("/users/:id", updateUser);
22
- router.delete("/users/:id", deleteUser);
23
-
24
- export default router;
@@ -1,50 +0,0 @@
1
- import * as userRepo from "../repositories/user.repository.js";
2
- import { successResponse, errorResponse } from "../utils/response.js";
3
-
4
- export const getUsers = async () => {
5
- try {
6
- const users = await userRepo.getAllUsers();
7
- return successResponse(users, "Users fetched successfully");
8
- } catch (err) {
9
- return errorResponse(err, "Failed to fetch users");
10
- }
11
- };
12
-
13
- export const getUser = async (id) => {
14
- try {
15
- const user = await userRepo.getUserById(id);
16
- if (!user) throw new Error("User not found");
17
- return successResponse(user, "User fetched successfully");
18
- } catch (err) {
19
- return errorResponse(err, "Failed to fetch user");
20
- }
21
- };
22
-
23
- export const createNewUser = async (data) => {
24
- try {
25
- const user = await userRepo.createUser(data);
26
- return successResponse(user, "User created successfully");
27
- } catch (err) {
28
- return errorResponse(err, "Failed to create user");
29
- }
30
- };
31
-
32
- export const updateExistingUser = async (id, data) => {
33
- try {
34
- const user = await userRepo.updateUser(id, data);
35
- if (!user) throw new Error("User not found");
36
- return successResponse(user, "User updated successfully");
37
- } catch (err) {
38
- return errorResponse(err, "Failed to update user");
39
- }
40
- };
41
-
42
- export const deleteExistingUser = async (id) => {
43
- try {
44
- const user = await userRepo.deleteUser(id);
45
- if (!user) throw new Error("User not found");
46
- return successResponse(user, "User deleted successfully");
47
- } catch (err) {
48
- return errorResponse(err, "Failed to delete user");
49
- }
50
- };
@@ -1,5 +0,0 @@
1
- const asyncHandler = (fn) => (req, res, next) => {
2
- Promise.resolve(fn(req, res, next)).catch(next);
3
- };
4
-
5
- export default asyncHandler;
@@ -1,13 +0,0 @@
1
- export const successResponse = (data, message = "Success") => ({
2
- status: 200,
3
- success: true,
4
- message,
5
- data,
6
- });
7
-
8
- export const errorResponse = (error, message = "Error") => ({
9
- status: error.status || 500,
10
- success: false,
11
- message,
12
- error: error.message || message,
13
- });
@@ -1,42 +0,0 @@
1
- import express from "express";
2
- import morgan from "morgan";
3
- import createError from "http-errors";
4
- import cors from "cors";
5
- import apiRoutes from "./routes/user.route.js";
6
- import authRoutes from "./routes/auth.route.js";
7
- import { connectDB } from "./configs/db.js";
8
- import { ensureUsersTable } from "./models/user.model.js";
9
-
10
- await connectDB();
11
- await ensureUsersTable();
12
-
13
- const app = express();
14
-
15
- // Middlewares
16
- app.use(cors());
17
- app.use(express.json());
18
- app.use(express.urlencoded({ extended: true }));
19
- app.use(morgan("dev"));
20
-
21
- // Routes
22
- app.get("/", (req, res) => res.json({ message: "API is running 🚀" }));
23
- app.use("/api", apiRoutes); // user routes
24
- app.use("/api/auth", authRoutes); // login route
25
-
26
- // 404 handler
27
- app.use((req, res, next) => {
28
- next(createError.NotFound());
29
- });
30
-
31
- // Global error handler
32
- app.use((err, req, res) => {
33
- res.status(err.status || 500).json({
34
- success: false,
35
- message: err.message || "Internal Server Error",
36
- });
37
- });
38
-
39
- const PORT = process.env.PORT || 3000;
40
- app.listen(PORT, () =>
41
- console.log(`🚀 Server running @ http://localhost:${PORT}`)
42
- );
@@ -1,17 +0,0 @@
1
- import dotenv from "dotenv";
2
-
3
- dotenv.config();
4
-
5
- const config = {
6
- db: {
7
- host: process.env.DB_HOST || "localhost",
8
- user: process.env.DB_USER || "root",
9
- password: process.env.DB_PASSWORD || "",
10
- database: process.env.DB_DATABASE || "",
11
- connectionLimit: parseInt(process.env.DB_CONNECTION_LIMIT || "10", 10),
12
- },
13
- port: parseInt(process.env.PORT || "3000", 10),
14
- listPerPage: parseInt(process.env.LIST_PER_PAGE || "50", 10),
15
- };
16
-
17
- export default config;
@@ -1,50 +0,0 @@
1
- import mysql from "mysql2/promise";
2
- import config from "../configs/config.js";
3
-
4
- let pool;
5
-
6
- export const connectDB = async () => {
7
- try {
8
- pool = mysql.createPool({
9
- host: config.db.host,
10
- user: config.db.user,
11
- password: config.db.password,
12
- database: config.db.database,
13
- waitForConnections: true,
14
- connectionLimit: config.db.connectionLimit || 10,
15
- queueLimit: 0,
16
- });
17
-
18
- // Simple test query
19
- const connection = await pool.getConnection();
20
- try {
21
- const [rows] = await connection.query("SELECT 1 + 1 AS solution");
22
- console.log("The solution is:", rows[0].solution);
23
- } finally {
24
- connection.release();
25
- }
26
-
27
- console.log(`✅ MysqlDB Connected: ${config.db.host}`);
28
- } catch (error) {
29
- console.error("❌ MysqlDB connection error:", error.message);
30
- process.exit(1);
31
- }
32
- };
33
-
34
- export const getPool = () => {
35
- if (!pool) {
36
- throw new Error("Pool not initialized. Call connectDB() first.");
37
- }
38
- return pool;
39
- };
40
-
41
- export const query = async (sql, params = []) => {
42
- const p = getPool();
43
- try {
44
- const [rows, fields] = await p.query(sql, params);
45
- return rows;
46
- } catch (error) {
47
- console.error("❌ Query error:", error.message);
48
- throw error;
49
- }
50
- };
@@ -1,83 +0,0 @@
1
- import asyncHandler from "../utils/asyncHandler.js";
2
- import jwt from "jsonwebtoken";
3
- import bcrypt from "bcryptjs";
4
- import { successResponse, errorResponse } from "../utils/response.js";
5
- import * as userRepo from "../repositories/user.repository.js";
6
-
7
- export const login = asyncHandler(async (req, res) => {
8
- try {
9
- const { email, password } = req.body;
10
-
11
- if (!email || !password) {
12
- return res
13
- .status(400)
14
- .json(errorResponse({ status: 400 }, "Email and password required"));
15
- }
16
-
17
- const user = await userRepo.getUserByEmail(email);
18
- if (!user) {
19
- return res
20
- .status(401)
21
- .json(errorResponse({ status: 401 }, "Invalid credentials"));
22
- }
23
-
24
- const isMatch = await bcrypt.compare(password, user.password);
25
- if (!isMatch) {
26
- return res
27
- .status(401)
28
- .json(errorResponse({ status: 401 }, "Invalid credentials"));
29
- }
30
-
31
- const token = jwt.sign(
32
- { id: user.id, email: user.email },
33
- process.env.JWT_SECRET,
34
- { expiresIn: process.env.JWT_EXPIRES_IN || "1d" }
35
- );
36
-
37
- res.json(successResponse({ token }, "Login successful"));
38
- } catch (error) {
39
- res.status(500).json(errorResponse(error, "Login failed"));
40
- }
41
- });
42
-
43
- export const signup = asyncHandler(async (req, res) => {
44
- try {
45
- const { name, email, password, age } = req.body;
46
-
47
- if (!name || !email || !password) {
48
- return res
49
- .status(400)
50
- .json(
51
- errorResponse({ status: 400 }, "Name, email, and password required")
52
- );
53
- }
54
-
55
- const existingUser = await userRepo.getUserByEmail(email);
56
- if (existingUser) {
57
- return res
58
- .status(400)
59
- .json(errorResponse({ status: 400 }, "User already exists"));
60
- }
61
-
62
- const hashedPassword = await bcrypt.hash(password, 10);
63
-
64
- const newUser = await userRepo.createUser({
65
- name,
66
- email,
67
- password: hashedPassword,
68
- age: age || 0,
69
- });
70
-
71
- const token = jwt.sign(
72
- { id: newUser.id, email: newUser.email },
73
- process.env.JWT_SECRET,
74
- { expiresIn: process.env.JWT_EXPIRES_IN || "1d" }
75
- );
76
-
77
- res
78
- .status(201)
79
- .json(successResponse({ token, user: newUser }, "Signup successful"));
80
- } catch (error) {
81
- res.status(500).json(errorResponse(error, "Signup failed"));
82
- }
83
- });
@@ -1,27 +0,0 @@
1
- import * as userService from "../services/user.service.js";
2
- import asyncHandler from "../utils/asyncHandler.js";
3
-
4
- export const getUsers = asyncHandler(async (req, res) => {
5
- const result = await userService.getUsers();
6
- res.status(result.status).json(result);
7
- });
8
-
9
- export const getUser = asyncHandler(async (req, res) => {
10
- const result = await userService.getUser(req.params.id);
11
- res.status(result.status).json(result);
12
- });
13
-
14
- export const createUser = asyncHandler(async (req, res) => {
15
- const result = await userService.createNewUser(req.body);
16
- res.status(result.status).json(result);
17
- });
18
-
19
- export const updateUser = asyncHandler(async (req, res) => {
20
- const result = await userService.updateExistingUser(req.params.id, req.body);
21
- res.status(result.status).json(result);
22
- });
23
-
24
- export const deleteUser = asyncHandler(async (req, res) => {
25
- const result = await userService.deleteExistingUser(req.params.id);
26
- res.status(result.status).json(result);
27
- });
@@ -1,9 +0,0 @@
1
- PORT=8000
2
- DB_HOST=localhost
3
- DB_USER=root
4
- DB_PASSWORD=
5
- DB_DATABASE=possys
6
- DB_CONNECTION_LIMIT=10
7
- LIST_PER_PAGE=300
8
- JWT_SECRET=your_jwt_secret_here
9
- JWT_EXPIRES_IN=1d
@@ -1,64 +0,0 @@
1
- /**
2
- * Auth middleware to protect routes using JWT
3
- *
4
- * How to use:
5
- * 1. Generate a JWT token after user login using `jwt.sign(payload, JWT_SECRET, { expiresIn })`.
6
- * 2. Send the token in the request headers: Authorization: Bearer <token>
7
- * 3. Add this middleware to routes you want to protect:
8
- * router.get('/protected', protect, protectedController)
9
- * 4. Inside your controller, `req.user` will have the decoded user info
10
- */
11
-
12
- import jwt from "jsonwebtoken";
13
- import { errorResponse } from "../utils/response.js";
14
-
15
- export const protect = (req, res, next) => {
16
- try {
17
- const authHeader = req.headers.authorization;
18
-
19
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
20
- return res
21
- .status(401)
22
- .json(
23
- errorResponse(
24
- { status: 401 },
25
- "Authorization token missing or malformed"
26
- )
27
- );
28
- }
29
-
30
- // Get token from headers
31
- const token = authHeader.split(" ")[1];
32
-
33
- if (!token) {
34
- return res
35
- .status(401)
36
- .json(errorResponse({ status: 401 }, "Not authorized, token missing"));
37
- }
38
-
39
- // Verify token
40
- let decoded;
41
- try {
42
- decoded = jwt.verify(token, process.env.JWT_SECRET);
43
- } catch (err) {
44
- if (err.name === "TokenExpiredError") {
45
- return res
46
- .status(401)
47
- .json(errorResponse({ status: 401 }, "Token expired"));
48
- }
49
- return res
50
- .status(401)
51
- .json(errorResponse({ status: 401 }, "Unauthorized: Invalid token"));
52
- }
53
-
54
- // Attach decoded user info to request
55
- req.user = decoded;
56
-
57
- next();
58
- } catch (err) {
59
- // Catch any unexpected errors
60
- return res
61
- .status(500)
62
- .json(errorResponse({ status: 500 }, "Server error: " + err.message));
63
- }
64
- };
@@ -1,22 +0,0 @@
1
- import { query } from "../configs/db.js";
2
-
3
- export const ensureUsersTable = async () => {
4
- const sql = `
5
- CREATE TABLE IF NOT EXISTS users (
6
- id INT AUTO_INCREMENT PRIMARY KEY,
7
- name VARCHAR(255) NOT NULL,
8
- email VARCHAR(255) NOT NULL UNIQUE,
9
- password VARCHAR(255) NOT NULL,
10
- age INT DEFAULT 0,
11
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
12
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
13
- ) ENGINE=InnoDB;
14
- `;
15
- try {
16
- await query(sql);
17
- console.log("✅ Users table ensured");
18
- } catch (error) {
19
- console.error("❌ Error ensuring users table:", error.message);
20
- throw error;
21
- }
22
- };