-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
import errorHandler from './middleware/error/errorHandler.middlware.js'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the import path for |
||
import validateRequest from './middleware/ai/validateRequest.middleware.js'; | ||
|
||
const start = async () => { | ||
await startMongoose(); | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(asyncHandler(validateRequest)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
app.use(asyncHandler(gptMiddleware)) | ||
app.use(errorHandler) | ||
|
||
|
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.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
return res.status(statusCode).json({ status, message }); | ||
} | ||
next() | ||
} | ||
|
||
export default validateRequest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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 beasyncHandler.middleware.js
instead ofasyncHandler.middlware.js
.