import {z} from 'zod' export type ColumnInfo = {name?: string | undefined, typeText?: string | undefined, typeJson?: string | undefined, typeName?: number | undefined, typePrecision?: number | undefined, typeScale?: number | undefined, typeIntervalType?: string | undefined, position?: number | undefined, comment?: string | undefined, nullable?: boolean | undefined, partitionIndex?: number | undefined, columnId?: string | undefined}; export type CreateTableRequest = {name: string, schemaName: string, catalogName: string, tableType?: number | undefined, dataSourceFormat?: number | undefined, columns?: Array | undefined, storageLocation?: string | undefined, comment?: string | undefined, properties?: Record | undefined}; export type CreateApiTablesArgs = {body: CreateTableRequest}; export type CreateApiTablesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const columnInfo = z.object({name: z.string().optional(), typeText: z.string().optional(), typeJson: z.string().optional(), typeName: z.number().int().optional(), typePrecision: z.number().int().optional(), typeScale: z.number().int().optional(), typeIntervalType: z.string().optional(), position: z.number().int().optional(), comment: z.string().optional(), nullable: z.boolean().optional(), partitionIndex: z.number().int().optional(), columnId: z.string().optional()}); export const tableInfo = z.object({name: z.string().optional(), schemaName: z.string().optional(), catalogName: z.string().optional(), tableType: z.number().int().optional(), dataSourceFormat: z.number().int().optional(), columns: z.array(columnInfo).optional(), storageLocation: z.string().optional(), owner: z.string().optional(), comment: z.string().optional(), properties: z.object({}).optional(), storageCredentialName: z.string().optional(), fullName: z.string().optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional(), deletedAt: z.string().optional(), tableId: 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 a table * * POST /tables */ export const createApiTables = async (args: CreateApiTablesArgs, options: CreateApiTablesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/tables${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(tableInfo, await res.json()) };