1 | |
2 | type Telnyx = { |
3 | apiKey: string |
4 | } |
5 | |
6 | * List available phone numbers |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Telnyx, |
11 | filter_phone_number__starts_with_: string | undefined, |
12 | filter_phone_number__ends_with_: string | undefined, |
13 | filter_phone_number__contains_: string | undefined, |
14 | filter_locality_: string | undefined, |
15 | filter_administrative_area_: string | undefined, |
16 | filter_country_code_: string | undefined, |
17 | filter_national_destination_code_: string | undefined, |
18 | filter_rate_center_: string | undefined, |
19 | filter_phone_number_type_: |
20 | | 'local' |
21 | | 'toll_free' |
22 | | 'mobile' |
23 | | 'national' |
24 | | 'shared_cost' |
25 | | undefined, |
26 | filter_features_: string | undefined, |
27 | filter_limit_: string | undefined, |
28 | filter_best_effort_: string | undefined, |
29 | filter_quickship_: string | undefined, |
30 | filter_reservable_: string | undefined, |
31 | filter_exclude_held_numbers_: string | undefined |
32 | ) { |
33 | const url = new URL(`https://api.telnyx.com/v2/available_phone_numbers`) |
34 | for (const [k, v] of [ |
35 | ['filter[phone_number][starts_with]', filter_phone_number__starts_with_], |
36 | ['filter[phone_number][ends_with]', filter_phone_number__ends_with_], |
37 | ['filter[phone_number][contains]', filter_phone_number__contains_], |
38 | ['filter[locality]', filter_locality_], |
39 | ['filter[administrative_area]', filter_administrative_area_], |
40 | ['filter[country_code]', filter_country_code_], |
41 | ['filter[national_destination_code]', filter_national_destination_code_], |
42 | ['filter[rate_center]', filter_rate_center_], |
43 | ['filter[phone_number_type]', filter_phone_number_type_], |
44 | ['filter[features]', filter_features_], |
45 | ['filter[limit]', filter_limit_], |
46 | ['filter[best_effort]', filter_best_effort_], |
47 | ['filter[quickship]', filter_quickship_], |
48 | ['filter[reservable]', filter_reservable_], |
49 | ['filter[exclude_held_numbers]', filter_exclude_held_numbers_] |
50 | ]) { |
51 | if (v !== undefined && v !== '' && k !== undefined) { |
52 | url.searchParams.append(k, v) |
53 | } |
54 | } |
55 | const response = await fetch(url, { |
56 | method: 'GET', |
57 | headers: { |
58 | Authorization: 'Bearer ' + auth.apiKey |
59 | }, |
60 | body: undefined |
61 | }) |
62 | if (!response.ok) { |
63 | const text = await response.text() |
64 | throw new Error(`${response.status} ${text}`) |
65 | } |
66 | return await response.json() |
67 | } |
68 |
|