import {z} from 'zod' export type GetApiApiV31AutomatedRecipesArgs = {template_id?: number | undefined, state?: ('extracting_data' | 'building_dependencies' | 'ready_to_work' | 'failed') | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiApiV31AutomatedRecipesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const recipeBuildErrorV31 = z.object({code: z.string().optional(), message: z.string().optional(), stage: z.enum(["extraction", "transformation_build", "final_build", "null"]).nullable().optional(), data_source: z.string().nullable().optional(), occurred_at: z.string().nullable().optional(), details: z.object({}).optional()}).nullable(); export const recipeListItemV31 = z.object({recipe_id: z.string().optional(), template_id: z.number().int().optional(), template_title: z.string().optional(), title: z.string().optional(), workspace_id: z.number().int().optional(), state: z.enum(["extracting_data", "building_dependencies", "ready_to_work", "failed"]).optional(), degraded: z.boolean().nullable().optional(), not_delivered_reasons: z.array(z.enum(["connection_unavailable", "account_disabled", "permission_denied", "extraction_failing", "report_type_deprecated", "paused", "not_running", "timed_out", "unknown", "source_failed"])).optional(), error: recipeBuildErrorV31.optional(), datatable_id: z.number().int().nullable().optional(), last_build_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const recipePageV31 = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(recipeListItemV31).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 }; /** * List recipes * * GET /api/v3.1/automated-recipes/ * * Also accepts auth: cookieAuth */ export const getApiApiV31AutomatedRecipes = async (args: GetApiApiV31AutomatedRecipesArgs, options?: GetApiApiV31AutomatedRecipesOptions) => { const {baseUrl = "https://embedded.improvado.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.template_id != null) query.append("template_id", String(args.template_id)) if (args.state != null) query.append("state", String(args.state)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3.1/automated-recipes/${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(recipePageV31, await res.json()) };