import {z} from 'zod' export type WhereFiltersObject = {column: string, operator?: ('eq' | 'ne' | 'gt' | 'ge' | 'lt' | 'le') | undefined, values: Array, dataType?: (string | null) | undefined, type?: ('SCALAR' | 'TIME_STRING') | undefined}; export type OrderByObject = {column: string, order?: ('ASC' | 'DESC') | undefined, dataType?: (string | null) | undefined}; export type GetApiV2StorageBranchBranchIdTablesTableIdDataPreviewArgs = {tableId: string, branchId: string, limit?: number | undefined, changedSince?: (string | null) | undefined, changedUntil?: (string | null) | undefined, columns?: (string | null) | undefined, whereColumn?: (string | null) | undefined, 'whereValues[]'?: (Array | null) | undefined, whereOperator?: ('eq' | 'ne' | null) | undefined, format?: ('rfc' | 'json' | null) | undefined, 'whereFilters[]'?: (Array | null) | undefined, 'orderBy[]'?: (Array | null) | undefined, fulltextSearch?: (string | null) | undefined, cache?: boolean | undefined}; export type GetApiV2StorageBranchBranchIdTablesTableIdDataPreviewOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2StorageBranchBranchIdTablesTableIdDataPreviewResponse = z.object({columns: z.array(z.string()), rows: z.array(z.array(z.object({columnName: z.string(), value: z.union([z.string(), z.number(), z.boolean()]).nullable(), isTruncated: z.boolean()})))}); 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 }; /** * Data preview * * GET /v2/storage/branch/{branchId}/tables/{tableId}/data-preview * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdTablesTableIdDataPreview = async (args: GetApiV2StorageBranchBranchIdTablesTableIdDataPreviewArgs, options?: GetApiV2StorageBranchBranchIdTablesTableIdDataPreviewOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.changedSince != null) query.append("changedSince", String(args.changedSince)) if (args.changedUntil != null) query.append("changedUntil", String(args.changedUntil)) if (args.columns != null) query.append("columns", String(args.columns)) if (args.whereColumn != null) query.append("whereColumn", String(args.whereColumn)) for (const item of args['whereValues[]'] ?? []) query.append("whereValues[]", String(item)) if (args.whereOperator != null) query.append("whereOperator", String(args.whereOperator)) if (args.format != null) query.append("format", String(args.format)) for (const item of args['whereFilters[]'] ?? []) query.append("whereFilters[]", String(item)) for (const item of args['orderBy[]'] ?? []) query.append("orderBy[]", String(item)) if (args.fulltextSearch != null) query.append("fulltextSearch", String(args.fulltextSearch)) if (args.cache != null) query.append("cache", String(args.cache)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/tables/${encodeURIComponent(String(args.tableId))}/data-preview${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(getApiV2StorageBranchBranchIdTablesTableIdDataPreviewResponse, await res.json()) };