forked from exelearning/exelearning
-
Notifications
You must be signed in to change notification settings - Fork 1
Add experimental NestJS backend scaffold #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erseco
wants to merge
2
commits into
main
Choose a base branch
from
feature/plan-migration-from-symfony-to-nestjs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| APP_NAME=eXeLearning API | ||
| PORT=3000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module.exports = { | ||
| root: true, | ||
| parser: '@typescript-eslint/parser', | ||
| parserOptions: { | ||
| project: './tsconfig.json' | ||
| }, | ||
| plugins: ['@typescript-eslint'], | ||
| extends: [ | ||
| 'eslint:recommended', | ||
| 'plugin:@typescript-eslint/recommended', | ||
| 'plugin:@typescript-eslint/recommended-requiring-type-checking' | ||
| ], | ||
| env: { | ||
| node: true, | ||
| es2021: true | ||
| }, | ||
| ignorePatterns: ['dist', 'node_modules'], | ||
| rules: { | ||
| '@typescript-eslint/no-misused-promises': ['error', { checksVoidReturn: false }] | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # eXeLearning NestJS backend (experimental) | ||
|
|
||
| Este directorio contiene el código inicial del backend NestJS que convivirá con el backend Symfony actual durante la migración. El objetivo es ofrecer un punto de partida funcional y probado para exponer la API REST v2 mediante Node.js y TypeScript. | ||
|
|
||
| ## Características incluidas | ||
|
|
||
| - Configuración base de NestJS con `ConfigModule`, versionado de rutas y validación global. | ||
| - Módulos `Users` y `Projects` con endpoints CRUD mínimos para facilitar pruebas de contrato. | ||
| - Almacenamiento en memoria para desacoplar la capa HTTP de la persistencia mientras se preparan los repositorios definitivos. | ||
| - Esquema de linters, pruebas unitarias (Jest) y compilación TypeScript alineados con las prácticas del monorepo. | ||
|
|
||
| ## Primeros pasos | ||
|
|
||
| ```bash | ||
| cd nest-backend | ||
| # Instalar dependencias | ||
| yarn install | ||
| # Servidor de desarrollo con recarga en caliente | ||
| yarn start:dev | ||
| # Compilar a JavaScript listo para producción | ||
| yarn build | ||
| ``` | ||
|
|
||
| El servidor escucha en `http://localhost:3000/api/v2` por defecto y acepta la variable `PORT` para personalizar el puerto. | ||
|
|
||
| ## Próximos pasos sugeridos | ||
|
|
||
| 1. Sustituir el almacenamiento en memoria por repositorios conectados a la base de datos mediante TypeORM o Prisma. | ||
| 2. Implementar autenticación y autorización equivalentes a Symfony (usuarios, roles, JWT, CAS/OIDC). | ||
| 3. Añadir módulos para páginas, bloques e iDevices replicando las operaciones avanzadas de la API actual. | ||
| 4. Incorporar pruebas de contrato compartidas entre Symfony y NestJS para garantizar paridad funcional. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { Config } from 'jest'; | ||
|
|
||
| const config: Config = { | ||
| moduleFileExtensions: ['js', 'json', 'ts'], | ||
| rootDir: 'src', | ||
| testRegex: '.*\\.spec\\.ts$', | ||
| transform: { | ||
| '^.+\\.(t|j)s$': 'ts-jest' | ||
| }, | ||
| collectCoverageFrom: ['**/*.(t|j)s'], | ||
| coverageDirectory: '../coverage', | ||
| testEnvironment: 'node' | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/nest-cli", | ||
| "collection": "@nestjs/schematics", | ||
| "sourceRoot": "src" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "@exelearning/api", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "description": "Experimental NestJS backend for eXeLearning", | ||
| "scripts": { | ||
| "start": "node dist/main.js", | ||
| "start:dev": "ts-node-dev --respawn --transpileOnly src/main.ts", | ||
| "build": "tsc -p tsconfig.build.json", | ||
| "lint": "eslint \"src/**/*.ts\"", | ||
| "test": "jest" | ||
| }, | ||
| "dependencies": { | ||
| "@nestjs/common": "^10.3.0", | ||
| "@nestjs/config": "^3.3.0", | ||
| "@nestjs/core": "^10.3.0", | ||
| "@nestjs/mapped-types": "^2.1.0", | ||
| "@nestjs/platform-express": "^10.3.0", | ||
| "class-transformer": "^0.5.1", | ||
| "class-validator": "^0.14.1", | ||
| "reflect-metadata": "^0.2.2", | ||
| "rxjs": "^7.8.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@nestjs/testing": "^10.3.0", | ||
| "@types/express": "^4.17.21", | ||
| "@types/jest": "^29.5.14", | ||
| "@types/node": "^22.10.1", | ||
| "@typescript-eslint/eslint-plugin": "^8.15.0", | ||
| "@typescript-eslint/parser": "^8.15.0", | ||
| "eslint": "^9.20.0", | ||
| "jest": "^29.7.0", | ||
| "ts-jest": "^29.2.5", | ||
| "ts-node-dev": "^2.0.0", | ||
| "typescript": "^5.7.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { ConfigModule } from '@nestjs/config'; | ||
| import { UsersModule } from './users/users.module'; | ||
| import { ProjectsModule } from './projects/projects.module'; | ||
| import { OdeExportModule } from './ode-export/ode-export.module'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| ConfigModule.forRoot({ | ||
| isGlobal: true, | ||
| envFilePath: ['.env', '.env.local'] | ||
| }), | ||
| UsersModule, | ||
| ProjectsModule, | ||
| OdeExportModule | ||
| ] | ||
| }) | ||
| export class AppModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export interface ApplicationConfig { | ||
| appName: string; | ||
| port: number; | ||
| } | ||
|
|
||
| export const defaultConfig = (): ApplicationConfig => ({ | ||
| appName: process.env.APP_NAME ?? 'eXeLearning API', | ||
| port: process.env.PORT ? Number(process.env.PORT) : 3000 | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import 'reflect-metadata'; | ||
| import { Logger, ValidationPipe, VersioningType } from '@nestjs/common'; | ||
| import { NestFactory } from '@nestjs/core'; | ||
| import { AppModule } from './app.module'; | ||
|
|
||
| async function bootstrap(): Promise<void> { | ||
| const app = await NestFactory.create(AppModule, { | ||
| bufferLogs: true | ||
| }); | ||
|
|
||
| app.setGlobalPrefix('api/v2'); | ||
| app.enableVersioning({ | ||
| type: VersioningType.URI, | ||
| defaultVersion: '2' | ||
| }); | ||
| app.useGlobalPipes( | ||
| new ValidationPipe({ | ||
| transform: true, | ||
| whitelist: true, | ||
| forbidNonWhitelisted: true | ||
| }) | ||
| ); | ||
|
|
||
| const logger = new Logger('Bootstrap'); | ||
| const port = process.env.PORT ? Number(process.env.PORT) : 3000; | ||
| await app.listen(port); | ||
| logger.log(`NestJS backend listening on port ${port}`); | ||
| } | ||
|
|
||
| bootstrap().catch((error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Failed to bootstrap NestJS backend', error); | ||
| process.exitCode = 1; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export const ODE_SESSION_ID_REGEX = /^[A-Za-z0-9_-]{3,128}$/; | ||
|
|
||
| export const ODE_EXPORT_RELATIVE_PATH_REGEX = | ||
| /^(?:[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)*)$/; | ||
|
|
||
| export const ODE_EXPORT_ALLOWED_TYPES = [ | ||
| 'elp', | ||
| 'html5', | ||
| 'html5-sp', | ||
| 'scorm12', | ||
| 'scorm2004', | ||
| 'ims', | ||
| 'epub3', | ||
| 'properties' | ||
| ] as const; | ||
|
|
||
| export type OdeExportType = (typeof ODE_EXPORT_ALLOWED_TYPES)[number]; | ||
|
|
||
| export const ODE_EXPORT_PREVIEW_TYPE: OdeExportType = 'html5'; |
12 changes: 12 additions & 0 deletions
12
nest-backend/src/ode-export/dto/ode-export-download-params.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { IsIn, IsString, Matches } from 'class-validator'; | ||
| import { ODE_EXPORT_ALLOWED_TYPES, ODE_SESSION_ID_REGEX, OdeExportType } from '../constants'; | ||
|
|
||
| export class OdeExportDownloadParamsDto { | ||
| @IsString() | ||
| @Matches(ODE_SESSION_ID_REGEX) | ||
| odeSessionId!: string; | ||
|
|
||
| @IsString() | ||
| @IsIn(ODE_EXPORT_ALLOWED_TYPES) | ||
| exportType!: OdeExportType; | ||
| } |
8 changes: 8 additions & 0 deletions
8
nest-backend/src/ode-export/dto/ode-export-file-request.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { IsString, Matches } from 'class-validator'; | ||
| import { ODE_EXPORT_RELATIVE_PATH_REGEX } from '../constants'; | ||
|
|
||
| export class OdeExportFileRequestDto { | ||
| @IsString() | ||
| @Matches(ODE_EXPORT_RELATIVE_PATH_REGEX) | ||
| relativePath!: string; | ||
| } |
8 changes: 8 additions & 0 deletions
8
nest-backend/src/ode-export/dto/ode-export-preview-params.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { IsString, Matches } from 'class-validator'; | ||
| import { ODE_SESSION_ID_REGEX } from '../constants'; | ||
|
|
||
| export class OdeExportPreviewParamsDto { | ||
| @IsString() | ||
| @Matches(ODE_SESSION_ID_REGEX) | ||
| odeSessionId!: string; | ||
| } |
22 changes: 22 additions & 0 deletions
22
nest-backend/src/ode-export/dto/ode-export-response.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export class OdeExportResponseDto { | ||
| responseMessage!: 'OK' | string; | ||
| urlZipFile!: string; | ||
| zipFileName!: string; | ||
| exportProjectName!: string; | ||
| urlPreviewIndex!: string; | ||
| generatedAt!: string; | ||
|
|
||
| constructor(init: Partial<OdeExportResponseDto>) { | ||
| Object.assign(this, init); | ||
| } | ||
| } | ||
|
|
||
| export class OdeExportFileHandleDto { | ||
| fileName!: string; | ||
| mimeType!: string; | ||
| stream!: NodeJS.ReadableStream; | ||
|
|
||
| constructor(init: Partial<OdeExportFileHandleDto>) { | ||
| Object.assign(this, init); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Controller, Get, Param, Res } from '@nestjs/common'; | ||
| import { Response } from 'express'; | ||
| import { StreamableFile } from '@nestjs/common'; | ||
| import { OdeExportService } from './ode-export.service'; | ||
| import { OdeExportDownloadParamsDto } from './dto/ode-export-download-params.dto'; | ||
| import { OdeExportPreviewParamsDto } from './dto/ode-export-preview-params.dto'; | ||
| import { OdeExportResponseDto } from './dto/ode-export-response.dto'; | ||
| import { OdeExportFileRequestDto } from './dto/ode-export-file-request.dto'; | ||
|
|
||
| @Controller({ path: 'ode-export', version: '2' }) | ||
| export class OdeExportController { | ||
| constructor(private readonly odeExportService: OdeExportService) {} | ||
|
|
||
| @Get(':odeSessionId/:exportType/download') | ||
| async download(@Param() params: OdeExportDownloadParamsDto): Promise<OdeExportResponseDto> { | ||
| return this.odeExportService.generateDownload(params.odeSessionId, params.exportType); | ||
| } | ||
|
|
||
| @Get(':odeSessionId/preview') | ||
| async preview(@Param() params: OdeExportPreviewParamsDto): Promise<OdeExportResponseDto> { | ||
| return this.odeExportService.generatePreview(params.odeSessionId); | ||
| } | ||
|
|
||
| @Get('files/tmp/:relativePath(*)') | ||
| async getFile( | ||
| @Param() params: OdeExportFileRequestDto, | ||
| @Res({ passthrough: true }) res: Response | ||
| ): Promise<StreamableFile> { | ||
| const file = await this.odeExportService.openArtifact(params.relativePath); | ||
| res.setHeader('Content-Type', file.mimeType); | ||
| res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(file.fileName)}"`); | ||
|
|
||
| return new StreamableFile(file.stream); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { OdeExportController } from './ode-export.controller'; | ||
| import { OdeExportService } from './ode-export.service'; | ||
|
|
||
| @Module({ | ||
| controllers: [OdeExportController], | ||
| providers: [OdeExportService], | ||
| exports: [OdeExportService] | ||
| }) | ||
| export class OdeExportModule {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/v2prefix when enabling URI versioningThe bootstrap configuration sets
app.setGlobalPrefix('api/v2')and also enables URI versioning with default version'2'while every controller is registered for version'2'. In NestJS the version segment is appended after the global prefix, so the effective routes become/api/v2/v2/usersand/api/v2/v2/projects. The README states the service should be reachable underhttp://localhost:3000/api/v2, but those endpoints will 404 because they are mounted under/api/v2/v2/.... Either drop the hard-coded/v2from the global prefix or disable explicit versioning to expose the endpoints at/api/v2/*as documented.Useful? React with 👍 / 👎.