REST API
Relevant source files
The following files were used as context for generating this wiki page:The Ever Demand platform features a headless commerce framework that exposes a rich set of APIs, including REST, GraphQL, and real-time WebSockets. The REST API is built using the NestJS framework, providing a structured and scalable way to handle server-side application logic. It serves as a primary interface for various client applications (web and mobile) to interact with the platform's core functionalities like authentication, product management, and more.
The backend API is a real-time system written in TypeScript on NodeJS. It leverages NestJS for building server-side applications, ExpressJS as the web framework, and MongoDB as the primary database. This combination allows for efficient and reliable handling of HTTP requests for the platform's diverse features.
Sources: README.md, packages/core/README.md
Core Architecture
The REST API is organized into modules, controllers, and services, following the standard NestJS architecture. Controllers are responsible for handling incoming requests and returning responses to the client. They are defined using the @Controller decorator.
The main ApplicationModule imports various feature modules, such as AuthModule and ProductModule, which in turn provide the controllers that define the REST endpoints. This modular structure helps in organizing the codebase and separating concerns.
Sources: packages/core/src/app.module.ts:153-176
Request Handling Flow
The following diagram illustrates the typical flow of a REST API request within the NestJS application.
This diagram shows how a client request is processed through middleware and guards before reaching the appropriate controller and service for business logic execution.
Sources: packages/core/src/controllers/auth/auth.controller.ts, packages/core/src/controllers/product/product.controller.ts
API Endpoints
The REST API is divided into logical groups based on functionality, each managed by a dedicated controller.
Authentication API (/auth)
The AuthController manages user authentication, registration, and integration with third-party providers like Facebook and Google. It uses Passport.js strategies, wrapped in NestJS Guards (LocalAuthGuard, FacebookAuthGuard, GoogleAuthGuard), to protect and handle authentication logic.
Sources: packages/core/src/controllers/auth/auth.controller.ts
Login Sequence
The diagram below shows the sequence of events during a user login request via the /auth/login endpoint.
This sequence illustrates how the LocalAuthGuard first validates user credentials via the AuthService before the AuthController generates and returns a JSON Web Token (JWT).
Sources: packages/core/src/controllers/auth/auth.controller.ts:32-36
Authentication Endpoints
| Method | Endpoint | Description | Guards |
|---|---|---|---|
POST | /auth/register | Registers a new user. | |
POST | /auth/login | Authenticates a user with email and password and returns a JWT. | LocalAuthGuard |
GET | /auth/facebook | Initiates the Facebook OAuth2 authentication flow. | FacebookAuthGuard |
GET | /auth/facebook/callback | Facebook OAuth2 callback URL. Handles the response from Facebook. | FacebookAuthGuard |
GET | /auth/google | Initiates the Google OAuth2 authentication flow. | GoogleAuthGuard |
GET | /auth/google/callback | Google OAuth2 callback URL. Handles the response from Google. | GoogleAuthGuard |
GET | /auth/me | Retrieves the profile of the currently authenticated user. | JwtAuthGuard |
POST | /auth/request-password | Initiates a password reset request for a given email. | |
POST | /auth/reset-password | Resets the user's password using a valid token. |
Sources: packages/core/src/controllers/auth/auth.controller.ts:26-101
Product API (/product)
The ProductController is responsible for handling product-related operations, specifically focusing on product image management. It provides endpoints to upload and retrieve product images. It uses the FileInterceptor from NestJS for handling file uploads.
Sources: packages/core/src/controllers/product/product.controller.ts
Product Endpoints
| Method | Endpoint | Description |
|---|---|---|
POST | /product/:id/image | Uploads an image for a specific product. The image is processed and associated with the product ID. |
GET | /product/image/:id | Retrieves a specific product image by its ID. |
Sources: packages/core/src/controllers/product/product.controller.ts:25-45
Test API (/test)
A simple TestController is included for development and testing purposes. It provides an endpoint to test functionalities like sending emails without affecting production data.
Sources: packages/core/src/controllers/test.controller.ts
Test Endpoints
| Method | Endpoint | Description |
|---|---|---|
GET | /test/email | Sends a test email. Useful for verifying email service configuration. |
Sources: packages/core/src/controllers/test.controller.ts:14-17
Summary
The REST API is a fundamental component of the Ever Demand backend, providing essential HTTP-based endpoints for client applications. Built on the robust NestJS framework, it offers a clear, modular, and secure interface for core platform features like authentication and data management. While the platform also supports GraphQL and WebSockets for other use cases, the REST API remains a crucial part of the overall headless architecture.
Sources: README.md, packages/core/README.md, packages/core/src/app.module.ts