Edits history of script submission #15216 for ' Upload data (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Upload data
     * During an active session, use the **Upload data* endpoint to uploads a page of bank accounts or bank transactions data to the session.
    
    Make sure you created configuration for the account using the [*Set upload configuration*](https://docs.codat.io/lending-api#/operations/set-bank-statement-upload-configuration) endpoint before attempting an upload. 
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	connectionId: string,
    	datasetId: string,
    	path: 'auth/get' | undefined,
    	body:
    		| ({
    				id?: string
    				name?: string
    				informalName?: string
    				holder?: string
    				type?: 'Unknown' | 'Credit' | 'Debit'
    				balance?: { available?: number; current?: number; limit?: number }
    				identifiers?: {
    					type: 'Credit' | 'Account' | 'Card' | 'Depository' | 'Investment' | 'Loan' | 'Other'
    					subtype?: string
    					number?: string
    					bankCode?: string
    					iban?: string
    					bic?: string
    					maskedAccountNumber?: string
    				}
    				currency?: string
    				institution?: { id?: string; name?: string }
    		  } & { modifiedDate?: string & {} } & { sourceModifiedDate?: string & {} })
    		| ({
    				results?: {
    					id?: string
    					accountId?: string
    					description?: string
    					amount?: number
    					currency?: string
    					postedDate?: string
    					authorizedDate?: string
    					code?:
    						| 'Unknown'
    						| 'Credit'
    						| 'Other'
    						| 'Fee'
    						| 'Payment'
    						| 'Cash'
    						| 'Transfer'
    						| 'Interest'
    						| 'Cashback'
    						| 'Cheque'
    						| 'DirectDebit'
    						| 'Purchase'
    						| 'StandingOrder'
    						| 'Adjustment'
    						| 'NotSupported'
    					merchantName?: string
    					transactionCategoryRef?: { id: string; name?: string }
    				} & { modifiedDate?: string & {} } & {
    						sourceModifiedDate?: string & {}
    					}[]
    		  } & {
    				pageNumber: number
    				pageSize: number
    				totalResults: number
    				_links: {
    					self: { href?: string }
    					current: { href?: string }
    					next?: { href?: string }
    					previous?: { href?: string }
    				}
    		  })
    		| {}
    ) {
    	const url = new URL(
    		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/bankStatements/upload/dataset/${datasetId}/upload`
    	)
    	for (const [k, v] of [['path', path]]) {
    		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.text()
    }
    

    Submitted by hugo697 235 days ago