import {z} from 'zod' export type InputMappingLoadRequest = {input: Array<{source: string, source_branch_id?: number | undefined, destination: string, description?: (string | null) | undefined, days?: (number | null) | undefined, changed_since?: (string | null) | undefined, columns?: Array | undefined, column_types?: (Array<{source: string, description?: (string | null) | undefined, type?: string | undefined, destination?: string | undefined, length?: (string | number) | undefined, nullable?: boolean | undefined, convert_empty_values_to_null?: boolean | undefined}>) | undefined, where_column?: string | undefined, where_values?: Array | undefined, where_operator?: ('eq' | 'ne') | undefined, limit?: number | undefined, overwrite?: boolean | undefined, load_type?: ('COPY' | 'CLONE' | 'VIEW' | 'AUTO') | undefined, use_view?: boolean | undefined, keep_internal_timestamp_column?: boolean | undefined, incremental?: boolean | undefined, file_type?: string | undefined, backend?: string | undefined}>, preserve?: boolean | undefined}; export type CreateApiV2StorageInputMappingLoadPreviewArgs = {backend?: (string | null) | undefined, body: InputMappingLoadRequest}; export type CreateApiV2StorageInputMappingLoadPreviewOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const inputMappingLoadPreviewResponse = z.object({input: z.array(z.object({source: z.string(), destination: z.string(), loadType: z.enum(["COPY", "CLONE", "VIEW"]), possibleLoadTypes: z.array(z.enum(["COPY", "CLONE", "VIEW"]))}))}); 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 }; /** * Input mapping load preview * * POST /v2/storage/input-mapping-load/preview * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageInputMappingLoadPreview = async (args: CreateApiV2StorageInputMappingLoadPreviewArgs, options?: CreateApiV2StorageInputMappingLoadPreviewOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.backend != null) query.append("backend", String(args.backend)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/input-mapping-load/preview${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(inputMappingLoadPreviewResponse, await res.json()) };