1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Get treasury outbound transfers |
6 | * Returns a list of OutboundTransfers sent from the specified FinancialAccount. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | ending_before: string | undefined, |
11 | expand: any, |
12 | financial_account: string | undefined, |
13 | limit: string | undefined, |
14 | starting_after: string | undefined, |
15 | status: |
16 | | "canceled" |
17 | | "failed" |
18 | | "posted" |
19 | | "processing" |
20 | | "returned" |
21 | | undefined |
22 | ) { |
23 | const url = new URL(`https://api.stripe.com/v1/treasury/outbound_transfers`); |
24 | for (const [k, v] of [ |
25 | ["ending_before", ending_before], |
26 | ["financial_account", financial_account], |
27 | ["limit", limit], |
28 | ["starting_after", starting_after], |
29 | ["status", status], |
30 | ]) { |
31 | if (v !== undefined && v !== "") { |
32 | url.searchParams.append(k, v); |
33 | } |
34 | } |
35 | encodeParams({ expand }).forEach((v, k) => { |
36 | if (v !== undefined && v !== "") { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | }); |
40 | const response = await fetch(url, { |
41 | method: "GET", |
42 | headers: { |
43 | "Content-Type": "application/x-www-form-urlencoded", |
44 | Authorization: "Bearer " + auth.token, |
45 | }, |
46 | body: undefined, |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|
55 | function encodeParams(o: any) { |
56 | function iter(o: any, path: string) { |
57 | if (Array.isArray(o)) { |
58 | o.forEach(function (a) { |
59 | iter(a, path + "[]"); |
60 | }); |
61 | return; |
62 | } |
63 | if (o !== null && typeof o === "object") { |
64 | Object.keys(o).forEach(function (k) { |
65 | iter(o[k], path + "[" + k + "]"); |
66 | }); |
67 | return; |
68 | } |
69 | data.push(path + "=" + o); |
70 | } |
71 | const data: string[] = []; |
72 | Object.keys(o).forEach(function (k) { |
73 | if (o[k] !== undefined) { |
74 | iter(o[k], k); |
75 | } |
76 | }); |
77 | return new URLSearchParams(data.join("&")); |
78 | } |
79 |
|