Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: createAdaptivePlaywrightRouter utility #2415

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Dictionary } from '@crawlee/types';

import type { CrawlingContext } from './crawlers/crawler_commons';
import type { CrawlingContext, RestrictedCrawlingContext } from './crawlers/crawler_commons';
import { MissingRouteError } from './errors';
import type { Request } from './request';
import type { Awaitable } from './typedefs';

const defaultRoute = Symbol('default-route');

export interface RouterHandler<Context extends CrawlingContext = CrawlingContext> extends Router<Context> {
export interface RouterHandler<Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'> = CrawlingContext> extends Router<Context> {
(ctx: Context): Awaitable<void>;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ export type RouterRoutes<Context, UserData extends Dictionary> = {
* });
* ```
*/
export class Router<Context extends CrawlingContext> {
export class Router<Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'>> {
private readonly routes: Map<string | symbol, (ctx: Context) => Awaitable<void>> = new Map();
private readonly middlewares: ((ctx: Context) => Awaitable<void>)[] = [];

Expand Down Expand Up @@ -173,7 +173,7 @@ export class Router<Context extends CrawlingContext> {
* ```
*/
static create<
Context extends CrawlingContext = CrawlingContext,
Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'> = CrawlingContext,
UserData extends Dictionary = GetUserDataFromRequest<Context['request']>,
>(routes?: RouterRoutes<Context, UserData>): RouterHandler<Context> {
const router = new Router<Context>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addTimeoutToPromise } from '@apify/timeout';
import { extractUrlsFromPage } from '@crawlee/browser';
import type { RestrictedCrawlingContext, StatisticState, StatisticPersistedState } from '@crawlee/core';
import { Configuration, RequestHandlerResult, Statistics, withCheckedStorageAccess } from '@crawlee/core';
import type { RestrictedCrawlingContext, StatisticState, StatisticPersistedState, GetUserDataFromRequest, RouterRoutes } from '@crawlee/core';
import { Configuration, RequestHandlerResult, Router, Statistics, withCheckedStorageAccess } from '@crawlee/core';
import type { Awaitable, Dictionary } from '@crawlee/types';
import { extractUrlsFromCheerio } from '@crawlee/utils';
import { load, type Cheerio, type Element } from 'cheerio';
Expand Down Expand Up @@ -64,7 +64,7 @@ class AdaptivePlaywrightCrawlerStatistics extends Statistics {
}
}

interface AdaptivePlaywrightCrawlerContext extends RestrictedCrawlingContext {
export interface AdaptivePlaywrightCrawlerContext extends RestrictedCrawlingContext {
/**
* Wait for an element matching the selector to appear and return a Cheerio object of matched elements.
*/
Expand Down Expand Up @@ -356,3 +356,10 @@ export class AdaptivePlaywrightCrawler extends PlaywrightCrawler {
}
}
}

export function createAdaptivePlaywrightRouter<
Context extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext,
UserData extends Dictionary = GetUserDataFromRequest<Context['request']>,
>(routes?: RouterRoutes<Context, UserData>) {
return Router.create<Context>(routes);
}