import {z} from 'zod' export type GetApiV2StorageBranchBranchIdTableListArgs = {branchId: string, page?: ({after?: string | undefined, limit?: number | undefined}) | undefined, include?: (Array<'columns' | 'metadata' | 'definition'>) | undefined, filter?: ({bucketId?: string | undefined, ids?: string | undefined, sourceTableId?: string | undefined, changedSince?: string | undefined, name?: string | undefined, lastUpdatedBy?: string | undefined, inheritProduction?: boolean | undefined}) | undefined}; export type GetApiV2StorageBranchBranchIdTableListOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tableListV2BucketResponse = z.object({id: z.string(), name: z.string(), displayName: z.string(), stage: z.enum(["in", "out", "sys"]), backend: z.string(), idBranch: z.number().int()}); export const tableAttributionResponse = z.object({componentId: z.string().nullable(), configurationId: z.string().nullable(), configurationRowId: z.string().nullable(), branchId: z.string().nullable()}); export const tableListV2SourceProjectResponse = z.object({id: z.number().int(), name: z.string().nullable()}); export const tableListV2MetadataResponse = z.object({id: z.string(), key: z.string(), value: z.string().nullable(), provider: z.string(), timestamp: z.string()}); export const tableListV2SourceTableResponse = z.object({id: z.string(), project: tableListV2SourceProjectResponse, metadata: z.array(tableListV2MetadataResponse).nullable().optional()}); export const tableListV2AliasFilterResponse = z.object({column: z.string(), operator: z.enum(["eq", "ne"]).nullable(), values: z.array(z.unknown()).nullable()}); 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 tableListV2ItemResponse = z.object({id: z.string(), bucketId: z.string(), name: z.string(), displayName: z.string(), branchId: z.number().int(), bucket: tableListV2BucketResponse, primaryKey: z.array(z.string()), isAlias: z.boolean(), isAliasable: z.boolean(), isTyped: z.boolean(), tableType: z.string(), rowsCount: z.number().int().nullable(), dataSizeBytes: z.number().int().nullable(), created: z.string().nullable(), lastImportDate: z.string().nullable(), lastChangeDate: z.string().nullable(), description: z.string().nullable(), isDescriptionSystemManaged: z.boolean(), createdBy: tableAttributionResponse.nullable(), lastUpdatedBy: tableAttributionResponse.nullable(), sourceTable: tableListV2SourceTableResponse.nullable().optional(), aliasColumnsAutoSync: z.boolean().nullable().optional(), aliasFilter: tableListV2AliasFilterResponse.nullable().optional(), selectSql: z.string().nullable().optional(), columns: z.array(z.string()).nullable().optional(), metadata: z.array(tableListV2MetadataResponse).nullable().optional(), definition: tableDefinitionResponse.nullable().optional()}); export const paginationMeta = z.object({total: z.number().int(), limit: z.number().int(), cursor: z.object({param: z.string(), encoding: z.string(), fields: z.array(z.object({name: z.string(), type: z.enum(["integer", "string", "date-time"]), nullable: z.boolean()})), order: z.string()})}); export const paginationLinks = z.object({self: z.string(), first: z.string(), next: z.string().nullable()}); export const tableListV2Response = z.object({data: z.array(tableListV2ItemResponse), meta: paginationMeta, links: paginationLinks}); 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 tables (cursor-paginated) * * GET /v2/storage/branch/{branchId}/table-list * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdTableList = async (args: GetApiV2StorageBranchBranchIdTableListArgs, options?: GetApiV2StorageBranchBranchIdTableListOptions) => { 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 (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.include != null) query.append("include", args.include.join(',')) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/table-list${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(tableListV2Response, await res.json()) };