Post terminal locations location

Updates a Location object by setting the values of the parameters passed. Any parameters not provided will be left unchanged.

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 terminal locations location
6
 * Updates a Location object by setting the values of the parameters passed. Any parameters not provided will be left unchanged.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  location: string,
11
  body: {
12
    address?: {
13
      city?: string;
14
      country?: string;
15
      line1?: string;
16
      line2?: string;
17
      postal_code?: string;
18
      state?: string;
19
      [k: string]: unknown;
20
    };
21
    configuration_overrides?: string | "";
22
    display_name?: string;
23
    expand?: string[];
24
    metadata?: { [k: string]: string } | "";
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.stripe.com/v1/terminal/locations/${location}`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/x-www-form-urlencoded",
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: encodeParams(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45

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