Apify

Apify and Crawlee Official Forum

b
F
A
J
A

Crawlee Router as a folder with different files for each Handler

Hey, everyone! πŸ‘‹
Is it possible to create a folder named routes in my project and then construct export const router in index.ts file by adding all my handlers from different files?

Like this
Plain Text
// Many imports...

export const router = createPlaywrightRouter();

router.addDefaultHandler(defaultHandler);
router.addHandler('catalog-page', catalogHandler);
router.addHandler('review-page', reviewsHandler);


And if this is possible, what should be the type of the handler in a separate file?
M
L
A
5 comments
Crawlee Router as a folder with different files for each Handler
From the sources, I've understood that handler should have type
Plain Text
handler: (ctx: Omit<Context, 'request'> & { request: Request<UserData> }) => Awaitable<void>

but is it possible to use this type or any alias in my own files?
Because it seems that Context is defined as a type parameter of the Router class
This seems to work

Plain Text
type Handler = (ctx: Omit<PlaywrightCrawlingContext<Dictionary>, 'request'> & { request: Request<Dictionary>; }) => Awaitable<void>
const handleRoute: Handler = async ({ page }) => {
    console.log(await page.title());
};

const router = createPlaywrightRouter();
router.addHandler('handler', handleRoute);
Thanks a lot Lukas! That's indeed what I was looking for
just advanced to level 1! Thanks for your contributions! πŸŽ‰
Add a reply
Sign up and join the conversation on Discord
Join