This project is a backend API for managing products and orders in an e-commerce application. Built with Express and TypeScript, it uses MongoDB for data storage and Mongoose for data modeling. Zod is employed for data validation to ensure integrity.
- Product Management
- Create, retrieve, update, delete, and search for products.
- Order Management
- Create, retrieve all orders, and retrieve orders by user email.
- Inventory Management
- Update product inventory when orders are created.
- Express: Framework for building the API.
- TypeScript: Ensures type safety and better code quality.
- MongoDB & Mongoose: For database operations and data modeling.
- Zod: For schema validation.
-
Create a New Product
- Endpoint:
/api/products
- Method:
POST
- Sample Request Body:
{ "name": "iPhone 13", "description": "A sleek and powerful smartphone with cutting-edge features.", "price": 999, "category": "Electronics", "tags": ["smartphone", "Apple", "iOS"], "variants": [ { "type": "Color", "value": "Midnight Blue" }, { "type": "Storage Capacity", "value": "256GB" } ], "inventory": { "quantity": 50, "inStock": true } }
- Endpoint:
-
Retrieve a List of All Products
- Endpoint:
/api/products
- Method:
GET
- Endpoint:
-
Retrieve a Specific Product by ID
- Endpoint:
/api/products/:productId
- Method:
GET
- Endpoint:
-
Update Product Information
- Endpoint:
/api/products/:productId
- Method:
PUT
- Endpoint:
-
Delete a Product
- Endpoint:
/api/products/:productId
- Method:
DELETE
- Endpoint:
-
Search a Product
- Endpoint:
/api/products?searchTerm=term
- Method:
GET
- Endpoint:
-
Create a New Order
- Endpoint:
/api/orders
- Method:
POST
- Sample Request Body:
{ "email": "level2@programming-hero.com", "productId": "5fd67e890b60c903cd8544a3", "price": 999, "quantity": 1 }
- Endpoint:
-
Retrieve All Orders
- Endpoint:
/api/orders
- Method:
GET
- Endpoint:
-
Retrieve Orders by User Email
- Endpoint:
/api/orders?email=email@example.com
- Method:
GET
- Endpoint:
- name: string
- description: string
- price: number
- category: string
- tags: array of strings
- variants: array of objects
- type: string
- value: string
- inventory: object
- quantity: number
- inStock: boolean
- email: string
- productId: ObjectId
- price: number
- quantity: number
Using Zod for request validation:
- Ensure product and order data adhere to the specified structure.
- Provide meaningful error messages on validation failure.
- Handle validation errors with meaningful messages.
- Handle insufficient quantity errors.
- Handle not found errors for products and orders.
- Gracefully handle unknown routes.
-
Clone the Repository
git clone https://github.com/Tanzeebul-Tamim/TS_Mongoose_E-commerce_API
-
Install Dependencies
npm install
-
Environment Variables
- Create a
.env
file in the root directory. - Add the following variables:
PORT=5000 MONGODB_URI=your_mongodb_connection_string
- Create a
-
Run the Application
npm run dev
-
Access the API
- The API will be available at
http://localhost:5000
- The API will be available at
- Ensure code quality using ESLint.
- Run linting:
npm run lint
- Automatically fix linting issues:
npm run lint:fix
- Format your code using Prettier.
- Run Prettier:
npm run format
The API is deployed at vercel and can be accessed through here