Edits history of script submission #17757 for ' List persons. (personio)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Personio = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * List persons.
     * - This endpoint returns a list of persons.
    - The endpoint supports pagination and filtering based on various parameters such as id, first_name, last_name, created_at, and updated_at.
      - Filters are combined using the logical "AND" operator, which requires that all conditions be true for the output to be included.
    - The endpoint requires the personio:persons:read scope.
    
     */
    export async function main(
    	auth: Personio,
    	limit: string | undefined,
    	cursor: string | undefined,
    	id: string | undefined,
    	email: string | undefined,
    	first_name: string | undefined,
    	last_name: string | undefined,
    	preferred_name: string | undefined,
    	created_at: string | undefined,
    	created_at_gt: string | undefined,
    	created_at_lt: 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`)
    	for (const [k, v] of [
    		['limit', limit],
    		['cursor', cursor],
    		['id', id],
    		['email', email],
    		['first_name', first_name],
    		['last_name', last_name],
    		['preferred_name', preferred_name],
    		['created_at', created_at],
    		['created_at.gt', created_at_gt],
    		['created_at.lt', created_at_lt],
    		['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