import {z} from 'zod' export type CreateWorkspaceRequest = {backend?: ('snowflake' | 'bigquery' | 'abs' | null) | undefined, backendSize?: (string | null) | undefined, readOnlyStorageAccess?: (boolean | null) | undefined, networkPolicy?: ('system' | 'user' | 'not-defined' | null) | undefined, loginType?: ('null' | 'default' | 'snowflake-legacy-service' | 'snowflake-person-sso' | 'snowflake-person-keypair' | 'snowflake-service-keypair' | 'none' | 'governed' | null) | undefined, publicKey?: (string | null) | undefined, useCase?: ('normal' | 'reader' | null) | undefined, configurationVersion?: (number | null) | undefined}; export type CreateApiV2StorageBranchBranchIdWorkspacesArgs = {componentId?: string | undefined, configurationId?: string | undefined, async?: boolean | undefined, branchId: string, 'X-KBC-Backend'?: ({context?: (string | null) | undefined}) | undefined, body: CreateWorkspaceRequest}; export type CreateApiV2StorageBranchBranchIdWorkspacesOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 workspaceCreatedResponse = z.object({id: z.number().int(), type: z.enum(["file", "table"]), name: z.string().nullable(), component: z.string().nullable(), configurationId: z.string().nullable(), configurationVersion: z.number().int().nullable(), created: z.string(), connection: z.union([z.object({backend: z.enum(["snowflake", "synapse", "exasol", "teradata", "bigquery", "postgres", "ducklake"]), region: z.string(), host: z.string(), database: z.string().nullable(), schema: z.string().nullable(), warehouse: z.string().nullable(), user: z.string().optional(), loginType: z.enum(["default", "snowflake-legacy-service", "snowflake-person-sso", "snowflake-person-keypair", "snowflake-service-keypair", "none", "governed"]), ssoLoginAvailable: z.boolean(), account: z.string().optional(), role: z.string().nullable().optional(), password: z.string().optional(), credentials: z.object({type: z.string().optional(), project_id: z.string().optional(), private_key_id: z.string().optional(), private_key: z.string().optional(), client_email: z.string().optional(), client_id: z.string().optional(), auth_uri: z.string().optional(), token_uri: z.string().optional(), auth_provider_x509_cert_url: z.string().optional(), client_x509_cert_url: z.string().optional()}).optional()}), z.object({backend: z.string(), container: z.string().nullable(), region: z.string(), connectionString: z.string().optional()})]), backendSize: z.string().nullable(), statementTimeoutSeconds: z.number().int(), creatorToken: z.object({id: z.number().int(), description: z.string().nullable()}), actor: actorResponse.nullable(), readOnlyStorageAccess: z.boolean(), platformUsageType: z.enum(["service", "user", "reader"]).nullable(), state: z.enum(["active", "deactivated"]), lastActivityAt: z.string().nullable(), warnAt: z.string().nullable(), deactivateAt: z.string().nullable(), deleteAt: z.string().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 }; /** * Create workspace * * POST /v2/storage/branch/{branchId}/workspaces * * Also accepts auth: ManageKey, BearerAuth */ export const createApiV2StorageBranchBranchIdWorkspaces = async (args: CreateApiV2StorageBranchBranchIdWorkspacesArgs, options?: CreateApiV2StorageBranchBranchIdWorkspacesOptions) => { 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 (args['X-KBC-Backend'] != null) headers.set("X-KBC-Backend", JSON.stringify(args['X-KBC-Backend'])) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/workspaces${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(workspaceCreatedResponse, await res.json()) };