feat(router): allow inline router definition#1877
Conversation
|
|
||
| export type AllowedHttpMethods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'OPTIONS' | 'CONNECT' | 'PATCH'; | ||
|
|
||
| export type RouterRoutes<Context, UserData extends Dictionary> = { |
There was a problem hiding this comment.
don't have strong options about this type name, feel free to suggest better
There was a problem hiding this comment.
those changes should go to the core package, to the router.ts file i guess. the types package is here mainly to define the storage client interface (that must be adhered in apify-client/storage-local/memory-storage clients), and a place for utility types - but this is router specific, no reason to define it elsewhere i guess
i dont mind the name (i cant find any better right now, and its not a big deal i guess)
B4nan
left a comment
There was a problem hiding this comment.
looking good, lets just move the types directly to router.ts
|
|
||
| export type AllowedHttpMethods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'OPTIONS' | 'CONNECT' | 'PATCH'; | ||
|
|
||
| export type RouterRoutes<Context, UserData extends Dictionary> = { |
There was a problem hiding this comment.
those changes should go to the core package, to the router.ts file i guess. the types package is here mainly to define the storage client interface (that must be adhered in apify-client/storage-local/memory-storage clients), and a place for utility types - but this is router specific, no reason to define it elsewhere i guess
i dont mind the name (i cant find any better right now, and its not a big deal i guess)
| static create<Context extends CrawlingContext = CrawlingContext>(): RouterHandler<Context> { | ||
| static create< | ||
| Context extends CrawlingContext = CrawlingContext, | ||
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']> |
There was a problem hiding this comment.
same for the other places
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']> | |
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']>, |
| >( | ||
| routes?: RouterRoutes<Context, UserData>, | ||
| ): RouterHandler<Context> { |
There was a problem hiding this comment.
i would keep it on one line here
| >( | |
| routes?: RouterRoutes<Context, UserData>, | |
| ): RouterHandler<Context> { | |
| >(routes?: RouterRoutes<Context, UserData>): RouterHandler<Context> { |
| return Router.create<Context>(); | ||
| export function createHttpRouter< | ||
| Context extends HttpCrawlingContext = HttpCrawlingContext, | ||
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']> // quick&dirty copy-paste |
There was a problem hiding this comment.
:]
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']> // quick&dirty copy-paste | |
| UserData extends Dictionary = GetUserDataFromRequest<Context['request']>, |
|
@B4nan comments resolved (in IDE, not it web UI), thx for them!
not sure if and where to add it to docs ¯_(ツ)_/¯ it could be our little secret :D |
|
i'm glad that I've added the tests cause it taught me about |
|
We should have a routing guide I guess, for now, let's add at least something to the https://crawlee.dev/api/core/class/Router I see the middleware example is not rendered correctly... |
|
@B4nan added docs and fixed the middleware code issue |
|
Perfect, thanks! Let's just wait for the green CI, looking good to me. |


As mentioned in discussion #1873
Crawlee is so good that router definition often can be straightforward, so it would be nice to have the option to define it "inline".
It also could be great for tutorials and snippets in docs.
I will add tests after agreeing that the "signature" is ok :)
I've used this wildcard to find&edit all crawlers, so hopefully I've changed all of them
