-
Notifications
You must be signed in to change notification settings - Fork 23
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
Aggiunto endpoint API per generare il curriculum #150
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import Handlebars from 'handlebars'; | ||
|
||
export async function renderResume(resumeData, theme) { | ||
const themePath = join(process.cwd(), '..', '..', 'themes', theme); | ||
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. 🛠️ Refactor suggestion Use a more robust method for theme path construction The current path construction method uses multiple Here's a suggested approach: import { config } from '../config'; // Assume we have a config file
// In the function
const themePath = join(config.themesBasePath, theme); |
||
const templateContent = readFileSync(join(themePath, 'resume.hbs'), 'utf8'); | ||
const template = Handlebars.compile(templateContent); | ||
|
||
return template(resumeData); | ||
Comment on lines
+7
to
+10
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. Add error handling and input validation The current implementation lacks error handling for file reading and template compilation. It also doesn't validate the inputs. This could lead to unhandled exceptions and potential security issues. Consider adding the following improvements:
These changes will make the function more robust and easier to debug. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { renderResume } from '../../lib/renderResume'; | ||
|
||
export default async function handler(req, res) { | ||
if (req.method !== 'POST') { | ||
return res.status(405).json({ error: 'Metodo non consentito' }); | ||
} | ||
|
||
try { | ||
const { resumeData, theme = 'elegant' } = req.body; | ||
|
||
if (!resumeData) { | ||
return res | ||
.status(400) | ||
.json({ error: 'I dati del curriculum sono obbligatori' }); | ||
} | ||
Comment on lines
+9
to
+15
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. Enhance request body validation. While the current validation checks for the presence of
Example implementation: const allowedThemes = ['elegant', 'modern', 'classic'];
const { resumeData, theme = 'elegant' } = req.body;
if (!resumeData || typeof resumeData !== 'object') {
return res.status(400).json({ error: 'I dati del curriculum sono obbligatori e devono essere un oggetto' });
}
if (!allowedThemes.includes(theme)) {
return res.status(400).json({ error: 'Tema non valido' });
}
// Additional validation for resumeData structure could be added here |
||
|
||
const html = await renderResume(resumeData, theme); | ||
const encodedHtml = Buffer.from(html).toString('base64'); | ||
const url = `data:text/html;base64,${encodedHtml}`; | ||
|
||
res.status(200).json({ url }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ error: 'Errore interno del server' }); | ||
} | ||
Comment on lines
+22
to
+25
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. Improve error handling and logging. While the current error handling catches errors, it could be improved:
Example implementation: import logger from '../../lib/logger'; // Assume this is your logging setup
// In the catch block:
logger.error('Error generating resume', {
error: error.message,
stack: process.env.NODE_ENV !== 'production' ? error.stack : undefined,
requestId: req.headers['x-request-id'], // Assuming you're using request IDs
});
const errorResponse = process.env.NODE_ENV === 'production'
? { error: 'Errore interno del server' }
: { error: error.message, stack: error.stack };
res.status(500).json(errorResponse); |
||
} |
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.
🛠️ Refactor suggestion
Consider using asynchronous file reading
The function is declared as asynchronous, but it uses
readFileSync
which is a blocking operation. Consider using the asynchronous versionreadFile
from thefs/promises
module to maintain consistency and prevent potential performance issues.Here's a suggested change:
📝 Committable suggestion