Post radar value lists

Creates a new ValueList object, which can then be referenced in rules.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post radar value lists
6
 * Creates a new ValueList object, which can then be referenced in rules.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    alias: string;
12
    expand?: string[];
13
    item_type?:
14
      | "card_bin"
15
      | "card_fingerprint"
16
      | "case_sensitive_string"
17
      | "country"
18
      | "customer_id"
19
      | "email"
20
      | "ip_address"
21
      | "sepa_debit_fingerprint"
22
      | "string"
23
      | "us_bank_account_fingerprint";
24
    metadata?: { [k: string]: string };
25
    name: string;
26
  }
27
) {
28
  const url = new URL(`https://api.stripe.com/v1/radar/value_lists`);
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/x-www-form-urlencoded",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: encodeParams(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44

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