import {z} from 'zod' export type GetApiApiOrgIdPipelinesBackfillArgs = {org_id: string}; export type GetApiApiOrgIdPipelinesBackfillOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const deletionStatus = z.enum(["not_required", "pending", "in_progress", "completed"]); export const backfillJobStatus = z.object({chunk_period_minutes: z.number().int().nullable().optional(), chunks_completed: z.number().int().gte(0).nullable().optional(), chunks_total: z.number().int().gte(0).nullable().optional(), created_at: z.number().int().nullable().optional(), current_position: z.number().int(), delay_between_chunks_secs: z.number().int().nullable().optional(), delete_before_backfill: z.boolean().nullable().optional(), deletion_job_ids: z.array(z.string()).nullable().optional(), deletion_status: deletionStatus.nullable().optional(), enabled: z.boolean(), end_time: z.number().int(), error: z.string().nullable().optional(), job_id: z.string(), last_triggered_at: z.number().int().nullable().optional(), pipeline_id: z.string(), pipeline_name: z.string().nullable().optional(), progress_percent: z.number().int().gte(0), start_time: z.number().int(), status: z.string()}); export const getApiApiOrgIdPipelinesBackfillResponse = z.array(backfillJobStatus); 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 all backfill jobs for an organization * * GET /api/{org_id}/pipelines/backfill */ export const getApiApiOrgIdPipelinesBackfill = async (args: GetApiApiOrgIdPipelinesBackfillArgs, options: GetApiApiOrgIdPipelinesBackfillOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/pipelines/backfill${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(getApiApiOrgIdPipelinesBackfillResponse, await res.json()) };