Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main.js to include validateRequest middleware #6

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -6,13 +6,15 @@ import setupSwagger from './helper/swagger/swagger.config.js';
import gptMiddleware from './middleware/ai/gpt.middleware.js';
import asyncHandler from './middleware/error/asyncHandler.middlware.js';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the import path for asyncHandler. It should be asyncHandler.middleware.js instead of asyncHandler.middlware.js.

import errorHandler from './middleware/error/errorHandler.middlware.js';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the import path for errorHandler. It should be errorHandler.middleware.js instead of errorHandler.middlware.js.

import validateRequest from './middleware/ai/validateRequest.middleware.js';

const start = async () => {
await startMongoose();

const app = express();

app.use(express.json());
app.use(asyncHandler(validateRequest))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using asyncHandler with validateRequest is a good approach, but ensure that validateRequest is indeed an asynchronous function. If it's not, you may not need to wrap it with asyncHandler, which could lead to unnecessary complexity.

app.use(asyncHandler(gptMiddleware))
app.use(errorHandler)

21 changes: 21 additions & 0 deletions src/middleware/ai/validateRequest.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import aiService from '../../service/ai/ai.service.js';

const context = 'You are an AI that acts as a middleware for API request validation. You will receive the body, header, query, method, and URL of a request. Your task is to analyze whether all fields in the request adhere to established patterns and are present. If any field is invalid or missing, return an error message in JSON format: { message, statusCode, status }. If it is a creation (POST) or update (PUT) request, you must also check if the schema for that operation exists and is valid using the get_schemas function. If the request is valid and all fields are correct, return a success message in JSON format: { message: "OK", statusCode: "200", status: "OK" }. Important, don’t send ```JSON in the response.';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context string is excessively long and could be simplified or broken down into smaller, more manageable parts. Consider summarizing the key points to improve readability.


const validateRequest = async (req, res, next) => {
console.log(`Request Method: ${req.method}, Request URL: ${req.url}`);
console.log(`Request Body: ${JSON.stringify(req.body)}`);
console.log(`Request Query: ${JSON.stringify(req.query)}`);
console.log(`Request Headers: ${JSON.stringify(req.headers)}`);

const response = await aiService({ body: req.body, header: req.headers, query: req.query, method: req.method, url: req.url, firstContext: context });

const { message, statusCode, status } = JSON.parse(response.content);
console.log({ message, statusCode, status });
if (statusCode != 200) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using != for comparison can lead to unexpected results due to type coercion. It is recommended to use !== for strict comparison.

return res.status(statusCode).json({ status, message });
}
next()
}

export default validateRequest
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no newline at the end of the file. It is a good practice to end files with a newline to avoid issues in version control.

Loading
Oops, something went wrong.