import {z} from 'zod' export type GetApiV1ScheduledScansOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduledScanListResponse = z.object({schedules: z.array(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 }; /** * List recurring On-Page.ai scan schedules for the caller's organization. * * GET /v1/scheduled-scans */ export const getApiV1ScheduledScans = async (options?: GetApiV1ScheduledScansOptions) => { const {baseUrl = "https://api.on-page.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduledScanListResponse, await res.json()) };