Apify Discord Mirror

Updated 5 months ago

Crawlee Router as a folder with different files for each Handler

At a glance

The community member is asking if it's possible to create a folder named "routes" in their project and then construct an export const router in an index.ts file by adding all their handlers from different files. They provide an example of how this could be done. The community members in the comments discuss the type of the handler in a separate file, with one suggesting the type Handler = (ctx: Omit<playwrightcrawlingcontext<dictionary>, 'request'> & { request: Request<dictionary>; }) => Awaitable<void>. This seems to be the answer the original community member was looking for.

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