//native
type Personio = {
clientId: string
clientSecret: string
}
/**
* List employments of a given Person.
* Returns a list of employments of a given person. The employments are returned in sorted order, with the most recent employments appearing first.
*/
export async function main(
auth: Personio,
person_id: string,
limit: string | undefined,
cursor: string | undefined,
id: string | undefined,
updated_at: string | undefined,
updated_at_gt: string | undefined,
updated_at_lt: string | undefined
) {
const url = new URL(`https://api.personio.de/v2/persons/${person_id}/employments`)
for (const [k, v] of [
['limit', limit],
['cursor', cursor],
['id', id],
['updated_at', updated_at],
['updated_at.gt', updated_at_gt],
['updated_at.lt', updated_at_lt]
]) {
if (v !== undefined && v !== '' && k !== undefined) {
url.searchParams.append(k, v)
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
},
body: undefined
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.json()
}
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
const params = new URLSearchParams({
grant_type: 'client_credentials',
client_id: auth.clientId,
client_secret: auth.clientSecret
})
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
})
if (!response.ok) {
const text = await response.text()
throw new Error(`OAuth token request failed: ${response.status} ${text}`)
}
const data = await response.json()
return data.access_token
}
Submitted by hugo697 235 days ago