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