Edits history of script submission #8994 for ' Get Freshdesk Contacts (freshdesk)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Freshdesk = {
    	apiKey: string
    	baseUrl: string
    }
    
    export async function main(
    	resource: Freshdesk,
    	filterType?: 'email' | 'mobile' | 'phone' | 'company_id' | 'updated_since',
    	filterValue?: string,
    	filterStatus?: 'blocked' | 'deleted' | 'unverified' | 'verified',
    	perPage: number = 0
    ) {
    	const baseUrl = resource.baseUrl.replace(/\/$/, '') // Remove trailing slash from base URL.
    	const encodedKey = btoa(`${resource.apiKey}:X`) // Base64 encode the API key with a colon and 'X'.
    
    	// Validate filterValue if filterType is provided
    	if (filterType && !filterValue) {
    		throw new Error('filterValue must be provided if filterType is specified.')
    	}
    
    	// Construct the query parameters.
    	const queryParams = new URLSearchParams()
    	if (filterType && filterValue) {
    		queryParams.append(filterType, filterValue)
    	}
    	if (filterStatus) {
    		queryParams.append('state', filterStatus)
    	}
    	if (perPage > 0) {
    		queryParams.append('per_page', perPage.toString())
    	}
    
    	const endpoint = `${baseUrl}/api/v2/contacts?${queryParams.toString()}`
    
    	const response = await fetch(endpoint, {
    		method: 'GET',
    		headers: {
    			Authorization: `Basic ${encodedKey}`,
    			'Content-Type': 'application/json'
    		}
    	})
    
    	if (!response.ok) {
    		console.log(response)
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    	return data
    }
    

    Submitted by hugo697 652 days ago