import {z} from 'zod' export type ArrowIpcSchema = {ipcMessage: string}; export type RelationalTableSnapshot = {schema: ArrowIpcSchema}; export type RelationalTableSpec = {connector: string, sourceCatalog: string, sourceSchema: string, sourceTable: string, snapshot?: (RelationalTableSnapshot | null) | undefined}; export type ListingTableOptionsInput = {fileType: string, formatOptions?: {[key: string]: string} | undefined, fileExtension?: string | undefined, tablePartitionSchema?: (ArrowIpcSchema | null) | undefined, collectStat?: (boolean | null) | undefined, targetPartitions?: (number | null) | undefined}; export type ListingTableSnapshotInput = {fileSchema?: (ArrowIpcSchema | null) | undefined, options?: (ListingTableOptionsInput | null) | undefined}; export type ListingTableSpecInput = {connector: string, tablePaths: Array, snapshot?: (ListingTableSnapshotInput | null) | undefined}; export type TableSpecInput = {relational?: (RelationalTableSpec | null) | undefined, listing?: (ListingTableSpecInput | null) | undefined}; export type TableInput = {name?: string | undefined, spec: TableSpecInput, createTime?: (string | null) | undefined, updateTime?: (string | null) | undefined, revision?: number | undefined, revisionHash?: string | undefined}; export type BodyCreateWorkspaceTableV1WorkspacesWorkspaceIdCreateTablePost = {catalogId: string, schemaId: string, tableId: string, table: TableInput}; export type CreateApiV1WorkspacesWorkspaceIdCreateTableArgs = {workspace_id: string, body: BodyCreateWorkspaceTableV1WorkspacesWorkspaceIdCreateTablePost}; export type CreateApiV1WorkspacesWorkspaceIdCreateTableOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const arrowIpcSchema = z.object({ipcMessage: z.string().min(1)}); export const relationalTableSnapshot = z.object({schema: arrowIpcSchema}); export const relationalTableSpec = z.object({connector: z.string(), sourceCatalog: z.string().min(1), sourceSchema: z.string().min(1), sourceTable: z.string().min(1), snapshot: relationalTableSnapshot.nullable().optional()}); export const listingTableOptionsOutput = z.object({fileType: z.string().min(1), formatOptions: z.record(z.string(), z.string()).optional(), fileExtension: z.string().optional(), tablePartitionSchema: arrowIpcSchema.nullable().optional(), collectStat: z.boolean().nullable().optional(), targetPartitions: z.string().nullable().optional()}); export const listingTableSnapshotOutput = z.object({fileSchema: arrowIpcSchema.nullable().optional(), options: listingTableOptionsOutput.nullable().optional()}); export const listingTableSpecOutput = z.object({connector: z.string(), tablePaths: z.array(z.string()), snapshot: listingTableSnapshotOutput.nullable().optional()}); export const tableSpecOutput = z.object({relational: relationalTableSpec.nullable().optional(), listing: listingTableSpecOutput.nullable().optional()}); export const tableOutput = z.object({name: z.string(), spec: tableSpecOutput, createTime: z.string().nullable().optional(), updateTime: z.string().nullable().optional(), revision: z.string().optional(), revisionHash: z.string().optional()}); 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 Workspace Table * * POST /v1/workspaces/{workspace_id}:createTable */ export const createApiV1WorkspacesWorkspaceIdCreateTable = async (args: CreateApiV1WorkspacesWorkspaceIdCreateTableArgs, options: CreateApiV1WorkspacesWorkspaceIdCreateTableOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}:createTable${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(tableOutput, await res.json()) };