1 | |
2 |
|
3 | |
4 | * List Products |
5 | * List products, newest first. `active` filters on active or archived products. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Stripe, |
9 | active: "true" | "false" | undefined, |
10 | limit: number | undefined, |
11 | starting_after: string | undefined, |
12 | ending_before: string | undefined |
13 | ) { |
14 | const url = new URL(`https://api.stripe.com/v1/products`) |
15 | url.search = toForm({ |
16 | active, |
17 | limit, |
18 | starting_after, |
19 | ending_before, |
20 | }).toString() |
21 |
|
22 | const response = await fetch(url, { |
23 | headers: { |
24 | Authorization: `Bearer ${auth.token}`, |
25 | Accept: "application/json", |
26 | "Stripe-Version": "2025-03-31.basil", |
27 | }, |
28 | }) |
29 |
|
30 | if (!response.ok) { |
31 | throw new Error(`${response.status} ${await response.text()}`) |
32 | } |
33 |
|
34 | return await response.json() |
35 | } |
36 |
|
37 | |
38 | function toForm( |
39 | params: { [key: string]: any }, |
40 | prefix = "", |
41 | out = new URLSearchParams() |
42 | ) { |
43 | for (const [key, value] of Object.entries(params)) { |
44 | if (value == null || value === "") continue |
45 | const name = prefix ? `${prefix}[${key}]` : key |
46 | if (typeof value === "object") toForm(value, name, out) |
47 | else out.append(name, String(value)) |
48 | } |
49 | return out |
50 | } |
51 |
|