|
| 1 | +import { z } from 'zod'; |
| 2 | + |
1 | 3 | import type { Page } from "playwright";
|
2 | 4 |
|
3 |
| -export type Config = { |
| 5 | +const Page: z.ZodType<Page> = z.any(); |
| 6 | + |
| 7 | +export const configSchema = z.object({ |
4 | 8 | /**
|
5 | 9 | * URL to start the crawl
|
6 | 10 | * @example "https://www.builder.io/c/docs/developers"
|
7 | 11 | * @default ""
|
8 | 12 | */
|
9 |
| - url: string; |
| 13 | + url: z.string(), |
10 | 14 | /**
|
11 | 15 | * Pattern to match against for links on a page to subsequently crawl
|
12 | 16 | * @example "https://www.builder.io/c/docs/**"
|
13 | 17 | * @default ""
|
14 | 18 | */
|
15 |
| - match: string | string[]; |
| 19 | + match: z.string().or(z.array(z.string())), |
| 20 | + |
16 | 21 | /**
|
17 | 22 | * Selector to grab the inner text from
|
18 | 23 | * @example ".docs-builder-container"
|
19 | 24 | * @default ""
|
20 | 25 | */
|
21 |
| - selector?: string; |
| 26 | + selector: z.string().optional(), |
22 | 27 | /**
|
23 | 28 | * Don't crawl more than this many pages
|
24 | 29 | * @default 50
|
25 | 30 | */
|
26 |
| - maxPagesToCrawl: number; |
| 31 | + maxPagesToCrawl: z.number().int().positive(), |
27 | 32 | /**
|
28 | 33 | * File name for the finished data
|
29 | 34 | * @default "output.json"
|
30 | 35 | */
|
31 |
| - outputFileName: string; |
| 36 | + outputFileName: z.string(), |
32 | 37 | /** 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(), |
34 | 42 | /** 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(), |
39 | 52 | /** 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