UNPKG

myex-cli

Version:

Opinionated Express.js framework with CLI tools

221 lines (213 loc) 6.21 kB
import express from 'express'; import { userController } from '../controllers/user.controller.js'; import { authMiddleware } from '../middlewares/auth.middleware.js'; const router = express.Router(); // Apply authentication middleware to all user routes router.use(authMiddleware.authenticateToken); /** * @swagger * /api/users: * get: * summary: Get all users (admin only) * tags: [Users] * security: * - BearerAuth: [] * responses: * 200: * description: List of all users * content: * application/json: * schema: * type: object * properties: * status: * type: string * example: success * data: * type: object * properties: * users: * type: array * items: * $ref: '#/components/schemas/User' * 401: * $ref: '#/components/responses/UnauthorizedError' * 403: * $ref: '#/components/responses/ForbiddenError' * 500: * $ref: '#/components/responses/ServerError' */ router.get('/', authMiddleware.requireAdmin, userController.getAllUsers); /** * @swagger * /api/users/me: * get: * summary: Get current user profile * tags: [Users] * security: * - BearerAuth: [] * responses: * 200: * description: Current user profile * content: * application/json: * schema: * type: object * properties: * status: * type: string * example: success * data: * type: object * properties: * user: * $ref: '#/components/schemas/User' * 401: * $ref: '#/components/responses/UnauthorizedError' * 404: * $ref: '#/components/responses/NotFoundError' * 500: * $ref: '#/components/responses/ServerError' */ router.get('/me', userController.getCurrentUser); /** * @swagger * /api/users/{id}: * get: * summary: Get user by ID * tags: [Users] * security: * - BearerAuth: [] * parameters: * - in: path * name: id * required: true * schema: * type: string * description: User ID * responses: * 200: * description: User profile * content: * application/json: * schema: * type: object * properties: * status: * type: string * example: success * data: * type: object * properties: * user: * $ref: '#/components/schemas/User' * 401: * $ref: '#/components/responses/UnauthorizedError' * 403: * $ref: '#/components/responses/ForbiddenError' * 404: * $ref: '#/components/responses/NotFoundError' * 500: * $ref: '#/components/responses/ServerError' */ router.get('/:id', userController.getUserById); /** * @swagger * /api/users/{id}: * put: * summary: Update user profile * tags: [Users] * security: * - BearerAuth: [] * parameters: * - in: path * name: id * required: true * schema: * type: string * description: User ID * requestBody: * required: true * content: * application/json: * schema: * type: object * properties: * name: * type: string * example: John Doe Updated * email: * type: string * format: email * example: john.updated@example.com * responses: * 200: * description: User updated successfully * content: * application/json: * schema: * type: object * properties: * status: * type: string * example: success * message: * type: string * example: User updated successfully * data: * type: object * properties: * user: * $ref: '#/components/schemas/User' * 400: * $ref: '#/components/responses/ValidationError' * 401: * $ref: '#/components/responses/UnauthorizedError' * 403: * $ref: '#/components/responses/ForbiddenError' * 404: * $ref: '#/components/responses/NotFoundError' * 500: * $ref: '#/components/responses/ServerError' */ router.put('/:id', userController.updateUser); /** * @swagger * /api/users/{id}: * delete: * summary: Delete a user (admin only) * tags: [Users] * security: * - BearerAuth: [] * parameters: * - in: path * name: id * required: true * schema: * type: string * description: User ID * responses: * 200: * description: User deleted successfully * content: * application/json: * schema: * type: object * properties: * status: * type: string * example: success * message: * type: string * example: User deleted successfully * 401: * $ref: '#/components/responses/UnauthorizedError' * 403: * $ref: '#/components/responses/ForbiddenError' * 404: * $ref: '#/components/responses/NotFoundError' * 500: * $ref: '#/components/responses/ServerError' */ router.delete('/:id', authMiddleware.requireAdmin, userController.deleteUser); export const userRoutes = router;