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
chore: improve types to get rid of some `as any`
Martin Adámekb312bf7b8298
diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.tsindex d59434e77..0b3a6d014 100644--- a/packages/basic-crawler/src/internals/basic-crawler.ts+++ b/packages/basic-crawler/src/internals/basic-crawler.ts@@ -152,7 +152,7 @@ export interface BasicCrawlerOptions<Context extends CrawlingContext = BasicCraw* The exceptions are logged to the request using the* {@apilink Request.pushErrorMessage|`Request.pushErrorMessage()`} function.*/- requestHandler?: RequestHandler<LoadedContext<Context>>;+ requestHandler?: RequestHandler<Context>;/*** Static list of URLs to be processed.@@ -650,8 +650,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContextthis.robotsTxtFileCache = new LruCache({ maxLength: 1000 });this.handleSkippedRequest = this.handleSkippedRequest.bind(this);- // FIXME any- this.requestHandler = (requestHandler as any) ?? this.router;+ this.requestHandler = requestHandler ?? this.router;this.failedRequestHandler = failedRequestHandler;this.errorHandler = errorHandler;diff --git a/packages/browser-crawler/src/internals/browser-crawler.ts b/packages/browser-crawler/src/internals/browser-crawler.tsindex 983d0f382..6b7878d3e 100644--- a/packages/browser-crawler/src/internals/browser-crawler.ts+++ b/packages/browser-crawler/src/internals/browser-crawler.ts@@ -114,7 +114,7 @@ export interface BrowserCrawlerOptions<* The exceptions are logged to the request using the* {@apilink Request.pushErrorMessage|`Request.pushErrorMessage()`} function.*/- requestHandler?: BrowserRequestHandler<LoadedContext<Context>>;+ requestHandler?: BrowserRequestHandler<Context>;/*** User-provided function that allows modifying the request object before it gets retried by the crawler.@@ -325,7 +325,6 @@ export abstract class BrowserCrawler<preNavigationHooks = [],postNavigationHooks = [],requestHandler,- failedRequestHandler,headless,ignoreShadowRoots,ignoreIframes,@@ -342,9 +341,7 @@ export abstract class BrowserCrawler<config,);- // FIXME any- this.userProvidedRequestHandler = (requestHandler as any) ?? this.router;- this.failedRequestHandler = failedRequestHandler; // FIXME is this even needed?+ this.userProvidedRequestHandler = requestHandler ?? this.router;// Cookies should be persisted per session only if session pool is usedif (!this.useSessionPool && persistCookiesPerSession) {diff --git a/packages/http-crawler/src/internals/http-crawler.ts b/packages/http-crawler/src/internals/http-crawler.tsindex 42fccebd2..04b88440a 100644--- a/packages/http-crawler/src/internals/http-crawler.ts+++ b/packages/http-crawler/src/internals/http-crawler.ts@@ -404,8 +404,7 @@ export class HttpCrawler<config,);- // FIXME any- this.requestHandler = (requestHandler as any) ?? this.router;+ this.requestHandler = requestHandler ?? this.router;// Cookies should be persisted per session only if session pool is usedif (!this.useSessionPool && persistCookiesPerSession) {diff --git a/packages/playwright-crawler/src/internals/playwright-crawler.ts b/packages/playwright-crawler/src/internals/playwright-crawler.tsindex 5e8216556..62144d57e 100644--- a/packages/playwright-crawler/src/internals/playwright-crawler.ts+++ b/packages/playwright-crawler/src/internals/playwright-crawler.ts@@ -4,7 +4,6 @@ import type {BrowserHook,BrowserRequestHandler,GetUserDataFromRequest,- LoadedContext,RouterHandler,RouterRoutes,RouteSchemas,@@ -25,8 +24,8 @@ export interface PlaywrightCrawlingContext<UserData extends Dictionary = Dictionextends BrowserCrawlingContext<PlaywrightCrawler, Page, Response, PlaywrightController, UserData>,PlaywrightContextUtils {}export interface PlaywrightHook extends BrowserHook<PlaywrightCrawlingContext, PlaywrightGotoOptions> {}-export interface PlaywrightRequestHandler extends BrowserRequestHandler<LoadedContext<PlaywrightCrawlingContext>> {}-export type PlaywrightGotoOptions = Dictionary & Parameters<Page['goto']>[1];+export interface PlaywrightRequestHandler extends BrowserRequestHandler<PlaywrightCrawlingContext> {}+export type PlaywrightGotoOptions = Parameters<Page['goto']>[1];export interface PlaywrightCrawlerOptionsextends BrowserCrawlerOptions<PlaywrightCrawlingContext, { browserPlugins: [PlaywrightPlugin] }> {diff --git a/packages/puppeteer-crawler/src/internals/puppeteer-crawler.ts b/packages/puppeteer-crawler/src/internals/puppeteer-crawler.tsindex 918da00c8..4491ca397 100644--- a/packages/puppeteer-crawler/src/internals/puppeteer-crawler.ts+++ b/packages/puppeteer-crawler/src/internals/puppeteer-crawler.ts@@ -4,7 +4,6 @@ import type {BrowserHook,BrowserRequestHandler,GetUserDataFromRequest,- LoadedContext,RouterHandler,RouterRoutes,RouteSchemas,@@ -26,7 +25,7 @@ export interface PuppeteerCrawlingContext<UserData extends Dictionary = Dictionaextends BrowserCrawlingContext<PuppeteerCrawler, Page, HTTPResponse, PuppeteerController, UserData>,PuppeteerContextUtils {}export interface PuppeteerHook extends BrowserHook<PuppeteerCrawlingContext, PuppeteerGoToOptions> {}-export interface PuppeteerRequestHandler extends BrowserRequestHandler<LoadedContext<PuppeteerCrawlingContext>> {}+export interface PuppeteerRequestHandler extends BrowserRequestHandler<PuppeteerCrawlingContext> {}export type PuppeteerGoToOptions = Parameters<Page['goto']>[1];export interface PuppeteerCrawlerOptionsdiff --git a/test/core/crawlers/cheerio_crawler.test.ts b/test/core/crawlers/cheerio_crawler.test.tsindex a79fcd5ed..16dabca49 100644--- a/test/core/crawlers/cheerio_crawler.test.ts+++ b/test/core/crawlers/cheerio_crawler.test.ts@@ -271,7 +271,8 @@ describe('CheerioCrawler', () => {maxRequestRetries: 0,maxConcurrency: 1,requestHandler: ({ $, body, request }) => {- tmp.push(body, $.html(), request.loadedUrl);+ // test that `request.loadedUrl` is no longer optional by calling `toLowerCase` on it directly (no optional chaining)+ tmp.push(body, $.html(), request.loadedUrl.toLowerCase());},});@@ -280,7 +281,6 @@ describe('CheerioCrawler', () => {expect(tmp).toHaveLength(3);expect(tmp[0]).toBe(responseSamples.html);expect(tmp[1]).toBe(tmp[0]);- // test that `request.loadedUrl` is no longer optionalexpect(tmp[2].length).toBe(sources[0].length);});