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

Conversation

thadeucbr
Copy link
Owner

No description provided.

@@ -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.

@@ -6,13 +6,15 @@
import gptMiddleware from './middleware/ai/gpt.middleware.js';
import asyncHandler from './middleware/error/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.


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.

@@ -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 { 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.

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.

@thadeucbr thadeucbr merged commit 8af9a4a into main Sep 30, 2024
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant