1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List absence periods. |
8 | * - This endpoint returns a list of absence periods. |
9 | - It supports pagination and filtering based on various parameters. |
10 | - Filters are combined using the logical "AND" operator, which requires that all conditions be true for the output to be included - except when filtering by date ranges, as in this case it'll match absences overlapping the provided period. |
11 | - This endpoint requires the personio:absences:read scope. |
12 |
|
13 | */ |
14 | export async function main( |
15 | auth: Personio, |
16 | limit: string | undefined, |
17 | cursor: string | undefined, |
18 | id: string | undefined, |
19 | absence_type_id: string | undefined, |
20 | person_id: string | undefined, |
21 | starts_from_date_time_gte: string | undefined, |
22 | starts_from_date_time_lte: string | undefined, |
23 | ends_at_date_time_lte: string | undefined, |
24 | ends_at_date_time_gte: string | undefined, |
25 | updated_at_gte: string | undefined, |
26 | updated_at_lte: string | undefined, |
27 | Beta: string |
28 | ) { |
29 | const url = new URL(`https://api.personio.de/v2/absence-periods`) |
30 | for (const [k, v] of [ |
31 | ['limit', limit], |
32 | ['cursor', cursor], |
33 | ['id', id], |
34 | ['absence_type.id', absence_type_id], |
35 | ['person.id', person_id], |
36 | ['starts_from.date_time.gte', starts_from_date_time_gte], |
37 | ['starts_from.date_time.lte', starts_from_date_time_lte], |
38 | ['ends_at.date_time.lte', ends_at_date_time_lte], |
39 | ['ends_at.date_time.gte', ends_at_date_time_gte], |
40 | ['updated_at.gte', updated_at_gte], |
41 | ['updated_at.lte', updated_at_lte] |
42 | ]) { |
43 | if (v !== undefined && v !== '' && k !== undefined) { |
44 | url.searchParams.append(k, v) |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: 'GET', |
49 | headers: { |
50 | Beta: Beta, |
51 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
52 | }, |
53 | body: undefined |
54 | }) |
55 | if (!response.ok) { |
56 | const text = await response.text() |
57 | throw new Error(`${response.status} ${text}`) |
58 | } |
59 | return await response.json() |
60 | } |
61 |
|
62 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
63 | const params = new URLSearchParams({ |
64 | grant_type: 'client_credentials', |
65 | client_id: auth.clientId, |
66 | client_secret: auth.clientSecret |
67 | }) |
68 |
|
69 | const response = await fetch(tokenUrl, { |
70 | method: 'POST', |
71 | headers: { |
72 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
73 | 'Content-Type': 'application/x-www-form-urlencoded' |
74 | }, |
75 | body: params.toString() |
76 | }) |
77 |
|
78 | if (!response.ok) { |
79 | const text = await response.text() |
80 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
81 | } |
82 |
|
83 | const data = await response.json() |
84 | return data.access_token |
85 | } |
86 |
|