Browse Source

✅ test(users): 修复用户API测试中的参数类型问题

- 将用户ID参数从字符串类型改为数字类型,与API要求保持一致
- 移除不必要的toString()转换,简化测试代码
- 确保测试用例中的ID参数类型统一为数字

♻️ refactor(crud): 优化通用CRUD路由的ID参数类型定义

- 为z.coerce.number()添加明确的类型参数<number>
- 增强类型安全性,确保ID参数的类型一致性
- 改进OpenAPI文档生成的准确性
yourname 2 months ago
parent
commit
fe5abced25

+ 8 - 8
src/server/api/users/__tests__/users.integration.test.ts

@@ -174,7 +174,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
       });
 
       const response = await client.users[':id'].$get({
-        param: { id: testUser.id.toString() }
+        param: { id: testUser.id }
       },
       {
         headers: {
@@ -193,7 +193,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
 
     it('应该返回404当用户不存在时', async () => {
       const response = await client.users[':id'].$get({
-        param: { id: '999999' }
+        param: { id: 999999 }
       },
       {
         headers: {
@@ -224,7 +224,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
       };
 
       const response = await client.users[':id'].$put({
-        param: { id: testUser.id.toString() },
+        param: { id: testUser.id },
         json: updateData
       },
       {
@@ -242,7 +242,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
 
       // 验证数据库中的更新
       const getResponse = await client.users[':id'].$get({
-        param: { id: testUser.id.toString() }
+        param: { id: testUser.id }
       });
       expect(getResponse.status).toBe(200);
       if (getResponse.status === 200) {
@@ -258,7 +258,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
       };
 
       const response = await client.users[':id'].$put({
-        param: { id: '999999' },
+        param: { id: 999999 },
         json: updateData
       },
       {
@@ -285,7 +285,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
       });
 
       const response = await client.users[':id'].$delete({
-        param: { id: testUser.id.toString() }
+        param: { id: testUser.id }
       },
       {
         headers: {
@@ -300,14 +300,14 @@ describe('用户API集成测试 (使用hono/testing)', () => {
 
       // 验证再次获取用户返回404
       const getResponse = await client.users[':id'].$get({
-        param: { id: testUser.id.toString() }
+        param: { id: testUser.id }
       });
       IntegrationTestAssertions.expectStatus(getResponse, 404);
     });
 
     it('应该返回404当删除不存在的用户时', async () => {
       const response = await client.users[':id'].$delete({
-        param: { id: '999999' }
+        param: { id: 999999 }
       },
       {
         headers: {

+ 3 - 3
src/server/utils/generic-crud.routes.ts

@@ -124,7 +124,7 @@ export function createCrudRoutes<
     middleware,
     request: {
       params: z.object({
-        id: z.coerce.number().openapi({
+        id: z.coerce.number<number>().openapi({
           param: { name: 'id', in: 'path' },
           example: 1,
           description: '资源ID'
@@ -158,7 +158,7 @@ export function createCrudRoutes<
     middleware,
     request: {
       params: z.object({
-        id: z.coerce.number().openapi({
+        id: z.coerce.number<number>().openapi({
           param: { name: 'id', in: 'path' },
           example: 1,
           description: '资源ID'
@@ -197,7 +197,7 @@ export function createCrudRoutes<
     middleware,
     request: {
       params: z.object({
-        id: z.coerce.number().openapi({
+        id: z.coerce.number<number>().openapi({
           param: { name: 'id', in: 'path' },
           example: 1,
           description: '资源ID'