custom.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. import { parseWithAwait } from '../../utils/parseWithAwait';
  10. // 创建用户路由 - 自定义业务逻辑(密码加密等)
  11. const createUserRoute = createRoute({
  12. method: 'post',
  13. path: '/',
  14. middleware: [authMiddleware],
  15. request: {
  16. body: {
  17. content: {
  18. 'application/json': { schema: CreateUserDto }
  19. }
  20. }
  21. },
  22. responses: {
  23. 201: {
  24. description: '用户创建成功',
  25. content: {
  26. 'application/json': { schema: UserSchema }
  27. }
  28. },
  29. 400: {
  30. description: '参数错误',
  31. content: { 'application/json': { schema: ErrorSchema } }
  32. },
  33. 500: {
  34. description: '创建用户失败',
  35. content: { 'application/json': { schema: ErrorSchema } }
  36. }
  37. }
  38. });
  39. // 更新用户路由 - 自定义业务逻辑
  40. const updateUserRoute = createRoute({
  41. method: 'put',
  42. path: '/{id}',
  43. middleware: [authMiddleware],
  44. request: {
  45. params: z.object({
  46. id: z.coerce.number().openapi({
  47. param: { name: 'id', in: 'path' },
  48. example: 1,
  49. description: '用户ID'
  50. })
  51. }),
  52. body: {
  53. content: {
  54. 'application/json': { schema: UpdateUserDto }
  55. }
  56. }
  57. },
  58. responses: {
  59. 200: {
  60. description: '用户更新成功',
  61. content: {
  62. 'application/json': { schema: UserSchema }
  63. }
  64. },
  65. 400: {
  66. description: '参数错误',
  67. content: { 'application/json': { schema: ErrorSchema } }
  68. },
  69. 404: {
  70. description: '用户不存在',
  71. content: { 'application/json': { schema: ErrorSchema } }
  72. },
  73. 500: {
  74. description: '更新用户失败',
  75. content: { 'application/json': { schema: ErrorSchema } }
  76. }
  77. }
  78. });
  79. // 删除用户路由 - 自定义业务逻辑
  80. const deleteUserRoute = createRoute({
  81. method: 'delete',
  82. path: '/{id}',
  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<AuthContext>()
  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. const userService = new UserService(AppDataSource);
  154. const success = await userService.deleteUser(id);
  155. if (!success) {
  156. return c.json({ code: 404, message: '资源不存在' }, 404);
  157. }
  158. return c.body(null, 204);
  159. } catch (error) {
  160. return c.json({
  161. code: 500,
  162. message: error instanceof Error ? error.message : '删除用户失败'
  163. }, 500);
  164. }
  165. });
  166. export default app;