1 | |
2 | type Gorgias = { |
3 | username: string; |
4 | apiKey: string; |
5 | domain: string; |
6 | }; |
7 | |
8 | * Create a rule |
9 | * |
10 | */ |
11 | export async function main( |
12 | auth: Gorgias, |
13 | body: { |
14 | code: string; |
15 | code_ast?: {}; |
16 | deactivated_datetime?: string; |
17 | description?: string; |
18 | event_types?: |
19 | | "ticket-created" |
20 | | "ticket-updated" |
21 | | "ticket-message-created" |
22 | | "ticket-assigned" |
23 | | "ticket-self-unsnoozed"; |
24 | name: string; |
25 | priority?: number; |
26 | }, |
27 | ) { |
28 | const url = new URL(`https://${auth.domain}.gorgias.com/api/rules`); |
29 |
|
30 | const response = await fetch(url, { |
31 | method: "POST", |
32 | headers: { |
33 | "Content-Type": "application/json", |
34 | Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`), |
35 | }, |
36 | body: JSON.stringify(body), |
37 | }); |
38 | if (!response.ok) { |
39 | const text = await response.text(); |
40 | throw new Error(`${response.status} ${text}`); |
41 | } |
42 | return await response.json(); |
43 | } |
44 |
|