import {z} from 'zod' export type StorageName = string; export type TableUpdateRequest = {displayName: StorageName}; export type UpdateApiV2StorageBranchBranchIdTablesTableIdArgs = {tableId: string, async?: boolean | undefined, branchId: string, body: TableUpdateRequest}; export type UpdateApiV2StorageBranchBranchIdTablesTableIdOptions = {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 actorResponse = z.object({type: z.enum(["token", "session", "pat", "system"]), id: z.string(), name: z.string(), adminId: z.number().int().nullable()}); export const sharedBucketResponse = z.object({id: z.string(), name: z.string(), displayName: z.string(), stage: z.enum(["in", "out", "sys"]), description: z.string(), sharing: z.enum(["organization", "organization-project", "specific-projects", "specific-users", "null"]).nullable(), created: z.string(), lastChangeDate: z.string().nullable(), dataSizeBytes: z.number().int(), rowsCount: z.number().int(), backend: z.string(), hasExternalSchema: z.boolean(), databaseName: z.string(), path: z.string(), project: z.object({id: z.number().int(), name: z.string()}), color: z.string().nullable(), tables: z.array(z.object({id: z.string(), name: z.string(), displayName: z.string(), path: z.string()})).optional(), sharingParameters: z.union([z.object({projects: z.array(z.object({id: z.number().int(), name: z.string()})).optional(), users: z.array(z.object({id: z.number().int(), name: z.string(), email: z.string()})).optional()}), z.array(z.unknown())]).optional(), sharedBy: z.object({id: z.number().int().nullable(), name: z.string().nullable(), date: z.string()}).optional(), sharedByActor: actorResponse.nullable(), owner: z.object({id: z.number().int(), name: z.string(), email: z.string()}).nullable(), metadata: z.array(metadataItemResponse).optional()}); export const tableBucketResponse = z.object({uri: z.string(), id: z.string(), name: z.string(), displayName: z.string(), idBranch: z.number().int(), stage: z.enum(["in", "out", "sys"]), description: z.string(), tables: z.string(), created: z.string(), lastChangeDate: z.string().nullable(), updated: z.string().nullable(), isReadOnly: z.boolean(), dataSizeBytes: z.number().int(), rowsCount: z.number().int(), isMaintenance: z.boolean(), backend: z.string(), sharing: z.enum(["organization", "organization-project", "specific-projects", "specific-users", "null"]).nullable(), hasExternalSchema: z.boolean(), databaseName: z.string(), path: z.string(), isSnowflakeSharedDatabase: z.boolean(), isLinkedBucket: z.boolean(), color: z.string().nullable(), bucketSharingAllowed: z.boolean(), sharingParameters: z.union([z.object({projects: z.array(z.object({id: z.number().int(), name: z.string()})).optional(), users: z.array(z.object({id: z.number().int(), name: z.string(), email: z.string()})).optional()}), z.array(z.unknown())]).optional(), sharedBy: z.object({id: z.number().int().nullable(), name: z.string().nullable(), date: z.string()}).optional(), sharedByActor: actorResponse.nullable(), owner: z.object({id: z.number().int(), name: z.string(), email: z.string()}).nullable(), sourceBucket: sharedBucketResponse.optional(), backendPath: z.array(z.string()), metadata: z.array(metadataItemResponse).optional()}); export const tableUpdateResponse = z.object({uri: z.string(), id: z.string(), name: z.string(), displayName: z.string(), transactional: z.boolean(), primaryKey: z.array(z.string()), indexType: z.string().nullable(), indexKey: z.array(z.string()), distributionType: z.string().nullable(), distributionKey: z.array(z.string()), syntheticPrimaryKeyEnabled: z.boolean(), created: z.string().nullable(), lastImportDate: z.string().nullable(), lastChangeDate: z.string().nullable(), rowsCount: z.number().int().nullable(), dataSizeBytes: z.number().int().nullable(), isAlias: z.boolean(), isAliasable: z.boolean(), isTyped: z.boolean(), isDescriptionSystemManaged: z.boolean(), timestampType: z.string(), tableType: z.string(), path: z.string(), columns: z.array(z.string()), columnMetadata: z.record(z.string(), z.array(z.object({}))), attributes: z.array(z.object({})), metadata: z.array(metadataItemResponse), bucket: tableBucketResponse, sourceTable: z.object({id: z.string(), uri: z.string(), project: z.object({id: z.number().int(), name: z.string()}), columnMetadata: z.record(z.string(), z.array(z.object({}))).optional()}).optional(), aliasColumnsAutoSync: z.boolean().optional(), aliasFilter: z.object({column: z.string(), operator: z.string(), values: z.array(z.string())}).optional(), selectSql: z.string().optional(), isPersisted: z.boolean().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 }; /** * Update a table * * PUT /v2/storage/branch/{branchId}/tables/{tableId} * * Also accepts auth: ManageKey, BearerAuth */ export const updateApiV2StorageBranchBranchIdTablesTableId = async (args: UpdateApiV2StorageBranchBranchIdTablesTableIdArgs, options?: UpdateApiV2StorageBranchBranchIdTablesTableIdOptions) => { 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 (args.async != null) query.append("async", String(args.async)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/tables/${encodeURIComponent(String(args.tableId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(tableUpdateResponse, await res.json()) };