Explorar o código

refactor: convert config to zod schema

Igor Perzic %!s(int64=2) %!d(string=hai) anos
pai
achega
f6b85be07f
Modificáronse 1 ficheiros con 29 adicións e 13 borrados
  1. 29 13
      src/config.ts

+ 29 - 13
src/config.ts

@@ -1,41 +1,57 @@
+import { z } from 'zod';
+
 import type { Page } from "playwright";
 
-export type Config = {
+const Page: z.ZodType<Page> = z.any();
+
+export const configSchema = z.object({
   /**
    * URL to start the crawl
    * @example "https://www.builder.io/c/docs/developers"
    * @default ""
    */
-  url: string;
+  url: z.string(),
   /**
    * Pattern to match against for links on a page to subsequently crawl
    * @example "https://www.builder.io/c/docs/**"
    * @default ""
    */
-  match: string | string[];
+  match: z.string().or(z.array(z.string())),
+
   /**
    * Selector to grab the inner text from
    * @example ".docs-builder-container"
    * @default ""
    */
-  selector?: string;
+  selector: z.string().optional(),
   /**
    * Don't crawl more than this many pages
    * @default 50
    */
-  maxPagesToCrawl: number;
+  maxPagesToCrawl: z.number().int().positive(),
   /**
    * File name for the finished data
    * @default "output.json"
    */
-  outputFileName: string;
+  outputFileName: z.string(),
   /** Optional cookie to be set. E.g. for Cookie Consent */
-  cookie?: { name: string; value: string };
+  cookie: z.object({
+    name: z.string(),
+    value: z.string(),
+  }).optional(),
   /** Optional function to run for each page found */
-  onVisitPage?: (options: {
-    page: Page;
-    pushData: (data: any) => Promise<void>;
-  }) => Promise<void>;
+  onVisitPage: z.function()
+      .args(z.object({
+        page: Page,
+        pushData: z.function()
+            .args(z.any())
+            .returns(z.promise(z.void()))
+      }))
+      .returns(z.promise(z.void()))
+      .optional(),
   /** Optional timeout for waiting for a selector to appear */
-  waitForSelectorTimeout?: number;
-};
+  waitForSelectorTimeout: z.number().int().nonnegative().optional(),
+});
+
+export type Config = z.infer<typeof configSchema>;
+