diff --git a/__tests__/unit/Response.test.js b/__tests__/unit/Response.test.js index 9fb0c78..9e0b878 100644 --- a/__tests__/unit/Response.test.js +++ b/__tests__/unit/Response.test.js @@ -13,7 +13,7 @@ it("Check Response Class sending 'entity' field after 'code' field", () => { const response = new Response(res); - const test = () => response.code(response.CONFLICT_REQUEST); + const test = () => response.code(response.CONFLICT_409); expect(test).toThrow("You must set the entity value before to set the code"); }); @@ -28,7 +28,7 @@ it("Check Response Class without sending the 'code' field", () => { const test = () => response.entity(entities.USER).send(); expect(test).toThrow( - "This request dont have a defined error code, set this with: response.code(response.SUCCESS_POST); for example" + "This request dont have a defined error code, set this with: response.code(response.CREATED_201); for example" ); }); @@ -63,7 +63,7 @@ it("Check Response Class with successfully correctly usage", () => { const objectResponse = response .entity(entities.USER) - .code(response.SUCCESS_POST) + .code(response.CREATED_201) .message("Successful post") .data(dataRequest) .metadata(metadataRequest) diff --git a/src/middlewares/AuthMiddleware.js b/src/middlewares/AuthMiddleware.js index 938ffda..9c60584 100644 --- a/src/middlewares/AuthMiddleware.js +++ b/src/middlewares/AuthMiddleware.js @@ -14,7 +14,7 @@ module.exports = async (req, res, next) => { const badRequest = response .isError() .entity(entities.USER) - .code(response.INVALID_AUTH); + .code(response.UNAUTHORIZED_401); const authHeader = req.headers.authorization; diff --git a/src/services/AuthenticationUserService.js b/src/services/AuthenticationUserService.js index d1b53b8..23ef7d7 100644 --- a/src/services/AuthenticationUserService.js +++ b/src/services/AuthenticationUserService.js @@ -18,7 +18,7 @@ module.exports = async (req, res) => { return response .isError() .entity(entities.USER) - .code(response.RESOURCE_NOT_FOUND) + .code(response.NOT_FOUND_404) .message("User not found") .send(); @@ -26,7 +26,7 @@ module.exports = async (req, res) => { return response .isError() .entity(entities.USER) - .code(response.INVALID_REQUEST) + .code(response.BAD_REQUEST_400) .message("Invalid Password") .send(); @@ -34,7 +34,7 @@ module.exports = async (req, res) => { return response .entity(entities.USER) - .code(response.SUCCESS_GET) + .code(response.OK_200) .data({ user }) .metadata({ token: jwt.generate({ id: user.id }) }) .send(); diff --git a/src/services/ConsultDialogService.js b/src/services/ConsultDialogService.js index 68335f1..515f003 100644 --- a/src/services/ConsultDialogService.js +++ b/src/services/ConsultDialogService.js @@ -25,7 +25,7 @@ module.exports = async (req, res) => { return response .isError() .entity(entities.DIALOG) - .code(response.RESOURCE_NOT_FOUND) + .code(response.NOT_FOUND_404) .message("Resource not found") .send(); @@ -39,7 +39,7 @@ module.exports = async (req, res) => { return response .entity(entities.DIALOG) - .code(response.SUCCESS_GET) + .code(response.OK_200) .data(data) .send(); } catch (err) { @@ -49,7 +49,9 @@ module.exports = async (req, res) => { .isError() .entity(entities.DIALOG) .code( - isValidationError ? response.INVALID_REQUEST : response.INTERNAL_ERROR + isValidationError + ? response.BAD_REQUEST_400 + : response.INTERNAL_SERVER_ERROR_500 ) .message(isValidationError ? err.message : "Dialog consultation failed") .send(); diff --git a/src/services/CreateDialogService.js b/src/services/CreateDialogService.js index 8dfe236..400a737 100644 --- a/src/services/CreateDialogService.js +++ b/src/services/CreateDialogService.js @@ -18,7 +18,7 @@ module.exports = async (req, res) => { return response .entity(entities.DIALOG) - .code(response.SUCCESS_POST) + .code(response.CREATED_201) .data(dialog) .send(); } catch (error) { @@ -28,7 +28,9 @@ module.exports = async (req, res) => { .isError() .entity(entities.DIALOG) .code( - isValidationError ? response.INVALID_REQUEST : response.INTERNAL_ERROR + isValidationError + ? response.BAD_REQUEST_400 + : response.INTERNAL_SERVER_ERROR_500 ) .message(isValidationError ? error.message : "Dialog creation failed") .send(); diff --git a/src/services/RegisterUserService.js b/src/services/RegisterUserService.js index 06cbcfd..8cfd7bf 100644 --- a/src/services/RegisterUserService.js +++ b/src/services/RegisterUserService.js @@ -17,7 +17,7 @@ module.exports = async (req, res) => { return response .isError() .entity(entities.USER) - .code(response.INVALID_REQUEST) + .code(response.BAD_REQUEST_400) .message("User already exists") .send(); @@ -32,7 +32,7 @@ module.exports = async (req, res) => { return response .entity(entities.USER) - .code(response.SUCCESS_POST) + .code(response.CREATED_201) .data({ user }) .metadata({ token: jwt.generate({ id: user.id }), @@ -42,8 +42,8 @@ module.exports = async (req, res) => { const isValidationError = error.name === "ValidationError"; const code = isValidationError - ? response.INVALID_REQUEST - : response.INTERNAL_ERROR; + ? response.BAD_REQUEST_400 + : response.INTERNAL_SERVER_ERROR_500; const message = isValidationError ? error.message : "Registration failed"; diff --git a/src/utils/responses.js b/src/utils/responses.js index 2b6d0e8..ed00323 100644 --- a/src/utils/responses.js +++ b/src/utils/responses.js @@ -15,7 +15,7 @@ module.exports = class Response { }, code: { message: - "This request dont have a defined error code, set this with: response.code(response.SUCCESS_POST); for example", + "This request dont have a defined error code, set this with: response.code(response.CREATED_201); for example", value: undefined, }, }; @@ -86,63 +86,51 @@ module.exports = class Response { } // When the action required is a post request and request is successful - get SUCCESS_POST() { + get CREATED_201() { this.status = 201; return `success-created/${this.required.entity.value}`; } // When the action required is a get request and request is successful - get SUCCESS_GET() { + get OK_200() { this.status = 200; return `success-get/${this.required.entity.value}`; } // When the action required is a delete request and request is successful - get SUCCESS_DELETE() { + get NO_CONTENT_204() { this.status = 204; return `success-deleted/${this.response.entity}`; } - // When the action required is a put request and request is successful - get SUCCESS_UPDATE() { - this.status = 204; - return `success-updated/${this.response.entity}`; - } - // When the client dont have permission to do X action - get INVALID_AUTH() { + get UNAUTHORIZED_401() { this.status = 401; return `invalid-auth/${this.response.entity}`; } // When the resource not exists on database or any system place - get RESOURCE_NOT_FOUND() { + get NOT_FOUND_404() { this.status = 404; return `not-found/${this.response.entity}`; } // When the client send a bad request: invalid password, invalid email, or send a invalid field // Or the current resource already exists, etc - get INVALID_REQUEST() { + get BAD_REQUEST_400() { this.status = 400; return `invalid-request/${this.response.entity}`; } // When the user request dont can be continued by conflicts with other resources - get CONFLICT_REQUEST() { + get CONFLICT_409() { this.status = 409; return `conflict-request/${this.response.entity}`; } // A internal server error - get INTERNAL_ERROR() { + get INTERNAL_SERVER_ERROR_500() { this.status = 500; return `internal-server-error/${this.response.entity}`; } - - // A error that not have tratative - get UNKNOWN_ERROR() { - this.status = 500; - return `unkown-error/${this.response.entity}`; - } };