"Compile" request handler functions to custom functions for each route instead of having a single big function that covers all possible cases.
Here's the basic setup:
const schema = { properties: { id: { type: 'number' } } };
function compileHandler(routePath, schema) {
// 1. Build the logic as a string
const code = `
return function handler(req, res) {
${schema.properties.id ? 'if(typeof req.body.id !== "number") return res.error();' : ''}
// Actual logic...
res.send("Success");
}
//# sourceURL=routes/${routePath}.js
`;
// 2. Generate the function
return new Function(code)();
}
"Compile" request handler functions to custom functions for each route instead of having a single big function that covers all possible cases.
Here's the basic setup: