1 | |
2 | type Telnyx = { |
3 | apiKey: string |
4 | } |
5 | |
6 | * List phone numbers |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Telnyx, |
11 | page_number_: string | undefined, |
12 | page_size_: string | undefined, |
13 | filter_tag_: string | undefined, |
14 | filter_phone_number_: string | undefined, |
15 | filter_status_: |
16 | | 'purchase_pending' |
17 | | 'purchase_failed' |
18 | | 'port_pending' |
19 | | 'active' |
20 | | 'deleted' |
21 | | 'port_failed' |
22 | | 'emergency_only' |
23 | | 'ported_out' |
24 | | 'port_out_pending' |
25 | | undefined, |
26 | filter_connection_id_: string | undefined, |
27 | filter_voice_connection_name__contains_: string | undefined, |
28 | filter_voice_connection_name__starts_with_: string | undefined, |
29 | filter_voice_connection_name__ends_with_: string | undefined, |
30 | filter_voice_connection_name__eq_: string | undefined, |
31 | filter_voice_usage_payment_method_: 'pay-per-minute' | 'channel' | undefined, |
32 | filter_billing_group_id_: string | undefined, |
33 | filter_emergency_address_id_: string | undefined, |
34 | filter_customer_reference_: string | undefined, |
35 | filter_number_type__eq_: |
36 | | 'local' |
37 | | 'national' |
38 | | 'toll_free' |
39 | | 'mobile' |
40 | | 'shared_cost' |
41 | | undefined, |
42 | sort: 'purchased_at' | 'phone_number' | 'connection_name' | 'usage_payment_method' | undefined |
43 | ) { |
44 | const url = new URL(`https://api.telnyx.com/v2/phone_numbers`) |
45 | for (const [k, v] of [ |
46 | ['page[number]', page_number_], |
47 | ['page[size]', page_size_], |
48 | ['filter[tag]', filter_tag_], |
49 | ['filter[phone_number]', filter_phone_number_], |
50 | ['filter[status]', filter_status_], |
51 | ['filter[connection_id]', filter_connection_id_], |
52 | ['filter[voice.connection_name][contains]', filter_voice_connection_name__contains_], |
53 | ['filter[voice.connection_name][starts_with]', filter_voice_connection_name__starts_with_], |
54 | ['filter[voice.connection_name][ends_with]', filter_voice_connection_name__ends_with_], |
55 | ['filter[voice.connection_name][eq]', filter_voice_connection_name__eq_], |
56 | ['filter[voice.usage_payment_method]', filter_voice_usage_payment_method_], |
57 | ['filter[billing_group_id]', filter_billing_group_id_], |
58 | ['filter[emergency_address_id]', filter_emergency_address_id_], |
59 | ['filter[customer_reference]', filter_customer_reference_], |
60 | ['filter[number_type][eq]', filter_number_type__eq_], |
61 | ['sort', sort] |
62 | ]) { |
63 | if (v !== undefined && v !== '' && k !== undefined) { |
64 | url.searchParams.append(k, v) |
65 | } |
66 | } |
67 | const response = await fetch(url, { |
68 | method: 'GET', |
69 | headers: { |
70 | Authorization: 'Bearer ' + auth.apiKey |
71 | }, |
72 | body: undefined |
73 | }) |
74 | if (!response.ok) { |
75 | const text = await response.text() |
76 | throw new Error(`${response.status} ${text}`) |
77 | } |
78 | return await response.json() |
79 | } |
80 |
|