import {z} from 'zod' export type Scheduling = {days?: number | undefined, hours?: number | undefined, minutes?: number | undefined, interval?: ('hour' | 'day' | 'week' | 'month') | undefined, timezone?: string | undefined, recurrence?: number | undefined, lookback_window?: ({name?: ('default' | 'live' | 'max') | undefined}) | undefined}; export type UpdateApiApiV3LoadsLoadIdArgs = {load_id: number, body: {run_policy: 'after extraction' | 'schedule', schedules?: Array | undefined}}; export type UpdateApiApiV3LoadsLoadIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataTable = z.object({id: z.number().int().optional(), title: z.string().optional(), datasource_name: z.string().optional()}); export const scheduling = z.object({days: z.number().int().gte(0).optional(), hours: z.number().int().gte(0).lte(23).optional(), minutes: z.number().int().gte(0).lte(59).optional(), interval: z.enum(["hour", "day", "week", "month"]).optional(), timezone: z.string().optional(), recurrence: z.number().int().optional(), lookback_window: z.object({name: z.enum(["default", "live", "max"]).optional()}).optional()}); export const load = z.object({id: z.number().int().optional(), data_table: dataTable.optional(), destination_connection: z.object({id: z.string().optional(), title: z.string().optional(), destination_name: z.string().optional()}).optional(), destination_table: z.string().optional(), run_policy: z.enum(["after extraction", "schedule"]).optional(), schedules: z.array(scheduling).optional(), status: z.string().optional(), last_update: z.string().optional(), error_text: z.string().optional(), uploaded_rows_count: z.number().int().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 }; /** * Update a load * * PUT /api/v3/loads/{load_id} * * Also accepts auth: cookieAuth */ export const updateApiApiV3LoadsLoadId = async (args: UpdateApiApiV3LoadsLoadIdArgs, options?: UpdateApiApiV3LoadsLoadIdOptions) => { const {baseUrl = "https://embedded.improvado.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/loads/${encodeURIComponent(String(args.load_id))}${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(load, await res.json()) };