1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List Compensation Types |
8 | * Returns a list of Compensation Types for an authorized company. The types include one-time and recurring Compensation Types. Bonuses should use recurring or one time types. [Click here for more information.](https://support.personio.de/hc/en-us/articles/15465321997853-Alpha-Test-I-The-Payroll-API) |
9 |
|
10 | */ |
11 | export async function main(auth: Personio, limit: string | undefined, cursor: string | undefined) { |
12 | const url = new URL(`https://api.personio.de/v2/compensations/types`) |
13 | for (const [k, v] of [ |
14 | ['limit', limit], |
15 | ['cursor', cursor] |
16 | ]) { |
17 | if (v !== undefined && v !== '' && k !== undefined) { |
18 | url.searchParams.append(k, v) |
19 | } |
20 | } |
21 | const response = await fetch(url, { |
22 | method: 'GET', |
23 | headers: { |
24 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
25 | }, |
26 | body: undefined |
27 | }) |
28 | if (!response.ok) { |
29 | const text = await response.text() |
30 | throw new Error(`${response.status} ${text}`) |
31 | } |
32 | return await response.json() |
33 | } |
34 |
|
35 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
36 | const params = new URLSearchParams({ |
37 | grant_type: 'client_credentials', |
38 | client_id: auth.clientId, |
39 | client_secret: auth.clientSecret |
40 | }) |
41 |
|
42 | const response = await fetch(tokenUrl, { |
43 | method: 'POST', |
44 | headers: { |
45 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
46 | 'Content-Type': 'application/x-www-form-urlencoded' |
47 | }, |
48 | body: params.toString() |
49 | }) |
50 |
|
51 | if (!response.ok) { |
52 | const text = await response.text() |
53 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
54 | } |
55 |
|
56 | const data = await response.json() |
57 | return data.access_token |
58 | } |
59 |
|