1 | |
2 | type Telnyx = { |
3 | apiKey: string |
4 | } |
5 | |
6 | * List available phone number blocks |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Telnyx, |
11 | filter_locality_: string | undefined, |
12 | filter_country_code_: string | undefined, |
13 | filter_national_destination_code_: string | undefined, |
14 | filter_phone_number_type_: |
15 | | 'local' |
16 | | 'toll_free' |
17 | | 'mobile' |
18 | | 'national' |
19 | | 'shared_cost' |
20 | | undefined |
21 | ) { |
22 | const url = new URL(`https://api.telnyx.com/v2/available_phone_number_blocks`) |
23 | for (const [k, v] of [ |
24 | ['filter[locality]', filter_locality_], |
25 | ['filter[country_code]', filter_country_code_], |
26 | ['filter[national_destination_code]', filter_national_destination_code_], |
27 | ['filter[phone_number_type]', filter_phone_number_type_] |
28 | ]) { |
29 | if (v !== undefined && v !== '' && k !== undefined) { |
30 | url.searchParams.append(k, v) |
31 | } |
32 | } |
33 | const response = await fetch(url, { |
34 | method: 'GET', |
35 | headers: { |
36 | Authorization: 'Bearer ' + auth.apiKey |
37 | }, |
38 | body: undefined |
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 |
|