custom.routes.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { UserService } from '../services/user.service';
  4. import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
  5. import { CreateUserDto, UpdateUserDto, UserSchema } from '../schemas/user.schema';
  6. import { parseWithAwait } from '@d8d/shared-utils';
  7. // 创建用户路由 - 自定义业务逻辑(密码加密等)
  8. const createUserRoute = createRoute({
  9. method: 'post',
  10. path: '/',
  11. // 暂时移除认证中间件,等待 auth-module 创建
  12. // middleware: [authMiddleware],
  13. request: {
  14. body: {
  15. content: {
  16. 'application/json': { schema: CreateUserDto }
  17. }
  18. }
  19. },
  20. responses: {
  21. 201: {
  22. description: '用户创建成功',
  23. content: {
  24. 'application/json': { schema: UserSchema }
  25. }
  26. },
  27. 400: {
  28. description: '参数错误',
  29. content: { 'application/json': { schema: ErrorSchema } }
  30. },
  31. 500: {
  32. description: '创建用户失败',
  33. content: { 'application/json': { schema: ErrorSchema } }
  34. }
  35. }
  36. });
  37. // 更新用户路由 - 自定义业务逻辑
  38. const updateUserRoute = createRoute({
  39. method: 'put',
  40. path: '/{id}',
  41. // 暂时移除认证中间件,等待 auth-module 创建
  42. // middleware: [authMiddleware],
  43. request: {
  44. params: z.object({
  45. id: z.coerce.number().openapi({
  46. param: { name: 'id', in: 'path' },
  47. example: 1,
  48. description: '用户ID'
  49. })
  50. }),
  51. body: {
  52. content: {
  53. 'application/json': { schema: UpdateUserDto }
  54. }
  55. }
  56. },
  57. responses: {
  58. 200: {
  59. description: '用户更新成功',
  60. content: {
  61. 'application/json': { schema: UserSchema }
  62. }
  63. },
  64. 400: {
  65. description: '参数错误',
  66. content: { 'application/json': { schema: ErrorSchema } }
  67. },
  68. 404: {
  69. description: '用户不存在',
  70. content: { 'application/json': { schema: ErrorSchema } }
  71. },
  72. 500: {
  73. description: '更新用户失败',
  74. content: { 'application/json': { schema: ErrorSchema } }
  75. }
  76. }
  77. });
  78. // 删除用户路由 - 自定义业务逻辑
  79. const deleteUserRoute = createRoute({
  80. method: 'delete',
  81. path: '/{id}',
  82. // 暂时移除认证中间件,等待 auth-module 创建
  83. // middleware: [authMiddleware],
  84. request: {
  85. params: z.object({
  86. id: z.coerce.number().openapi({
  87. param: { name: 'id', in: 'path' },
  88. example: 1,
  89. description: '用户ID'
  90. })
  91. })
  92. },
  93. responses: {
  94. 204: { description: '用户删除成功' },
  95. 404: {
  96. description: '用户不存在',
  97. content: { 'application/json': { schema: ErrorSchema } }
  98. },
  99. 500: {
  100. description: '删除用户失败',
  101. content: { 'application/json': { schema: ErrorSchema } }
  102. }
  103. }
  104. });
  105. const app = new OpenAPIHono()
  106. .openapi(createUserRoute, async (c) => {
  107. try {
  108. const data = c.req.valid('json');
  109. const userService = new UserService(AppDataSource);
  110. const result = await userService.createUser(data);
  111. return c.json(await parseWithAwait(UserSchema, result), 201);
  112. } catch (error) {
  113. if (error instanceof z.ZodError) {
  114. return c.json({
  115. code: 400,
  116. message: '参数错误',
  117. errors: error.issues
  118. }, 400);
  119. }
  120. return c.json({
  121. code: 500,
  122. message: error instanceof Error ? error.message : '创建用户失败'
  123. }, 500);
  124. }
  125. })
  126. .openapi(updateUserRoute, async (c) => {
  127. try {
  128. const { id } = c.req.valid('param');
  129. const data = c.req.valid('json');
  130. const userService = new UserService(AppDataSource);
  131. const result = await userService.updateUser(id, data);
  132. if (!result) {
  133. return c.json({ code: 404, message: '资源不存在' }, 404);
  134. }
  135. return c.json(await parseWithAwait(UserSchema, result), 200);
  136. } catch (error) {
  137. if (error instanceof z.ZodError) {
  138. return c.json({
  139. code: 400,
  140. message: '参数错误',
  141. errors: error.issues
  142. }, 400);
  143. }
  144. return c.json({
  145. code: 500,
  146. message: error instanceof Error ? error.message : '更新用户失败'
  147. }, 500);
  148. }
  149. })
  150. .openapi(deleteUserRoute, async (c) => {
  151. try {
  152. const { id } = c.req.valid('param');
  153. // 注意:这里需要传入 DataSource,暂时留空,等待重构
  154. const userService = new UserService(AppDataSource);
  155. const success = await userService.deleteUser(id);
  156. if (!success) {
  157. return c.json({ code: 404, message: '资源不存在' }, 404);
  158. }
  159. return c.body(null, 204);
  160. } catch (error) {
  161. return c.json({
  162. code: 500,
  163. message: error instanceof Error ? error.message : '删除用户失败'
  164. }, 500);
  165. }
  166. });
  167. export default app;