Improve code consistency and formatting

Standardize import order and formatting across DTOs and controllers. Ensure `HttpCode` usage aligns with correct HTTP status codes. Add CORS setup to `main.ts` for local development support.
This commit is contained in:
Mathis H (Avnyr) 2024-10-24 16:14:40 +02:00
parent bfe49f65ec
commit d5370220a4
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
11 changed files with 72 additions and 57 deletions

View File

@ -1,7 +1,7 @@
import { Controller, Get } from "@nestjs/common"; import { Controller, Get } from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import { AppService } from "./app.service"; import { AppService } from "./app.service";
import { ApiTags } from '@nestjs/swagger';
@Controller() @Controller()
@ApiTags("useless") @ApiTags("useless")

View File

@ -10,12 +10,12 @@ import {
UnauthorizedException, UnauthorizedException,
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { SignInDto, SignUpDto } from "apps/backend/src/app/auth/auth.dto"; import { SignInDto, SignUpDto } from "apps/backend/src/app/auth/auth.dto";
import { AuthService } from "apps/backend/src/app/auth/auth.service"; import { AuthService } from "apps/backend/src/app/auth/auth.service";
import { UserGuard } from "./auth.guard"; import { UserGuard } from "./auth.guard";
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('User authentification') @ApiTags("User authentification")
@Controller("auth") @Controller("auth")
export class AuthController { export class AuthController {
constructor(private readonly authService: AuthService) {} constructor(private readonly authService: AuthService) {}

View File

@ -1,3 +1,4 @@
import { ApiProperty } from "@nestjs/swagger";
import { import {
IsEmail, IsEmail,
IsNotEmpty, IsNotEmpty,
@ -6,7 +7,6 @@ import {
MaxLength, MaxLength,
MinLength, MinLength,
} from "class-validator"; } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
export class SignUpDto { export class SignUpDto {
/* /*
@ -24,7 +24,7 @@ export class SignUpDto {
**/ **/
@ApiProperty({ @ApiProperty({
example: 'jean@paul.fr', example: "jean@paul.fr",
}) })
@MaxLength(32) @MaxLength(32)
@IsEmail() @IsEmail()
@ -32,7 +32,7 @@ export class SignUpDto {
email: string; email: string;
@ApiProperty({ @ApiProperty({
example: 'zSEs-6ze$', example: "zSEs-6ze$",
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
@ -43,15 +43,17 @@ export class SignUpDto {
} }
export class SignInDto { export class SignInDto {
@MaxLength(32)@ApiProperty({ @MaxLength(32)
example: 'jean@paul.fr', @ApiProperty({
example: "jean@paul.fr",
}) })
@IsEmail() @IsEmail()
@IsNotEmpty() @IsNotEmpty()
email: string; email: string;
@IsString()@ApiProperty({ @IsString()
example: 'zSEs-6ze$', @ApiProperty({
example: "zSEs-6ze$",
}) })
@IsNotEmpty() @IsNotEmpty()
@IsStrongPassword({ @IsStrongPassword({

View File

@ -9,11 +9,11 @@ import {
Query, Query,
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard"; import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { AuthorsService } from "apps/backend/src/app/authors/authors.service"; import { AuthorsService } from "apps/backend/src/app/authors/authors.service";
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('File authors') @ApiTags("File authors")
@Controller("authors") @Controller("authors")
export class AuthorsController { export class AuthorsController {
constructor(private readonly authorService: AuthorsService) {} constructor(private readonly authorService: AuthorsService) {}

View File

@ -20,17 +20,20 @@ import {
StreamableFile, StreamableFile,
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import {
ApiBearerAuth,
ApiBody,
ApiConsumes,
ApiHeaders,
ApiOperation,
ApiSecurity,
ApiTags,
} from "@nestjs/swagger";
import { CreateFileTypeDto } from "apps/backend/src/app/files/files.dto"; import { CreateFileTypeDto } from "apps/backend/src/app/files/files.dto";
import { AdminGuard, InsertAdminState } from "../auth/auth.guard"; import { AdminGuard, InsertAdminState } from "../auth/auth.guard";
import { FilesService } from "./files.service"; import { FilesService } from "./files.service";
import {
ApiBearerAuth, ApiBody, ApiConsumes,
ApiHeaders, ApiOperation,
ApiSecurity,
ApiTags
} from '@nestjs/swagger';
@ApiTags('Files') @ApiTags("Files")
@Controller("files") @Controller("files")
export class FilesController { export class FilesController {
constructor(private readonly filesService: FilesService) {} constructor(private readonly filesService: FilesService) {}
@ -39,9 +42,9 @@ export class FilesController {
@ApiConsumes("application/octet-stream") @ApiConsumes("application/octet-stream")
@ApiBody({ @ApiBody({
schema: { schema: {
type: 'string', type: "string",
format: 'binary' format: "binary",
} },
}) })
@ApiHeaders([ @ApiHeaders([
{ {
@ -49,18 +52,21 @@ export class FilesController {
description: "Nom du fichier", description: "Nom du fichier",
example: "Logo marianne", example: "Logo marianne",
allowEmptyValue: false, allowEmptyValue: false,
required: true required: true,
}, },
{ {
name: "group_id", name: "group_id",
description: "Groupe de fichier optionnel", description: "Groupe de fichier optionnel",
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c", example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
allowEmptyValue: true allowEmptyValue: true,
}, },
{ {
name: "machine_id", name: "machine_id",
description: "Identifiant(s) de machine(s)", description: "Identifiant(s) de machine(s)",
example: ["dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c", "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c"], example: [
"dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
"dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
],
allowEmptyValue: false, allowEmptyValue: false,
required: true, required: true,
}, },
@ -73,13 +79,15 @@ export class FilesController {
}, },
{ {
name: "is_documentation", name: "is_documentation",
description: "Admin uniquement, défini le fichier comme étant une documentation", description:
"Admin uniquement, défini le fichier comme étant une documentation",
enum: ["true", "false"], enum: ["true", "false"],
allowEmptyValue: false, allowEmptyValue: false,
}, },
{ {
name: "is_restricted", name: "is_restricted",
description: "Admin uniquement, rend impossible l'écrasement d'un fichier (non implémenté pour l'instant)", description:
"Admin uniquement, rend impossible l'écrasement d'un fichier (non implémenté pour l'instant)",
enum: ["true", "false"], enum: ["true", "false"],
allowEmptyValue: false, allowEmptyValue: false,
}, },
@ -170,7 +178,7 @@ export class FilesController {
return; return;
} }
@HttpCode(HttpStatus.FOUND) @HttpCode(HttpStatus.OK)
@Get("find") @Get("find")
async findMany( async findMany(
@Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number,
@ -180,7 +188,7 @@ export class FilesController {
return this.filesService.search(limit, offset, search); return this.filesService.search(limit, offset, search);
} }
@HttpCode(HttpStatus.FOUND) @HttpCode(HttpStatus.OK)
@Get("types") @Get("types")
async getTypes() { async getTypes() {
console.log("Performing request"); console.log("Performing request");

View File

@ -1,25 +1,20 @@
import { DefaultValuePipe } from "@nestjs/common"; import { DefaultValuePipe } from "@nestjs/common";
import { ApiBearerAuth, ApiProperty } from "@nestjs/swagger";
import { IsUUID, MaxLength, MinLength } from "class-validator"; import { IsUUID, MaxLength, MinLength } from "class-validator";
import { ApiBearerAuth, ApiProperty } from '@nestjs/swagger';
export class CreateFileTypeDto { export class CreateFileTypeDto {
@ApiProperty({ @ApiProperty({
description: "Admin uniquement, nom d'affichage.", description: "Admin uniquement, nom d'affichage.",
examples: [ examples: [".scad", "jpg"],
".scad",
"jpg"
]
}) })
@MaxLength(128) @MaxLength(128)
@MinLength(3) @MinLength(3)
name: string; name: string;
@ApiProperty({ @ApiProperty({
description: "Admin uniquement, Multipurpose Internet Mail Extensions (MIME)", description:
examples: [ "Admin uniquement, Multipurpose Internet Mail Extensions (MIME)",
"application/x-openscad", examples: ["application/x-openscad", "image/jpeg"],
"image/jpeg"
]
}) })
@MaxLength(64) @MaxLength(64)
@MinLength(4) @MinLength(4)

View File

@ -10,13 +10,13 @@ import {
Query, Query,
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard"; import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { CreateGroupDto } from "apps/backend/src/app/groups/groups.dto"; import { CreateGroupDto } from "apps/backend/src/app/groups/groups.dto";
import { ISearchQuery } from "apps/backend/src/app/groups/groups.types"; import { ISearchQuery } from "apps/backend/src/app/groups/groups.types";
import { GroupsService } from "./groups.service"; import { GroupsService } from "./groups.service";
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('File groups') @ApiTags("File groups")
@Controller("groups") @Controller("groups")
export class GroupsController { export class GroupsController {
constructor(private readonly groupsService: GroupsService) {} constructor(private readonly groupsService: GroupsService) {}

View File

@ -1,10 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString, MaxLength, MinLength } from "class-validator"; import { IsString, MaxLength, MinLength } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
export class CreateGroupDto { export class CreateGroupDto {
@ApiProperty({ @ApiProperty({
description: "Nom unique.", description: "Nom unique.",
example: "Numérique en communs" example: "Numérique en communs",
}) })
@IsString() @IsString()
@MinLength(4) @MinLength(4)

View File

@ -13,16 +13,16 @@ import {
Query, Query,
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import { ApiResponse, ApiTags } from "@nestjs/swagger";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard"; import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { IMachinesTable } from "apps/backend/src/app/db/schema";
import { import {
CreateMachineDto, CreateMachineDto,
TypeDto, TypeDto,
} from "apps/backend/src/app/machines/machines.dto"; } from "apps/backend/src/app/machines/machines.dto";
import { MachinesService } from "apps/backend/src/app/machines/machines.service"; import { MachinesService } from "apps/backend/src/app/machines/machines.service";
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { IMachinesTable } from 'apps/backend/src/app/db/schema';
@ApiTags('Machines') @ApiTags("Machines")
@Controller("machines") @Controller("machines")
export class MachinesController { export class MachinesController {
constructor(private readonly machineService: MachinesService) {} constructor(private readonly machineService: MachinesService) {}
@ -68,13 +68,13 @@ export class MachinesController {
return await this.machineService.removeFileType(machineId, body.fileTypeId); return await this.machineService.removeFileType(machineId, body.fileTypeId);
} }
@HttpCode(HttpStatus.FOUND) @HttpCode(HttpStatus.OK)
@Get("types/:machineId") @Get("types/:machineId")
async getTypesOfMachine(@Param("machineId") machineId: string) { async getTypesOfMachine(@Param("machineId") machineId: string) {
return await this.machineService.getFilesTypes(machineId); return await this.machineService.getFilesTypes(machineId);
} }
@HttpCode(HttpStatus.FOUND) @HttpCode(HttpStatus.OK)
@Get("files/:machineId") @Get("files/:machineId")
async getFilesForMachine( async getFilesForMachine(
@Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number,

View File

@ -1,16 +1,16 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsUUID, MaxLength, MinLength } from "class-validator"; import { IsUUID, MaxLength, MinLength } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
export class CreateMachineDto { export class CreateMachineDto {
@ApiProperty({ @ApiProperty({
example: "Découpeuse laser portable" example: "Découpeuse laser portable",
}) })
@MaxLength(128) @MaxLength(128)
@MinLength(4) @MinLength(4)
machineName: string; machineName: string;
@ApiProperty({ @ApiProperty({
example: "Découpe au laser" example: "Découpe au laser",
}) })
@MaxLength(64) @MaxLength(64)
@MinLength(2) @MinLength(2)
@ -19,8 +19,9 @@ export class CreateMachineDto {
export class TypeDto { export class TypeDto {
@ApiProperty({ @ApiProperty({
description: "Un identifiant unique présent en base de donnée qui représente un MIME", description:
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c" "Un identifiant unique présent en base de donnée qui représente un MIME",
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
}) })
@IsUUID() @IsUUID()
fileTypeId: string; fileTypeId: string;

View File

@ -2,7 +2,9 @@ import { Logger } from "@nestjs/common";
import { NestFactory } from "@nestjs/core"; import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import cors from "cors";
import helmet from "helmet"; import helmet from "helmet";
import { AppModule } from "./app/app.module"; import { AppModule } from "./app/app.module";
async function bootstrap() { async function bootstrap() {
@ -11,9 +13,9 @@ async function bootstrap() {
.setDescription("Définition de l'api du FabLab Explorer") .setDescription("Définition de l'api du FabLab Explorer")
.setVersion("1.0") .setVersion("1.0")
.addBearerAuth({ .addBearerAuth({
type: 'http', type: "http",
scheme: 'bearer', scheme: "bearer",
in: 'header', in: "header",
}) })
.build(); .build();
@ -23,6 +25,13 @@ async function bootstrap() {
app.use(helmet()); app.use(helmet());
const port = process.env.PORT || 3333; const port = process.env.PORT || 3333;
const corsOptions = {
origin: "http://localhost:3000",
methods: ["GET", "POST", "PUT", "DELETE"],
};
app.use(cors(corsOptions));
const document = SwaggerModule.createDocument(app, config); const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("api", app, document); SwaggerModule.setup("api", app, document);