Edits history of script submission #15085 for ' Get payment method analysis (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Get payment method analysis
     * The *List payment methods* endpoint returns a [payment method analysis](https://docs.
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	reportId: string,
    	maxAge: string | undefined,
    	page: string | undefined,
    	pageSize: string | undefined,
    	query: string | undefined,
    	orderBy: string | undefined
    ) {
    	const url = new URL(
    		`https://api.codat.io/companies/${companyId}/reports/spendAnalysis/${reportId}/paymentMethods`
    	)
    	for (const [k, v] of [
    		['maxAge', maxAge],
    		['page', page],
    		['pageSize', pageSize],
    		['query', query],
    		['orderBy', orderBy]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: `Basic ${auth.encodedKey}`
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago