1 | |
2 | |
3 | * Get Employees |
4 | * 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. |
5 | */ |
6 | export async function main(auth: RT.BambooHr, sort?: string | undefined) { |
7 | const url = new URL(`https://${auth.companyDomain}.bamboohr.com/api/v1/employees`) |
8 | for (const [k, v] of [['sort', sort]]) { |
9 | if (v !== undefined && v !== '') { |
10 | url.searchParams.append(k, v) |
11 | } |
12 | } |
13 | encodeParams({ filter, page }).forEach((v, k) => { |
14 | if (v !== undefined && v !== '') { |
15 | url.searchParams.append(k, v) |
16 | } |
17 | }) |
18 | const response = await fetch(url, { |
19 | method: 'GET', |
20 | headers: { |
21 | Authorization: 'Basic ' + btoa(`${auth.apiKey}:x`) |
22 | }, |
23 | body: undefined |
24 | }) |
25 | if (!response.ok) { |
26 | const text = await response.text() |
27 | throw new Error(`${response.status} ${text}`) |
28 | } |
29 | return await response.json() |
30 | } |
31 |
|
32 | function encodeParams(o: any) { |
33 | function iter(o: any, path: string) { |
34 | if (Array.isArray(o)) { |
35 | o.forEach(function (a) { |
36 | iter(a, path + '[]') |
37 | }) |
38 | return |
39 | } |
40 | if (o !== null && typeof o === 'object') { |
41 | Object.keys(o).forEach(function (k) { |
42 | iter(o[k], path + '[' + k + ']') |
43 | }) |
44 | return |
45 | } |
46 | data.push(path + '=' + o) |
47 | } |
48 | const data: string[] = [] |
49 | Object.keys(o).forEach(function (k) { |
50 | if (o[k] !== undefined) { |
51 | iter(o[k], k) |
52 | } |
53 | }) |
54 | return new URLSearchParams(data.join('&')) |
55 | } |
56 |
|