1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post accounts account capabilities capability |
6 | * Updates an existing Account Capability. Request or remove a capability by updating its requested parameter. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | account: string, |
11 | capability: string, |
12 | body: { expand?: string[]; requested?: boolean } |
13 | ) { |
14 | const url = new URL( |
15 | `https://api.stripe.com/v1/accounts/${account}/capabilities/${capability}` |
16 | ); |
17 |
|
18 | const response = await fetch(url, { |
19 | method: "POST", |
20 | headers: { |
21 | "Content-Type": "application/x-www-form-urlencoded", |
22 | Authorization: "Bearer " + auth.token, |
23 | }, |
24 | body: encodeParams(body), |
25 | }); |
26 | if (!response.ok) { |
27 | const text = await response.text(); |
28 | throw new Error(`${response.status} ${text}`); |
29 | } |
30 | return await response.json(); |
31 | } |
32 |
|
33 | function encodeParams(o: any) { |
34 | function iter(o: any, path: string) { |
35 | if (Array.isArray(o)) { |
36 | o.forEach(function (a) { |
37 | iter(a, path + "[]"); |
38 | }); |
39 | return; |
40 | } |
41 | if (o !== null && typeof o === "object") { |
42 | Object.keys(o).forEach(function (k) { |
43 | iter(o[k], path + "[" + k + "]"); |
44 | }); |
45 | return; |
46 | } |
47 | data.push(path + "=" + o); |
48 | } |
49 | const data: string[] = []; |
50 | Object.keys(o).forEach(function (k) { |
51 | if (o[k] !== undefined) { |
52 | iter(o[k], k); |
53 | } |
54 | }); |
55 | return new URLSearchParams(data.join("&")); |
56 | } |
57 |
|