deploy-bbc 1.0.0 → 1.2.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/CLAUDE.md +85 -11
- package/cli/package.json +9 -9
- package/cli/src/cli/index.ts +8 -3
- package/cli/src/helpers/log-next-steps.ts +22 -19
- package/cli/src/helpers/scaffold-project.ts +30 -4
- package/cli/src/templates/base/package.json +2 -1
- package/cli/src/templates/base/src/controllers/user/create-user.controller.ts +84 -0
- package/cli/src/templates/base/src/controllers/user/delete-user.controller.ts +60 -0
- package/cli/src/templates/base/src/controllers/user/get-user.controller.ts +74 -0
- package/cli/src/templates/base/src/controllers/user/get-users.controller.ts +41 -0
- package/cli/src/templates/base/src/controllers/user/update-user.controller.ts +111 -0
- package/cli/src/templates/base/src/models/user.model.ts +83 -0
- package/cli/src/templates/base/src/routes/index.ts +5 -0
- package/cli/src/templates/base/src/routes/user.route.ts +17 -0
- package/cli/src/templates/base/src/types/common/api-response.types.ts +30 -0
- package/cli/src/templates/base/src/types/index.ts +5 -2
- package/cli/src/templates/base/src/types/models/user.types.ts +26 -0
- package/cli/src/templates/base-bun-native/package.json +2 -1
- package/cli/src/templates/base-bun-native/src/controllers/user/create-user.controller.ts +76 -0
- package/cli/src/templates/base-bun-native/src/controllers/user/delete-user.controller.ts +53 -0
- package/cli/src/templates/base-bun-native/src/controllers/user/get-user.controller.ts +67 -0
- package/cli/src/templates/base-bun-native/src/controllers/user/get-users.controller.ts +40 -0
- package/cli/src/templates/base-bun-native/src/controllers/user/update-user.controller.ts +101 -0
- package/cli/src/templates/base-bun-native/src/index.ts +3 -3
- package/cli/src/templates/base-bun-native/src/models/user.model.ts +83 -0
- package/cli/src/templates/base-bun-native/src/routes/index.ts +10 -6
- package/cli/src/templates/base-bun-native/src/routes/user.route.ts +50 -0
- package/cli/src/templates/base-bun-native/src/types/common/api-response.types.ts +30 -0
- package/cli/src/templates/base-bun-native/src/types/index.ts +5 -2
- package/cli/src/templates/base-bun-native/src/types/models/user.types.ts +26 -0
- package/cli/src/templates/base-express/package.json +2 -1
- package/cli/src/templates/base-express/src/controllers/user/create-user.controller.ts +75 -0
- package/cli/src/templates/base-express/src/controllers/user/delete-user.controller.ts +56 -0
- package/cli/src/templates/base-express/src/controllers/user/get-user.controller.ts +70 -0
- package/cli/src/templates/base-express/src/controllers/user/get-users.controller.ts +41 -0
- package/cli/src/templates/base-express/src/controllers/user/update-user.controller.ts +102 -0
- package/cli/src/templates/base-express/src/models/user.model.ts +83 -0
- package/cli/src/templates/base-express/src/routes/index.ts +5 -0
- package/cli/src/templates/base-express/src/routes/user.route.ts +17 -0
- package/cli/src/templates/base-express/src/types/common/api-response.types.ts +30 -0
- package/cli/src/templates/base-express/src/types/index.ts +5 -2
- package/cli/src/templates/base-express/src/types/models/user.types.ts +26 -0
- package/cli/src/utils/parse-name-and-path.ts +18 -2
- package/package.json +3 -3
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Context } from "hono";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { user_model } from "../../models/user.model.js";
|
|
4
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
5
|
+
import type { UserResponse } from "../../types/models/user.types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validation schemas
|
|
9
|
+
*/
|
|
10
|
+
const user_id_schema = z.string().uuid("Invalid user ID format");
|
|
11
|
+
const update_user_schema = z.object({
|
|
12
|
+
email: z.string().email("Invalid email address").optional(),
|
|
13
|
+
name: z.string().min(2, "Name must be at least 2 characters").max(100, "Name must not exceed 100 characters").optional(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Helper to format user response
|
|
18
|
+
*/
|
|
19
|
+
function format_user_response(user: any): UserResponse {
|
|
20
|
+
return {
|
|
21
|
+
id: user.id,
|
|
22
|
+
email: user.email,
|
|
23
|
+
name: user.name,
|
|
24
|
+
createdAt: user.createdAt.toISOString(),
|
|
25
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Update user by ID
|
|
31
|
+
* PATCH /users/:id
|
|
32
|
+
*/
|
|
33
|
+
export async function update_user(c: Context) {
|
|
34
|
+
try {
|
|
35
|
+
const id = c.req.param("id");
|
|
36
|
+
const body = await c.req.json();
|
|
37
|
+
|
|
38
|
+
// Validate ID format
|
|
39
|
+
const id_validation = user_id_schema.safeParse(id);
|
|
40
|
+
if (!id_validation.success) {
|
|
41
|
+
return c.json<ApiError>(
|
|
42
|
+
{
|
|
43
|
+
success: false,
|
|
44
|
+
error: "Invalid user ID",
|
|
45
|
+
details: id_validation.error.errors.map((err) => ({
|
|
46
|
+
field: err.path.join("."),
|
|
47
|
+
message: err.message,
|
|
48
|
+
})),
|
|
49
|
+
},
|
|
50
|
+
400
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Validate request body
|
|
55
|
+
const body_validation = update_user_schema.safeParse(body);
|
|
56
|
+
if (!body_validation.success) {
|
|
57
|
+
return c.json<ApiError>(
|
|
58
|
+
{
|
|
59
|
+
success: false,
|
|
60
|
+
error: "Validation failed",
|
|
61
|
+
details: body_validation.error.errors.map((err) => ({
|
|
62
|
+
field: err.path.join("."),
|
|
63
|
+
message: err.message,
|
|
64
|
+
})),
|
|
65
|
+
},
|
|
66
|
+
400
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Check if email is being updated and if it already exists
|
|
71
|
+
if (body_validation.data.email) {
|
|
72
|
+
const existing_user = await user_model.find_by_email(body_validation.data.email);
|
|
73
|
+
if (existing_user && existing_user.id !== id) {
|
|
74
|
+
return c.json<ApiError>(
|
|
75
|
+
{
|
|
76
|
+
success: false,
|
|
77
|
+
error: "User with this email already exists",
|
|
78
|
+
},
|
|
79
|
+
409
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Update user
|
|
85
|
+
const user = await user_model.update(id, body_validation.data);
|
|
86
|
+
|
|
87
|
+
if (!user) {
|
|
88
|
+
return c.json<ApiError>(
|
|
89
|
+
{
|
|
90
|
+
success: false,
|
|
91
|
+
error: "User not found",
|
|
92
|
+
},
|
|
93
|
+
404
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return c.json<ApiResponse<UserResponse>>({
|
|
98
|
+
success: true,
|
|
99
|
+
data: format_user_response(user),
|
|
100
|
+
message: "User updated successfully",
|
|
101
|
+
});
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return c.json<ApiError>(
|
|
104
|
+
{
|
|
105
|
+
success: false,
|
|
106
|
+
error: "Failed to update user",
|
|
107
|
+
},
|
|
108
|
+
500
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { User, CreateUserInput, UpdateUserInput } from "../types/models/user.types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* User model
|
|
5
|
+
*
|
|
6
|
+
* This is a simple in-memory implementation.
|
|
7
|
+
* Replace with your database ORM (Drizzle, Prisma, etc.) in production.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// In-memory storage (for demonstration)
|
|
11
|
+
const users: User[] = [];
|
|
12
|
+
|
|
13
|
+
export const user_model = {
|
|
14
|
+
/**
|
|
15
|
+
* Find all users
|
|
16
|
+
*/
|
|
17
|
+
find_all: async (): Promise<User[]> => {
|
|
18
|
+
return users;
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Find user by ID
|
|
23
|
+
*/
|
|
24
|
+
find_by_id: async (id: string): Promise<User | undefined> => {
|
|
25
|
+
return users.find((user) => user.id === id);
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Find user by email
|
|
30
|
+
*/
|
|
31
|
+
find_by_email: async (email: string): Promise<User | undefined> => {
|
|
32
|
+
return users.find((user) => user.email === email);
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a new user
|
|
37
|
+
*/
|
|
38
|
+
create: async (input: CreateUserInput): Promise<User> => {
|
|
39
|
+
const user: User = {
|
|
40
|
+
id: crypto.randomUUID(),
|
|
41
|
+
email: input.email,
|
|
42
|
+
name: input.name,
|
|
43
|
+
createdAt: new Date(),
|
|
44
|
+
updatedAt: new Date(),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
users.push(user);
|
|
48
|
+
return user;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Update user by ID
|
|
53
|
+
*/
|
|
54
|
+
update: async (id: string, input: UpdateUserInput): Promise<User | undefined> => {
|
|
55
|
+
const index = users.findIndex((user) => user.id === id);
|
|
56
|
+
|
|
57
|
+
if (index === -1) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
users[index] = {
|
|
62
|
+
...users[index],
|
|
63
|
+
...input,
|
|
64
|
+
updatedAt: new Date(),
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return users[index];
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Delete user by ID
|
|
72
|
+
*/
|
|
73
|
+
delete: async (id: string): Promise<boolean> => {
|
|
74
|
+
const index = users.findIndex((user) => user.id === id);
|
|
75
|
+
|
|
76
|
+
if (index === -1) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
users.splice(index, 1);
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
|
+
import user_route from "./user.route.js";
|
|
2
3
|
|
|
3
4
|
const routes = new Hono();
|
|
4
5
|
|
|
6
|
+
// Root route
|
|
5
7
|
routes.get("/", (c) => {
|
|
6
8
|
return c.json({
|
|
7
9
|
message: "Welcome to your Bun backend!",
|
|
@@ -9,4 +11,7 @@ routes.get("/", (c) => {
|
|
|
9
11
|
});
|
|
10
12
|
});
|
|
11
13
|
|
|
14
|
+
// User routes
|
|
15
|
+
routes.route("/users", user_route);
|
|
16
|
+
|
|
12
17
|
export default routes;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { get_users } from "../controllers/user/get-users.controller.js";
|
|
3
|
+
import { get_user } from "../controllers/user/get-user.controller.js";
|
|
4
|
+
import { create_user } from "../controllers/user/create-user.controller.js";
|
|
5
|
+
import { update_user } from "../controllers/user/update-user.controller.js";
|
|
6
|
+
import { delete_user } from "../controllers/user/delete-user.controller.js";
|
|
7
|
+
|
|
8
|
+
const user_route = new Hono();
|
|
9
|
+
|
|
10
|
+
// User CRUD routes
|
|
11
|
+
user_route.get("/", get_users);
|
|
12
|
+
user_route.get("/:id", get_user);
|
|
13
|
+
user_route.post("/", create_user);
|
|
14
|
+
user_route.patch("/:id", update_user);
|
|
15
|
+
user_route.delete("/:id", delete_user);
|
|
16
|
+
|
|
17
|
+
export default user_route;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common API response types
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface ApiResponse<T = unknown> {
|
|
6
|
+
success: boolean;
|
|
7
|
+
data?: T;
|
|
8
|
+
error?: string;
|
|
9
|
+
message?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface PaginatedResponse<T> extends ApiResponse<T[]> {
|
|
13
|
+
pagination: {
|
|
14
|
+
page: number;
|
|
15
|
+
limit: number;
|
|
16
|
+
total: number;
|
|
17
|
+
totalPages: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ValidationError {
|
|
22
|
+
field: string;
|
|
23
|
+
message: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ApiError {
|
|
27
|
+
success: false;
|
|
28
|
+
error: string;
|
|
29
|
+
details?: ValidationError[];
|
|
30
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User model types
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface User {
|
|
6
|
+
id: string;
|
|
7
|
+
email: string;
|
|
8
|
+
name: string;
|
|
9
|
+
createdAt: Date;
|
|
10
|
+
updatedAt: Date;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface CreateUserInput {
|
|
14
|
+
email: string;
|
|
15
|
+
name: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface UpdateUserInput {
|
|
19
|
+
email?: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type UserResponse = Omit<User, "createdAt" | "updatedAt"> & {
|
|
24
|
+
createdAt: string;
|
|
25
|
+
updatedAt: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { user_model } from "../../models/user.model.js";
|
|
3
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
4
|
+
import type { UserResponse } from "../../types/models/user.types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validation schema
|
|
8
|
+
*/
|
|
9
|
+
const create_user_schema = z.object({
|
|
10
|
+
email: z.string().email("Invalid email address"),
|
|
11
|
+
name: z.string().min(2, "Name must be at least 2 characters").max(100, "Name must not exceed 100 characters"),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Helper to format user response
|
|
16
|
+
*/
|
|
17
|
+
function format_user_response(user: any): UserResponse {
|
|
18
|
+
return {
|
|
19
|
+
id: user.id,
|
|
20
|
+
email: user.email,
|
|
21
|
+
name: user.name,
|
|
22
|
+
createdAt: user.createdAt.toISOString(),
|
|
23
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a new user
|
|
29
|
+
* POST /users
|
|
30
|
+
*/
|
|
31
|
+
export async function create_user(req: Request): Promise<Response> {
|
|
32
|
+
try {
|
|
33
|
+
const body = await req.json();
|
|
34
|
+
|
|
35
|
+
// Validate request body
|
|
36
|
+
const validation = create_user_schema.safeParse(body);
|
|
37
|
+
if (!validation.success) {
|
|
38
|
+
const errorResponse: ApiError = {
|
|
39
|
+
success: false,
|
|
40
|
+
error: "Validation failed",
|
|
41
|
+
details: validation.error.errors.map((err) => ({
|
|
42
|
+
field: err.path.join("."),
|
|
43
|
+
message: err.message,
|
|
44
|
+
})),
|
|
45
|
+
};
|
|
46
|
+
return Response.json(errorResponse, { status: 400 });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check if email already exists
|
|
50
|
+
const existing_user = await user_model.find_by_email(validation.data.email);
|
|
51
|
+
if (existing_user) {
|
|
52
|
+
const errorResponse: ApiError = {
|
|
53
|
+
success: false,
|
|
54
|
+
error: "User with this email already exists",
|
|
55
|
+
};
|
|
56
|
+
return Response.json(errorResponse, { status: 409 });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Create user
|
|
60
|
+
const user = await user_model.create(validation.data);
|
|
61
|
+
|
|
62
|
+
const response: ApiResponse<UserResponse> = {
|
|
63
|
+
success: true,
|
|
64
|
+
data: format_user_response(user),
|
|
65
|
+
message: "User created successfully",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return Response.json(response, { status: 201 });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const errorResponse: ApiError = {
|
|
71
|
+
success: false,
|
|
72
|
+
error: "Failed to create user",
|
|
73
|
+
};
|
|
74
|
+
return Response.json(errorResponse, { status: 500 });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { user_model } from "../../models/user.model.js";
|
|
3
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validation schema
|
|
7
|
+
*/
|
|
8
|
+
const user_id_schema = z.string().uuid("Invalid user ID format");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Delete user by ID
|
|
12
|
+
* DELETE /users/:id
|
|
13
|
+
*/
|
|
14
|
+
export async function delete_user(id: string): Promise<Response> {
|
|
15
|
+
try {
|
|
16
|
+
// Validate ID format
|
|
17
|
+
const validation = user_id_schema.safeParse(id);
|
|
18
|
+
if (!validation.success) {
|
|
19
|
+
const errorResponse: ApiError = {
|
|
20
|
+
success: false,
|
|
21
|
+
error: "Invalid user ID",
|
|
22
|
+
details: validation.error.errors.map((err) => ({
|
|
23
|
+
field: err.path.join("."),
|
|
24
|
+
message: err.message,
|
|
25
|
+
})),
|
|
26
|
+
};
|
|
27
|
+
return Response.json(errorResponse, { status: 400 });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const deleted = await user_model.delete(id);
|
|
31
|
+
|
|
32
|
+
if (!deleted) {
|
|
33
|
+
const errorResponse: ApiError = {
|
|
34
|
+
success: false,
|
|
35
|
+
error: "User not found",
|
|
36
|
+
};
|
|
37
|
+
return Response.json(errorResponse, { status: 404 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const response: ApiResponse = {
|
|
41
|
+
success: true,
|
|
42
|
+
message: "User deleted successfully",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return Response.json(response);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
const errorResponse: ApiError = {
|
|
48
|
+
success: false,
|
|
49
|
+
error: "Failed to delete user",
|
|
50
|
+
};
|
|
51
|
+
return Response.json(errorResponse, { status: 500 });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { user_model } from "../../models/user.model.js";
|
|
3
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
4
|
+
import type { UserResponse } from "../../types/models/user.types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validation schema
|
|
8
|
+
*/
|
|
9
|
+
const user_id_schema = z.string().uuid("Invalid user ID format");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Helper to format user response
|
|
13
|
+
*/
|
|
14
|
+
function format_user_response(user: any): UserResponse {
|
|
15
|
+
return {
|
|
16
|
+
id: user.id,
|
|
17
|
+
email: user.email,
|
|
18
|
+
name: user.name,
|
|
19
|
+
createdAt: user.createdAt.toISOString(),
|
|
20
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get user by ID
|
|
26
|
+
* GET /users/:id
|
|
27
|
+
*/
|
|
28
|
+
export async function get_user(id: string): Promise<Response> {
|
|
29
|
+
try {
|
|
30
|
+
// Validate ID format
|
|
31
|
+
const validation = user_id_schema.safeParse(id);
|
|
32
|
+
if (!validation.success) {
|
|
33
|
+
const errorResponse: ApiError = {
|
|
34
|
+
success: false,
|
|
35
|
+
error: "Invalid user ID",
|
|
36
|
+
details: validation.error.errors.map((err) => ({
|
|
37
|
+
field: err.path.join("."),
|
|
38
|
+
message: err.message,
|
|
39
|
+
})),
|
|
40
|
+
};
|
|
41
|
+
return Response.json(errorResponse, { status: 400 });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const user = await user_model.find_by_id(id);
|
|
45
|
+
|
|
46
|
+
if (!user) {
|
|
47
|
+
const errorResponse: ApiError = {
|
|
48
|
+
success: false,
|
|
49
|
+
error: "User not found",
|
|
50
|
+
};
|
|
51
|
+
return Response.json(errorResponse, { status: 404 });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const response: ApiResponse<UserResponse> = {
|
|
55
|
+
success: true,
|
|
56
|
+
data: format_user_response(user),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return Response.json(response);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const errorResponse: ApiError = {
|
|
62
|
+
success: false,
|
|
63
|
+
error: "Failed to fetch user",
|
|
64
|
+
};
|
|
65
|
+
return Response.json(errorResponse, { status: 500 });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { user_model } from "../../models/user.model.js";
|
|
2
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
3
|
+
import type { UserResponse } from "../../types/models/user.types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Helper to format user response
|
|
7
|
+
*/
|
|
8
|
+
function format_user_response(user: any): UserResponse {
|
|
9
|
+
return {
|
|
10
|
+
id: user.id,
|
|
11
|
+
email: user.email,
|
|
12
|
+
name: user.name,
|
|
13
|
+
createdAt: user.createdAt.toISOString(),
|
|
14
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get all users
|
|
20
|
+
* GET /users
|
|
21
|
+
*/
|
|
22
|
+
export async function get_users(): Promise<Response> {
|
|
23
|
+
try {
|
|
24
|
+
const users = await user_model.find_all();
|
|
25
|
+
const formatted_users = users.map(format_user_response);
|
|
26
|
+
|
|
27
|
+
const response: ApiResponse<UserResponse[]> = {
|
|
28
|
+
success: true,
|
|
29
|
+
data: formatted_users,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return Response.json(response);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
const errorResponse: ApiError = {
|
|
35
|
+
success: false,
|
|
36
|
+
error: "Failed to fetch users",
|
|
37
|
+
};
|
|
38
|
+
return Response.json(errorResponse, { status: 500 });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { user_model } from "../../models/user.model.js";
|
|
3
|
+
import type { ApiResponse, ApiError } from "../../types/common/api-response.types.js";
|
|
4
|
+
import type { UserResponse } from "../../types/models/user.types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validation schemas
|
|
8
|
+
*/
|
|
9
|
+
const user_id_schema = z.string().uuid("Invalid user ID format");
|
|
10
|
+
const update_user_schema = z.object({
|
|
11
|
+
email: z.string().email("Invalid email address").optional(),
|
|
12
|
+
name: z.string().min(2, "Name must be at least 2 characters").max(100, "Name must not exceed 100 characters").optional(),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Helper to format user response
|
|
17
|
+
*/
|
|
18
|
+
function format_user_response(user: any): UserResponse {
|
|
19
|
+
return {
|
|
20
|
+
id: user.id,
|
|
21
|
+
email: user.email,
|
|
22
|
+
name: user.name,
|
|
23
|
+
createdAt: user.createdAt.toISOString(),
|
|
24
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Update user by ID
|
|
30
|
+
* PATCH /users/:id
|
|
31
|
+
*/
|
|
32
|
+
export async function update_user(id: string, req: Request): Promise<Response> {
|
|
33
|
+
try {
|
|
34
|
+
const body = await req.json();
|
|
35
|
+
|
|
36
|
+
// Validate ID format
|
|
37
|
+
const id_validation = user_id_schema.safeParse(id);
|
|
38
|
+
if (!id_validation.success) {
|
|
39
|
+
const errorResponse: ApiError = {
|
|
40
|
+
success: false,
|
|
41
|
+
error: "Invalid user ID",
|
|
42
|
+
details: id_validation.error.errors.map((err) => ({
|
|
43
|
+
field: err.path.join("."),
|
|
44
|
+
message: err.message,
|
|
45
|
+
})),
|
|
46
|
+
};
|
|
47
|
+
return Response.json(errorResponse, { status: 400 });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Validate request body
|
|
51
|
+
const body_validation = update_user_schema.safeParse(body);
|
|
52
|
+
if (!body_validation.success) {
|
|
53
|
+
const errorResponse: ApiError = {
|
|
54
|
+
success: false,
|
|
55
|
+
error: "Validation failed",
|
|
56
|
+
details: body_validation.error.errors.map((err) => ({
|
|
57
|
+
field: err.path.join("."),
|
|
58
|
+
message: err.message,
|
|
59
|
+
})),
|
|
60
|
+
};
|
|
61
|
+
return Response.json(errorResponse, { status: 400 });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Check if email is being updated and if it already exists
|
|
65
|
+
if (body_validation.data.email) {
|
|
66
|
+
const existing_user = await user_model.find_by_email(body_validation.data.email);
|
|
67
|
+
if (existing_user && existing_user.id !== id) {
|
|
68
|
+
const errorResponse: ApiError = {
|
|
69
|
+
success: false,
|
|
70
|
+
error: "User with this email already exists",
|
|
71
|
+
};
|
|
72
|
+
return Response.json(errorResponse, { status: 409 });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Update user
|
|
77
|
+
const user = await user_model.update(id, body_validation.data);
|
|
78
|
+
|
|
79
|
+
if (!user) {
|
|
80
|
+
const errorResponse: ApiError = {
|
|
81
|
+
success: false,
|
|
82
|
+
error: "User not found",
|
|
83
|
+
};
|
|
84
|
+
return Response.json(errorResponse, { status: 404 });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const response: ApiResponse<UserResponse> = {
|
|
88
|
+
success: true,
|
|
89
|
+
data: format_user_response(user),
|
|
90
|
+
message: "User updated successfully",
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return Response.json(response);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const errorResponse: ApiError = {
|
|
96
|
+
success: false,
|
|
97
|
+
error: "Failed to update user",
|
|
98
|
+
};
|
|
99
|
+
return Response.json(errorResponse, { status: 500 });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -6,8 +6,8 @@ import { router } from "./routes/index.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Simple router matcher for Bun native server
|
|
8
8
|
*/
|
|
9
|
-
function match_route(pathname: string, method: string): Response | null {
|
|
10
|
-
return router(pathname, method);
|
|
9
|
+
async function match_route(pathname: string, method: string, req: Request): Promise<Response | null> {
|
|
10
|
+
return await router(pathname, method, req);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
const server = Bun.serve({
|
|
@@ -28,7 +28,7 @@ const server = Bun.serve({
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
// Match routes
|
|
31
|
-
const response = match_route(url.pathname, req.method);
|
|
31
|
+
const response = await match_route(url.pathname, req.method, req);
|
|
32
32
|
if (response) {
|
|
33
33
|
logger(req, response, Date.now() - start);
|
|
34
34
|
return response;
|