Browse Source

🔥 feat(users): remove individual user CRUD API endpoints

- 删除用户详情获取接口(src/server/api/users/[id]/get.ts)
- 删除用户更新接口(src/server/api/users/[id]/put.ts)
- 删除用户删除接口(src/server/api/users/[id]/delete.ts)

♻️ refactor(test): optimize vitest coverage exclusion configuration

- 移除vitest.config.ts中重复的配置文件排除规则
- 添加测试工具目录(src/server/__test_utils__)到覆盖率排除列表
yourname 2 months ago
parent
commit
fa44bbbb8e

+ 0 - 54
src/server/api/users/[id]/delete.ts

@@ -1,54 +0,0 @@
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
-import { UserService } from '@/server/modules/users/user.service';
-import { z } from '@hono/zod-openapi';
-import { authMiddleware } from '@/server/middleware/auth.middleware';
-import { ErrorSchema } from '@/server/utils/errorHandler';
-import { AppDataSource } from '@/server/data-source';
-import { AuthContext } from '@/server/types/context';
-
-const userService = new UserService(AppDataSource);
-
-const DeleteParams = z.object({
-  id: z.coerce.number().openapi({
-    param: { name: 'id', in: 'path' },
-    example: 1,
-    description: '用户ID'
-  })
-});
-
-const routeDef = createRoute({
-  method: 'delete',
-  path: '/{id}',
-  middleware: [authMiddleware],
-  request: {
-    params: DeleteParams
-  },
-  responses: {
-    204: { 
-      description: '用户删除成功' 
-    },
-    404: {
-      description: '用户不存在',
-      content: { 'application/json': { schema: ErrorSchema } }
-    },
-    500: {
-      description: '服务器错误',
-      content: { 'application/json': { schema: ErrorSchema } }
-    }
-  }
-});
-
-const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
-  try {
-    const { id } = c.req.valid('param');
-    await userService.deleteUser(id);
-    return c.body(null, 204);
-  } catch (error) {
-    return c.json({ 
-      code: 500, 
-      message: error instanceof Error ? error.message : '删除用户失败' 
-    }, 500);
-  }
-});
-
-export default app;

+ 0 - 59
src/server/api/users/[id]/get.ts

@@ -1,59 +0,0 @@
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
-import { UserService } from '@/server/modules/users/user.service';
-import { z } from '@hono/zod-openapi';
-import { authMiddleware } from '@/server/middleware/auth.middleware';
-import { ErrorSchema } from '@/server/utils/errorHandler';
-import { AppDataSource } from '@/server/data-source';
-import { AuthContext } from '@/server/types/context';
-import { UserSchema } from '@/server/modules/users/user.schema';
-
-const userService = new UserService(AppDataSource);
-
-const GetParams = z.object({
-  id: z.coerce.number().openapi({
-    param: { name: 'id', in: 'path' },
-    example: 1,
-    description: '用户ID'
-  })
-});
-
-const routeDef = createRoute({
-  method: 'get',
-  path: '/{id}',
-  middleware: [authMiddleware],
-  request: {
-    params: GetParams
-  },
-  responses: {
-    200: {
-      description: '成功获取用户详情',
-      content: { 'application/json': { schema: UserSchema } }
-    },
-    404: {
-      description: '用户不存在',
-      content: { 'application/json': { schema: ErrorSchema } }
-    },
-    500: {
-      description: '服务器错误',
-      content: { 'application/json': { schema: ErrorSchema } }
-    }
-  }
-});
-
-const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
-  try {
-    const { id } = c.req.valid('param');
-    const user = await userService.getUserById(id);
-    if (!user) {
-      return c.json({ code: 404, message: '用户不存在' }, 404);
-    }
-    return c.json(user, 200);
-  } catch (error) {
-    return c.json({
-      code: 500,
-      message: error instanceof Error ? error.message : '获取用户详情失败'
-    }, 500);
-  }
-});
-
-export default app;

+ 0 - 71
src/server/api/users/[id]/put.ts

@@ -1,71 +0,0 @@
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
-import { UserService } from '@/server/modules/users/user.service';
-import { z } from '@hono/zod-openapi';
-import { authMiddleware } from '@/server/middleware/auth.middleware';
-import { ErrorSchema } from '@/server/utils/errorHandler';
-import { AppDataSource } from '@/server/data-source';
-import { AuthContext } from '@/server/types/context';
-import { UserSchema, UpdateUserDto } from '@/server/modules/users/user.schema';
-
-const userService = new UserService(AppDataSource);
-
-const UpdateParams = z.object({
-  id: z.coerce.number().openapi({
-    param: { name: 'id', in: 'path' },
-    example: 1,
-    description: '用户ID'
-  })
-});
-
-const routeDef = createRoute({
-  method: 'put',
-  path: '/{id}',
-  middleware: [authMiddleware],
-  request: {
-    params: UpdateParams,
-    body: {
-      content: {
-        'application/json': {
-          schema: UpdateUserDto
-        }
-      }
-    }
-  },
-  responses: {
-    200: {
-      description: '用户更新成功',
-      content: { 'application/json': { schema: UserSchema } }
-    },
-    400: {
-      description: '无效输入',
-      content: { 'application/json': { schema: ErrorSchema } }
-    },
-    404: {
-      description: '用户不存在',
-      content: { 'application/json': { schema: ErrorSchema } }
-    },
-    500: {
-      description: '服务器错误',
-      content: { 'application/json': { schema: ErrorSchema } }
-    }
-  }
-});
-
-const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
-  try {
-    const { id } = c.req.valid('param');
-    const data = c.req.valid('json');
-    const user = await userService.updateUser(id, data);
-    if (!user) {
-      return c.json({ code: 404, message: '用户不存在' }, 404);
-    }
-    return c.json(user, 200);
-  } catch (error) {
-    return c.json({
-      code: 500,
-      message: error instanceof Error ? error.message : '更新用户失败'
-    }, 500);
-  }
-});
-
-export default app;

+ 1 - 6
vitest.config.ts

@@ -22,12 +22,6 @@ export default defineConfig({
       '**/coverage/**',
       'tests/e2e/**',  // 排除Playwright E2E测试文件
       'src/client/**',  // 排除客户端代码,由组件测试配置处理
-      'vitest.config.ts',
-      'vitest.config.components.ts',
-      'vite.config.ts',
-      'server.js',
-      'eslint.config.js',
-      'debug-page.js',
     ],
 
     // 覆盖率配置
@@ -55,6 +49,7 @@ export default defineConfig({
         'server.js',
         'eslint.config.js',
         'debug-page.js',
+        'src/server/__test_utils__/**',
       ],
       thresholds: {
         branches: 70,