Post reviews review approve

Approves a Review object, closing it and removing it from the list of reviews.

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 reviews review approve
6
 * Approves a Review object, closing it and removing it from the list of reviews.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  review: string,
11
  body: { expand?: string[] }
12
) {
13
  const url = new URL(`https://api.stripe.com/v1/reviews/${review}/approve`);
14

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

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