1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List persons. |
8 | * - This endpoint returns a list of persons. |
9 | - The endpoint supports pagination and filtering based on various parameters such as id, first_name, last_name, created_at, and updated_at. |
10 | - Filters are combined using the logical "AND" operator, which requires that all conditions be true for the output to be included. |
11 | - The endpoint requires the personio:persons: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 | email: string | undefined, |
20 | first_name: string | undefined, |
21 | last_name: string | undefined, |
22 | preferred_name: string | undefined, |
23 | created_at: string | undefined, |
24 | created_at_gt: string | undefined, |
25 | created_at_lt: string | undefined, |
26 | updated_at: string | undefined, |
27 | updated_at_gt: string | undefined, |
28 | updated_at_lt: string | undefined |
29 | ) { |
30 | const url = new URL(`https://api.personio.de/v2/persons`) |
31 | for (const [k, v] of [ |
32 | ['limit', limit], |
33 | ['cursor', cursor], |
34 | ['id', id], |
35 | ['email', email], |
36 | ['first_name', first_name], |
37 | ['last_name', last_name], |
38 | ['preferred_name', preferred_name], |
39 | ['created_at', created_at], |
40 | ['created_at.gt', created_at_gt], |
41 | ['created_at.lt', created_at_lt], |
42 | ['updated_at', updated_at], |
43 | ['updated_at.gt', updated_at_gt], |
44 | ['updated_at.lt', updated_at_lt] |
45 | ]) { |
46 | if (v !== undefined && v !== '' && k !== undefined) { |
47 | url.searchParams.append(k, v) |
48 | } |
49 | } |
50 | const response = await fetch(url, { |
51 | method: 'GET', |
52 | headers: { |
53 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
54 | }, |
55 | body: undefined |
56 | }) |
57 | if (!response.ok) { |
58 | const text = await response.text() |
59 | throw new Error(`${response.status} ${text}`) |
60 | } |
61 | return await response.json() |
62 | } |
63 |
|
64 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
65 | const params = new URLSearchParams({ |
66 | grant_type: 'client_credentials', |
67 | client_id: auth.clientId, |
68 | client_secret: auth.clientSecret |
69 | }) |
70 |
|
71 | const response = await fetch(tokenUrl, { |
72 | method: 'POST', |
73 | headers: { |
74 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
75 | 'Content-Type': 'application/x-www-form-urlencoded' |
76 | }, |
77 | body: params.toString() |
78 | }) |
79 |
|
80 | if (!response.ok) { |
81 | const text = await response.text() |
82 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
83 | } |
84 |
|
85 | const data = await response.json() |
86 | return data.access_token |
87 | } |
88 |
|