@madhusha_99/exp-gen 1.1.4 → 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.
- package/package.json +1 -1
- package/LICENSE +0 -21
- package/README.md +0 -97
- package/bin/copyDir.js +0 -37
- package/bin/copyDir.js.map +0 -1
- package/bin/copyTemplates.js +0 -44
- package/bin/copyTemplates.js.map +0 -1
- package/bin/installDependencies.js +0 -36
- package/bin/installDependencies.js.map +0 -1
- package/bin/scaffoldProject.js +0 -45
- package/bin/scaffoldProject.js.map +0 -1
- package/bin/script.js +0 -139
- package/bin/script.js.map +0 -1
- package/bin/type/projectConfig.js +0 -2
- package/bin/type/projectConfig.js.map +0 -1
- package/src/templates/js/mongodb/app.js +0 -42
- package/src/templates/js/mongodb/configs/db.js +0 -14
- package/src/templates/js/mongodb/controllers/auth.controller.js +0 -71
- package/src/templates/js/mongodb/controllers/user.controller.js +0 -27
- package/src/templates/js/mongodb/default.env +0 -4
- package/src/templates/js/mongodb/middlewares/auth.middleware.js +0 -64
- package/src/templates/js/mongodb/models/user.model.js +0 -13
- package/src/templates/js/mongodb/package.json +0 -29
- package/src/templates/js/mongodb/repositories/user.repository.js +0 -21
- package/src/templates/js/mongodb/routes/auth.route.js +0 -9
- package/src/templates/js/mongodb/routes/user.route.js +0 -24
- package/src/templates/js/mongodb/services/user.service.js +0 -50
- package/src/templates/js/mongodb/utils/asyncHandler.js +0 -5
- package/src/templates/js/mongodb/utils/response.js +0 -13
- package/src/templates/js/mysql/.gitkeep +0 -0
- package/src/templates/js/none/app.js +0 -32
- package/src/templates/js/none/default.env +0 -4
- package/src/templates/js/none/package.json +0 -23
- package/src/templates/js/none/routes/api.route.js +0 -9
- package/src/templates/js/postgresql/default.env +0 -10
- package/src/templates/js/postgresql/package.json +0 -29
- package/src/templates/js/postgresql/src/app.js +0 -42
- package/src/templates/js/postgresql/src/configs/db.js +0 -29
- package/src/templates/js/postgresql/src/controllers/auth.controller.js +0 -72
- package/src/templates/js/postgresql/src/controllers/user.controller.js +0 -27
- package/src/templates/js/postgresql/src/middlewares/auth.middleware.js +0 -64
- package/src/templates/js/postgresql/src/repositories/user.repository.js +0 -46
- package/src/templates/js/postgresql/src/routes/auth.route.js +0 -9
- package/src/templates/js/postgresql/src/routes/user.route.js +0 -24
- package/src/templates/js/postgresql/src/services/user.service.js +0 -50
- package/src/templates/js/postgresql/src/utils/asyncHandler.js +0 -5
- package/src/templates/js/postgresql/src/utils/response.js +0 -13
- package/src/templates/js/sqlite/.gitkeep +0 -0
- package/src/templates/ts/mongodb/default.env +0 -4
- package/src/templates/ts/mongodb/package.json +0 -34
- package/src/templates/ts/mongodb/src/app.ts +0 -45
- package/src/templates/ts/mongodb/src/configs/db.ts +0 -11
- package/src/templates/ts/mongodb/src/controllers/auth.controller.ts +0 -78
- package/src/templates/ts/mongodb/src/controllers/user.controller.ts +0 -31
- package/src/templates/ts/mongodb/src/dtos/user.dto.ts +0 -13
- package/src/templates/ts/mongodb/src/interfaces/IUser.ts +0 -8
- package/src/templates/ts/mongodb/src/interfaces/express.d.ts +0 -9
- package/src/templates/ts/mongodb/src/middlewares/auth.middleware.ts +0 -82
- package/src/templates/ts/mongodb/src/models/user.model.ts +0 -14
- package/src/templates/ts/mongodb/src/repositories/user.repository.ts +0 -22
- package/src/templates/ts/mongodb/src/routes/auth.route.ts +0 -9
- package/src/templates/ts/mongodb/src/routes/user.route.ts +0 -25
- package/src/templates/ts/mongodb/src/services/user.service.ts +0 -52
- package/src/templates/ts/mongodb/src/utils/asyncHandler.ts +0 -14
- package/src/templates/ts/mongodb/src/utils/response.ts +0 -18
- package/src/templates/ts/mongodb/tsconfig.json +0 -16
- package/src/templates/ts/none/default.env +0 -4
- package/src/templates/ts/none/package.json +0 -34
- package/src/templates/ts/none/src/app.ts +0 -40
- package/src/templates/ts/none/src/routes/api.route.ts +0 -10
- package/src/templates/ts/none/tsconfig.json +0 -16
|
@@ -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,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
|
-
});
|
|
File without changes
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ts-mongodb",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "app.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"dev": "tsx watch src/app.ts",
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [],
|
|
12
|
-
"author": "",
|
|
13
|
-
"license": "ISC",
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"bcryptjs": "^3.0.3",
|
|
16
|
-
"cors": "^2.8.6",
|
|
17
|
-
"dotenv": "^17.3.1",
|
|
18
|
-
"express": "^5.2.1",
|
|
19
|
-
"http-errors": "^2.0.1",
|
|
20
|
-
"jsonwebtoken": "^9.0.3",
|
|
21
|
-
"mongoose": "^9.3.1",
|
|
22
|
-
"morgan": "^1.10.1"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@types/bcryptjs": "^2.4.6",
|
|
26
|
-
"@types/cors": "^2.8.19",
|
|
27
|
-
"@types/express": "^5.0.6",
|
|
28
|
-
"@types/jsonwebtoken": "^9.0.10",
|
|
29
|
-
"@types/morgan": "^1.9.10",
|
|
30
|
-
"@types/node": "^25.5.0",
|
|
31
|
-
"tsx": "^4.21.0",
|
|
32
|
-
"typescript": "^5.9.3"
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import express, { Request, Response, NextFunction } from "express";
|
|
2
|
-
import morgan from "morgan";
|
|
3
|
-
import createError from "http-errors";
|
|
4
|
-
import cors from "cors";
|
|
5
|
-
import dotenv from "dotenv";
|
|
6
|
-
import { connectDB } from "./configs/db";
|
|
7
|
-
import userRoutes from "./routes/user.route";
|
|
8
|
-
import authRoutes from "./routes/auth.route";
|
|
9
|
-
|
|
10
|
-
dotenv.config({ quiet: true });
|
|
11
|
-
const PORT = process.env.PORT || 3000;
|
|
12
|
-
|
|
13
|
-
connectDB();
|
|
14
|
-
|
|
15
|
-
const app = express();
|
|
16
|
-
|
|
17
|
-
// Middlewares
|
|
18
|
-
app.use(cors());
|
|
19
|
-
app.use(express.json());
|
|
20
|
-
app.use(express.urlencoded({ extended: true }));
|
|
21
|
-
app.use(morgan("dev"));
|
|
22
|
-
|
|
23
|
-
// Routes
|
|
24
|
-
app.get("/", (_: Request, res: Response) =>
|
|
25
|
-
res.json({ message: "API is running 🚀" })
|
|
26
|
-
);
|
|
27
|
-
app.use("/api", userRoutes); // user routes
|
|
28
|
-
app.use("/api/auth", authRoutes); // login route
|
|
29
|
-
|
|
30
|
-
// 404 handler
|
|
31
|
-
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
32
|
-
next(createError.NotFound());
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// Global error handler
|
|
36
|
-
app.use((err: any, req: Request, res: Response) => {
|
|
37
|
-
res.status(err.status || 500).json({
|
|
38
|
-
success: false,
|
|
39
|
-
message: err.message || "Internal Server Error",
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
app.listen(PORT, () => {
|
|
44
|
-
console.log(`🚀 Server running @ http://localhost:${PORT}`);
|
|
45
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
export const connectDB = async () => {
|
|
4
|
-
try {
|
|
5
|
-
const conn = await mongoose.connect(process.env.MONGO_URL as string);
|
|
6
|
-
console.log(`✅ MongoDB Connected: ${conn.connection.host}`);
|
|
7
|
-
} catch (error: any) {
|
|
8
|
-
console.error("❌ MongoDB connection error:", error.message);
|
|
9
|
-
process.exit(1);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { User } from "../models/user.model.js";
|
|
2
|
-
import asyncHandler from "../utils/asyncHandler.js";
|
|
3
|
-
import jwt, { SignOptions } from "jsonwebtoken";
|
|
4
|
-
import bcrypt from "bcryptjs";
|
|
5
|
-
import { successResponse, errorResponse } from "../utils/response.js";
|
|
6
|
-
import type { Request, Response } from "express";
|
|
7
|
-
|
|
8
|
-
// Login endpoint
|
|
9
|
-
export const login = asyncHandler(async (req: Request, res: Response) => {
|
|
10
|
-
try {
|
|
11
|
-
const { email, password } = req.body;
|
|
12
|
-
|
|
13
|
-
// Find user
|
|
14
|
-
const user = await User.findOne({ email });
|
|
15
|
-
if (!user) {
|
|
16
|
-
return res.status(401).json(errorResponse(null, "Invalid credentials"));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Check password
|
|
20
|
-
const isMatch = await bcrypt.compare(password, user.password);
|
|
21
|
-
if (!isMatch) {
|
|
22
|
-
return res.status(401).json(errorResponse(null, "Invalid credentials"));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Generate token
|
|
26
|
-
const token = jwt.sign(
|
|
27
|
-
{ id: user._id, email: user.email },
|
|
28
|
-
process.env.JWT_SECRET as string,
|
|
29
|
-
{
|
|
30
|
-
expiresIn:
|
|
31
|
-
(process.env.JWT_EXPIRES_IN as SignOptions["expiresIn"]) || "1d",
|
|
32
|
-
}
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
res.json(successResponse({ token }, "Login successful"));
|
|
36
|
-
} catch (error: any) {
|
|
37
|
-
res.status(500).json(errorResponse(error, "Login failed"));
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
export const signup = asyncHandler(async (req: Request, res: Response) => {
|
|
42
|
-
try {
|
|
43
|
-
const { name, email, password, age } = req.body;
|
|
44
|
-
|
|
45
|
-
// Check if user already exists
|
|
46
|
-
const existingUser = await User.findOne({ email });
|
|
47
|
-
if (existingUser) {
|
|
48
|
-
return res.status(400).json(errorResponse(null, "User already exists"));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Hash password
|
|
52
|
-
const hashedPassword = await bcrypt.hash(password, 10);
|
|
53
|
-
|
|
54
|
-
// Create user
|
|
55
|
-
const newUser = await User.create({
|
|
56
|
-
name,
|
|
57
|
-
email,
|
|
58
|
-
password: hashedPassword,
|
|
59
|
-
age: age || 0,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Generate token
|
|
63
|
-
const token = jwt.sign(
|
|
64
|
-
{ id: newUser._id, email: newUser.email },
|
|
65
|
-
process.env.JWT_SECRET as string,
|
|
66
|
-
{
|
|
67
|
-
expiresIn:
|
|
68
|
-
(process.env.JWT_EXPIRES_IN as SignOptions["expiresIn"]) || "1d",
|
|
69
|
-
}
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
res
|
|
73
|
-
.status(201)
|
|
74
|
-
.json(successResponse({ token, user: newUser }, "Signup successful"));
|
|
75
|
-
} catch (error: any) {
|
|
76
|
-
res.status(500).json(errorResponse(error, "Login failed"));
|
|
77
|
-
}
|
|
78
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as userService from "../services/user.service.js";
|
|
2
|
-
import asyncHandler from "../utils/asyncHandler.js";
|
|
3
|
-
import type { Request, Response } from "express";
|
|
4
|
-
|
|
5
|
-
export const getUsers = asyncHandler(async (req: Request, res: Response) => {
|
|
6
|
-
const result = await userService.getUsers();
|
|
7
|
-
res.status(result.status).json(result);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
export const getUser = asyncHandler(async (req: Request, res: Response) => {
|
|
11
|
-
const result = await userService.getUser(req.params.id as string);
|
|
12
|
-
res.status(result.status).json(result);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
export const createUser = asyncHandler(async (req: Request, res: Response) => {
|
|
16
|
-
const result = await userService.createNewUser(req.body);
|
|
17
|
-
res.status(result.status).json(result);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export const updateUser = asyncHandler(async (req: Request, res: Response) => {
|
|
21
|
-
const result = await userService.updateExistingUser(
|
|
22
|
-
req.params.id as string,
|
|
23
|
-
req.body
|
|
24
|
-
);
|
|
25
|
-
res.status(result.status).json(result);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export const deleteUser = asyncHandler(async (req: Request, res: Response) => {
|
|
29
|
-
const result = await userService.deleteExistingUser(req.params.id as string);
|
|
30
|
-
res.status(result.status).json(result);
|
|
31
|
-
});
|
|
@@ -1,82 +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
|
-
import { Request, Response, NextFunction } from "express";
|
|
15
|
-
import type { HttpError } from "http-errors";
|
|
16
|
-
|
|
17
|
-
export const protect = (req: Request, res: Response, next: NextFunction) => {
|
|
18
|
-
try {
|
|
19
|
-
const authHeader = req.headers.authorization;
|
|
20
|
-
|
|
21
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
22
|
-
return res
|
|
23
|
-
.status(401)
|
|
24
|
-
.json(
|
|
25
|
-
errorResponse(
|
|
26
|
-
{ status: 401 } as HttpError<number>,
|
|
27
|
-
"Authorization token missing or malformed"
|
|
28
|
-
)
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Get token from headers
|
|
33
|
-
const token = authHeader.split(" ")[1];
|
|
34
|
-
|
|
35
|
-
if (!token) {
|
|
36
|
-
return res
|
|
37
|
-
.status(401)
|
|
38
|
-
.json(
|
|
39
|
-
errorResponse(
|
|
40
|
-
{ status: 401 } as HttpError<number>,
|
|
41
|
-
"Not authorized, token missing"
|
|
42
|
-
)
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Verify token
|
|
47
|
-
let decoded;
|
|
48
|
-
try {
|
|
49
|
-
decoded = jwt.verify(token, process.env.JWT_SECRET as string);
|
|
50
|
-
} catch (err: any) {
|
|
51
|
-
if (err.name === "TokenExpiredError") {
|
|
52
|
-
return res
|
|
53
|
-
.status(401)
|
|
54
|
-
.json(
|
|
55
|
-
errorResponse({ status: 401 } as HttpError<number>, "Token expired")
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
return res
|
|
59
|
-
.status(401)
|
|
60
|
-
.json(
|
|
61
|
-
errorResponse(
|
|
62
|
-
{ status: 401 } as HttpError<number>,
|
|
63
|
-
"Unauthorized: Invalid token"
|
|
64
|
-
)
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Attach decoded user info to request
|
|
69
|
-
req.user = decoded;
|
|
70
|
-
|
|
71
|
-
next();
|
|
72
|
-
} catch (err: any) {
|
|
73
|
-
return res
|
|
74
|
-
.status(500)
|
|
75
|
-
.json(
|
|
76
|
-
errorResponse(
|
|
77
|
-
{ status: 500 } as HttpError<number>,
|
|
78
|
-
"Server error: " + err.message
|
|
79
|
-
)
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
import { IUser } from "../interfaces/IUser";
|
|
3
|
-
|
|
4
|
-
const userSchema = new mongoose.Schema<IUser>(
|
|
5
|
-
{
|
|
6
|
-
name: { type: String, required: true },
|
|
7
|
-
email: { type: String, required: true, unique: true },
|
|
8
|
-
password: { type: String, required: true },
|
|
9
|
-
age: { type: Number, default: 0 },
|
|
10
|
-
},
|
|
11
|
-
{ timestamps: true }
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
export const User = mongoose.model<IUser>("User", userSchema);
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { User } from "../models/user.model.js";
|
|
2
|
-
import { CreateUserDTO, UpdateUserDTO } from "../dtos/user.dto.js";
|
|
3
|
-
|
|
4
|
-
export const getAllUsers = async () => {
|
|
5
|
-
return await User.find({});
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const getUserById = async (id: string) => {
|
|
9
|
-
return await User.findById(id);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const createUser = async (data: CreateUserDTO) => {
|
|
13
|
-
return await User.create(data);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const updateUser = async (id: string, data: UpdateUserDTO) => {
|
|
17
|
-
return await User.findByIdAndUpdate(id, data, { new: true });
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const deleteUser = async (id: string) => {
|
|
21
|
-
return await User.findByIdAndDelete(id);
|
|
22
|
-
};
|
|
@@ -1,25 +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
|
-
import type { Request, Response } from "express";
|
|
12
|
-
|
|
13
|
-
const userRoutes = Router();
|
|
14
|
-
|
|
15
|
-
userRoutes.get("/protected", protect, (req: Request, res: Response) => {
|
|
16
|
-
res.json(successResponse({ user: req.user }, "Protected route accessed"));
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
userRoutes.get("/users", getUsers);
|
|
20
|
-
userRoutes.get("/users/:id", protect, getUser);
|
|
21
|
-
userRoutes.post("/users", createUser);
|
|
22
|
-
userRoutes.put("/users/:id", updateUser);
|
|
23
|
-
userRoutes.delete("/users/:id", deleteUser);
|
|
24
|
-
|
|
25
|
-
export default userRoutes;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import * as userRepo from "../repositories/user.repository";
|
|
2
|
-
import { successResponse, errorResponse } from "../utils/response";
|
|
3
|
-
import type { HttpError } from "http-errors";
|
|
4
|
-
import { CreateUserDTO, UpdateUserDTO } from "../dtos/user.dto";
|
|
5
|
-
|
|
6
|
-
export const getUsers = async () => {
|
|
7
|
-
try {
|
|
8
|
-
const users = await userRepo.getAllUsers();
|
|
9
|
-
return successResponse(users, "Users fetched successfully");
|
|
10
|
-
} catch (err) {
|
|
11
|
-
return errorResponse(err as HttpError, "Failed to fetch users");
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const getUser = async (id: string) => {
|
|
16
|
-
try {
|
|
17
|
-
const user = await userRepo.getUserById(id);
|
|
18
|
-
if (!user) throw new Error("User not found");
|
|
19
|
-
return successResponse(user, "User fetched successfully");
|
|
20
|
-
} catch (err) {
|
|
21
|
-
return errorResponse(err as HttpError, "Failed to fetch user");
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const createNewUser = async (data: CreateUserDTO) => {
|
|
26
|
-
try {
|
|
27
|
-
const user = await userRepo.createUser(data);
|
|
28
|
-
return successResponse(user, "User created successfully");
|
|
29
|
-
} catch (err) {
|
|
30
|
-
return errorResponse(err as HttpError, "Failed to create user");
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const updateExistingUser = async (id: string, data: UpdateUserDTO) => {
|
|
35
|
-
try {
|
|
36
|
-
const user = await userRepo.updateUser(id, data);
|
|
37
|
-
if (!user) throw new Error("User not found");
|
|
38
|
-
return successResponse(user, "User updated successfully");
|
|
39
|
-
} catch (err) {
|
|
40
|
-
return errorResponse(err as HttpError, "Failed to update user");
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const deleteExistingUser = async (id: string) => {
|
|
45
|
-
try {
|
|
46
|
-
const user = await userRepo.deleteUser(id);
|
|
47
|
-
if (!user) throw new Error("User not found");
|
|
48
|
-
return successResponse(user, "User deleted successfully");
|
|
49
|
-
} catch (err) {
|
|
50
|
-
return errorResponse(err as HttpError, "Failed to delete user");
|
|
51
|
-
}
|
|
52
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Request, Response, NextFunction } from "express";
|
|
2
|
-
|
|
3
|
-
type AsyncHandler = (
|
|
4
|
-
req: Request,
|
|
5
|
-
res: Response,
|
|
6
|
-
next: NextFunction
|
|
7
|
-
) => Promise<any>;
|
|
8
|
-
|
|
9
|
-
const asyncHandler =
|
|
10
|
-
(fn: AsyncHandler) => (req: Request, res: Response, next: NextFunction) => {
|
|
11
|
-
Promise.resolve(fn(req, res, next)).catch(next);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export default asyncHandler;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { HttpError } from "http-errors";
|
|
2
|
-
|
|
3
|
-
export const successResponse = <T>(data: T, message: string = "Success") => ({
|
|
4
|
-
status: 200,
|
|
5
|
-
success: true,
|
|
6
|
-
message,
|
|
7
|
-
data,
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
export const errorResponse = (
|
|
11
|
-
error?: HttpError | null,
|
|
12
|
-
message: string = "Error"
|
|
13
|
-
) => ({
|
|
14
|
-
status: error?.status || 500,
|
|
15
|
-
success: false,
|
|
16
|
-
message,
|
|
17
|
-
error: error?.message || message,
|
|
18
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
// File Layout
|
|
5
|
-
"target": "es2022",
|
|
6
|
-
"module": "esnext",
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"outDir": "./dist",
|
|
9
|
-
// "verbatimModuleSyntax": true,
|
|
10
|
-
// "esModuleInterop": true,
|
|
11
|
-
// "forceConsistentCasingInFileNames": true,
|
|
12
|
-
// "strict": true,
|
|
13
|
-
// "skipLibCheck": true,
|
|
14
|
-
"typeRoots": ["src/interfaces", "src/dtos", "./node_modules/@types"]
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ts-none",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "app.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"dev": "tsx watch src/app.ts",
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [],
|
|
12
|
-
"author": "",
|
|
13
|
-
"license": "ISC",
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"bcryptjs": "^3.0.3",
|
|
16
|
-
"cors": "^2.8.6",
|
|
17
|
-
"dotenv": "^17.3.1",
|
|
18
|
-
"express": "^5.2.1",
|
|
19
|
-
"http-errors": "^2.0.1",
|
|
20
|
-
"jsonwebtoken": "^9.0.3",
|
|
21
|
-
"mongoose": "^9.3.1",
|
|
22
|
-
"morgan": "^1.10.1"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@types/bcryptjs": "^2.4.6",
|
|
26
|
-
"@types/cors": "^2.8.19",
|
|
27
|
-
"@types/express": "^5.0.6",
|
|
28
|
-
"@types/jsonwebtoken": "^9.0.10",
|
|
29
|
-
"@types/morgan": "^1.9.10",
|
|
30
|
-
"@types/node": "^25.5.0",
|
|
31
|
-
"tsx": "^4.21.0",
|
|
32
|
-
"typescript": "^5.9.3"
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import express, { Request, Response, NextFunction } from "express";
|
|
2
|
-
import morgan from "morgan";
|
|
3
|
-
import createError from "http-errors";
|
|
4
|
-
import cors from "cors";
|
|
5
|
-
import dotenv from "dotenv";
|
|
6
|
-
import apiRoutes from "./src/routes/api.route";
|
|
7
|
-
|
|
8
|
-
dotenv.config({ quiet: true });
|
|
9
|
-
const PORT = process.env.PORT || 3000;
|
|
10
|
-
|
|
11
|
-
const app = express();
|
|
12
|
-
|
|
13
|
-
// Middlewares
|
|
14
|
-
app.use(cors());
|
|
15
|
-
app.use(express.json());
|
|
16
|
-
app.use(express.urlencoded({ extended: true }));
|
|
17
|
-
app.use(morgan("dev"));
|
|
18
|
-
|
|
19
|
-
// Routes
|
|
20
|
-
app.get("/", (_: Request, res: Response) =>
|
|
21
|
-
res.json({ message: "API is running 🚀" })
|
|
22
|
-
);
|
|
23
|
-
app.use("/api", apiRoutes); // user routes
|
|
24
|
-
|
|
25
|
-
// 404 handler
|
|
26
|
-
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
27
|
-
next(createError.NotFound());
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// Global error handler
|
|
31
|
-
app.use((err: any, req: Request, res: Response) => {
|
|
32
|
-
res.status(err.status || 500).json({
|
|
33
|
-
success: false,
|
|
34
|
-
message: err.message || "Internal Server Error",
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
app.listen(PORT, () => {
|
|
39
|
-
console.log(`🚀 Server running @ http://localhost:${PORT}`);
|
|
40
|
-
});
|