Skip to content

Commit f6b85be

Browse files
committed
refactor: convert config to zod schema
1 parent 63ad8be commit f6b85be

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/config.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
1+
import { z } from 'zod';
2+
13
import type { Page } from "playwright";
24

3-
export type Config = {
5+
const Page: z.ZodType<Page> = z.any();
6+
7+
export const configSchema = z.object({
48
/**
59
* URL to start the crawl
610
* @example "https://www.builder.io/c/docs/developers"
711
* @default ""
812
*/
9-
url: string;
13+
url: z.string(),
1014
/**
1115
* Pattern to match against for links on a page to subsequently crawl
1216
* @example "https://www.builder.io/c/docs/**"
1317
* @default ""
1418
*/
15-
match: string | string[];
19+
match: z.string().or(z.array(z.string())),
20+
1621
/**
1722
* Selector to grab the inner text from
1823
* @example ".docs-builder-container"
1924
* @default ""
2025
*/
21-
selector?: string;
26+
selector: z.string().optional(),
2227
/**
2328
* Don't crawl more than this many pages
2429
* @default 50
2530
*/
26-
maxPagesToCrawl: number;
31+
maxPagesToCrawl: z.number().int().positive(),
2732
/**
2833
* File name for the finished data
2934
* @default "output.json"
3035
*/
31-
outputFileName: string;
36+
outputFileName: z.string(),
3237
/** Optional cookie to be set. E.g. for Cookie Consent */
33-
cookie?: { name: string; value: string };
38+
cookie: z.object({
39+
name: z.string(),
40+
value: z.string(),
41+
}).optional(),
3442
/** Optional function to run for each page found */
35-
onVisitPage?: (options: {
36-
page: Page;
37-
pushData: (data: any) => Promise<void>;
38-
}) => Promise<void>;
43+
onVisitPage: z.function()
44+
.args(z.object({
45+
page: Page,
46+
pushData: z.function()
47+
.args(z.any())
48+
.returns(z.promise(z.void()))
49+
}))
50+
.returns(z.promise(z.void()))
51+
.optional(),
3952
/** Optional timeout for waiting for a selector to appear */
40-
waitForSelectorTimeout?: number;
41-
};
53+
waitForSelectorTimeout: z.number().int().nonnegative().optional(),
54+
});
55+
56+
export type Config = z.infer<typeof configSchema>;
57+

0 commit comments

Comments
 (0)