import {z} from 'zod' export type GetApiSchedulesArgs = {page?: number | undefined, page_size?: number | undefined, environment?: string | undefined, app?: string | undefined, sort?: ('created_at' | 'updated_at') | undefined}; export type GetApiSchedulesOptions = {baseUrl?: string | undefined, AccessTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({num_pages: z.number().int(), page: z.number().int(), page_size: z.number().int(), total: z.number().int()}); 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 listSchedulesResponse = z.object({$schema: z.string().optional(), pages: pagination, schedules: z.array(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 }; /** * List schedules * * GET /schedules * * Also accepts auth: APIKeyAuth */ export const getApiSchedules = async (args: GetApiSchedulesArgs, options?: GetApiSchedulesOptions) => { const {baseUrl = "https://api.tower.dev/v1", fetch: fetchFn = fetch, AccessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.environment != null) query.append("environment", String(args.environment)) if (args.app != null) query.append("app", String(args.app)) if (args.sort != null) query.append("sort", String(args.sort)) if (AccessTokenAuth !== undefined) headers.set('authorization', `Bearer ${AccessTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/schedules${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(listSchedulesResponse, await res.json()) };