Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
"@nestjs/core": "^11.0.0",
"@nestjs/platform-express": "^11.0.0",
"@org/shared-types": "^0.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"tslib": "^2.3.0"
Expand Down
12 changes: 6 additions & 6 deletions apps/backend/src/app/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Controller, Post, Body } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
import type { AuthResponse } from '@org/shared-types';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('login')
login(@Body() body: { email: string; password: string }): Promise<AuthResponse> {
return this.authService.login(body.email, body.password);
login(@Body() dto: LoginDto): Promise<AuthResponse> {
return this.authService.login(dto.email, dto.password);
}

@Post('register')
register(
@Body() body: { name: string; email: string; password: string },
): Promise<AuthResponse> {
return this.authService.register(body.name, body.email, body.password);
register(@Body() dto: RegisterDto): Promise<AuthResponse> {
return this.authService.register(dto.name, dto.email, dto.password);
}
}
10 changes: 10 additions & 0 deletions apps/backend/src/app/auth/dto/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsEmail, IsString, MinLength } from 'class-validator';

export class LoginDto {
@IsEmail()
email: string;

@IsString()
@MinLength(1)
password: string;
}
14 changes: 14 additions & 0 deletions apps/backend/src/app/auth/dto/register.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IsEmail, IsString, MinLength } from 'class-validator';

export class RegisterDto {
@IsString()
@MinLength(1)
name: string;

@IsEmail()
email: string;

@IsString()
@MinLength(6)
password: string;
}
38 changes: 13 additions & 25 deletions apps/backend/src/app/strapi-client/strapi-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,28 @@ export class StrapiClientService {
}
}

async get<T>(path: string): Promise<T[]> {
const res = await this.request(path);
if (!res.ok) {
throw new ServiceUnavailableException(`Strapi returned ${res.status}`);
}
let body: StrapiListResponse<T>;
private async parseJson<T>(res: Response): Promise<T> {
try {
body = (await res.json()) as StrapiListResponse<T>;
return (await res.json()) as T;
} catch {
throw new ServiceUnavailableException('Strapi returned invalid JSON');
}
return body.data ?? [];
}

async getById<T>(path: string): Promise<T> {
async get<T>(path: string): Promise<T[]> {
const res = await this.request(path);
if (res.status === 404) {
throw new NotFoundException('Resource not found');
}
if (!res.ok) {
throw new ServiceUnavailableException(`Strapi returned ${res.status}`);
}
let body: StrapiSingleResponse<T>;
try {
body = (await res.json()) as StrapiSingleResponse<T>;
} catch {
throw new ServiceUnavailableException('Strapi returned invalid JSON');
}
const body = await this.parseJson<StrapiListResponse<T>>(res);
return body.data ?? [];
}

async getById<T>(path: string): Promise<T> {
const res = await this.request(path);
if (res.status === 404) throw new NotFoundException('Resource not found');
if (!res.ok) throw new ServiceUnavailableException(`Strapi returned ${res.status}`);
const body = await this.parseJson<StrapiSingleResponse<T>>(res);
return body.data;
}

Expand All @@ -70,12 +64,6 @@ export class StrapiClientService {
} catch { /* ignore */ }
throw new HttpException({ message }, res.status);
}
let result: T;
try {
result = (await res.json()) as T;
} catch {
throw new ServiceUnavailableException('Strapi returned invalid JSON');
}
return result;
return this.parseJson<T>(res);
}
}
3 changes: 2 additions & 1 deletion apps/backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* This is only a minimal backend to get started.
*/

import { Logger } from '@nestjs/common';
import { Logger, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({ origin: process.env['CORS_ORIGIN'] ?? 'http://localhost:3000' });
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3000;
Expand Down
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading