Edits history of script submission #14164 for ' Get Employees (bamboo_hr)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Get Employees
     * Retrieve a paginated list of employees with optional filtering and sorting. Returns a fixed set of simple employee fields that support efficient filter and sort operations. For more complex employee data, use the single-employee endpoint or the Datasets API for bulk queries.
     */
    export async function main(auth: RT.BambooHr, sort?: string | undefined) {
    	const url = new URL(`https://${auth.companyDomain}.bamboohr.com/api/v1/employees`)
    	for (const [k, v] of [['sort', sort]]) {
    		if (v !== undefined && v !== '') {
    			url.searchParams.append(k, v)
    		}
    	}
    	encodeParams({ filter, page }).forEach((v, k) => {
    		if (v !== undefined && v !== '') {
    			url.searchParams.append(k, v)
    		}
    	})
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.apiKey}:x`)
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    
    function encodeParams(o: any) {
    	function iter(o: any, path: string) {
    		if (Array.isArray(o)) {
    			o.forEach(function (a) {
    				iter(a, path + '[]')
    			})
    			return
    		}
    		if (o !== null && typeof o === 'object') {
    			Object.keys(o).forEach(function (k) {
    				iter(o[k], path + '[' + k + ']')
    			})
    			return
    		}
    		data.push(path + '=' + o)
    	}
    	const data: string[] = []
    	Object.keys(o).forEach(function (k) {
    		if (o[k] !== undefined) {
    			iter(o[k], k)
    		}
    	})
    	return new URLSearchParams(data.join('&'))
    }
    

    Submitted by hugo697 235 days ago