Edits history of script submission #14952 for ' Create account (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Create account
     * The *Create account* endpoint creates a new [account](https://docs.
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	connectionId: string,
    	timeoutInMinutes: string | undefined,
    	body: {
    		nominalCode?: string
    		name?: string
    		description?: string
    		fullyQualifiedCategory?: string
    		fullyQualifiedName?: string
    		currency?: string
    		currentBalance?: number
    		type?: 'Unknown' | 'Asset' | 'Expense' | 'Income' | 'Liability' | 'Equity'
    		status?: 'Unknown' | 'Active' | 'Archived' | 'Pending'
    		isBankAccount?: false | true
    		validDatatypeLinks?: { property?: string; links?: string[] }[]
    		supplementalData?: { content?: {} }
    	}
    ) {
    	const url = new URL(
    		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/accounts`
    	)
    	for (const [k, v] of [['timeoutInMinutes', timeoutInMinutes]]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: `Basic ${auth.encodedKey}`
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago