0

List all Accounts

by
Published Apr 8, 2025

Returns a list of your organization's >(s).

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * List all Accounts
7
 * Returns a list of your organization's >(s).
8
 */
9
export async function main(
10
  auth: Persona,
11
  page: any,
12
  fields: string | undefined,
13
  filter: any,
14
  Key_Inflection?: string,
15
  Idempotency_Key?: string,
16
  Persona_Version?: string,
17
) {
18
  const url = new URL(`https://api.withpersona.com/api/v1/accounts`);
19
  for (const [k, v] of [["fields", fields]]) {
20
    if (v !== undefined && v !== "" && k !== undefined) {
21
      url.searchParams.append(k, v);
22
    }
23
  }
24
  encodeParams({ page, filter }).forEach((v, k) => {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  });
29
  const headers: Record<string, string> = {
30
    Authorization: `Bearer ${auth.apiKey}`,
31
  };
32
  if (Key_Inflection) {
33
    headers["Key-Inflection"] = Key_Inflection;
34
  }
35
  if (Idempotency_Key) {
36
    headers["Idempotency-Key"] = Idempotency_Key;
37
  }
38
  if (Persona_Version) {
39
    headers["Persona-Version"] = Persona_Version;
40
  }
41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers,
44
    body: undefined,
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