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