1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List Allowed Custom Attributes |
8 | * This endpoint is an alias for `/company/employees/attributes`. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | X_Personio_Partner_ID?: string, |
13 | X_Personio_App_ID?: string |
14 | ) { |
15 | const url = new URL(`https://api.personio.de/v1/company/employees/custom-attributes`) |
16 |
|
17 | const response = await fetch(url, { |
18 | method: 'GET', |
19 | headers: { |
20 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
21 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
22 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
23 | }, |
24 | body: undefined |
25 | }) |
26 | if (!response.ok) { |
27 | const text = await response.text() |
28 | throw new Error(`${response.status} ${text}`) |
29 | } |
30 | return await response.text() |
31 | } |
32 |
|
33 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
34 | const params = new URLSearchParams({ |
35 | grant_type: 'client_credentials', |
36 | client_id: auth.clientId, |
37 | client_secret: auth.clientSecret |
38 | }) |
39 |
|
40 | const response = await fetch(tokenUrl, { |
41 | method: 'POST', |
42 | headers: { |
43 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
44 | 'Content-Type': 'application/x-www-form-urlencoded' |
45 | }, |
46 | body: params.toString() |
47 | }) |
48 |
|
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
52 | } |
53 |
|
54 | const data = await response.json() |
55 | return data.access_token |
56 | } |
57 | } |
58 |
|