Edits history of script submission #11172 for ' Retrieve a visitor with User ID (intercom)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Intercom = {
    	apiVersion: string
    	token: string
    }
    /**
     * Retrieve a visitor with User ID
     * You can fetch the details of a single visitor.
     */
    export async function main(auth: Intercom, user_id: string | undefined) {
    	const url = new URL(`https://api.intercom.io/visitors`)
    	for (const [k, v] of [['user_id', user_id]]) {
    		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