1 | |
2 | type Intercom = { |
3 | apiVersion: string |
4 | token: string |
5 | } |
6 | |
7 | * List all companies |
8 | * You can list companies. |
9 | */ |
10 | export async function main( |
11 | auth: Intercom, |
12 | page: string | undefined, |
13 | per_page: string | undefined, |
14 | order: string | undefined |
15 | ) { |
16 | const url = new URL(`https://api.intercom.io/companies/list`) |
17 | for (const [k, v] of [ |
18 | ['page', page], |
19 | ['per_page', per_page], |
20 | ['order', order] |
21 | ]) { |
22 | if (v !== undefined && v !== '' && k !== undefined) { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: 'POST', |
28 | headers: { |
29 | 'Intercom-Version': auth.apiVersion, |
30 | Authorization: 'Bearer ' + auth.token |
31 | }, |
32 | body: undefined |
33 | }) |
34 | if (!response.ok) { |
35 | const text = await response.text() |
36 | throw new Error(`${response.status} ${text}`) |
37 | } |
38 | return await response.json() |
39 | } |
40 |
|