import {z} from 'zod' export type CreateApiV1ScheduledScansArgs = {body: {name: string, url: string, keyword: string, depth?: ('lite' | 'standard' | 'deep') | undefined, region?: ('US' | 'CA' | 'UK' | 'AU' | 'NZ' | 'ES' | 'DE' | 'IT' | 'FR' | 'IE' | 'NL' | 'CH' | 'SE' | 'NO' | 'DK' | 'FI' | 'ZA' | 'MX' | 'BR' | 'CO' | 'IN' | 'SG' | 'MY' | 'JP' | 'KE' | 'AE' | 'HK') | undefined, locale?: string | undefined, cadence: {frequency: 'daily' | 'weekly' | 'monthly', timezone: string, time: string, day_of_week?: number | undefined, day_of_month?: number | undefined}, max_credits_per_run: number, monthly_credit_budget: number, alert_thresholds?: ({score_drop?: number | undefined, entity_loss_count?: number | undefined, competitor_gap_increase?: number | undefined}) | undefined}}; export type CreateApiV1ScheduledScansOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduledScanEnvelopeResponse = z.object({schedule: z.object({id: z.string(), name: z.string(), url: z.string(), keyword: z.string(), depth: z.enum(["lite", "standard", "deep"]), region: z.string(), locale: z.string().nullable(), cadence: z.object({frequency: z.enum(["daily", "weekly", "monthly"]), timezone: z.string().min(1).max(100), time: z.string(), day_of_week: z.number().int().gte(0).lte(6).optional(), day_of_month: z.number().int().gte(1).lte(28).optional()}), status: z.enum(["active", "paused"]), pause_reason: z.string().nullable(), max_credits_per_run: z.number(), monthly_credit_budget: z.number(), current_month_spend: z.number(), alert_thresholds: z.object({score_drop: z.number().gte(0).lte(100).optional(), entity_loss_count: z.number().int().gte(0).lte(10000).optional(), competitor_gap_increase: z.number().gte(0).lte(100).optional()}).optional(), next_run_at: z.string().nullable(), last_run_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a recurring diagnostic scan schedule. * * POST /v1/scheduled-scans */ export const createApiV1ScheduledScans = async (args: CreateApiV1ScheduledScansArgs, options?: CreateApiV1ScheduledScansOptions) => { const {baseUrl = "https://api.on-page.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/scheduled-scans${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduledScanEnvelopeResponse, await res.json()) };