Backend Architecture (API)
Relevant source files
The following files were used as context for generating this wiki page:
The Traduora backend is a monolithic API built with the NestJS framework. It provides a comprehensive RESTful API for managing all aspects of the translation platform, including projects, users, terms, and translations. The API is designed to be consumed by the official web application (webapp) and third-party clients or scripts.
Key technologies used in the backend include TypeScript, NestJS for the application framework, TypeORM for database interaction, and JWT for stateless authentication. The API is structured into modules, controllers, and services, following NestJS conventions to create a scalable and maintainable codebase. It supports core functionalities like user authentication, project management, and a powerful import/export system for various translation formats.
Core Technologies
The backend relies on a modern stack centered around NestJS. The dependencies listed in api/package.json reveal the core components of the architecture.
| Library | Purpose |
|---|---|
@nestjs/core | The core NestJS framework for building the application. |
@nestjs/common | Provides common utilities, decorators, and exception filters. |
@nestjs/typeorm | Integrates the TypeORM object-relational mapper (ORM). |
typeorm | The ORM used for database interactions. |
@nestjs/jwt | Implements JSON Web Token (JWT) for authentication. |
@nestjs/passport | Provides an authentication middleware layer based on Passport.js. |
@nestjs/swagger | Generates API documentation using the OpenAPI (Swagger) specification. |
class-validator | Decorator-based validation for request data transfer objects (DTOs). |
class-transformer | Transforms plain objects to class instances and vice-versa. |
bcrypt | Library for hashing passwords. |
pg, mysql2, better-sqlite3 | Database drivers for PostgreSQL, MySQL, and SQLite3. |
Sources: api/package.json:30-70
API Structure
The API follows the standard NestJS architectural pattern, organizing code into modules, controllers, and services. Controllers handle incoming HTTP requests, services contain the business logic, and modules group related components.
API Versioning
All API endpoints are prefixed with /api/v1/, indicating that the API is versioned. This allows for future changes without breaking existing client integrations.
Sources: api/src/controllers/project.controller.ts:12, api/test/project.e2e-spec.ts:16
Controllers and Endpoints
Controllers are responsible for defining the API routes. For example, the ProjectController manages all project-related operations. It uses decorators like @Get, @Post, @Patch, and @Delete to map HTTP methods to controller functions.
The following diagram illustrates the request flow for creating a new project.
This diagram shows how a client request to create a project is handled by the ProjectController, which involves authorization, a database transaction to create multiple related entities, and finally returning the created project data.
Sources: api/src/controllers/project.controller.ts:39-66
A summary of key endpoints in the ProjectController:
| Method | Endpoint | Description |
|---|---|---|
GET | /api/v1/projects | Lists all projects the authenticated user has access to. |
POST | /api/v1/projects | Creates a new project. |
GET | /api/v1/projects/:projectId | Retrieves a single project by its ID. |
PATCH | /api/v1/projects/:projectId | Updates a project's details (e.g., name, description). |
DELETE | /api/v1/projects/:projectId | Deletes a project. |
Sources: api/src/controllers/project.controller.ts, api/test/project.e2e-spec.ts
Authentication and Authorization
The API uses JWT-based authentication. Access tokens are obtained via the /api/v1/auth/token endpoint using different grant types.
Grant Types
Two primary grant types are supported for obtaining an access token:
password: For user authentication. The client sends the user'susername(email) andpassword.client_credentials: For machine-to-machine authentication. The client sends aclient_idandclient_secret.
Sources: api/src/domain/http.ts:18, api/test/auth.e2e-spec.ts:100-110, 137-154
The authentication flow for a user is shown below:
This diagram shows a user exchanging their credentials for a JWT access token.
Sources: api/test/auth.e2e-spec.ts:100-110
Authorization
Once authenticated, authorization is handled on a per-endpoint basis. Most endpoints are protected using the @UseGuards(AuthGuard()) decorator. Within the controllers, an AuthorizationService is used to verify if a user or client has the necessary permissions (e.g., ProjectRole.Admin) to perform a specific action, like deleting a project.
Sources: api/src/controllers/project.controller.ts:12, 107-108
Data Transfer Objects (DTOs)
The API uses Data Transfer Objects (DTOs) to define the shape of request and response bodies. These are TypeScript classes that leverage class-validator decorators for input validation and class-transformer for object serialization. The @nestjs/swagger package uses these DTOs to automatically generate API documentation.
Request DTO Example
The CreateProjectRequest DTO defines the payload for creating a new project.
// Source: api/src/domain/http.ts:401-411
export class CreateProjectRequest {
@Length(1, 255)
@ApiProperty({ minLength: 1, maxLength: 255 })
@Validate(IsNotOnlyWhitespace)
name: string;
@IsOptional()
@Length(0, 255)
@ApiPropertyOptional({ minLength: 0, maxLength: 255 })
description: string;
}
Response DTO Example
Responses are wrapped in a consistent structure. For instance, ProjectResponse wraps a ProjectDTO.
// Source: api/src/domain/http.ts:83-96, 260-263
export class ProjectDTO {
@ApiProperty()
id: string;
@ApiProperty()
name: string;
// ... other properties
}
export class ProjectResponse extends ServiceApiResponse<ProjectDTO> {
@ApiProperty()
data: ProjectDTO;
}
This structure ensures that all API responses have a predictable format, typically { "data": { ... } }.
Sources: api/src/domain/http.ts, api/test/project.e2e-spec.ts:16-26
Import and Export
A key feature of Traduora is the ability to import and export translation data in various formats. This is handled by dedicated endpoints.
- Import:
POST /api/v1/projects/:projectId/imports - Export:
GET /api/v1/projects/:projectId/exports
Sources: api/test/import.e2e-spec.ts:128, api/test/export.e2e-spec.ts:98
Supported Formats
The ImportExportFormat enum defines the list of supported file formats for both import and export operations.
| Format Enum Value | File Format |
|---|---|
androidxml | Android Resources (.xml) |
csv | Comma-Separated Values |
xliff12 | XLIFF 1.2 |
jsonflat | JSON (flat key-value) |
jsonnested | JSON (nested objects) |
yamlflat | YAML (flat key-value) |
yamlnested | YAML (nested objects) |
properties | Java Properties (.properties) |
po | Gettext (.po) |
strings | Apple Strings (.strings) |
php | PHP Array |
resx | .NET Resource File (.resx) |
Sources: api/src/domain/http.ts:456-470
The import process can automatically create new terms and update translations for a given locale. The response from an import operation provides a summary of changes.
{
"data": {
"terms": {
"added": 1,
"skipped": 1
},
"translations": {
"upserted": 2
}
}
}
Sources: api/test/import.e2e-spec.ts:200-210