#Code refactoring - reusing a common handler in multiple crawlers while keeping code hints

1 messages · Page 1 of 1 (latest)

worldly merlin
#

I have some code that looks something like this:

router.addHandler('log-in', async ({...}) => {...})

I want to reuse this handler in multiple crawlers. I could of course simply create the function log_in in some other file, but then I would use code hints for page/request etc. is there a better way to go about reusing the handler?

green egret
#

Why tf has this not been answered 🥲 The documentation even says We would most likely go even further and split even the routes into separate files. and yet this is very unclear without losing linting and useful dev features. I'm upset.

worldly merlin
#

Yea... I ended up adding and removing a dummy router while developing to deal with this. Hacky solution, but it works.
When I asked this question, I was super new to the lovely thing called javascript. From what I've seen in the past two months - this isn't as much an issue with Crawlee, as it is an issue with Javascript in general... just a badly designed language.
I haven't tried this myself, but I think moving to typescript might solve this issue. (I can't use it since I'm limited by my team ;p)

green egret
#

I am using typescript and have XP across both. I think your idea is a bit of a red herring 😄

plucky sage
#

router.addHandler takes label and handler function as a parameter. You may import both from different file.

worldly merlin
worldly merlin
green egret
soft wolfBOT
#

@green egret just advanced to level 1! Thanks for your contributions! 🎉

green egret