1 | |
2 | type Mollie = { |
3 | token: string; |
4 | }; |
5 | |
6 | * List invoices |
7 | * Retrieve a list of all your invoices, optionally filtered by year or by invoice reference. |
8 |
|
9 | The results are paginated. |
10 |
|
11 | > 🔑 Access with |
12 | > |
13 | > Access token with **invoices.read** |
14 | */ |
15 | export async function main( |
16 | auth: Mollie, |
17 | reference: string | undefined, |
18 | year: string | undefined, |
19 | month: string | undefined, |
20 | from: string | undefined, |
21 | limit: string | undefined, |
22 | sort: string | undefined, |
23 | ) { |
24 | const url = new URL(`https://api.mollie.com/v2/invoices`); |
25 | for (const [k, v] of [ |
26 | ["reference", reference], |
27 | ["year", year], |
28 | ["month", month], |
29 | ["from", from], |
30 | ["limit", limit], |
31 | ["sort", sort], |
32 | ]) { |
33 | if (v !== undefined && v !== "" && k !== undefined) { |
34 | url.searchParams.append(k, v); |
35 | } |
36 | } |
37 | const response = await fetch(url, { |
38 | method: "GET", |
39 | headers: { |
40 | Authorization: "Bearer " + auth.token, |
41 | }, |
42 | body: undefined, |
43 | }); |
44 | if (!response.ok) { |
45 | const text = await response.text(); |
46 | throw new Error(`${response.status} ${text}`); |
47 | } |
48 | return await response.text(); |
49 | } |
50 |
|