From 6a021886ffe1244a01cfbe0ce2dfd3fea64f8de4 Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Wed, 15 Feb 2023 14:17:19 -0300 Subject: [PATCH] Teste deploy na vercel - 8 - Back-end. --- back-end/.gitignore | 2 +- back-end/.vercelignore | 4 +--- back-end/package.json | 8 +------- back-end/src/Errors/TodoErrors.ts | 4 ++-- back-end/src/Errors/userErrors.ts | 4 ++-- back-end/src/index.ts | 2 +- back-end/src/useCases/todoCases/TodoCases.ts | 16 ++++++++-------- back-end/src/useCases/userCases/UserCases.ts | 17 ++++++++--------- 8 files changed, 24 insertions(+), 33 deletions(-) diff --git a/back-end/.gitignore b/back-end/.gitignore index d4a9008..b93a530 100644 --- a/back-end/.gitignore +++ b/back-end/.gitignore @@ -80,7 +80,7 @@ typings/ # Nuxt.js build / generate output .nuxt -dist +# dist # Gatsby files .cache/ diff --git a/back-end/.vercelignore b/back-end/.vercelignore index 18fcf41..97ceeb9 100644 --- a/back-end/.vercelignore +++ b/back-end/.vercelignore @@ -6,6 +6,4 @@ bin package-lock.json yarn.lock yarn-error.log -.env -.env** -**.env \ No newline at end of file +.env \ No newline at end of file diff --git a/back-end/package.json b/back-end/package.json index b13726e..6990522 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -4,14 +4,8 @@ "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", - "tsup": { - "entry": ["src/index.ts", "src/errors"], - "splitting": false, - "sourcemap": true, - "clean": true - }, "scripts": { - "start": "tsup && node dist/index.js", + "start": "npx tsup src && node dist/index.js", "dev": "tsx watch src/index.ts", "build": "tsup", "test": "echo \"Error: no test specified\" && exit 1", diff --git a/back-end/src/Errors/TodoErrors.ts b/back-end/src/Errors/TodoErrors.ts index 270074f..0e2de8f 100644 --- a/back-end/src/Errors/TodoErrors.ts +++ b/back-end/src/Errors/TodoErrors.ts @@ -1,8 +1,8 @@ import { CustomError } from "./CustomError"; -export class ErrorTodo extends CustomError { +export class TodoErrors extends CustomError { constructor( - public error: Record | any, + public error: Record, public statusCode: number ){ super(error, statusCode); diff --git a/back-end/src/Errors/userErrors.ts b/back-end/src/Errors/userErrors.ts index 593d267..21f3a40 100644 --- a/back-end/src/Errors/userErrors.ts +++ b/back-end/src/Errors/userErrors.ts @@ -1,8 +1,8 @@ import { CustomError } from "./CustomError"; -export class ErrorUser extends CustomError { +export class UserErrors extends CustomError { constructor( - public error: Record | any, + public error: Record, public statusCode: number ){ super(error, statusCode); diff --git a/back-end/src/index.ts b/back-end/src/index.ts index 0fb8638..6fcff2e 100644 --- a/back-end/src/index.ts +++ b/back-end/src/index.ts @@ -2,10 +2,10 @@ import { Request, Response, NextFunction } from "express"; import { app } from "./server"; import "express-async-errors"; +import { CustomError } from "./errors/CustomError"; import { userRoutes } from "./routes/userRoutes"; import { todoRoutes } from "./routes/todoRoutes"; import { authMiddleware } from "./middlewares/authMiddleware"; -import { CustomError } from "./errors/CustomError"; app.use("/user", userRoutes); app.use("/todo", authMiddleware, todoRoutes); diff --git a/back-end/src/useCases/todoCases/TodoCases.ts b/back-end/src/useCases/todoCases/TodoCases.ts index 5b8c026..4479b24 100644 --- a/back-end/src/useCases/todoCases/TodoCases.ts +++ b/back-end/src/useCases/todoCases/TodoCases.ts @@ -1,6 +1,7 @@ import { z } from "zod"; import { ITodoRepository } from "../../models/todoModel/interfaces"; +import { TodoErrors } from "../../errors/TodoErrors"; import { TCreateRequest, createRequestSchema, @@ -8,7 +9,6 @@ import { TDeleteRequest, TGetAllTodos } from "./validations"; -import { ErrorTodo } from "../../errors/TodoErrors"; export class TodoCases { constructor( @@ -30,7 +30,7 @@ export class TodoCases { validationErrors[error.path[0]] = error.message }); - throw new ErrorTodo(validationErrors, 400); + throw new TodoErrors(validationErrors, 400); }; const { todo } = await validationRequest; @@ -47,7 +47,7 @@ export class TodoCases { const { idUser, idTodo, selectTypeDeleteAll } = request; if ( !idTodo && !selectTypeDeleteAll ) { - throw new ErrorTodo( + throw new TodoErrors( { error: "choose one of these options query: idTodo or selectType" }, 406 ); @@ -56,7 +56,7 @@ export class TodoCases { if ( idTodo ) { const todo = await this.todoRepository.findTodo(idTodo); if ( !todo || todo.id_user !== idUser ) { - throw new ErrorTodo({error: "Todo not found."}, 404); + throw new TodoErrors({error: "Todo not found."}, 404); }; }; @@ -65,7 +65,7 @@ export class TodoCases { selectTypeDeleteAll !== "deleteAllComplete" && selectTypeDeleteAll !== "deleteAllIncomplete" if ( verifyTypeSelectDeleteAll ) { - throw new ErrorTodo( + throw new TodoErrors( {error: "To delete all todos choose one of the types: deleteAllComplete or deleteAllIncomplete"}, 406 ); @@ -87,11 +87,11 @@ export class TodoCases { .findTodo(idTodo); if (!todo || todo.id_user !== idUser) { - throw new ErrorTodo({error: "Todo not found."}, 404); + throw new TodoErrors({error: "Todo not found."}, 404); }; if (String(status) !== "true" && String(status) !== "false") { - throw new ErrorTodo({error: "Status is only allowed: true or false."}, 406); + throw new TodoErrors({error: "Status is only allowed: true or false."}, 406); }; const transformBoolean = @@ -109,7 +109,7 @@ export class TodoCases { const { idUser, typeList } = request; if ( typeList !== "complete" && typeList !== "incomplete" ){ - throw new ErrorTodo( + throw new TodoErrors( {error: "Type of valid lists: complete or incomplete."}, 406 ); diff --git a/back-end/src/useCases/userCases/UserCases.ts b/back-end/src/useCases/userCases/UserCases.ts index d631037..c3ecc66 100644 --- a/back-end/src/useCases/userCases/UserCases.ts +++ b/back-end/src/useCases/userCases/UserCases.ts @@ -4,6 +4,7 @@ import { IUserRepository } from "../../models/userModel/interfaces"; import { IBCryptAdapter } from "../../adapters/IBcryptAdapter"; import { IJwtAdapter } from "../../adapters/IJwtAdapter"; +import { UserErrors } from "../../errors/UserErrors"; import { TRegisterRequest, registerRequestSchema, @@ -11,8 +12,6 @@ import { signInRequestSchema } from "./validations"; -import { ErrorUser } from "../../errors/UserErrors"; - export class UserCases { constructor( private userRepository: IUserRepository, @@ -35,7 +34,7 @@ export class UserCases { validationErrors[error.path[0]] = error.message }); - throw new ErrorUser(validationErrors, 400); + throw new UserErrors(validationErrors, 400); }; const {userName, password} = await validationRequest; @@ -43,7 +42,7 @@ export class UserCases { const user = await this.userRepository.findUser({userName}); if(user) { - throw new ErrorUser({error: "A user with that name already exists."}, 409); + throw new UserErrors({error: "A user with that name already exists."}, 409); }; const passwordHash = await this.bcryptAdapter.hashEncrypt({password}); @@ -74,7 +73,7 @@ export class UserCases { validationErrors[error.path[0]] = error.message }); - throw new ErrorUser(validationErrors, 400); + throw new UserErrors(validationErrors, 400); }; const {userName, password} = await validationRequest; @@ -82,7 +81,7 @@ export class UserCases { const user = await this.userRepository.findUser({userName}); if (!user) { - throw new ErrorUser({error: "Username does not exist."}, 404); + throw new UserErrors({error: "Username does not exist."}, 404); }; const decryptPassword = await this.bcryptAdapter.compareHash({ @@ -91,7 +90,7 @@ export class UserCases { }); if ( !decryptPassword ) { - throw new ErrorUser({error: "Invalid password."}, 406); + throw new UserErrors({error: "Invalid password."}, 406); }; const generateToken = this.jwtAdapter.generateToken({ @@ -106,7 +105,7 @@ export class UserCases { async getUserByToken (token: string) { if ( !token ) { - throw new ErrorUser({error: "The token is required."}, 406); + throw new UserErrors({error: "The token is required."}, 406); }; const decryptJwt = this.jwtAdapter.getTokenData({ @@ -118,7 +117,7 @@ export class UserCases { }); if (!user) { - throw new ErrorUser({error: "Invalid token."}, 401); + throw new UserErrors({error: "Invalid token."}, 401); }; return user;