| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
- import { UserService } from '../../modules/users/user.service';
- import { authMiddleware } from '../../middleware/auth.middleware';
- import { ErrorSchema } from '../../utils/errorHandler';
- import { AppDataSource } from '../../data-source';
- import { AuthContext } from '../../types/context';
- import { UserSchema, CreateUserDto } from '@/server/modules/users/user.schema';
- const userService = new UserService(AppDataSource);
- const createUserRoute = createRoute({
- method: 'post',
- path: '/',
- middleware: authMiddleware,
- request: {
- body: {
- content: {
- 'application/json': {
- schema: CreateUserDto
- }
- }
- }
- },
- responses: {
- 201: {
- description: '创建成功',
- content: {
- 'application/json': {
- schema: UserSchema
- }
- }
- },
- 400: {
- description: '输入数据无效',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- },
- 500: {
- description: '服务器错误',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- }
- }
- });
- const app = new OpenAPIHono<AuthContext>().openapi(createUserRoute, async (c) => {
- try {
- const data = c.req.valid('json');
- const user = await userService.createUser(data);
- return c.json(user, 201);
- } catch (error) {
- return c.json({ code: 500, message: '服务器错误' }, 500);
- }
- });
- export default app;
|