import {z} from 'zod' export type ArrowIpcSchema = {ipcMessage: string}; export type RelationalTableSnapshot = {schema: ArrowIpcSchema}; export type RelationalSchemaSnapshot = {tables?: {[key: string]: RelationalTableSnapshot} | undefined}; export type RelationalSchemaSpec = {connector: string, sourceCatalog: string, sourceSchema: string, snapshot?: (RelationalSchemaSnapshot | null) | undefined}; export type SchemaSpec = {relational?: (RelationalSchemaSpec | null) | undefined}; export type SchemaInput = {name?: string | undefined, spec: SchemaSpec, createTime?: (string | null) | undefined, updateTime?: (string | null) | undefined, revision?: number | undefined, revisionHash?: string | undefined}; export type BodyCreateWorkspaceSchemaV1WorkspacesWorkspaceIdCreateSchemaPost = {catalogId: string, schemaId: string, schema: SchemaInput}; export type CreateApiV1WorkspacesWorkspaceIdCreateSchemaArgs = {workspace_id: string, body: BodyCreateWorkspaceSchemaV1WorkspacesWorkspaceIdCreateSchemaPost}; export type CreateApiV1WorkspacesWorkspaceIdCreateSchemaOptions = {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 relationalSchemaSnapshot = z.object({tables: z.record(z.string(), relationalTableSnapshot).optional()}); export const relationalSchemaSpec = z.object({connector: z.string(), sourceCatalog: z.string().min(1), sourceSchema: z.string().min(1), snapshot: relationalSchemaSnapshot.nullable().optional()}); export const schemaSpec = z.object({relational: relationalSchemaSpec.nullable().optional()}); export const schemaOutput = z.object({name: z.string(), spec: schemaSpec, 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 Schema * * POST /v1/workspaces/{workspace_id}:createSchema */ export const createApiV1WorkspacesWorkspaceIdCreateSchema = async (args: CreateApiV1WorkspacesWorkspaceIdCreateSchemaArgs, options: CreateApiV1WorkspacesWorkspaceIdCreateSchemaOptions) => { 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))}:createSchema${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(schemaOutput, await res.json()) };