import {z} from 'zod' export type CreateTableMetadataRequest = {provider: string, metadata?: (Array<{key: string, value: string | null}>) | undefined, columnsMetadata?: ({[key: string]: Array<{columnName?: string | undefined, key: string, value: string | null}>}) | undefined}; export type CreateApiV2StorageBranchBranchIdTablesIdMetadataArgs = {id: string, branchId: string, body: CreateTableMetadataRequest}; export type CreateApiV2StorageBranchBranchIdTablesIdMetadataOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const metadataItemResponse = z.object({id: z.string(), key: z.string(), value: z.string().nullable(), provider: z.string(), timestamp: z.string()}); export const tableCreateOrUpdateMetadataResponse = z.object({metadata: z.array(metadataItemResponse), columnsMetadata: z.record(z.string(), z.array(metadataItemResponse))}); export const createApiV2StorageBranchBranchIdTablesIdMetadataResponse = z.union([tableCreateOrUpdateMetadataResponse, z.array(metadataItemResponse)]); 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 }; /** * Create or update table metadata * * POST /v2/storage/branch/{branchId}/tables/{id}/metadata * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageBranchBranchIdTablesIdMetadata = async (args: CreateApiV2StorageBranchBranchIdTablesIdMetadataArgs, options?: CreateApiV2StorageBranchBranchIdTablesIdMetadataOptions) => { 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 (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/tables/${encodeURIComponent(String(args.id))}/metadata${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(createApiV2StorageBranchBranchIdTablesIdMetadataResponse, await res.json()) };