Get apps secrets find

Finds a secret in the secret store by name and scope.

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 apps secrets find
6
 * Finds a secret in the secret store by name and scope.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  expand: any,
11
  name: string | undefined,
12
  scope: any
13
) {
14
  const url = new URL(`https://api.stripe.com/v1/apps/secrets/find`);
15
  for (const [k, v] of [["name", name]]) {
16
    if (v !== undefined && v !== "") {
17
      url.searchParams.append(k, v);
18
    }
19
  }
20
  encodeParams({ expand, scope }).forEach((v, k) => {
21
    if (v !== undefined && v !== "") {
22
      url.searchParams.append(k, v);
23
    }
24
  });
25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      "Content-Type": "application/x-www-form-urlencoded",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39

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