Edits history of script submission #13402 for ' List phone numbers (telnyx)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Telnyx = {
    	apiKey: string
    }
    /**
     * List phone numbers
     *
     */
    export async function main(
    	auth: Telnyx,
    	page_number_: string | undefined,
    	page_size_: string | undefined,
    	filter_tag_: string | undefined,
    	filter_phone_number_: string | undefined,
    	filter_status_:
    		| 'purchase_pending'
    		| 'purchase_failed'
    		| 'port_pending'
    		| 'active'
    		| 'deleted'
    		| 'port_failed'
    		| 'emergency_only'
    		| 'ported_out'
    		| 'port_out_pending'
    		| undefined,
    	filter_connection_id_: string | undefined,
    	filter_voice_connection_name__contains_: string | undefined,
    	filter_voice_connection_name__starts_with_: string | undefined,
    	filter_voice_connection_name__ends_with_: string | undefined,
    	filter_voice_connection_name__eq_: string | undefined,
    	filter_voice_usage_payment_method_: 'pay-per-minute' | 'channel' | undefined,
    	filter_billing_group_id_: string | undefined,
    	filter_emergency_address_id_: string | undefined,
    	filter_customer_reference_: string | undefined,
    	filter_number_type__eq_:
    		| 'local'
    		| 'national'
    		| 'toll_free'
    		| 'mobile'
    		| 'shared_cost'
    		| undefined,
    	sort: 'purchased_at' | 'phone_number' | 'connection_name' | 'usage_payment_method' | undefined
    ) {
    	const url = new URL(`https://api.telnyx.com/v2/phone_numbers`)
    	for (const [k, v] of [
    		['page[number]', page_number_],
    		['page[size]', page_size_],
    		['filter[tag]', filter_tag_],
    		['filter[phone_number]', filter_phone_number_],
    		['filter[status]', filter_status_],
    		['filter[connection_id]', filter_connection_id_],
    		['filter[voice.connection_name][contains]', filter_voice_connection_name__contains_],
    		['filter[voice.connection_name][starts_with]', filter_voice_connection_name__starts_with_],
    		['filter[voice.connection_name][ends_with]', filter_voice_connection_name__ends_with_],
    		['filter[voice.connection_name][eq]', filter_voice_connection_name__eq_],
    		['filter[voice.usage_payment_method]', filter_voice_usage_payment_method_],
    		['filter[billing_group_id]', filter_billing_group_id_],
    		['filter[emergency_address_id]', filter_emergency_address_id_],
    		['filter[customer_reference]', filter_customer_reference_],
    		['filter[number_type][eq]', filter_number_type__eq_],
    		['sort', sort]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago