Edits history of script submission #10327 for ' Retrieves all contacts in a Xero organisation (xero)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xero = {
    	token: string
    }
    /**
     * Retrieves all contacts in a Xero organisation
     *
     */
    export async function main(
    	auth: Xero,
    	where: string | undefined,
    	order: string | undefined,
    	IDs: string | undefined,
    	page: string | undefined,
    	includeArchived: string | undefined,
    	summaryOnly: string | undefined,
    	searchTerm: string | undefined,
    	pageSize: string | undefined,
    	xero_tenant_id: string,
    	If_Modified_Since: string
    ) {
    	const url = new URL(`https://api.xero.com/api.xro/2.0/Contacts`)
    	for (const [k, v] of [
    		['where', where],
    		['order', order],
    		['IDs', IDs],
    		['page', page],
    		['includeArchived', includeArchived],
    		['summaryOnly', summaryOnly],
    		['searchTerm', searchTerm],
    		['pageSize', pageSize]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Accept: 'application/json',
    			'xero-tenant-id': xero_tenant_id,
    			'If-Modified-Since': If_Modified_Since,
    			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 515 days ago