import {z} from 'zod' export type GetApiV2StorageBranchBranchIdChangesTablesArgs = {branchId: string}; export type GetApiV2StorageBranchBranchIdChangesTablesOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const columnDefinitionResponse = z.object({type: z.string(), length: z.string().nullable(), nullable: z.boolean()}); export const tableChangeColumnResponse = z.object({name: z.string(), definition: columnDefinitionResponse.nullable()}); export const columnOrderChangeResponse = z.object({production: z.array(z.string()), branch: z.array(z.string()), columns: z.array(z.object({name: z.string(), productionPosition: z.number().int().nullable(), branchPosition: z.number().int().nullable()}))}); export const tableSchemaChangesResponse = z.object({addedColumns: z.array(tableChangeColumnResponse), removedColumns: z.array(tableChangeColumnResponse), typeChangedColumns: z.array(z.object({name: z.string(), production: columnDefinitionResponse.nullable(), branch: columnDefinitionResponse.nullable()})), primaryKey: z.object({production: z.array(z.string()), branch: z.array(z.string())}).nullable(), columnOrder: columnOrderChangeResponse.nullable()}); export const tableChangeResponse = z.object({id: z.string(), name: z.string(), displayName: z.string(), bucket: z.object({id: z.string(), name: z.string(), displayName: z.string()}), changeType: z.enum(["new", "modified", "deleted"]), isTyped: z.boolean(), summary: z.object({addedColumns: z.number().int(), removedColumns: z.number().int(), typeChangedColumns: z.number().int(), primaryKeyChanged: z.boolean(), columnOrderChanged: z.boolean()}), columns: z.array(tableChangeColumnResponse).optional(), primaryKey: z.array(z.string()).optional(), changes: tableSchemaChangesResponse.optional()}); export const tableChangesListResponse = z.object({tables: z.array(tableChangeResponse)}); 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 }; /** * List table changes in a development branch * * GET /v2/storage/branch/{branchId}/changes/tables * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdChangesTables = async (args: GetApiV2StorageBranchBranchIdChangesTablesArgs, options?: GetApiV2StorageBranchBranchIdChangesTablesOptions) => { 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 (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/changes/tables${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(tableChangesListResponse, await res.json()) };