1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Deletes an absence period by ID. |
8 | * Deletes an absence period by ID. |
9 | */ |
10 | export async function main(auth: Personio, id: string, Beta: string) { |
11 | const url = new URL(`https://api.personio.de/v2/absence-periods/${id}`) |
12 |
|
13 | const response = await fetch(url, { |
14 | method: 'DELETE', |
15 | headers: { |
16 | Beta: Beta, |
17 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
18 | }, |
19 | body: undefined |
20 | }) |
21 | if (!response.ok) { |
22 | const text = await response.text() |
23 | throw new Error(`${response.status} ${text}`) |
24 | } |
25 | return await response.text() |
26 | } |
27 |
|
28 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
29 | const params = new URLSearchParams({ |
30 | grant_type: 'client_credentials', |
31 | client_id: auth.clientId, |
32 | client_secret: auth.clientSecret |
33 | }) |
34 |
|
35 | const response = await fetch(tokenUrl, { |
36 | method: 'POST', |
37 | headers: { |
38 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
39 | 'Content-Type': 'application/x-www-form-urlencoded' |
40 | }, |
41 | body: params.toString() |
42 | }) |
43 |
|
44 | if (!response.ok) { |
45 | const text = await response.text() |
46 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
47 | } |
48 |
|
49 | const data = await response.json() |
50 | return data.access_token |
51 | } |
52 | } |
53 |
|