import {z} from 'zod' export type CreateApiDataCredentialsCredentialIdProbeTreeArgs = {credential_id: number, async?: boolean | undefined, request_id?: number | undefined, body: {depth: number, path?: string | undefined} | {depth: number, database?: string | undefined, table?: string | undefined}}; export type CreateApiDataCredentialsCredentialIdProbeTreeOptions = {baseUrl?: string | undefined, NexlaSessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const probeTreeCommon = z.object({status: z.literal("ok").optional(), message: z.literal("Ok").optional(), connection_type: z.string().optional()}); export const asyncResponse = z.object({request_id: z.number().int().optional(), status: z.enum(["pending", "running", "completed", "failed", "cancelled"]).optional(), request_started_at: z.string().optional(), progress: z.number().int().optional(), result: z.record(z.string(), z.unknown()).optional(), request_stopped_at: z.string().optional(), request_completed_at: z.string().optional()}); export const probeTreeWithAsync = z.union([probeTreeCommon, asyncResponse]); 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 }; /** * Preview Storage Structure * * POST /data_credentials/{credential_id}/probe/tree */ export const createApiDataCredentialsCredentialIdProbeTree = async (args: CreateApiDataCredentialsCredentialIdProbeTreeArgs, options?: CreateApiDataCredentialsCredentialIdProbeTreeOptions) => { const {baseUrl = "https://dataops.nexla.io/nexla-api", fetch: fetchFn = fetch, NexlaSessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.async != null) query.append("async", String(args.async)) if (args.request_id != null) query.append("request_id", String(args.request_id)) if (NexlaSessionToken !== undefined) headers.set('authorization', `Bearer ${NexlaSessionToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/data_credentials/${encodeURIComponent(String(args.credential_id))}/probe/tree${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(probeTreeWithAsync, await res.json()) };