import {z} from 'zod' export type RestScheduleBackfillRequest = {advanceSchedule?: boolean | undefined, attemptName?: string | undefined, count?: number | undefined, dryRun: boolean, fromTime?: string | undefined, skipExisting?: boolean | undefined}; export type CreateApiApiSchedulesIdBackfillArgs = {id: number, body: RestScheduleBackfillRequest}; export type CreateApiApiSchedulesIdBackfillOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const idAndName = z.object({id: z.string(), name: z.string()}); export const nameOptionalId = z.object({id: z.string().optional(), name: z.string()}); export const restSessionAttempt = z.object({cancelRequested: z.boolean(), createdAt: z.string(), done: z.boolean(), finishedAt: z.string().optional(), id: z.string(), index: z.number().int(), params: z.object({}), poolId: z.string().optional(), project: idAndName, retryAttemptName: z.string(), sessionId: z.string(), sessionTime: z.string(), sessionUuid: z.string(), status: z.enum(["blocked", "queued", "running", "canceling", "success", "error", "killed"]), success: z.boolean(), workflow: nameOptionalId}); export const restScheduleAttemptCollection = z.object({attempts: z.array(restSessionAttempt), id: z.string(), project: idAndName, workflow: idAndName}); 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 }; /** * Backfill sessions for past schedule times * * POST /api/schedules/{id}/backfill */ export const createApiApiSchedulesIdBackfill = async (args: CreateApiApiSchedulesIdBackfillArgs, options?: CreateApiApiSchedulesIdBackfillOptions) => { const {baseUrl = "https://api-workflow.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/schedules/${encodeURIComponent(String(args.id))}/backfill${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(restScheduleAttemptCollection, await res.json()) };