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 { ApiTags } from "@nestjs/swagger";
import { AppService } from "./app.service";
import { ApiTags } from '@nestjs/swagger';
@Controller()
@ApiTags("useless")

View File

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

View File

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

View File

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

View File

@ -20,17 +20,20 @@ import {
StreamableFile,
UseGuards,
} 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 { AdminGuard, InsertAdminState } from "../auth/auth.guard";
import { FilesService } from "./files.service";
import {
ApiBearerAuth, ApiBody, ApiConsumes,
ApiHeaders, ApiOperation,
ApiSecurity,
ApiTags
} from '@nestjs/swagger';
@ApiTags('Files')
@ApiTags("Files")
@Controller("files")
export class FilesController {
constructor(private readonly filesService: FilesService) {}
@ -39,9 +42,9 @@ export class FilesController {
@ApiConsumes("application/octet-stream")
@ApiBody({
schema: {
type: 'string',
format: 'binary'
}
type: "string",
format: "binary",
},
})
@ApiHeaders([
{
@ -49,18 +52,21 @@ export class FilesController {
description: "Nom du fichier",
example: "Logo marianne",
allowEmptyValue: false,
required: true
required: true,
},
{
name: "group_id",
description: "Groupe de fichier optionnel",
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
allowEmptyValue: true
allowEmptyValue: true,
},
{
name: "machine_id",
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,
required: true,
},
@ -73,13 +79,15 @@ export class FilesController {
},
{
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"],
allowEmptyValue: false,
},
{
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"],
allowEmptyValue: false,
},
@ -170,7 +178,7 @@ export class FilesController {
return;
}
@HttpCode(HttpStatus.FOUND)
@HttpCode(HttpStatus.OK)
@Get("find")
async findMany(
@Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number,
@ -180,7 +188,7 @@ export class FilesController {
return this.filesService.search(limit, offset, search);
}
@HttpCode(HttpStatus.FOUND)
@HttpCode(HttpStatus.OK)
@Get("types")
async getTypes() {
console.log("Performing request");

View File

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

View File

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

View File

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

View File

@ -13,16 +13,16 @@ import {
Query,
UseGuards,
} from "@nestjs/common";
import { ApiResponse, ApiTags } from "@nestjs/swagger";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { IMachinesTable } from "apps/backend/src/app/db/schema";
import {
CreateMachineDto,
TypeDto,
} from "apps/backend/src/app/machines/machines.dto";
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")
export class MachinesController {
constructor(private readonly machineService: MachinesService) {}
@ -68,13 +68,13 @@ export class MachinesController {
return await this.machineService.removeFileType(machineId, body.fileTypeId);
}
@HttpCode(HttpStatus.FOUND)
@HttpCode(HttpStatus.OK)
@Get("types/:machineId")
async getTypesOfMachine(@Param("machineId") machineId: string) {
return await this.machineService.getFilesTypes(machineId);
}
@HttpCode(HttpStatus.FOUND)
@HttpCode(HttpStatus.OK)
@Get("files/:machineId")
async getFilesForMachine(
@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 { ApiProperty } from '@nestjs/swagger';
export class CreateMachineDto {
@ApiProperty({
example: "Découpeuse laser portable"
example: "Découpeuse laser portable",
})
@MaxLength(128)
@MinLength(4)
machineName: string;
@ApiProperty({
example: "Découpe au laser"
example: "Découpe au laser",
})
@MaxLength(64)
@MinLength(2)
@ -19,8 +19,9 @@ export class CreateMachineDto {
export class TypeDto {
@ApiProperty({
description: "Un identifiant unique présent en base de donnée qui représente un MIME",
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c"
description:
"Un identifiant unique présent en base de donnée qui représente un MIME",
example: "dfd0fbb1-2bf3-4dbe-b86d-89b1bff5106c",
})
@IsUUID()
fileTypeId: string;

View File

@ -2,7 +2,9 @@ import { Logger } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import cors from "cors";
import helmet from "helmet";
import { AppModule } from "./app/app.module";
async function bootstrap() {
@ -11,9 +13,9 @@ async function bootstrap() {
.setDescription("Définition de l'api du FabLab Explorer")
.setVersion("1.0")
.addBearerAuth({
type: 'http',
scheme: 'bearer',
in: 'header',
type: "http",
scheme: "bearer",
in: "header",
})
.build();
@ -23,6 +25,13 @@ async function bootstrap() {
app.use(helmet());
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);
SwaggerModule.setup("api", app, document);