//native
type Codat = {
encodedKey: string
}
type Base64 = string
/**
* Upload bill credit note attachment
* ---
stoplight-id: c26f5b1b19168
---
The *Upload bill credit note attachment* endpoint uploads an attachment and assigns it against a specific `billCreditNoteId`.
[Bill Credit Notes](https://docs.codat.io/accounting-api#/schemas/BillCreditNote) are issued by a supplier for the purpose of recording credit.
**Integration-specific behaviour**
For more details on supported file types by integration see [Attachments](https://docs.codat.io/accounting-api#/schemas/Attachment).
*/
export async function main(
auth: Codat,
companyId: string,
connectionId: string,
billCreditNoteId: string,
body: {
file: {
base64: Base64
type:
| 'image/png'
| 'image/jpeg'
| 'image/gif'
| 'application/pdf'
| 'appication/json'
| 'text/csv'
| 'text/plain'
| 'audio/mpeg'
| 'audio/wav'
| 'video/mp4'
name: string
}
}
) {
const url = new URL(
`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/billCreditNotes/${billCreditNoteId}/attachment`
)
const formData = new FormData()
for (const [k, v] of Object.entries(body)) {
if (v !== undefined) {
if (['file'].includes(k)) {
const { base64, type, name } = v as {
base64: Base64
type: string
name: string
}
formData.append(
k,
new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
type
}),
name
)
} else {
formData.append(k, String(v))
}
}
}
const response = await fetch(url, {
method: 'POST',
headers: {
Authorization: `Basic ${auth.encodedKey}`
},
body: formData
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.text()
}
Submitted by hugo697 235 days ago