1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List Custom Reports |
8 | * This endpoint provides you with metadata about existing custom reports in your Personio account, such as report name, report type, report date / timeframe. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | report_ids: string | undefined, |
13 | status: string | undefined, |
14 | X_Personio_Partner_ID?: string, |
15 | X_Personio_App_ID?: string |
16 | ) { |
17 | const url = new URL(`https://api.personio.de/v1/company/custom-reports/reports`) |
18 | for (const [k, v] of [ |
19 | ['report_ids', report_ids], |
20 | ['status', status] |
21 | ]) { |
22 | if (v !== undefined && v !== '' && k !== undefined) { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: 'GET', |
28 | headers: { |
29 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
30 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
31 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
32 | }, |
33 | body: undefined |
34 | }) |
35 | if (!response.ok) { |
36 | const text = await response.text() |
37 | throw new Error(`${response.status} ${text}`) |
38 | } |
39 | return await response.json() |
40 | } |
41 |
|
42 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
43 | const params = new URLSearchParams({ |
44 | grant_type: 'client_credentials', |
45 | client_id: auth.clientId, |
46 | client_secret: auth.clientSecret |
47 | }) |
48 |
|
49 | const response = await fetch(tokenUrl, { |
50 | method: 'POST', |
51 | headers: { |
52 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
53 | 'Content-Type': 'application/x-www-form-urlencoded' |
54 | }, |
55 | body: params.toString() |
56 | }) |
57 |
|
58 | if (!response.ok) { |
59 | const text = await response.text() |
60 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
61 | } |
62 |
|
63 | const data = await response.json() |
64 | return data.access_token |
65 | } |
66 |
|