Edits history of script submission #17751 for ' List absence periods. (personio)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Personio = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * List absence periods.
     * - This endpoint returns a list of absence periods.
    - It supports pagination and filtering based on various parameters.
    - Filters are combined using the logical "AND" operator, which requires that all conditions be true for the output to be included - except when filtering by date ranges, as in this case it'll match absences overlapping the provided period.
    - This endpoint requires the personio:absences:read scope.
    
     */
    export async function main(
    	auth: Personio,
    	limit: string | undefined,
    	cursor: string | undefined,
    	id: string | undefined,
    	absence_type_id: string | undefined,
    	person_id: string | undefined,
    	starts_from_date_time_gte: string | undefined,
    	starts_from_date_time_lte: string | undefined,
    	ends_at_date_time_lte: string | undefined,
    	ends_at_date_time_gte: string | undefined,
    	updated_at_gte: string | undefined,
    	updated_at_lte: string | undefined,
    	Beta: string
    ) {
    	const url = new URL(`https://api.personio.de/v2/absence-periods`)
    	for (const [k, v] of [
    		['limit', limit],
    		['cursor', cursor],
    		['id', id],
    		['absence_type.id', absence_type_id],
    		['person.id', person_id],
    		['starts_from.date_time.gte', starts_from_date_time_gte],
    		['starts_from.date_time.lte', starts_from_date_time_lte],
    		['ends_at.date_time.lte', ends_at_date_time_lte],
    		['ends_at.date_time.gte', ends_at_date_time_gte],
    		['updated_at.gte', updated_at_gte],
    		['updated_at.lte', updated_at_lte]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Beta: Beta,
    			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