import { isPlainObject } from "@zachsents/zippy" import { ScrapflyClient } from "scrapfly-sdk" import * as z from "zod" const SCRAPFLY_SECRET_SCHEMA = z.object({ apiKey: z.string().min(1) }) /** * Creates the official authenticated Scrapfly client. * * @param secret - Resolved Scrapfly account secret. */ export function getScrapflyClient(secret: Record) { return new ScrapflyClient({ key: SCRAPFLY_SECRET_SCHEMA.parse(secret).apiKey, }) } /** * Converts provider snake_case object keys to camelCase recursively. * * @param value - Provider response value. */ export function camelCaseScrapflyValue(value: unknown): unknown { if (Array.isArray(value)) return value.map(camelCaseScrapflyValue) if (!isPlainObject(value)) return value return Object.fromEntries( Object.entries(value).map(([key, item]) => [ key.replace(/_([a-z0-9])/g, (_, character: string) => character.toUpperCase(), ), camelCaseScrapflyValue(item), ]), ) }