import {z} from 'zod' export type CreateBucketSharingToProjectsRequest = {targetProjectIds: Array}; export type CreateApiV2StorageBucketsBucketIdShareToProjectsArgs = {bucketId: string, body: CreateBucketSharingToProjectsRequest}; export type CreateApiV2StorageBucketsBucketIdShareToProjectsOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bucketForceUnlinkJobResult = z.object({}).nullable(); export const bucketListingUpdateJobResponse = z.union([z.object({bucketId: z.string(), exposureName: z.string(), exposureDescription: z.string().nullable(), bigquery: z.object({listingId: z.string(), subscribers: z.array(z.string())})}), z.object({idBucket: z.number().int(), backend: z.literal("snowflake"), snowflake: z.object({mode: z.enum(["grant", "share"]), grantees: z.array(z.string()).optional(), accounts: z.array(z.string()).optional(), added: z.array(z.string()), removed: z.array(z.string())})})]); export const bucketUpdateJobResult = z.object({}).nullable(); export const bucketListingCreateJobResult = z.union([z.object({idBucket: z.number().int(), listingId: z.string(), listingResourceName: z.string(), providerResourceName: z.string()}), z.object({idBucket: z.number().int(), backend: z.literal("snowflake"), mode: z.enum(["grant", "share"]), objectName: z.string()})]); export const bucketListingDeleteResponse = z.object({id: z.number().int().optional(), status: z.string().optional(), url: z.string().optional(), tableId: z.string().nullable().optional(), operationName: z.string().optional(), operationParams: z.record(z.string(), z.unknown()).optional(), createdTime: z.string().optional(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), runId: z.string().nullable().optional(), results: z.object({}).nullable().optional(), creatorToken: z.object({id: z.number().int().optional(), description: z.string().nullable().optional()}).optional(), metrics: z.record(z.string(), z.unknown()).optional(), error: z.object({code: z.string().nullable().optional(), message: z.string().nullable().optional(), exceptionId: z.string().nullable().optional()}).nullable().optional()}).nullable(); export const bucketDeleteResponse = z.object({id: z.number().int().optional(), status: z.string().optional(), url: z.string().optional(), tableId: z.string().nullable().optional(), operationName: z.string().optional(), operationParams: z.record(z.string(), z.unknown()).optional(), createdTime: z.string().optional(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), runId: z.string().nullable().optional(), results: z.object({}).nullable().optional(), creatorToken: z.object({id: z.number().int().optional(), description: z.string().nullable().optional()}).optional(), metrics: z.record(z.string(), z.unknown()).optional(), error: z.object({code: z.string().nullable().optional(), message: z.string().nullable().optional(), exceptionId: z.string().nullable().optional()}).nullable().optional()}).nullable(); 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 metadataItemResponse = z.object({id: z.string(), key: z.string(), value: z.string().nullable(), provider: z.string(), timestamp: z.string()}); 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 refreshJobResponse = z.object({uri: z.string(), id: z.string(), name: z.string(), displayName: z.string(), idBranch: z.number().int(), stage: z.string(), 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.string().nullable(), hasExternalSchema: z.boolean(), databaseName: z.string(), path: z.string(), isSnowflakeSharedDatabase: z.boolean(), color: z.string().nullable(), sharingParameters: 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()}).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(), warnings: z.array(z.object({message: z.string(), context: z.string().nullable()}))}); export const batchPurgeJobResult = z.object({purged: z.array(z.object({componentId: z.string(), configurationId: z.string()})), failed: z.array(z.object({componentId: z.string(), configurationId: z.string(), error: z.object({code: z.string(), message: z.string()})}))}); export const batchRestoreJobResult = z.object({restored: z.array(z.object({componentId: z.string(), configurationId: z.string(), version: z.number().int()})), failed: z.array(z.object({componentId: z.string(), configurationId: z.string(), error: z.object({code: z.string(), message: z.string()})}))}); export const fileDeleteJobResult = z.object({}).nullable(); export const tableSnapshotCreateJobResult = z.object({id: z.string(), description: z.string().nullable(), createdTime: z.string(), type: z.string(), creatorToken: z.object({id: z.number().int(), description: z.string()}), actor: actorResponse.nullable().optional(), uri: z.string(), dataFileId: z.number().int()}); export const tableDeleteJobResult = z.object({}); export const deleteRowsJobResult = z.object({}); export const tableExportAsyncJobResponse = z.object({file: z.object({id: z.number().int()}), cacheHit: z.boolean()}); export const primaryKeyCreateJobResult = z.object({}).nullable(); export const primaryKeyDeleteJobResult = z.object({}).nullable(); export const tableRestoreResult = z.object({tableId: z.string(), mode: z.enum(["undrop", "undropAs", "pointInTime"]), warnings: z.array(z.string())}); export const tableSwapResult = z.object({tableAStringId: z.string(), tableBStringId: z.string()}); export const sourceTableResponse = z.object({id: z.string(), uri: z.string(), project: z.object({id: z.number().int(), name: z.string()}), isAlias: z.boolean().optional()}); export const aliasFilterResponse = z.object({column: z.string(), operator: z.enum(["eq", "ne"]), values: z.array(z.string())}); export const tableCreateResponse = 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()), sourceTable: sourceTableResponse.optional(), aliasColumnsAutoSync: z.boolean().optional(), aliasFilter: aliasFilterResponse.optional(), selectSql: z.string().optional(), isPersisted: z.boolean().optional()}); export const tableUpdateJobResult = z.object({}).nullable(); export const columnCreateJobResult = z.object({}).nullable(); export const columnDeleteJobResult = z.object({}).nullable(); export const tableExportJobResponse = z.object({file: z.object({id: z.number().int()})}); export const jobResponse = z.object({id: z.string(), status: z.string(), url: z.string(), tableId: z.string().nullable(), operationName: z.string(), operationParams: z.object({}), createdTime: z.string(), startTime: z.string().nullable(), endTime: z.string().nullable(), runId: z.string().nullable(), results: z.union([bucketForceUnlinkJobResult, bucketListingUpdateJobResponse, bucketUpdateJobResult, bucketListingCreateJobResult, bucketListingDeleteResponse, bucketDeleteResponse, refreshJobResponse, batchPurgeJobResult, batchRestoreJobResult, fileDeleteJobResult, tableSnapshotCreateJobResult, tableDeleteJobResult, deleteRowsJobResult, tableExportAsyncJobResponse, primaryKeyCreateJobResult, primaryKeyDeleteJobResult, tableRestoreResult, tableSwapResult, tableCreateResponse, tableUpdateJobResult, columnCreateJobResult, columnDeleteJobResult, tableExportJobResponse]).nullable(), creatorToken: z.object({id: z.string().nullable(), description: z.string().nullable()}), actor: actorResponse.nullable(), metrics: z.object({inCompressed: z.boolean(), inBytes: z.number().int(), inBytesUncompressed: z.number().int(), outCompressed: z.boolean(), outBytes: z.number().int(), outBytesUncompressed: z.number().int()}), error: z.object({code: z.string(), message: z.string(), exceptionId: z.string(), contextParams: z.object({}).nullable(), uuid: z.string().nullable()}).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 }; /** * Share Bucket To Projects * * POST /v2/storage/buckets/{bucketId}/share-to-projects * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageBucketsBucketIdShareToProjects = async (args: CreateApiV2StorageBucketsBucketIdShareToProjectsArgs, options?: CreateApiV2StorageBucketsBucketIdShareToProjectsOptions) => { 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 (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/buckets/${encodeURIComponent(String(args.bucketId))}/share-to-projects${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(jobResponse, await res.json()) };