import {z} from 'zod' export type ScheduleOwner = {name: string, type: 'user' | 'service_account'}; export type RunParameter = {hidden?: boolean | undefined, is_override?: (boolean | null) | undefined, name: string, value: string}; export type UpdateScheduleParams = {$schema?: string | undefined, app_version?: (string | null) | undefined, cron?: string | undefined, environment?: string | undefined, name?: (string | null) | undefined, overlap_policy?: ('skip' | 'allow' | null) | undefined, owner?: ScheduleOwner | undefined, parameters?: Array | undefined, status?: ('active' | 'disabled' | null) | undefined, timezone?: (string | null) | undefined}; export type UpdateApiSchedulesIdOrNameArgs = {idOrName: string, 'X-Tower-Request-Number'?: number | undefined, body: UpdateScheduleParams}; export type UpdateApiSchedulesIdOrNameOptions = {baseUrl?: string | undefined, AccessTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleOwner = z.object({name: z.string().min(1), type: z.enum(["user", "service_account"])}); export const runParameter = z.object({hidden: z.boolean().optional(), is_override: z.boolean().nullable().optional(), name: z.string(), value: z.string()}); export const schedule = z.object({app_name: z.string(), app_status: z.enum(["active", "disabled"]), app_version: z.string().optional(), created_at: z.string(), cron: z.string(), environment: z.string(), id: z.string(), name: z.string(), overlap_policy: z.enum(["allow", "skip"]), owner: scheduleOwner.optional(), parameters: z.array(runParameter).optional(), status: z.enum(["active", "disabled"]), timezone: z.string(), updated_at: z.string()}); export const updateScheduleResponse = z.object({$schema: z.string().optional(), schedule: schedule}); 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 }; /** * Update schedule * * PUT /schedules/{idOrName} * * Also accepts auth: APIKeyAuth */ export const updateApiSchedulesIdOrName = async (args: UpdateApiSchedulesIdOrNameArgs, options?: UpdateApiSchedulesIdOrNameOptions) => { const {baseUrl = "https://api.tower.dev/v1", fetch: fetchFn = fetch, AccessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Tower-Request-Number'] != null) headers.set("X-Tower-Request-Number", String(args['X-Tower-Request-Number'])) if (AccessTokenAuth !== undefined) headers.set('authorization', `Bearer ${AccessTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/schedules/${encodeURIComponent(String(args.idOrName))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateScheduleResponse, await res.json()) };