Edits history of script submission #13372 for ' List available phone number blocks (telnyx)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Telnyx = {
    	apiKey: string
    }
    /**
     * List available phone number blocks
     *
     */
    export async function main(
    	auth: Telnyx,
    	filter_locality_: string | undefined,
    	filter_country_code_: string | undefined,
    	filter_national_destination_code_: string | undefined,
    	filter_phone_number_type_:
    		| 'local'
    		| 'toll_free'
    		| 'mobile'
    		| 'national'
    		| 'shared_cost'
    		| undefined
    ) {
    	const url = new URL(`https://api.telnyx.com/v2/available_phone_number_blocks`)
    	for (const [k, v] of [
    		['filter[locality]', filter_locality_],
    		['filter[country_code]', filter_country_code_],
    		['filter[national_destination_code]', filter_national_destination_code_],
    		['filter[phone_number_type]', filter_phone_number_type_]
    	]) {
    		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