feat: 添加路由装饰器系统和全局异常处理
- 新增装饰器支持(@Get, @Post, @Put, @Delete, @Patch, @Controller) - 实现路由自动注册机制(RouteScanner) - 添加全局异常处理中间件(Exception) - 实现统一响应体格式(ApiResponse) - 新增请求体解析中间件(BodyParser) - 重构控制器为类模式,支持装饰器路由 - 添加示例用户控制器(UserController) - 更新TypeScript配置支持装饰器 - 添加reflect-metadata依赖 - 完善项目文档 Breaking Changes: - 控制器现在返回数据而不是直接设置ctx.body - 新增统一的API响应格式
This commit is contained in:
@@ -1,11 +1,50 @@
|
||||
import type { Context } from 'koa';
|
||||
import prisma from '../libs/db.ts';
|
||||
import { BusinessError } from '../middlewares/exception.ts';
|
||||
import { Controller, Get } from '../decorators/route.ts';
|
||||
|
||||
export async function list(ctx: Context) {
|
||||
const list = await prisma.application.findMany({
|
||||
where: {
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
ctx.body = list;
|
||||
@Controller('/application')
|
||||
export class ApplicationController {
|
||||
@Get('/list')
|
||||
async list(ctx: Context) {
|
||||
try {
|
||||
const list = await prisma.application.findMany({
|
||||
where: {
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
// 直接返回数据,由路由中间件统一包装成响应格式
|
||||
return list;
|
||||
} catch (error) {
|
||||
// 抛出业务异常,由全局异常处理中间件捕获
|
||||
throw new BusinessError('获取应用列表失败', 1001, 500);
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/detail/:id')
|
||||
async detail(ctx: Context) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
const app = await prisma.application.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
throw new BusinessError('应用不存在', 1002, 404);
|
||||
}
|
||||
|
||||
return app;
|
||||
} catch (error) {
|
||||
if (error instanceof BusinessError) {
|
||||
throw error;
|
||||
}
|
||||
throw new BusinessError('获取应用详情失败', 1003, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保持向后兼容的导出方式
|
||||
const applicationController = new ApplicationController();
|
||||
export const list = applicationController.list.bind(applicationController);
|
||||
export const detail = applicationController.detail.bind(applicationController);
|
||||
|
||||
117
apps/server/controllers/user.ts
Normal file
117
apps/server/controllers/user.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import type { Context } from 'koa';
|
||||
import { Controller, Get, Post, Put, Delete } from '../decorators/route.ts';
|
||||
import { BusinessError } from '../middlewares/exception.ts';
|
||||
|
||||
/**
|
||||
* 用户控制器
|
||||
*/
|
||||
@Controller('/user')
|
||||
export class UserController {
|
||||
|
||||
@Get('/list')
|
||||
async list(ctx: Context) {
|
||||
// 模拟用户列表数据
|
||||
const users = [
|
||||
{ id: 1, name: 'Alice', email: 'alice@example.com', status: 'active' },
|
||||
{ id: 2, name: 'Bob', email: 'bob@example.com', status: 'inactive' },
|
||||
{ id: 3, name: 'Charlie', email: 'charlie@example.com', status: 'active' }
|
||||
];
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
@Get('/detail/:id')
|
||||
async detail(ctx: Context) {
|
||||
const { id } = ctx.params;
|
||||
|
||||
// 模拟根据ID查找用户
|
||||
const user = {
|
||||
id: Number(id),
|
||||
name: 'User ' + id,
|
||||
email: `user${id}@example.com`,
|
||||
status: 'active',
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
if (Number(id) > 100) {
|
||||
throw new BusinessError('用户不存在', 2001, 404);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Post('')
|
||||
async create(ctx: Context) {
|
||||
const body = (ctx.request as any).body;
|
||||
|
||||
// 模拟创建用户
|
||||
const newUser = {
|
||||
id: Date.now(),
|
||||
...body,
|
||||
createdAt: new Date().toISOString(),
|
||||
status: 'active'
|
||||
};
|
||||
|
||||
return newUser;
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
async update(ctx: Context) {
|
||||
const { id } = ctx.params;
|
||||
const body = (ctx.request as any).body;
|
||||
|
||||
// 模拟更新用户
|
||||
const updatedUser = {
|
||||
id: Number(id),
|
||||
...body,
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
return updatedUser;
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
async delete(ctx: Context) {
|
||||
const { id } = ctx.params;
|
||||
|
||||
if (Number(id) === 1) {
|
||||
throw new BusinessError('管理员账户不能删除', 2002, 403);
|
||||
}
|
||||
|
||||
// 模拟删除操作
|
||||
return {
|
||||
success: true,
|
||||
message: `用户 ${id} 已删除`,
|
||||
deletedAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
@Get('/search')
|
||||
async search(ctx: Context) {
|
||||
const { keyword, status } = ctx.query;
|
||||
|
||||
// 模拟搜索逻辑
|
||||
let results = [
|
||||
{ id: 1, name: 'Alice', email: 'alice@example.com', status: 'active' },
|
||||
{ id: 2, name: 'Bob', email: 'bob@example.com', status: 'inactive' }
|
||||
];
|
||||
|
||||
if (keyword) {
|
||||
results = results.filter(user =>
|
||||
user.name.toLowerCase().includes(String(keyword).toLowerCase()) ||
|
||||
user.email.toLowerCase().includes(String(keyword).toLowerCase())
|
||||
);
|
||||
}
|
||||
|
||||
if (status) {
|
||||
results = results.filter(user => user.status === status);
|
||||
}
|
||||
|
||||
return {
|
||||
keyword,
|
||||
status,
|
||||
total: results.length,
|
||||
results
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user