|
| 1 | +import { BaseResource } from "../BaseResource"; |
| 2 | +import { Configuration } from "../../Configuration"; |
| 3 | +import { HttpError } from "../../errors"; |
| 4 | +import { CreateWorkflowTransition, UpdateWorkflowTransition, WorkflowTransition } from "../../models/Workflow"; |
| 5 | + |
| 6 | +/** |
| 7 | + * WorkflowTransitions sub-resource |
| 8 | + * Manages state transitions within a workflow |
| 9 | + */ |
| 10 | +export class Transitions extends BaseResource { |
| 11 | + constructor(config: Configuration) { |
| 12 | + super(config); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * List all state transitions for a workflow |
| 17 | + */ |
| 18 | + async list(workspaceSlug: string, projectId: string, workflowId: string): Promise<WorkflowTransition[]> { |
| 19 | + const data = await this.get<WorkflowTransition[] | { results: WorkflowTransition[] }>( |
| 20 | + `/workspaces/${workspaceSlug}/projects/${projectId}/workflows/${workflowId}/state-transitions/` |
| 21 | + ); |
| 22 | + return Array.isArray(data) ? data : data.results; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a state transition for a workflow. |
| 27 | + * Returns null if the transition already exists (HTTP 400 "already exists"). |
| 28 | + */ |
| 29 | + async create( |
| 30 | + workspaceSlug: string, |
| 31 | + projectId: string, |
| 32 | + workflowId: string, |
| 33 | + data: CreateWorkflowTransition |
| 34 | + ): Promise<WorkflowTransition | null> { |
| 35 | + try { |
| 36 | + return await this.post<WorkflowTransition>( |
| 37 | + `/workspaces/${workspaceSlug}/projects/${projectId}/workflows/${workflowId}/state-transitions/`, |
| 38 | + data |
| 39 | + ); |
| 40 | + } catch (error) { |
| 41 | + if (error instanceof HttpError && error.statusCode === 400) { |
| 42 | + const response = error.response as unknown; |
| 43 | + const body = |
| 44 | + typeof response === "string" |
| 45 | + ? response.toLowerCase() |
| 46 | + : typeof response === "object" && |
| 47 | + response !== null && |
| 48 | + "detail" in response && |
| 49 | + typeof (response as { detail: unknown }).detail === "string" |
| 50 | + ? (response as { detail: string }).detail.toLowerCase() |
| 51 | + : ""; |
| 52 | + if (body.includes("already exists")) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | + throw error; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Update a workflow state transition |
| 62 | + */ |
| 63 | + async update( |
| 64 | + workspaceSlug: string, |
| 65 | + projectId: string, |
| 66 | + workflowId: string, |
| 67 | + transitionId: string, |
| 68 | + data: UpdateWorkflowTransition |
| 69 | + ): Promise<WorkflowTransition> { |
| 70 | + return this.patch<WorkflowTransition>( |
| 71 | + `/workspaces/${workspaceSlug}/projects/${projectId}/workflows/${workflowId}/state-transitions/${transitionId}/`, |
| 72 | + data |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Delete a workflow state transition |
| 78 | + */ |
| 79 | + async del(workspaceSlug: string, projectId: string, workflowId: string, transitionId: string): Promise<void> { |
| 80 | + return this.httpDelete( |
| 81 | + `/workspaces/${workspaceSlug}/projects/${projectId}/workflows/${workflowId}/state-transitions/${transitionId}/` |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments