Get Employee
One script reply has been approved by the moderators Verified

Get employee data by specifying a set of fields. This is suitable for getting basic employee information, including current values for fields that are part of a historical table, like job title, or compensation information. See the fields endpoint for a list of possible fields.

Created by hugo697 137 days ago Picked 1 time
Submitted by hugo697 Bun
Verified 137 days ago
1
//native
2
/**
3
 * Get Employee
4
 * Get employee data by specifying a set of fields. This is suitable for getting basic employee information, including current values for fields that are part of a historical table, like job title, or compensation information. See the [fields](ref:metadata-get-a-list-of-fields) endpoint for a list of possible fields.
5
 */
6
export async function main(
7
	auth: RT.BambooHr,
8
	id: string = '0',
9
	fields: string | undefined,
10
	onlyCurrent?: string | undefined,
11
	AcceptHeaderParameter?: string
12
) {
13
	const url = new URL(`https://${auth.companyDomain}.bamboohr.com/api/v1/employees/${id}`)
14
	for (const [k, v] of [
15
		['fields', fields],
16
		['onlyCurrent', onlyCurrent]
17
	]) {
18
		if (v !== undefined && v !== '') {
19
			url.searchParams.append(k, v)
20
		}
21
	}
22
	const response = await fetch(url, {
23
		method: 'GET',
24
		headers: {
25
			...(AcceptHeaderParameter ? { AcceptHeaderParameter: AcceptHeaderParameter } : {}),
26
			Authorization: 'Basic ' + btoa(`${auth.apiKey}:x`)
27
		},
28
		body: undefined
29
	})
30
	if (!response.ok) {
31
		const text = await response.text()
32
		throw new Error(`${response.status} ${text}`)
33
	}
34
	return await response.json()
35
}
36