Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix: fix crawling context type in `router.addHandler()` (#2355)
Martin Adámekd73c20240586
diff --git a/packages/core/src/crawlers/crawler_commons.ts b/packages/core/src/crawlers/crawler_commons.tsindex e7a5509d2..deec11441 100644--- a/packages/core/src/crawlers/crawler_commons.ts+++ b/packages/core/src/crawlers/crawler_commons.ts@@ -12,7 +12,9 @@ import type { Session } from '../session_pool/session';import type { RequestQueueOperationOptions, Dataset, RecordOptions } from '../storages';import { KeyValueStore } from '../storages';-export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary> extends Record<string, unknown>{+// we need `Record<string & {}, unknown>` here, otherwise `Omit<Context>` is resolved badly+// eslint-disable-next-line+export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary> extends Record<string & {}, unknown> {/*** The original {@apilink Request} object.*/diff --git a/test/core/router.test.ts b/test/core/router.test.tsindex 0bf817515..3dc9f3270 100644--- a/test/core/router.test.ts+++ b/test/core/router.test.ts@@ -1,6 +1,7 @@import { BasicCrawler } from '@crawlee/basic';import type { CrawlingContext } from '@crawlee/core';import { MissingRouteError, Router } from '@crawlee/core';+import { createPlaywrightRouter, type PlaywrightCrawlingContext } from 'crawlee';describe('Router', () => {test('should be callable and route based on the label', async () => {@@ -138,6 +139,14 @@ describe('Router', () => {});});+ test('context has correct type', async () => {+ const router = createPlaywrightRouter();+ router.addHandler('label', async (ctx: PlaywrightCrawlingContext) => {+ // just to test if this works on type level, the assertion is not actually executed+ expect(ctx.page.$$).toBeDefined();+ });+ });+test('addHandler accepts userdata generic', async () => {const testType = <T>(t: T): void => {};