Skip to content

Commit

Permalink
fix: change code request names
Browse files Browse the repository at this point in the history
  • Loading branch information
LaksCastro committed May 19, 2020
1 parent 6a83924 commit 0329b80
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
6 changes: 3 additions & 3 deletions __tests__/unit/Response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand All @@ -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"
);
});

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/AuthMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/services/AuthenticationUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ 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();

if (!password || !(await bcrypt.compare(password, user.password)))
return response
.isError()
.entity(entities.USER)
.code(response.INVALID_REQUEST)
.code(response.BAD_REQUEST_400)
.message("Invalid Password")
.send();

user.password = undefined;

return response
.entity(entities.USER)
.code(response.SUCCESS_GET)
.code(response.OK_200)
.data({ user })
.metadata({ token: jwt.generate({ id: user.id }) })
.send();
Expand Down
8 changes: 5 additions & 3 deletions src/services/ConsultDialogService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand All @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/services/CreateDialogService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/services/RegisterUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 }),
Expand All @@ -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";

Expand Down
30 changes: 9 additions & 21 deletions src/utils/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand Down Expand Up @@ -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}`;
}
};

0 comments on commit 0329b80

Please sign in to comment.