This project is an updated version of inventory management API. It allows you to manage inventory items with expanded CRUD operations. The project is written in TypeScript and includes validation, error handling and support of more paramters.
-
Clone the repository:
git clone https://github.com/sty2000/fullstack_intern_assignment.git
-
Install dependencies:
npm install
-
Start the server in development mode:
npm run start
-
Run the tests:
npm test
The project has been refactored to improve maintainability and clarity. Below is a summary of the purpose of each new file and directory:
- src/models/inventory.ts: Defines the
InventoryIteminterface and holds the inventory data structure. - src/routes/inventory.ts: Contains the API routes for inventory management, including creating, reading, updating, and deleting items.
- src/app.ts: Initializes the Express application, sets up middleware, and includes the inventory routes.
- src/index.ts: Entry point of the application, responsible for starting the server.
- tests/inventory.test.ts: Contains test cases for the inventory API using
jestandsupertest. - jest.config.js: Configuration file for
jestto enable TypeScript support and set the test environment. - package.json: Manages project dependencies, scripts, and metadata.
- tsconfig.json: TypeScript configuration file that specifies compiler options and the structure of the project.
- URL:
/inventory-all - Method:
GET - New Query Parameters:
category(optional): Filter items by categorypage(optional): Page number (default: 1)limit(optional): Number of items per page (default: 10)
-
URL:
/create-inventory -
Method:
POST -
New Validation Measures:
- Name: Must be a non-empty string.
- Category: Must be a non-empty string.
- Quantity: Must be a number and greater than 0. String numbers can be converted automatically.
- Price: Must be a number and greater than 0. String numbers can be converted automatically.
-
Error Handling:
- If validation fails, returns a
400 Bad Requestwith an array of error messages.
- If validation fails, returns a
- URL:
/update-inventory/:id - Method:
PUT - Response: Returns the updated inventory item
- URL:
/delete-inventory/:id - Method:
DELETE - Response: Returns the inventory item with the specified ID
- URL:
/inventory/:id - Method:
GET - Response: Returns the inventory item with the specified ID