1 | |
2 | type Gorgias = { |
3 | username: string; |
4 | apiKey: string; |
5 | domain: string; |
6 | }; |
7 | |
8 | * Create a user |
9 | * |
10 | */ |
11 | export async function main( |
12 | auth: Gorgias, |
13 | body: { |
14 | bio?: string; |
15 | email?: string; |
16 | external_id?: string; |
17 | meta?: {}; |
18 | name?: string; |
19 | roles: { |
20 | name?: |
21 | | "admin" |
22 | | "agent" |
23 | | "basic-agent" |
24 | | "lite-agent" |
25 | | "observer-agent" |
26 | | "user"; |
27 | }[]; |
28 | }, |
29 | ) { |
30 | const url = new URL(`https://${auth.domain}.gorgias.com/api/users`); |
31 |
|
32 | const response = await fetch(url, { |
33 | method: "POST", |
34 | headers: { |
35 | "Content-Type": "application/json", |
36 | Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`), |
37 | }, |
38 | body: JSON.stringify(body), |
39 | }); |
40 | if (!response.ok) { |
41 | const text = await response.text(); |
42 | throw new Error(`${response.status} ${text}`); |
43 | } |
44 | return await response.json(); |
45 | } |
46 |
|