1 | |
2 | type Circleci = { |
3 | token: string |
4 | } |
5 | |
6 | * List contexts |
7 | * List all contexts for an owner. |
8 | */ |
9 | export async function main( |
10 | auth: Circleci, |
11 | owner_id: string | undefined, |
12 | owner_slug: string | undefined, |
13 | owner_type: 'account' | 'organization' | undefined, |
14 | page_token: string | undefined |
15 | ) { |
16 | const url = new URL(`https://circleci.com/api/v2/context`) |
17 | for (const [k, v] of [ |
18 | ['owner-id', owner_id], |
19 | ['owner-slug', owner_slug], |
20 | ['owner-type', owner_type], |
21 | ['page-token', page_token] |
22 | ]) { |
23 | if (v !== undefined && v !== '' && k !== undefined) { |
24 | url.searchParams.append(k, v) |
25 | } |
26 | } |
27 | const response = await fetch(url, { |
28 | method: 'GET', |
29 | headers: { |
30 | 'Circle-Token': 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 |
|