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

@@ -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;
}
}