custom.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { UserService } from '../../modules/users/user.service';
  4. import { authMiddleware } from '../../middleware/auth.middleware';
  5. import { ErrorSchema } from '../../utils/errorHandler';
  6. import { AppDataSource } from '../../data-source';
  7. import { AuthContext } from '../../types/context';
  8. import { CreateUserDto, UpdateUserDto, UserSchema } from '../../modules/users/user.schema';
  9. // 创建用户路由 - 自定义业务逻辑(密码加密等)
  10. const createUserRoute = createRoute({
  11. method: 'post',
  12. path: '/',
  13. middleware: [authMiddleware],
  14. request: {
  15. body: {
  16. content: {
  17. 'application/json': { schema: CreateUserDto }
  18. }
  19. }
  20. },
  21. responses: {
  22. 201: {
  23. description: '用户创建成功',
  24. content: {
  25. 'application/json': { schema: UserSchema }
  26. }
  27. },
  28. 400: {
  29. description: '参数错误',
  30. content: { 'application/json': { schema: ErrorSchema } }
  31. },
  32. 500: {
  33. description: '创建用户失败',
  34. content: { 'application/json': { schema: ErrorSchema } }
  35. }
  36. }
  37. });
  38. // 更新用户路由 - 自定义业务逻辑
  39. const updateUserRoute = createRoute({
  40. method: 'put',
  41. path: '/{id}',
  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. middleware: [authMiddleware],
  83. request: {
  84. params: z.object({
  85. id: z.coerce.number().openapi({
  86. param: { name: 'id', in: 'path' },
  87. example: 1,
  88. description: '用户ID'
  89. })
  90. })
  91. },
  92. responses: {
  93. 204: { description: '用户删除成功' },
  94. 404: {
  95. description: '用户不存在',
  96. content: { 'application/json': { schema: ErrorSchema } }
  97. },
  98. 500: {
  99. description: '删除用户失败',
  100. content: { 'application/json': { schema: ErrorSchema } }
  101. }
  102. }
  103. });
  104. const app = new OpenAPIHono<AuthContext>()
  105. .openapi(createUserRoute, async (c) => {
  106. try {
  107. const data = c.req.valid('json');
  108. const userService = new UserService(AppDataSource);
  109. const result = await userService.createUser(data);
  110. return c.json(result, 201);
  111. } catch (error) {
  112. if (error instanceof z.ZodError) {
  113. return c.json({
  114. code: 400,
  115. message: '参数错误',
  116. errors: error.issues
  117. }, 400);
  118. }
  119. return c.json({
  120. code: 500,
  121. message: error instanceof Error ? error.message : '创建用户失败'
  122. }, 500);
  123. }
  124. })
  125. .openapi(updateUserRoute, async (c) => {
  126. try {
  127. const { id } = c.req.valid('param');
  128. const data = c.req.valid('json');
  129. const userService = new UserService(AppDataSource);
  130. const result = await userService.updateUser(id, data);
  131. if (!result) {
  132. return c.json({ code: 404, message: '用户不存在' }, 404);
  133. }
  134. return c.json(result, 200);
  135. } catch (error) {
  136. if (error instanceof z.ZodError) {
  137. return c.json({
  138. code: 400,
  139. message: '参数错误',
  140. errors: error.issues
  141. }, 400);
  142. }
  143. return c.json({
  144. code: 500,
  145. message: error instanceof Error ? error.message : '更新用户失败'
  146. }, 500);
  147. }
  148. })
  149. .openapi(deleteUserRoute, async (c) => {
  150. try {
  151. const { id } = c.req.valid('param');
  152. const userService = new UserService(AppDataSource);
  153. const success = await userService.deleteUser(id);
  154. if (!success) {
  155. return c.json({ code: 404, message: '用户不存在' }, 404);
  156. }
  157. return c.body(null, 204);
  158. } catch (error) {
  159. return c.json({
  160. code: 500,
  161. message: error instanceof Error ? error.message : '删除用户失败'
  162. }, 500);
  163. }
  164. });
  165. export default app;