1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Retrieve open positions |
8 | * This is the job positions XML feed from the Company Career Site. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | language: 'de' | 'en' | 'fr' | 'es' | 'nl' | 'it' | 'pt' | undefined, |
13 | X_Company_ID: string, |
14 | X_Personio_Partner_ID?: string, |
15 | X_Personio_App_ID?: string |
16 | ) { |
17 | const url = new URL(`https://api.personio.de/xml`) |
18 | for (const [k, v] of [['language', language]]) { |
19 | if (v !== undefined && v !== '' && k !== undefined) { |
20 | url.searchParams.append(k, v) |
21 | } |
22 | } |
23 | const response = await fetch(url, { |
24 | method: 'GET', |
25 | headers: { |
26 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
27 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
28 | 'X-Company-ID': X_Company_ID, |
29 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
30 | }, |
31 | body: undefined |
32 | }) |
33 | if (!response.ok) { |
34 | const text = await response.text() |
35 | throw new Error(`${response.status} ${text}`) |
36 | } |
37 | return await response.text() |
38 | } |
39 |
|
40 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
41 | const params = new URLSearchParams({ |
42 | grant_type: 'client_credentials', |
43 | client_id: auth.clientId, |
44 | client_secret: auth.clientSecret |
45 | }) |
46 |
|
47 | const response = await fetch(tokenUrl, { |
48 | method: 'POST', |
49 | headers: { |
50 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
51 | 'Content-Type': 'application/x-www-form-urlencoded' |
52 | }, |
53 | body: params.toString() |
54 | }) |
55 |
|
56 | if (!response.ok) { |
57 | const text = await response.text() |
58 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
59 | } |
60 |
|
61 | const data = await response.json() |
62 | return data.access_token |
63 | } |
64 | } |
65 |
|