Post apps secrets delete

Deletes a secret from 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
 * Post apps secrets delete
6
 * Deletes a secret from the secret store by name and scope.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    expand?: string[];
12
    name: string;
13
    scope: { type: "account" | "user"; user?: string; [k: string]: unknown };
14
  }
15
) {
16
  const url = new URL(`https://api.stripe.com/v1/apps/secrets/delete`);
17

18
  const response = await fetch(url, {
19
    method: "POST",
20
    headers: {
21
      "Content-Type": "application/x-www-form-urlencoded",
22
      Authorization: "Bearer " + auth.token,
23
    },
24
    body: encodeParams(body),
25
  });
26
  if (!response.ok) {
27
    const text = await response.text();
28
    throw new Error(`${response.status} ${text}`);
29
  }
30
  return await response.json();
31
}
32

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