1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List document metadata. |
8 | * Lists the metadata of Documents belonging to the provided owner ID. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | owner_id: string | undefined, |
13 | category_id: string | undefined, |
14 | created_at_gte: string | undefined, |
15 | created_at_lt: string | undefined, |
16 | limit: string | undefined, |
17 | cursor: string | undefined |
18 | ) { |
19 | const url = new URL(`https://api.personio.de/v2/document-management/documents`) |
20 | for (const [k, v] of [ |
21 | ['owner_id', owner_id], |
22 | ['category_id', category_id], |
23 | ['created_at.gte', created_at_gte], |
24 | ['created_at.lt', created_at_lt], |
25 | ['limit', limit], |
26 | ['cursor', cursor] |
27 | ]) { |
28 | if (v !== undefined && v !== '' && k !== undefined) { |
29 | url.searchParams.append(k, v) |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: 'GET', |
34 | headers: { |
35 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
36 | }, |
37 | body: undefined |
38 | }) |
39 | if (!response.ok) { |
40 | const text = await response.text() |
41 | throw new Error(`${response.status} ${text}`) |
42 | } |
43 | return await response.json() |
44 | } |
45 |
|
46 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
47 | const params = new URLSearchParams({ |
48 | grant_type: 'client_credentials', |
49 | client_id: auth.clientId, |
50 | client_secret: auth.clientSecret |
51 | }) |
52 |
|
53 | const response = await fetch(tokenUrl, { |
54 | method: 'POST', |
55 | headers: { |
56 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
57 | 'Content-Type': 'application/x-www-form-urlencoded' |
58 | }, |
59 | body: params.toString() |
60 | }) |
61 |
|
62 | if (!response.ok) { |
63 | const text = await response.text() |
64 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
65 | } |
66 |
|
67 | const data = await response.json() |
68 | return data.access_token |
69 | } |
70 |
|