refactor: 将 discovery tasks 接口参数改为 status (支持逗号分隔多值)
- 移除 `statuses` 参数,统一使用 `status` 参数 - 支持单状态:?status=PENDING - 支持多状态:?status=PENDING,FAILED - 简化 API 设计,更符合 REST 规范
This commit is contained in:
@@ -105,7 +105,6 @@ export async function GET(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
const validation = GetDiscoveryTasksQuerySchema.safeParse({
|
const validation = GetDiscoveryTasksQuerySchema.safeParse({
|
||||||
status: searchParams.get('status') || undefined,
|
status: searchParams.get('status') || undefined,
|
||||||
statuses: searchParams.get('statuses') || undefined, // 新增:支持多状态查询
|
|
||||||
limit: searchParams.get('limit') || '10',
|
limit: searchParams.get('limit') || '10',
|
||||||
offset: searchParams.get('offset') || '0',
|
offset: searchParams.get('offset') || '0',
|
||||||
})
|
})
|
||||||
@@ -121,16 +120,17 @@ export async function GET(request: NextRequest) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { status, statuses, limit, offset } = validation.data
|
const { status, limit, offset } = validation.data
|
||||||
|
|
||||||
// 构建查询条件:支持单状态和多状态查询
|
// 构建查询条件:支持单状态或多状态(逗号分隔)
|
||||||
let whereClause = {}
|
let whereClause = {}
|
||||||
if (statuses && statuses.length > 0) {
|
if (status) {
|
||||||
// 多状态查询:?statuses=PENDING,FAILED
|
// 支持单状态 (?status=PENDING) 或多状态 (?status=PENDING,FAILED)
|
||||||
whereClause = { status: { in: statuses } }
|
if (Array.isArray(status)) {
|
||||||
} else if (status) {
|
whereClause = { status: { in: status } }
|
||||||
// 单状态查询(向后兼容):?status=PENDING
|
} else {
|
||||||
whereClause = { status }
|
whereClause = { status }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const tasks = await prisma.projectDiscoveryTask.findMany({
|
const tasks = await prisma.projectDiscoveryTask.findMany({
|
||||||
|
|||||||
@@ -104,11 +104,11 @@ export const UpdateDiscoveryTaskSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const GetDiscoveryTasksQuerySchema = z.object({
|
export const GetDiscoveryTasksQuerySchema = z.object({
|
||||||
status: TaskStatusEnum.optional(), // 保持向后兼容:单状态查询
|
status: z.string().optional().transform((val) => {
|
||||||
statuses: z.string().optional().transform((val) => {
|
// 支持单状态或多状态(逗号分隔),如 "PENDING" 或 "PENDING,FAILED"
|
||||||
// 将逗号分隔的字符串转换为数组,如 "PENDING,FAILED" → ["PENDING", "FAILED"]
|
|
||||||
if (!val) return undefined;
|
if (!val) return undefined;
|
||||||
return val.split(',').map(s => s.trim() as TaskStatus).filter(Boolean);
|
const statuses = val.split(',').map(s => s.trim() as TaskStatus).filter(Boolean);
|
||||||
|
return statuses.length === 1 ? statuses[0] : statuses;
|
||||||
}),
|
}),
|
||||||
limit: z.coerce.number().int().positive().max(100).default(10),
|
limit: z.coerce.number().int().positive().max(100).default(10),
|
||||||
offset: z.coerce.number().int().nonnegative().default(0),
|
offset: z.coerce.number().int().nonnegative().default(0),
|
||||||
|
|||||||
Reference in New Issue
Block a user