import {z} from 'zod' export type GetApiV2StorageTablesIdArgs = {id: string}; export type GetApiV2StorageTablesIdOptions = {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 tableColumnMetadataResponse = z.record(z.string(), z.array(metadataItemResponse)); 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 tableDefinitionResponse = z.object({primaryKeysNames: z.array(z.string()), columns: z.array(z.object({name: z.string(), canBeFiltered: z.boolean().optional(), definition: z.object({type: z.string().optional(), nullable: z.boolean().optional(), length: z.string().nullable().optional(), default: z.string().nullable().optional(), description: z.string().optional()}), basetype: z.string().nullable().optional(), policyTagState: z.enum(["protected", "unprotected", "unknown"]).optional()})), description: z.string().optional(), distribution: z.object({type: z.string(), distributionColumnsNames: z.array(z.string())}).optional(), index: z.object({type: z.string(), indexColumnsNames: z.array(z.string())}).optional()}).and(z.record(z.string(), z.unknown())); export const tableSourceTableResponse = z.object({id: z.string(), uri: z.string(), project: z.object({id: z.number().int(), name: z.string()}), isAlias: z.boolean().optional(), metadata: z.array(metadataItemResponse).optional(), columnMetadata: z.union([tableColumnMetadataResponse, z.array(z.unknown())]).optional(), definition: tableDefinitionResponse.optional()}); export const aliasFilterResponse = z.object({column: z.string(), operator: z.enum(["eq", "ne"]), values: z.array(z.string())}); export const tableAttributionResponse = z.object({componentId: z.string().nullable(), configurationId: z.string().nullable(), configurationRowId: z.string().nullable(), branchId: z.string().nullable()}); export const tableDetailResponse = 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.enum(["MANAGED", "SELFMANAGED", "NONE"]), tableType: z.string(), path: z.string(), columns: z.array(z.string()), columnMetadata: tableColumnMetadataResponse, attributes: z.array(z.object({})), metadata: z.array(metadataItemResponse), bucket: tableBucketResponse, sourceTable: tableSourceTableResponse.optional(), aliasColumnsAutoSync: z.boolean().optional(), aliasFilter: aliasFilterResponse.optional(), selectSql: z.string().optional(), isPersisted: z.boolean().optional(), definition: tableDefinitionResponse, createdBy: tableAttributionResponse.nullable(), lastUpdatedBy: tableAttributionResponse.nullable()}); 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 }; /** * Table detail * * GET /v2/storage/tables/{id} * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageTablesId = async (args: GetApiV2StorageTablesIdArgs, options?: GetApiV2StorageTablesIdOptions) => { 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/tables/${encodeURIComponent(String(args.id))}${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(tableDetailResponse, await res.json()) };