Skip to content

Commit fd02b0a

Browse files
committed
make code more typescripty
1 parent fe7d3f0 commit fd02b0a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import type { CheerioCrawlerOptions, PlaywrightCrawlerOptions } from 'crawlee';
33

44
import type { ContentCrawlerTypes } from './const';
55

6+
/**
7+
* Utility type to make specific properties of T optional.
8+
* @template T - The type to make properties optional
9+
* @template K - The keys of T to make optional
10+
*/
11+
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
12+
613
export type OutputFormats = 'text' | 'markdown' | 'html';
714
export type SERPProxyGroup = 'GOOGLE_SERP' | 'SHADER';
815
export type ScrapingTool = 'browser-playwright' | 'raw-http';
@@ -95,6 +102,12 @@ export type SearchCrawlerUserData = {
95102
totalPages: number;
96103
};
97104

105+
/**
106+
* Type for createSearchRequest function parameters.
107+
* Makes pagination fields optional while keeping required fields mandatory.
108+
*/
109+
export type CreateSearchRequestUserData = Optional<SearchCrawlerUserData, 'timeMeasures' | 'collectedResults' | 'currentPage' | 'totalPages'>;
110+
98111
export type ContentCrawlerUserData = {
99112
query: string;
100113
responseId: string;

src/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { log } from 'crawlee';
77
import inputSchema from '../.actor/input_schema.json' with { type: 'json' };
88
import type { ContentCrawlerUserData,
99
ContentScraperSettings,
10+
CreateSearchRequestUserData,
1011
Input,
1112
OrganicResult, OutputFormats,
1213
SearchCrawlerUserData,
@@ -87,13 +88,7 @@ export function randomId() {
8788
* We add +1 to the calculated totalPages to account for pages that return fewer than 10 results.
8889
*/
8990
export function createSearchRequest(
90-
userData: Partial<SearchCrawlerUserData> & {
91-
query: string;
92-
responseId: string;
93-
maxResults: number;
94-
contentCrawlerKey: string;
95-
contentScraperSettings: ContentScraperSettings;
96-
},
91+
userData: CreateSearchRequestUserData,
9792
proxyConfiguration: ProxyConfiguration | undefined,
9893
startOffset = 0,
9994
): RequestOptions<SearchCrawlerUserData> {

0 commit comments

Comments
 (0)