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