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 CreateApiV2StorageWorkspacesWorkspaceIdInputMappingLoadArgs = {workspaceId: number, dryRun?: boolean | undefined, 'X-KBC-Backend'?: ({context?: (string | null) | undefined}) | undefined, body: InputMappingLoadRequest}; export type CreateApiV2StorageWorkspacesWorkspaceIdInputMappingLoadOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const inputMappingLoadDryRunResponse = z.object({dryRun: z.boolean(), operationName: z.string(), 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 * * POST /v2/storage/workspaces/{workspaceId}/input-mapping-load * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageWorkspacesWorkspaceIdInputMappingLoad = async (args: CreateApiV2StorageWorkspacesWorkspaceIdInputMappingLoadArgs, options?: CreateApiV2StorageWorkspacesWorkspaceIdInputMappingLoadOptions) => { 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.dryRun != null) query.append("dryRun", String(args.dryRun)) if (args['X-KBC-Backend'] != null) headers.set("X-KBC-Backend", JSON.stringify(args['X-KBC-Backend'])) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/workspaces/${encodeURIComponent(String(args.workspaceId))}/input-mapping-load${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(inputMappingLoadDryRunResponse, await res.json()) };