Aller au contenu principal

Standards de Code

Standards et conventions de code pour le projet MyTelevision API.


TypeScript

Conventions de Nommage

// Classes: PascalCase
class MovieService {}

// Interfaces: PascalCase (prefixe I optionnel)
interface MovieRepository {}

// Variables/Fonctions: camelCase
const movieCount = 10;
function getMovieById(id: string) {}

// Constantes: SCREAMING_SNAKE_CASE
const MAX_RETRIES = 3;

// Fichiers: kebab-case
// movie.service.ts
// create-movie.dto.ts

Types stricts

// Bon
function getUser(id: string): Promise<User | null> {
// ...
}

// Mauvais - jamais de any
function getUser(id): any {
// ...
}
TypeScript Strict Mode

Le projet utilise TypeScript en mode strict. L'utilisation de any est interdite. Tous les types doivent etre explicites.


Patterns NestJS

Controller

@Controller('movies')
@ApiTags('Movies')
export class MoviesController {
constructor(private readonly moviesService: MoviesService) {}

@Get()
@ApiOperation({ summary: 'List movies' })
findAll(@Query() query: PaginationDto): Promise<PaginatedResponse<Movie>> {
return this.moviesService.findAll(query);
}

@Get(':id')
@ApiOperation({ summary: 'Get movie by ID' })
findOne(@Param('id', ParseUUIDPipe) id: string): Promise<Movie> {
return this.moviesService.findById(id);
}
}

Service

@Injectable()
export class MoviesService {
constructor(
private readonly prisma: PrismaService,
private readonly cache: RedisService,
) {}

async findById(id: string): Promise<Movie> {
// Try cache first
const cached = await this.cache.get(`movie:${id}`);
if (cached) return cached;

// Fetch from DB
const movie = await this.prisma.movie.findUnique({ where: { id } });
if (!movie) throw new NotFoundException('Movie not found');

// Cache result
await this.cache.set(`movie:${id}`, movie, 300);
return movie;
}
}

DTO (Data Transfer Object)

export class CreateMovieDto {
@ApiProperty({ example: 'Inception' })
@IsString()
@MinLength(1)
@MaxLength(255)
title: string;

@ApiProperty({ required: false })
@IsString()
@IsOptional()
overview?: string;

@ApiProperty({ enum: ContentStatus, default: ContentStatus.DRAFT })
@IsEnum(ContentStatus)
@IsOptional()
status?: ContentStatus = ContentStatus.DRAFT;
}

Structure des Modules

Chaque fonctionnalite suit le pattern modulaire NestJS :

src/
├── application/
│ ├── dtos/{feature}/ # Data Transfer Objects
│ └── services/{feature}/ # Logique metier
├── infrastructure/
│ ├── config/ # Configuration
│ ├── database/prisma/ # Prisma ORM
│ ├── cache/ # Redis
│ └── guards/ # Guards d'authentification
├── presentation/
│ ├── controllers/api/v2/ # Endpoints publics
│ ├── controllers/admin/ # Endpoints admin
│ └── modules/ # Modules de presentation
└── shared/ # Constantes, types, utilitaires

Path Aliases

@/               -> src/
@domain/ -> src/domain/
@application/ -> src/application/
@infrastructure/ -> src/infrastructure/
@presentation/ -> src/presentation/
@shared/ -> src/shared/

Convention de Commits Git

Le projet suit la specification Conventional Commits :

# Format
<type>(<scope>): <subject>

# Types
feat: Nouvelle fonctionnalite
fix: Correction de bug
docs: Documentation
test: Tests
refactor: Refactoring
perf: Performance
ci: CI/CD
chore: Maintenance

# Exemples
feat(movies): add filtering by genre
fix(auth): resolve token expiration issue
docs(readme): update installation guide
test(news): add unit tests for NewsService

Scopes

Les scopes correspondent aux modules metier : auth, users, movies, series, news, livetv, radio, podcasts, replays, events, streaming, payments, etc.


Regles ESLint et Securite

Le projet integre des regles ESLint orientees securite :

RegleObjectif
no-eval, no-implied-eval, no-new-funcPrevention de l'injection de code
no-proto, no-extend-nativePrevention de la pollution de prototype
eqeqeqPrevention des bugs de coercion de type
no-buffer-constructorPrevention des vulnerabilites buffer

Checklist de Code Review

Avant chaque Pull Request, verifier :

  • Tests unitaires ajoutes
  • Documentation Swagger/OpenAPI mise a jour
  • Collection Postman mise a jour
  • Pas de console.log oublie
  • Gestion des erreurs appropriee
  • Pas de secrets dans le code
  • TypeScript strict (aucun any)
  • Couverture de tests >= 80%

Configuration IDE

VS Code - Extensions recommandees

  • ESLint -- Linting TypeScript
  • Prettier -- Formatage automatique
  • Prisma -- Support du schema Prisma
  • Jest Runner -- Execution des tests
  • GitLens -- Historique Git avance
  • Thunder Client -- Client HTTP integre

VS Code - Settings

{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.validate": ["typescript"]
}