2
0

get.ts 954 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
  2. import { ErrorSchema } from '@/server/utils/errorHandler'
  3. import { authMiddleware } from '@/server/middleware/auth.middleware'
  4. import { AuthContext } from '@/server/types/context'
  5. import { UserSchema } from '@/server/modules/users/user.schema'
  6. const UserResponseSchema = UserSchema.omit({
  7. password: true
  8. });
  9. const routeDef = createRoute({
  10. method: 'get',
  11. path: '/me',
  12. middleware: authMiddleware,
  13. responses: {
  14. 200: {
  15. description: '获取当前用户信息成功',
  16. content: {
  17. 'application/json': {
  18. schema: UserResponseSchema
  19. }
  20. }
  21. },
  22. 401: {
  23. description: '未授权',
  24. content: {
  25. 'application/json': {
  26. schema: ErrorSchema
  27. }
  28. }
  29. }
  30. }
  31. })
  32. const app = new OpenAPIHono<AuthContext>().openapi(routeDef, (c) => {
  33. const user = c.get('user')
  34. return c.json(user, 200)
  35. })
  36. export default app