import {z} from 'zod' export type GroupWorkspaceMapping = {groupName?: string | undefined, role?: ('admin' | 'member') | undefined}; export type WorkspaceHipaaUnsafe = {enabled?: boolean | undefined, updatedAt?: string | undefined, updatedBy?: string | undefined}; export type WorkspaceHipaaInfo = {accountEnabled?: boolean | undefined, unsafe?: WorkspaceHipaaUnsafe | undefined}; export type MetadataLabels = {[key: string]: string}; export type WorkspaceSandboxSettings = {disableProcessLogging?: boolean | undefined}; export type WorkspaceRuntime = {generation?: string | undefined, sandbox?: WorkspaceSandboxSettings | undefined}; export type Workspace = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, accountId?: string | undefined, displayName?: string | undefined, groupMappings?: Array | undefined, hipaaInfo?: WorkspaceHipaaInfo | undefined, id?: string | undefined, labels?: MetadataLabels | undefined, name?: string | undefined, region?: string | undefined, resourceCounts?: {[key: string]: number} | undefined, runtime?: WorkspaceRuntime | undefined, status?: ('created' | 'account_binded' | 'account_configured' | 'workspace_configured' | 'ready' | 'error') | undefined, statusReason?: string | undefined}; export type CreateApiWorkspacesArgs = {body: Workspace}; export type CreateApiWorkspacesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const groupWorkspaceMapping = z.object({groupName: z.string().optional(), role: z.enum(["admin", "member"]).optional()}); export const workspaceHipaaUnsafe = z.object({enabled: z.boolean().optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional()}); export const workspaceHipaaInfo = z.object({accountEnabled: z.boolean().optional(), unsafe: workspaceHipaaUnsafe.optional()}); export const metadataLabels = z.record(z.string(), z.string()); export const workspaceSandboxSettings = z.object({disableProcessLogging: z.boolean().optional()}); export const workspaceRuntime = z.object({generation: z.string().optional(), sandbox: workspaceSandboxSettings.optional()}); export const workspace = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), accountId: z.string().optional(), displayName: z.string().optional(), groupMappings: z.array(groupWorkspaceMapping).optional(), hipaaInfo: workspaceHipaaInfo.optional(), id: z.string().optional(), labels: metadataLabels.optional(), name: z.string().optional(), region: z.string().optional(), resourceCounts: z.record(z.string(), z.number().int()).optional(), runtime: workspaceRuntime.optional(), status: z.enum(["created", "account_binded", "account_configured", "workspace_configured", "ready", "error"]).optional(), statusReason: z.string().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 }; /** * Create workspace tenant * * POST /workspaces * * Also accepts auth: ApiKeyAuth */ export const createApiWorkspaces = async (args: CreateApiWorkspacesArgs, options?: CreateApiWorkspacesOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(workspace, await res.json()) };