Edits history of script submission #11175 for ' Retrieve companies (intercom)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Intercom = {
    	apiVersion: string
    	token: string
    }
    /**
     * Retrieve companies
     * You can fetch a single company by passing in `company_id` or `name`.
    
      `https://api.intercom.io/companies?name={name}`
    
      `https://api.intercom.io/companies?company_id={company_id}`
    
    You can fetch all companies and filter by `segment_id` or `tag_id` as a query parameter.
    
      `https://api.intercom.io/companies?tag_id={tag_id}`
    
      `https://api.intercom.io/companies?segment_id={segment_id}`
    
     */
    export async function main(
    	auth: Intercom,
    	name: string | undefined,
    	company_id: string | undefined,
    	tag_id: string | undefined,
    	segment_id: string | undefined,
    	page: string | undefined,
    	per_page: string | undefined
    ) {
    	const url = new URL(`https://api.intercom.io/companies`)
    	for (const [k, v] of [
    		['name', name],
    		['company_id', company_id],
    		['tag_id', tag_id],
    		['segment_id', segment_id],
    		['page', page],
    		['per_page', per_page]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			'Intercom-Version': auth.apiVersion,
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 536 days ago