custom.routes.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. import { authMiddleware } from '@d8d/core-module/auth-module';
  8. import { AuthContext } from '@d8d/shared-types';
  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. 401: {
  33. description: '认证失败',
  34. content: { 'application/json': { schema: ErrorSchema } }
  35. },
  36. 500: {
  37. description: '创建用户失败',
  38. content: { 'application/json': { schema: ErrorSchema } }
  39. }
  40. }
  41. });
  42. // 更新用户路由 - 自定义业务逻辑
  43. const updateUserRoute = createRoute({
  44. method: 'put',
  45. path: '/{id}',
  46. middleware: [authMiddleware],
  47. request: {
  48. params: z.object({
  49. id: z.coerce.number().openapi({
  50. param: { name: 'id', in: 'path' },
  51. example: 1,
  52. description: '用户ID'
  53. })
  54. }),
  55. body: {
  56. content: {
  57. 'application/json': { schema: UpdateUserDto }
  58. }
  59. }
  60. },
  61. responses: {
  62. 200: {
  63. description: '用户更新成功',
  64. content: {
  65. 'application/json': { schema: UserSchema }
  66. }
  67. },
  68. 400: {
  69. description: '参数错误',
  70. content: { 'application/json': { schema: ErrorSchema } }
  71. },
  72. 401: {
  73. description: '认证失败',
  74. content: { 'application/json': { schema: ErrorSchema } }
  75. },
  76. 404: {
  77. description: '用户不存在',
  78. content: { 'application/json': { schema: ErrorSchema } }
  79. },
  80. 500: {
  81. description: '更新用户失败',
  82. content: { 'application/json': { schema: ErrorSchema } }
  83. }
  84. }
  85. });
  86. // 删除用户路由 - 自定义业务逻辑
  87. const deleteUserRoute = createRoute({
  88. method: 'delete',
  89. path: '/{id}',
  90. middleware: [authMiddleware],
  91. request: {
  92. params: z.object({
  93. id: z.coerce.number().openapi({
  94. param: { name: 'id', in: 'path' },
  95. example: 1,
  96. description: '用户ID'
  97. })
  98. })
  99. },
  100. responses: {
  101. 204: { description: '用户删除成功' },
  102. 401: {
  103. description: '认证失败',
  104. content: { 'application/json': { schema: ErrorSchema } }
  105. },
  106. 404: {
  107. description: '用户不存在',
  108. content: { 'application/json': { schema: ErrorSchema } }
  109. },
  110. 500: {
  111. description: '删除用户失败',
  112. content: { 'application/json': { schema: ErrorSchema } }
  113. }
  114. }
  115. });
  116. const app = new OpenAPIHono<AuthContext>()
  117. .openapi(createUserRoute, async (c) => {
  118. try {
  119. const data = c.req.valid('json');
  120. const userService = new UserService(AppDataSource);
  121. const result = await userService.createUser(data);
  122. return c.json(await parseWithAwait(UserSchema, result), 201);
  123. } catch (error) {
  124. if (error instanceof z.ZodError) {
  125. return c.json({
  126. code: 400,
  127. message: '参数错误',
  128. errors: error.issues
  129. }, 400);
  130. }
  131. // 处理用户名重复错误
  132. if (error instanceof Error && error.message.includes('用户名已存在')) {
  133. return c.json({
  134. code: 400,
  135. message: '用户名已存在'
  136. }, 400);
  137. }
  138. return c.json({
  139. code: 500,
  140. message: error instanceof Error ? error.message : '创建用户失败'
  141. }, 500);
  142. }
  143. })
  144. .openapi(updateUserRoute, async (c) => {
  145. try {
  146. const { id } = c.req.valid('param');
  147. const data = c.req.valid('json');
  148. const userService = new UserService(AppDataSource);
  149. const result = await userService.updateUser(id, data);
  150. if (!result) {
  151. return c.json({ code: 404, message: '资源不存在' }, 404);
  152. }
  153. return c.json(await parseWithAwait(UserSchema, result), 200);
  154. } catch (error) {
  155. if (error instanceof z.ZodError) {
  156. return c.json({
  157. code: 400,
  158. message: '参数错误',
  159. errors: error.issues
  160. }, 400);
  161. }
  162. return c.json({
  163. code: 500,
  164. message: error instanceof Error ? error.message : '更新用户失败'
  165. }, 500);
  166. }
  167. })
  168. .openapi(deleteUserRoute, async (c) => {
  169. try {
  170. const { id } = c.req.valid('param');
  171. const userService = new UserService(AppDataSource);
  172. const success = await userService.deleteUser(id);
  173. if (!success) {
  174. return c.json({ code: 404, message: '资源不存在' }, 404);
  175. }
  176. return c.body(null, 204);
  177. } catch (error) {
  178. return c.json({
  179. code: 500,
  180. message: error instanceof Error ? error.message : '删除用户失败'
  181. }, 500);
  182. }
  183. });
  184. export default app;