import {z} from 'zod' export type TaskVcs = {reference: string, type: 'branch' | 'tag' | 'commit'}; export type RunTaskRequestTrigger = {type: 'manual' | 'pull_request' | 'push' | 'schedule', source: 'Firefly' | 'VCS'}; export type RunTaskRequest = {description?: string | undefined, vcs: TaskVcs, trigger: RunTaskRequestTrigger, taskId?: string | undefined, cmdArguments?: string | undefined}; export type CreateApiRunnersWorkspacesWorkspaceIdTasksDestroyArgs = {workspaceId: string, body: RunTaskRequest}; export type CreateApiRunnersWorkspacesWorkspaceIdTasksDestroyOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const taskError = z.object({type: z.literal("UNEXPECTED").optional(), description: z.string().optional()}); export const taskVcs = z.object({reference: z.string(), type: z.enum(["branch", "tag", "commit"])}); export const taskTrigger = z.object({type: z.enum(["manual", "pull_request", "push", "scheduled"]).optional()}); export const taskResponse = z.object({accountId: z.string().optional(), id: z.string(), status: z.enum(["pending", "running", "success", "failed", "canceled"]), error: taskError.optional(), currentStep: z.enum(["plan", "post_plan", "apply", "post_apply"]).optional(), workspaceId: z.string(), vcs: taskVcs.optional(), trigger: taskTrigger.optional(), type: z.enum(["plan", "apply", "destroy"]), cmdArguments: z.string().optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Destroy workspace infrastructure resources * * POST /runners/workspaces/{workspaceId}/tasks/destroy */ export const createApiRunnersWorkspacesWorkspaceIdTasksDestroy = async (args: CreateApiRunnersWorkspacesWorkspaceIdTasksDestroyArgs, options?: CreateApiRunnersWorkspacesWorkspaceIdTasksDestroyOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/runners/workspaces/${encodeURIComponent(String(args.workspaceId))}/tasks/destroy${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(taskResponse, await res.json()) };