import {z} from 'zod' export type ScheduleCreate = {name: string, cron_expr: string, endpoint: string, method?: string | undefined, description?: (string | null) | undefined, payload?: ({[key: string]: unknown} | null) | undefined, timezone?: string | undefined, timeout_seconds?: number | undefined, max_retries?: number | undefined, retry_delay_seconds?: number | undefined}; export type CreateApiSchedulesArgs = {body: ScheduleCreate}; export type CreateApiSchedulesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), method: z.string(), endpoint: z.string(), payload: z.record(z.string(), z.unknown()).nullable().optional(), cron_expr: z.string(), timezone: z.string(), timeout_seconds: z.number().int(), max_retries: z.number().int(), retry_delay_seconds: z.number().int(), enabled: z.boolean(), next_run_at: z.number().int().nullable().optional(), created_at: z.number().int().nullable().optional(), updated_at: z.number().int().nullable().optional()}); 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 Schedule * * POST /schedules */ export const createApiSchedules = async (args: CreateApiSchedulesArgs, options: CreateApiSchedulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/schedules${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(scheduleResponse, await res.json()) };