import {z} from 'zod' export type CoreEvent = {canaryRevision?: string | undefined, message?: string | undefined, revision?: string | undefined, status?: string | undefined, time?: string | undefined, type?: string | undefined}; export type CoreEvents = Array; export type PreviewMetadata = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, displayName?: string | undefined, name: string, resourceName?: string | undefined, resourceType?: string | undefined, workspace?: string | undefined}; export type PreviewSpec = {customDomain?: string | undefined, expires?: string | undefined, port?: number | undefined, prefixUrl?: string | undefined, public?: boolean | undefined, region?: string | undefined, requestHeaders?: {[key: string]: string} | undefined, responseHeaders?: {[key: string]: string} | undefined, ttl?: string | undefined, url?: string | undefined}; export type Status = 'DELETING' | 'TERMINATED' | 'FAILED' | 'DEACTIVATED' | 'DEACTIVATING' | 'UPLOADING' | 'BUILDING' | 'DEPLOYING' | 'DEPLOYED' | 'BUILT'; export type Preview = {events?: CoreEvents | undefined, metadata: PreviewMetadata, spec: PreviewSpec, status?: Status | undefined}; export type CreateApiSandboxesSandboxNamePreviewsArgs = {force?: boolean | undefined, body: Preview}; export type CreateApiSandboxesSandboxNamePreviewsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const coreEvent = z.object({canaryRevision: z.string().optional(), message: z.string().optional(), revision: z.string().optional(), status: z.string().optional(), time: z.string().optional(), type: z.string().optional()}); export const coreEvents = z.array(coreEvent); export const previewMetadata = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), displayName: z.string().optional(), name: z.string(), resourceName: z.string().optional(), resourceType: z.string().optional(), workspace: z.string().optional()}); export const previewSpec = z.object({customDomain: z.string().optional(), expires: z.string().optional(), port: z.number().int().optional(), prefixUrl: z.string().optional(), public: z.boolean().optional(), region: z.string().optional(), requestHeaders: z.record(z.string(), z.string()).optional(), responseHeaders: z.record(z.string(), z.string()).optional(), ttl: z.string().optional(), url: z.string().optional()}); export const status = z.enum(["DELETING", "TERMINATED", "FAILED", "DEACTIVATED", "DEACTIVATING", "UPLOADING", "BUILDING", "DEPLOYING", "DEPLOYED", "BUILT"]); export const preview = z.object({events: coreEvents.optional(), metadata: previewMetadata, spec: previewSpec, status: status.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 Sandbox Preview * * POST /sandboxes/{sandboxName}/previews * * Also accepts auth: ApiKeyAuth */ export const createApiSandboxesSandboxNamePreviews = async (args: CreateApiSandboxesSandboxNamePreviewsArgs, options?: CreateApiSandboxesSandboxNamePreviewsOptions) => { 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 (args.force != null) query.append("force", String(args.force)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/sandboxes/${encodeURIComponent(String(args.sandboxName))}/previews${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(preview, await res.json()) };