0

Post file links link

by
Published Oct 30, 2023

Updates an existing file link object. Expired links can no longer be updated.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post file links link
6
 * Updates an existing file link object. Expired links can no longer be updated.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  link: string,
11
  body: {
12
    expand?: string[];
13
    expires_at?: "now" | number | "";
14
    metadata?: { [k: string]: string } | "";
15
  }
16
) {
17
  const url = new URL(`https://api.stripe.com/v1/file_links/${link}`);
18

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

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