Skip to content

Commit

Permalink
fix: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauBouchard committed May 18, 2024
1 parent 4f67a16 commit 8477e90
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 98 deletions.
32 changes: 28 additions & 4 deletions src/controllers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,34 @@ class MainController extends DefaultController {
request: express.Request,
response: express.Response
) => {
const token = `${request.query.token}` || `${request.body.text}`;
const data = await getSingle(token);

response.status(200).send(data);
if (request.query.token || request.body.text) {
const token = `${request.query.token}` || `${request.body.text}`;
// check to see if token is at least 3 characters
if (token.length > 2) {
const data = await getSingle(token);
response.status(200).send(data);
} else {
response.status(400).send({
data: {
message:
"Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth",
},
meta: {
status: 400,
},
});
}
} else {
response.status(400).send({
data: {
message:
"Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth",
},
meta: {
status: 400,
},
});
}
};
}

Expand Down
27 changes: 21 additions & 6 deletions src/controllers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,28 @@ class SlackController extends DefaultController {
request: express.Request,
response: express.Response
) => {
const token = `${request.query.token}` || `${request.body.text}`;
const data = await getSingle(token);
if (request.query.token || request.body.text) {
const token = `${request.query.token}` || `${request.body.text}`;
// check to see if token is at least 3 characters
if (token.length > 2) {
const data = await getSingle(token);

response.status(200).send({
response_type: "in_channel",
text: `${data.data.message}`,
});
response.status(200).send({
response_type: "in_channel",
text: `${data.data.message}`,
});
} else {
response.status(400).send({
data: {
message:
"Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth",
},
meta: {
status: 400,
},
});
}
}
};
}

Expand Down
35 changes: 0 additions & 35 deletions src/middleware/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,6 @@ function validateTokenMiddleware(
},
});
}

// check to see if token exists in request
try {
if (request) {
if (request.query.token || request.body.text) {
const args = `${request.query.token}` || `${request.body.text}`;
// check to see if token is at least 3 characters
if (args.length > 2) {
next();
} else {
logger.info("Token is invalid, Please use a valid token");
response.status(400).send({
data: {
message:
"Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth",
},
meta: {
status: 400,
},
});
}
}
}
} catch (e) {
logger.info("Token is invalid, Please use a valid token");
response.status(400).send({
data: {
message:
"Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth",
},
meta: {
status: 400,
},
});
}
next();
}

Expand Down
53 changes: 0 additions & 53 deletions tests/middleware/validation.test.ts

This file was deleted.

0 comments on commit 8477e90

Please sign in to comment.