API Communication Layer
Relevant source files
The following files were used as context for generating this wiki page:
The API Communication Layer manages data exchange between the Angular frontend and the NestJS backend. It handles authentication persistence, cross-origin resource sharing (CORS), and standardized data formatting for the marketplace application.
Frontend Infrastructure
The frontend uses Angular's HttpClientModule to perform asynchronous requests. It implements an AuthInterceptor to handle authentication headers for outbound requests.
HTTP Configuration and Interceptors
The AppModule configures the global HTTP client behavior. It registers the AuthInterceptor within the HTTP_INTERCEPTORS multi-provider to ensure every request includes necessary security credentials.
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
],
Sources: marketplace-frontend/src/app/app.module.ts:80-84
Internationalization Loading
The frontend utilizes ngx-translate to load localization files via HTTP. The HttpLoaderFactory initializes the TranslateHttpLoader, which fetches translation assets from the server.
The diagram shows the dependency chain for loading translation files through the API layer.
Sources: marketplace-frontend/src/app/app.module.ts:50-54, marketplace-frontend/src/app/utils/http-loader-factory.ts:1-7
Backend Infrastructure
The backend, built on NestJS, provides a RESTful API. It uses middleware to process incoming requests and global filters to handle exceptions.
Server Configuration
The backend entry point configures essential communication parameters including CORS, JSON parsing, and cookie handling. The bootstrap function enables CORS with credentials support to allow frontend communication from different origins.
| Feature | Implementation | Description |
|---|---|---|
| CORS | app.enableCors() | Enables cross-origin requests and exposes Set-Cookie headers. |
| Body Parser | bodyParser.json() | Parses incoming request bodies as JSON. |
| Cookies | cookieParser() | Enables parsing of cookie headers for session management. |
| Auth Middleware | addAuthHeader | Processes authentication headers in the request pipeline. |
Sources: marketplace-backend/app/src/main.ts:23-32
API Documentation
The system automatically generates REST API documentation using Swagger. The documentation is accessible via a route defined in the environment variable SWAGGER_DOCS_ROUTE.
The diagram represents the flow for accessing the automatically generated API documentation.
Sources: marketplace-backend/app/src/main.ts:13-21
Data Transfer and Dependencies
The communication layer relies on specific libraries for JWT handling, HTTP requests, and data validation.
| Environment | Key Dependency | Purpose |
|---|---|---|
| Frontend | @angular/common/http | Core HTTP client for Angular. |
| Frontend | ngx-cookie-service | Manages browser cookies. |
| Backend | @nestjs/jwt | Handles JSON Web Token signing and verification. |
| Backend | body-parser | Parses incoming request streams. |
| Backend | mongoose | Interfaces with the MongoDB database. |
Sources: marketplace-backend/app/package.json:30-50, marketplace-frontend/package.json:20-40
Request Lifecycle
Requests originate from frontend components, pass through interceptors, and reach backend controllers via defined modules like AuthModule and CustomersModule.
The diagram illustrates the path of a request from the user interface to the backend processing logic.
Sources: marketplace-frontend/src/app/app.module.ts:80-84, marketplace-backend/app/src/main.ts:23-32, marketplace-backend/app/src/app.module.ts:18-32
Summary
The API Communication Layer provides a structured bridge between the Angular frontend and NestJS backend. By using interceptors on the frontend and middleware on the backend, the system ensures that authentication, data formatting, and cross-origin security are handled consistently across all marketplace features.