1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post products id |
6 | * Updates the specific product by setting the values of the parameters passed. Any parameters not provided will be left unchanged. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | id: string, |
11 | body: { |
12 | active?: boolean; |
13 | default_price?: string; |
14 | description?: string | ""; |
15 | expand?: string[]; |
16 | features?: { name: string; [k: string]: unknown }[] | ""; |
17 | images?: string[] | ""; |
18 | metadata?: { [k: string]: string } | ""; |
19 | name?: string; |
20 | package_dimensions?: |
21 | | { |
22 | height: number; |
23 | length: number; |
24 | weight: number; |
25 | width: number; |
26 | [k: string]: unknown; |
27 | } |
28 | | ""; |
29 | shippable?: boolean; |
30 | statement_descriptor?: string; |
31 | tax_code?: string | ""; |
32 | unit_label?: string | ""; |
33 | url?: string | ""; |
34 | } |
35 | ) { |
36 | const url = new URL(`https://api.stripe.com/v1/products/${id}`); |
37 |
|
38 | const response = await fetch(url, { |
39 | method: "POST", |
40 | headers: { |
41 | "Content-Type": "application/x-www-form-urlencoded", |
42 | Authorization: "Bearer " + auth.token, |
43 | }, |
44 | body: encodeParams(body), |
45 | }); |
46 | if (!response.ok) { |
47 | const text = await response.text(); |
48 | throw new Error(`${response.status} ${text}`); |
49 | } |
50 | return await response.json(); |
51 | } |
52 |
|
53 | function encodeParams(o: any) { |
54 | function iter(o: any, path: string) { |
55 | if (Array.isArray(o)) { |
56 | o.forEach(function (a) { |
57 | iter(a, path + "[]"); |
58 | }); |
59 | return; |
60 | } |
61 | if (o !== null && typeof o === "object") { |
62 | Object.keys(o).forEach(function (k) { |
63 | iter(o[k], path + "[" + k + "]"); |
64 | }); |
65 | return; |
66 | } |
67 | data.push(path + "=" + o); |
68 | } |
69 | const data: string[] = []; |
70 | Object.keys(o).forEach(function (k) { |
71 | if (o[k] !== undefined) { |
72 | iter(o[k], k); |
73 | } |
74 | }); |
75 | return new URLSearchParams(data.join("&")); |
76 | } |
77 |
|