0

Get radar value list items

by
Published Oct 30, 2023

Returns a list of ValueListItem objects. The objects are sorted in descending order by creation date, with the most recently created object appearing first.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get radar value list items
6
 * Returns a list of ValueListItem objects. The objects are sorted in descending order by creation date, with the most recently created object appearing first.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  created: any,
11
  ending_before: string | undefined,
12
  expand: any,
13
  limit: string | undefined,
14
  starting_after: string | undefined,
15
  value: string | undefined,
16
  value_list: string | undefined
17
) {
18
  const url = new URL(`https://api.stripe.com/v1/radar/value_list_items`);
19
  for (const [k, v] of [
20
    ["ending_before", ending_before],
21
    ["limit", limit],
22
    ["starting_after", starting_after],
23
    ["value", value],
24
    ["value_list", value_list],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  encodeParams({ created, expand }).forEach((v, k) => {
31
    if (v !== undefined && v !== "") {
32
      url.searchParams.append(k, v);
33
    }
34
  });
35
  const response = await fetch(url, {
36
    method: "GET",
37
    headers: {
38
      "Content-Type": "application/x-www-form-urlencoded",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49

50
function encodeParams(o: any) {
51
  function iter(o: any, path: string) {
52
    if (Array.isArray(o)) {
53
      o.forEach(function (a) {
54
        iter(a, path + "[]");
55
      });
56
      return;
57
    }
58
    if (o !== null && typeof o === "object") {
59
      Object.keys(o).forEach(function (k) {
60
        iter(o[k], path + "[" + k + "]");
61
      });
62
      return;
63
    }
64
    data.push(path + "=" + o);
65
  }
66
  const data: string[] = [];
67
  Object.keys(o).forEach(function (k) {
68
    if (o[k] !== undefined) {
69
      iter(o[k], k);
70
    }
71
  });
72
  return new URLSearchParams(data.join("&"));
73
}
74