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