1 | |
2 | type Telnyx = { |
3 | apiKey: string |
4 | } |
5 | |
6 | * Get all SIM cards |
7 | * Get all SIM cards belonging to the user that match the given filters. |
8 | */ |
9 | export async function main( |
10 | auth: Telnyx, |
11 | page_number_: string | undefined, |
12 | page_size_: string | undefined, |
13 | include_sim_card_group: string | undefined, |
14 | filter_sim_card_group_id_: string | undefined, |
15 | filter_tags_: string | undefined, |
16 | filter_iccid_: string | undefined, |
17 | filter_status_: string | undefined, |
18 | sort: 'current_billing_period_consumed_data.amount' | undefined |
19 | ) { |
20 | const url = new URL(`https://api.telnyx.com/v2/sim_cards`) |
21 | for (const [k, v] of [ |
22 | ['page[number]', page_number_], |
23 | ['page[size]', page_size_], |
24 | ['include_sim_card_group', include_sim_card_group], |
25 | ['filter[sim_card_group_id]', filter_sim_card_group_id_], |
26 | ['filter[tags]', filter_tags_], |
27 | ['filter[iccid]', filter_iccid_], |
28 | ['filter[status]', filter_status_], |
29 | ['sort', sort] |
30 | ]) { |
31 | if (v !== undefined && v !== '' && k !== undefined) { |
32 | url.searchParams.append(k, v) |
33 | } |
34 | } |
35 | const response = await fetch(url, { |
36 | method: 'GET', |
37 | headers: { |
38 | Authorization: 'Bearer ' + auth.apiKey |
39 | }, |
40 | body: undefined |
41 | }) |
42 | if (!response.ok) { |
43 | const text = await response.text() |
44 | throw new Error(`${response.status} ${text}`) |
45 | } |
46 | return await response.json() |
47 | } |
48 |
|