feat: 标准化响应体

This commit is contained in:
2026-01-11 16:39:59 +08:00
parent cd50716dc6
commit 45047f40aa
26 changed files with 313 additions and 250 deletions

View File

@@ -80,6 +80,7 @@ this.routeScanner.registerControllers([
## TC39 装饰器特性
### 1. 标准语法
```typescript
// TC39 标准装饰器使用 addInitializer
@Get('/users')
@@ -89,6 +90,7 @@ async getUsers(ctx: Context) {
```
### 2. 类型安全
```typescript
// 完整的 TypeScript 类型检查
@Controller('/api')
@@ -101,6 +103,7 @@ export class ApiController {
```
### 3. 无外部依赖
```typescript
// 不再需要 reflect-metadata
// 使用内置的 WeakMap 存储元数据
@@ -136,6 +139,7 @@ export class ApiController {
最终的API路径 = 全局前缀 + 控制器前缀 + 方法路径
例如:
- 全局前缀:`/api`
- 控制器前缀:`/user`
- 方法路径:`/list`
@@ -176,56 +180,11 @@ async getUser(ctx: Context) {
## 现有路由
项目中已注册的路由:
### ApplicationController
- `GET /api/application/list` - 获取应用列表
- `GET /api/application/detail/:id` - 获取应用详情
### UserController
- `GET /api/user/list` - 获取用户列表
- `GET /api/user/detail/:id` - 获取用户详情
- `POST /api/user` - 创建用户
- `PUT /api/user/:id` - 更新用户
- `DELETE /api/user/:id` - 删除用户
- `GET /api/user/search` - 搜索用户
## 与旧版本装饰器的区别
| 特性 | 实验性装饰器 | TC39 标准装饰器 |
|------|-------------|----------------|
| 标准化 | ❌ TypeScript 特有 | ✅ ECMAScript 标准 |
| 依赖 | ❌ 需要 reflect-metadata | ✅ 零依赖 |
| 性能 | ❌ 运行时反射 | ✅ 编译时优化 |
| 类型安全 | ⚠️ 部分支持 | ✅ 完整支持 |
| 未来兼容 | ❌ 可能被废弃 | ✅ 持续演进 |
## 迁移指南
从实验性装饰器迁移到 TC39 标准装饰器:
1. **更新 tsconfig.json**
```json
{
"experimentalDecorators": false,
"emitDecoratorMetadata": false
}
```
2. **移除依赖**
```bash
pnpm remove reflect-metadata
```
3. **代码无需修改**
- 装饰器语法保持不变
- 控制器代码无需修改
- 自动兼容新标准
## 注意事项
1. 需要 TypeScript 5.0+ 支持
2. 需要 Node.js 16+ 运行环境
3. 控制器类需要导出并在路由中间件中注册
4. 控制器方法应该返回数据而不是直接操作 `ctx.body`
5. TC39 装饰器使用 `addInitializer` 进行初始化,性能更优

View File

@@ -1,8 +1,8 @@
import { z } from 'zod';
export const listDeploymentsQuerySchema = z.object({
page: z.coerce.number().int().min(1).optional().default(1),
pageSize: z.coerce.number().int().min(1).max(100).optional().default(10),
page: z.coerce.number().int().min(1).optional(),
pageSize: z.coerce.number().int().min(1).max(100).optional(),
projectId: z.coerce.number().int().positive().optional(),
});

View File

@@ -20,22 +20,28 @@ export class DeploymentController {
where.projectId = projectId;
}
const isPagination = page !== undefined && pageSize !== undefined;
const result = await prisma.deployment.findMany({
where,
take: pageSize,
skip: (page - 1) * pageSize,
take: isPagination ? pageSize : undefined,
skip: isPagination ? (page! - 1) * pageSize! : 0,
orderBy: {
createdAt: 'desc',
},
});
const total = await prisma.deployment.count({ where });
return {
data: result,
page,
pageSize,
total,
};
if (isPagination) {
return {
list: result,
page,
pageSize,
total,
};
}
return result;
}
@Post('')

View File

@@ -66,19 +66,8 @@ export const updateProjectSchema = z.object({
*/
export const listProjectQuerySchema = z
.object({
page: z.coerce
.number()
.int()
.min(1, { message: '页码必须大于0' })
.optional()
.default(1),
limit: z.coerce
.number()
.int()
.min(1, { message: '每页数量必须大于0' })
.max(100, { message: '每页数量不能超过100' })
.optional()
.default(10),
page: z.coerce.number().int().min(1).optional(),
pageSize: z.coerce.number().int().min(1).max(100).optional(),
name: z.string().optional(),
})
.optional();

View File

@@ -29,27 +29,30 @@ export class ProjectController {
};
}
const isPagination = query?.page !== undefined && query?.pageSize !== undefined;
const [total, projects] = await Promise.all([
prisma.project.count({ where: whereCondition }),
prisma.project.findMany({
where: whereCondition,
skip: query ? (query.page - 1) * query.limit : 0,
take: query?.limit,
skip: isPagination ? (query.page! - 1) * query.pageSize! : 0,
take: isPagination ? query.pageSize : undefined,
orderBy: {
createdAt: 'desc',
},
}),
]);
return {
data: projects,
pagination: {
page: query?.page || 1,
limit: query?.limit || 10,
if (isPagination) {
return {
list: projects,
page: query.page,
pageSize: query.pageSize,
total,
totalPages: Math.ceil(total / (query?.limit || 10)),
},
};
};
}
return projects;
}
// GET /api/projects/:id - 获取单个项目

View File

@@ -84,15 +84,13 @@ export const listStepsQuerySchema = z
.number()
.int()
.min(1, { message: '页码必须大于0' })
.optional()
.default(1),
limit: z.coerce
.optional(),
pageSize: z.coerce
.number()
.int()
.min(1, { message: '每页数量必须大于0' })
.max(100, { message: '每页数量不能超过100' })
.optional()
.default(10),
.optional(),
})
.optional();

View File

@@ -26,27 +26,30 @@ export class StepController {
whereCondition.pipelineId = query.pipelineId;
}
const isPagination = query?.page !== undefined && query?.pageSize !== undefined;
const [total, steps] = await Promise.all([
prisma.step.count({ where: whereCondition }),
prisma.step.findMany({
where: whereCondition,
skip: query ? (query.page - 1) * query.limit : 0,
take: query?.limit,
skip: isPagination ? (query.page! - 1) * query.pageSize! : 0,
take: isPagination ? query.pageSize : undefined,
orderBy: {
order: 'asc',
},
}),
]);
return {
data: steps,
pagination: {
page: query?.page || 1,
limit: query?.limit || 10,
if (isPagination) {
return {
list: steps,
page: query.page,
pageSize: query.pageSize,
total,
totalPages: Math.ceil(total / (query?.limit || 10)),
},
};
};
}
return steps;
}
// GET /api/steps/:id - 获取单个步骤

View File

@@ -118,11 +118,6 @@ export class UserController {
results = results.filter((user) => user.status === status);
}
return {
keyword,
status,
total: results.length,
results,
};
return results;
}
}

Binary file not shown.