Edits history of script submission #10501 for ' Uploads a File to the inbox (xero)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xero = {
    	token: string
    }
    /**
     * Uploads a File to the inbox
     *
     */
    export async function main(
    	auth: Xero,
    	xero_tenant_id: string,
    	Idempotency_Key: string,
    	body: { body: string; name: string; filename: string; mimeType?: string }
    ) {
    	const url = new URL(`https://api.xero.com/files.xro/1.0//Files`)
    
    	const formData = new FormData()
    	for (const [k, v] of Object.entries(body)) {
    		if (v !== undefined && v !== '') {
    			formData.append(k, String(v))
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			Accept: 'application/json',
    			'xero-tenant-id': xero_tenant_id,
    			'Idempotency-Key': Idempotency_Key,
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: formData
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 515 days ago