1 | |
2 | type Codat = { |
3 | encodedKey: string |
4 | } |
5 | |
6 | * Upload data |
7 | * During an active session, use the **Upload data* endpoint to uploads a page of bank accounts or bank transactions data to the session. |
8 |
|
9 | 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. |
10 | */ |
11 | export async function main( |
12 | auth: Codat, |
13 | companyId: string, |
14 | connectionId: string, |
15 | datasetId: string, |
16 | path: 'auth/get' | undefined, |
17 | body: |
18 | | ({ |
19 | id?: string |
20 | name?: string |
21 | informalName?: string |
22 | holder?: string |
23 | type?: 'Unknown' | 'Credit' | 'Debit' |
24 | balance?: { available?: number; current?: number; limit?: number } |
25 | identifiers?: { |
26 | type: 'Credit' | 'Account' | 'Card' | 'Depository' | 'Investment' | 'Loan' | 'Other' |
27 | subtype?: string |
28 | number?: string |
29 | bankCode?: string |
30 | iban?: string |
31 | bic?: string |
32 | maskedAccountNumber?: string |
33 | } |
34 | currency?: string |
35 | institution?: { id?: string; name?: string } |
36 | } & { modifiedDate?: string & {} } & { sourceModifiedDate?: string & {} }) |
37 | | ({ |
38 | results?: { |
39 | id?: string |
40 | accountId?: string |
41 | description?: string |
42 | amount?: number |
43 | currency?: string |
44 | postedDate?: string |
45 | authorizedDate?: string |
46 | code?: |
47 | | 'Unknown' |
48 | | 'Credit' |
49 | | 'Other' |
50 | | 'Fee' |
51 | | 'Payment' |
52 | | 'Cash' |
53 | | 'Transfer' |
54 | | 'Interest' |
55 | | 'Cashback' |
56 | | 'Cheque' |
57 | | 'DirectDebit' |
58 | | 'Purchase' |
59 | | 'StandingOrder' |
60 | | 'Adjustment' |
61 | | 'NotSupported' |
62 | merchantName?: string |
63 | transactionCategoryRef?: { id: string; name?: string } |
64 | } & { modifiedDate?: string & {} } & { |
65 | sourceModifiedDate?: string & {} |
66 | }[] |
67 | } & { |
68 | pageNumber: number |
69 | pageSize: number |
70 | totalResults: number |
71 | _links: { |
72 | self: { href?: string } |
73 | current: { href?: string } |
74 | next?: { href?: string } |
75 | previous?: { href?: string } |
76 | } |
77 | }) |
78 | | {} |
79 | ) { |
80 | const url = new URL( |
81 | `https://api.codat.io/companies/${companyId}/connections/${connectionId}/bankStatements/upload/dataset/${datasetId}/upload` |
82 | ) |
83 | for (const [k, v] of [['path', path]]) { |
84 | if (v !== undefined && v !== '' && k !== undefined) { |
85 | url.searchParams.append(k, v) |
86 | } |
87 | } |
88 |
|
89 | const response = await fetch(url, { |
90 | method: 'POST', |
91 | headers: { |
92 | 'Content-Type': 'application/json', |
93 | Authorization: `Basic ${auth.encodedKey}` |
94 | }, |
95 | body: JSON.stringify(body) |
96 | }) |
97 | if (!response.ok) { |
98 | const text = await response.text() |
99 | throw new Error(`${response.status} ${text}`) |
100 | } |
101 | return await response.text() |
102 | } |
103 |
|