Consider adding router system to templates
Closed this issue · 5 comments
I really like the router system presented in https://sdk.apify.com/docs/guides/getting-started
. It includes some boilerplate but the way it forces you to work with labels makes it very readable. You don't need to remember the function names and you can just look at the routes.js
file to see what code will run. Looking for counter-arguments :)
I remember someone telling me that I got carried away when implementing that router and that it's unnecessarily complex. Maybe @VaclavRut ?
Initially yes, but I got used to it already, we need some standard and I really like the label approach which works nicely with the router approach.
I had a problem now that one dev used the same way how we have it now in the router to export functions and it probably broke the actor, it is fixed now though:
const standardizeTimeString = (hoursOrMinutes) => {
let result = hoursOrMinutes === undefined || hoursOrMinutes === null ? '' : hoursOrMinutes;
if (hoursOrMinutes && hoursOrMinutes.length === 1) {
result = `0${hoursOrMinutes}`;
}
return result;
};
exports.standardizeTimeString = standardizeTimeString;
Also some other dev started to use the globalContext and these things were harder for me to get used to and don't know if they are proper.
I think the router is harder for beginners to grasp but pays off for larger projects. The problem is what we learn the devs to do in these templates, they will continue doing. Or we could just tell every mp/partner to use the router system :)
Yeah, agree that we could take some ideas from it and make it better. Or we could add a Router
class to the SDK and just tell them to use that.
my only counter argument is that the code itself isn't navigable, you can't "go to definition" or lookup the function directly. routes are very nice way to wrap everything from main, like request queues, input, without relying on passing the same parameter around for more complex code.
standardizing the router would be better, but not as a class though. more like a Apify.utils member