Get accounts account persons

Returns a list of people associated with the account’s legal entity. The people are returned sorted by creation date, with the most recent people appearing first.

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
 * Get accounts account persons
6
 * Returns a list of people associated with the account’s legal entity. The people are returned sorted by creation date, with the most recent people appearing first.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  account: string,
11
  ending_before: string | undefined,
12
  expand: any,
13
  limit: string | undefined,
14
  relationship: any,
15
  starting_after: string | undefined
16
) {
17
  const url = new URL(`https://api.stripe.com/v1/accounts/${account}/persons`);
18
  for (const [k, v] of [
19
    ["ending_before", ending_before],
20
    ["limit", limit],
21
    ["starting_after", starting_after],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  encodeParams({ expand, relationship }).forEach((v, k) => {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  });
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      "Content-Type": "application/x-www-form-urlencoded",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46

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