Invern Spirit Backend is the server-side component of the Invern Spirit ecommerce application, designed to handle the core business logic, data management, and API endpoints. This backend service ensures seamless communication between the frontend interface and the underlying database, providing a robust foundation for the application's functionality.
- API Endpoints: Provides a set of RESTful API endpoints for client applications to interact with the system.
- Database Integration: Utilizes a relational database to store and manage application data efficiently.
- Authentication: Implements user authentication mechanisms to secure access to the application's resources.
- Validation: Includes data validation to ensure the integrity and reliability of the information processed.
- Payment Gateway Integration: Integrates with payment gateways to facilitate secure transactions within the application.
- Order Management: Manages the lifecycle of orders, from creation to fulfillment, to provide a seamless shopping experience.
- Product Catalog: Maintains a catalog of products with detailed information to enable product discovery and selection.
- Inventory Management: Tracks product inventory levels to prevent stock outs and manage supply chain operations.
- User Management: Manages user accounts, profiles and access rights to personalize the user experience.
- Monitoring and Observability: Generates logs to provide insights into application health, sales performance and customer behavior.
- Cloudflare Workers: Serverless platform for deploying and running server-side code in a distributed network.
- Node.js: JavaScript runtime environment that executes the server-side code.
- TypeScript: Superset of JavaScript that adds static typing, enhancing code quality and maintainability.
- Turso/SQLite: Embedded SQL database engine that provides lightweight and efficient data storage.
- Drizzle ORM: Lightweight TypeScript ORM for interacting with the relational database.
- Zod: TypeScript-first schema validation library with static type inference, providing robust data validation and type checking throughout the application.
- Jest: Testing framework used to write and run unit tests, ensuring code reliability.
- Husky: Git hooks manager that enforces code quality checks before commits and pushes.
Follow these instructions to set up and run the project on your local machine.
- Node.js: Ensure you have Node.js installed. You can download it from the official website.
- npm: Node.js package manager, which comes bundled with Node.js.
- Docker: Install Docker to run the local database server using a containerized environment.
-
Clone the Repository:
git clone https://github.com/manuelfesantos/invern-be.git
-
Navigate to the Project Directory:
cd invern-be -
Install Dependencies:
npm install
-
Build the Local Database:
npm run db:migrate:local
The application requires specific environment variables to function correctly.
-
Create a .dev.vars File:
cp .env.example .dev.vars
-
Configure the Variables: Open the
.dev.varsfile and replace the placeholder values with your actual configuration settings.
Start the Development Server:
npm run startThis command starts the local database server and the local application server in development mode, enabling features like hot-reloading for efficient development.
When running the application for the first time, you need to complete two additional steps:
-
Populate the Database: Make a POST request to:
/private/insert-test-dataInclude a JSON body with the
secretKeyproperty matching the value in yourINSERT_TEST_DATA_SECRETenvironment variable:{ "secretKey": "your-insert-test-data-secret-value" }This will populate your database with the necessary initial data.
-
Initialize Stock Bucket: Make a POST request to:
/private/stock/setupInclude a JSON body with the
secretKeyproperty matching the value in yourSETUP_STOCK_SECRETenvironment variable:{ "secretKey": "your-setup-stock-secret-value" }This will initialize the local stock bucket required for the application to function properly.
- Generate the migration files:
npm run db:generate
- Apply the migration:
- Local
npm run db:migrate:local
- Remote
This requires you to be logged in to your account via wrangler.
npm run db:migrate:remote
npx wrangler login
- Local
Many endpoints require authentication with a JWT token provided in the Authorization header:
Authorization: Bearer {access-token}
| Endpoint | Method | Description |
|---|---|---|
/public/countries |
GET | Retrieve all available countries |
/public/countries/:countryCode |
GET | Get details for a specific country |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/config |
GET | Get initial values for interacting with the API, including shopping cart, user if logged in and authorization token |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/products |
GET | Get all products (supports search parameter) |
/public/countries/:countryCode/products/:id |
GET | Get detailed information for a specific product |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/collections |
GET | Get all product collections |
/public/countries/:countryCode/collections/:id |
GET | Get detailed information for a specific collection |
Authentication required
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/cart |
GET | Retrieve the current user's cart |
/public/countries/:countryCode/cart/items/:productId |
PATCH | Add a quantity to a product in the cart (requires quantity in body) |
/public/countries/:countryCode/cart/items/:productId |
PUT | Update product quantity in cart (requires quantity in body) |
/public/countries/:countryCode/cart/items/:productId |
DELETE | Remove a product from the cart |
Authentication required
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages |
GET | Get available checkout stages |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages/personal-details |
GET | Get user's personal details |
/public/countries/:countryCode/checkout/stages/personal-details |
POST | Save personal details (first name, last name, email) |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages/address |
GET | Get saved address information |
/public/countries/:countryCode/checkout/stages/address |
POST | Create or update shipping address |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages/shipping |
GET | Get available shipping methods |
/public/countries/:countryCode/checkout/stages/shipping |
POST | Select a shipping method (requires ID in body) |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages/payment |
GET | Get payment session url |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/checkout/stages/review |
GET | Get order review information before proceeding to payment |
Authorization Required
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/user/signup |
POST | Create a new user account |
/public/countries/:countryCode/user/login |
POST | Login to an existing account |
/public/countries/:countryCode/user/logout |
POST | Log out current user |
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/user |
GET | Get current user profile |
/public/countries/:countryCode/user |
PUT | Update user information |
/public/countries/:countryCode/user |
DELETE | Delete user account |
Authentication required
| Endpoint | Method | Description |
|---|---|---|
/public/countries/:countryCode/orders |
GET | Get all orders for current user |
/public/countries/:countryCode/orders/:id |
GET | Get details for a specific order |
These endpoints are for administrative or internal use only
| Endpoint | Method | Description |
|---|---|---|
/private/stock/:productId |
GET | Get stock information for a product |
/private/stock/setup |
POST | Initialize stock management system |
| Endpoint | Method | Description |
|---|---|---|
/private/insert-test-data |
POST | Add test data to the system |
Responses typically follow a standard format:
{
"message": "Action description",
"data": { "key": "value" }
}Failed requests will return:
{
"issues": [
"Error 1 description",
"Error 2 description"
]
}- Frontend calls config endpoint to get initial authorization tokens
- User is now able to interact with shopping cart, checkout and order
- User signs up or logs in using the appropriate endpoints
- Upon successful authentication, the API returns a logged in access token
- The logged in access token is used to access user-specific endpoints (user profile, orders, etc.)
To run the test suite, execute:
npm testThis command runs all unit tests using Jest, ensuring that the application's functionalities work as intended.
We welcome contributions to enhance the Invern Spirit Backend. To contribute:
- Fork the Repository: Click the "Fork" button on the GitHub page to create a personal copy of the repository.
- Create a New Branch: Use a descriptive name for your branch.
- Make Changes: Implement your features or bug fixes.
- Run Tests: Ensure all tests pass before committing your changes.
- Commit Changes: Write clear and concise commit messages.
- Push to GitHub: Push your changes to your forked repository.
- Submit a Pull Request: Describe your changes and submit the pull request for review.
This project is licensed under the MIT License. For more details, refer to the LICENSE file.
We extend our gratitude to all contributors and open-source projects that have made this project possible.