1 | |
2 | type Telnyx = { |
3 | apiKey: string |
4 | } |
5 | |
6 | * Create an inventory coverage request |
7 | * Creates an inventory coverage request. If locality, npa or national_destination_code is used in groupBy, and no region or locality filters are used, the whole paginated set is returned. |
8 | */ |
9 | export async function main( |
10 | auth: Telnyx, |
11 | filter_npa_: string | undefined, |
12 | filter_nxx_: string | undefined, |
13 | filter_administrative_area_: string | undefined, |
14 | filter_phone_number_type_: |
15 | | 'local' |
16 | | 'toll_free' |
17 | | 'national' |
18 | | 'mobile' |
19 | | 'landline' |
20 | | 'shared_cost' |
21 | | undefined, |
22 | filter_country_code_: |
23 | | 'AT' |
24 | | 'AU' |
25 | | 'BE' |
26 | | 'BG' |
27 | | 'CA' |
28 | | 'CH' |
29 | | 'CN' |
30 | | 'CY' |
31 | | 'CZ' |
32 | | 'DE' |
33 | | 'DK' |
34 | | 'EE' |
35 | | 'ES' |
36 | | 'FI' |
37 | | 'FR' |
38 | | 'GB' |
39 | | 'GR' |
40 | | 'HU' |
41 | | 'HR' |
42 | | 'IE' |
43 | | 'IT' |
44 | | 'LT' |
45 | | 'LU' |
46 | | 'LV' |
47 | | 'NL' |
48 | | 'NZ' |
49 | | 'MX' |
50 | | 'NO' |
51 | | 'PL' |
52 | | 'PT' |
53 | | 'RO' |
54 | | 'SE' |
55 | | 'SG' |
56 | | 'SI' |
57 | | 'SK' |
58 | | 'US' |
59 | | undefined, |
60 | filter_count_: string | undefined, |
61 | filter_features_: string | undefined, |
62 | filter_groupBy_: 'locality' | 'npa' | 'national_destination_code' | undefined |
63 | ) { |
64 | const url = new URL(`https://api.telnyx.com/v2/inventory_coverage`) |
65 | for (const [k, v] of [ |
66 | ['filter[npa]', filter_npa_], |
67 | ['filter[nxx]', filter_nxx_], |
68 | ['filter[administrative_area]', filter_administrative_area_], |
69 | ['filter[phone_number_type]', filter_phone_number_type_], |
70 | ['filter[country_code]', filter_country_code_], |
71 | ['filter[count]', filter_count_], |
72 | ['filter[features]', filter_features_], |
73 | ['filter[groupBy]', filter_groupBy_] |
74 | ]) { |
75 | if (v !== undefined && v !== '' && k !== undefined) { |
76 | url.searchParams.append(k, v) |
77 | } |
78 | } |
79 | const response = await fetch(url, { |
80 | method: 'GET', |
81 | headers: { |
82 | Authorization: 'Bearer ' + auth.apiKey |
83 | }, |
84 | body: undefined |
85 | }) |
86 | if (!response.ok) { |
87 | const text = await response.text() |
88 | throw new Error(`${response.status} ${text}`) |
89 | } |
90 | return await response.json() |
91 | } |
92 |
|