1 | |
2 | type Kustomer = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create a workflow |
7 | * Creats a new workflow. |
8 | */ |
9 | export async function main( |
10 | auth: Kustomer, |
11 | body: { |
12 | name: string; |
13 | replaces?: string; |
14 | description?: string; |
15 | meta?: {}; |
16 | trigger?: |
17 | | { |
18 | id: string; |
19 | eventName?: string; |
20 | callable: false | true; |
21 | schema: {}; |
22 | meta?: {}; |
23 | transitions: { |
24 | meta?: {}; |
25 | target: string; |
26 | condition: { |
27 | op: |
28 | | "and" |
29 | | "or" |
30 | | "exists" |
31 | | "dne" |
32 | | "true" |
33 | | "false" |
34 | | "eq" |
35 | | "neq" |
36 | | "gt" |
37 | | "gte" |
38 | | "lt" |
39 | | "lte" |
40 | | "lt-date" |
41 | | "gt-date" |
42 | | "contains" |
43 | | "not-contains"; |
44 | values: unknown[]; |
45 | }; |
46 | }[]; |
47 | } |
48 | | { |
49 | id: string; |
50 | appVersion?: string; |
51 | meta?: {}; |
52 | eventName: string; |
53 | transitions: { |
54 | meta?: {}; |
55 | target: string; |
56 | condition: { |
57 | op: |
58 | | "and" |
59 | | "or" |
60 | | "exists" |
61 | | "dne" |
62 | | "true" |
63 | | "false" |
64 | | "eq" |
65 | | "neq" |
66 | | "gt" |
67 | | "gte" |
68 | | "lt" |
69 | | "lte" |
70 | | "lt-date" |
71 | | "gt-date" |
72 | | "contains" |
73 | | "not-contains"; |
74 | values: unknown[]; |
75 | }; |
76 | }[]; |
77 | }; |
78 | steps?: { |
79 | id: string; |
80 | appVersion?: string; |
81 | meta?: {}; |
82 | action?: string; |
83 | params?: {}; |
84 | transitions: { |
85 | meta?: {}; |
86 | target: string; |
87 | condition: { |
88 | op: |
89 | | "and" |
90 | | "or" |
91 | | "exists" |
92 | | "dne" |
93 | | "true" |
94 | | "false" |
95 | | "eq" |
96 | | "neq" |
97 | | "gt" |
98 | | "gte" |
99 | | "lt" |
100 | | "lte" |
101 | | "lt-date" |
102 | | "gt-date" |
103 | | "contains" |
104 | | "not-contains"; |
105 | values: unknown[]; |
106 | }; |
107 | }[]; |
108 | errorCases?: { |
109 | condition: { op: "and" | "or"; values: unknown[] }; |
110 | errorAction: { |
111 | type?: |
112 | | "continue" |
113 | | "retry-workflow" |
114 | | "retry-action" |
115 | | "stop-processing"; |
116 | }; |
117 | }[]; |
118 | }[]; |
119 | enabled?: false | true; |
120 | logging?: false | true; |
121 | }, |
122 | ) { |
123 | const url = new URL(`https://api.kustomerapp.com/v1/workflows`); |
124 |
|
125 | const response = await fetch(url, { |
126 | method: "POST", |
127 | headers: { |
128 | "Content-Type": "application/json", |
129 | Authorization: "Bearer " + auth.apiKey, |
130 | }, |
131 | body: JSON.stringify(body), |
132 | }); |
133 | if (!response.ok) { |
134 | const text = await response.text(); |
135 | throw new Error(`${response.status} ${text}`); |
136 | } |
137 | return await response.json(); |
138 | } |
139 |
|