cca-user-module
v0.2.11
Published
User module
Downloads
57
Readme
Below is an example of a README.md file for the cca-user-module npm package:
cca-user-module
cca-user-module is an npm package that provides endpoints and controllers to manage users in your application. It includes functionalities for creating, retrieving, updating, and deleting users. This module is designed to be integrated with Express and leverages dependency injection through a container-based setup.
Installation
Install the module via npm:
npm install cca-user-moduleFeatures
- Create User: Create a new user with required fields such as name, email, and password.
- Get User: Retrieve user details by user ID.
- Get All Users: Retrieve a list of all users.
- Update User: Update existing user information.
- Delete User: Delete a user by their ID.
Usage Example
Below is an example of how to integrate the UserController provided by cca-user-module into your Express application:
import express from 'express';
import { UserController } from 'cca-user-module';
import { createUserContainer } from 'cca-user-module'; // Assumes your container setup is provided by the module
const app = express();
app.use(express.json());
// Create a container and resolve the UserController instance
const container = createUserContainer();
const userController: UserController = container.resolve(UserController);
// Define user routes
app.post('/users', userController.createUser);
app.get('/users/:id', userController.getUser);
app.get('/users', userController.getAllUsers);
app.put('/users/:id', userController.updateUser);
app.delete('/users/:id', userController.deleteUser);
// Error handling middleware
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});
app.listen(3000, () => console.log('Server running on port 3000'));API Endpoints
1. Create a User
- URL:
/users - Method:
POST - Description: Creates a new user.
- Request Body:
- User details (e.g.,
name,email,password, etc.). adminPassword(optional): For additional admin-level validation.
- User details (e.g.,
- Response:
- Code:
201 Created - Content: JSON object with the newly created user details.
- Code:
2. Get a Single User
- URL:
/users/:id - Method:
GET - Description: Retrieves a user by their ID.
- Response:
- Code:
200 OK - Content: JSON object with user details.
- Code:
3. Get All Users
- URL:
/users - Method:
GET - Description: Retrieves all users.
- Response:
- Code:
200 OK - Content: JSON array containing user objects.
- Code:
4. Update a User
- URL:
/users/:id - Method:
PUT - Description: Updates an existing user.
- Request Body: JSON object with the fields to update.
- Response:
- Code:
200 OK - Content: JSON object with the updated user details.
- Code:
5. Delete a User
- URL:
/users/:id - Method:
DELETE - Description: Deletes a user by their ID.
- Response:
- Code:
204 No Content
- Code:
Error Handling
Ensure your Express application includes error-handling middleware to capture and process errors from the controller methods:
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});License
This project is licensed under the MIT License.
This README provides an overview of the cca-user-module package, including installation, usage examples, API endpoints, and error handling. For more details, please refer to the source code and inline documentation.
