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