This is a full-fledged E-Commerce API built with TypeScript, Prisma, and Express. It supports essential e-commerce functionalities like user authentication, product management, cart, order creation, and order tracking. The API is designed to allow users to perform CRUD operations on various e-commerce resources, such as products, orders, and cart items.
-
User Management:
- User registration and authentication with JWT tokens.
- Role-based access control with
ADMIN
andUSER
roles.
-
Product Management:
- CRUD operations for products (create, read, update, delete).
-
Cart Management:
- Add, update, and remove items in the cart.
-
Order Management:
- Place orders with multiple products.
- Track order status and history.
-
Address Management:
- Add, update, and retrieve user addresses.
- TypeScript: A strongly typed superset of JavaScript.
- Prisma ORM: A modern database toolkit to interact with the database.
- Express.js: A minimal web application framework for Node.js.
- PostgreSQL: The relational database for storing user, product, and order data.
- JWT (JSON Web Token): For secure authentication and authorization.
- Zod: A schema validation library for runtime validation and error handling.
- Nodemon: For automatic server restarts during development.
-
Clone the repository:
git clone https://github.com/yourusername/e-commerce-api.git cd e-commerce-api
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the root of your project and add the following variables:DATABASE_URL=your-database-url JWT_SECRET=your-jwt-secret
-
Set up the database:
-
Make sure you have PostgreSQL installed and running.
-
Run the Prisma migrations to set up your database schema:
npx prisma migrate dev
-
-
Run the development server:
For development, you can use
nodemon
to watch for file changes and automatically restart the server:npm run dev
For production, compile TypeScript to JavaScript and run the app:
npm run build npm start
Here are the main API endpoints available in the project:
- POST /auth/register: Register a new user.
- POST /auth/login: Login and obtain a JWT token.
- GET /auth/me: Login and display the authenticated user.
- GET /users: List all users (Admin only).
- GET /users/:id: Get a user by ID.
- PUT /users/:id/role: Change the role of a user (Admin only).
- POST /users/address: Add user address.
- GET /users/: Get all users
- GET /users/: List all the addresses of the user.
- GET /products: List all products.
- GET /products/:id: Get a product by ID.
- POST /products: Create a new product (Admin only).
- PUT /products/:id: Update a product (Admin only).
- DELETE /products/:id: Delete a product (Admin only).
- GET /products/search: Search for product
- GET /cart: List all items in the user's cart.
- POST /cart: Add a product to the cart.
- PUT /cart/:id: Update the quantity of an item in the cart.
- DELETE /cart/:id: Remove an item from the cart.
- GET /orders/index: List all orders for the authenticated user.
- GET /orders/:id: Get details of an order by ID.
- POST /orders: Create a new order from cart items.
- PUT /orders/:id/cancel: Cancel the order.
- PUT /orders/:id/status: Change the status of the order.
- GET /orders/:id: Get the order by ID.
- GET /addresses: Get all addresses for the authenticated user.
- POST /addresses: Add a new address.
- PUT /addresses/:id: Update an address.
- DELETE /addresses/:id: Delete an address.
e-commerce-api/
├── dist/ # Compiled JavaScript files for production
├── node_modules/ # Installed dependencies
├── prisma/ # Prisma schema and migration files
├── src/ # Source TypeScript files
│ ├── controllers/ # API endpoint logic for handling requests
│ ├── exceptions/ # Custom error classes and exceptions
│ ├── middlewares/ # Custom middlewares (e.g., authentication, logging)
│ ├── routes/ # Express route definitions and API endpoints
│ ├── schema/ # Validation schemas defined with Zod for runtime validation
│ ├── types/ # Custom TypeScript types and extended types for the app
│ ├── errorHandlers/ # Centralized error-handling utilities
│ └── index.ts # Main entry point to start the application
│ └── secrets.ts # Handles access to environment variables securely
├── .env # Environment variable configuration file
├── tsconfig.json # TypeScript configuration file
├── package.json # Project metadata and dependency information
└── README.md # Project documentation and usage guide
Contributions are welcome! Feel free to open issues or submit pull requests.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.