0
Get files
One script reply has been approved by the moderators Verified

Returns a list of the files that your account has access to. Stripe sorts and returns the files by their creation dates, placing the most recently created files at the top.

Created by hugo697 314 days ago Viewed 8986 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 314 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get files
6
 * Returns a list of the files that your account has access to. Stripe sorts and returns the files by their creation dates, placing the most recently created files at the top.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  created: any,
11
  ending_before: string | undefined,
12
  expand: any,
13
  limit: string | undefined,
14
  purpose:
15
    | "account_requirement"
16
    | "additional_verification"
17
    | "business_icon"
18
    | "business_logo"
19
    | "customer_signature"
20
    | "dispute_evidence"
21
    | "document_provider_identity_document"
22
    | "finance_report_run"
23
    | "identity_document"
24
    | "identity_document_downloadable"
25
    | "pci_document"
26
    | "selfie"
27
    | "sigma_scheduled_query"
28
    | "tax_document_user_upload"
29
    | "terminal_reader_splashscreen"
30
    | undefined,
31
  starting_after: string | undefined
32
) {
33
  const url = new URL(`https://api.stripe.com/v1/files`);
34
  for (const [k, v] of [
35
    ["ending_before", ending_before],
36
    ["limit", limit],
37
    ["purpose", purpose],
38
    ["starting_after", starting_after],
39
  ]) {
40
    if (v !== undefined && v !== "") {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  encodeParams({ created, expand }).forEach((v, k) => {
45
    if (v !== undefined && v !== "") {
46
      url.searchParams.append(k, v);
47
    }
48
  });
49
  const response = await fetch(url, {
50
    method: "GET",
51
    headers: {
52
      "Content-Type": "application/x-www-form-urlencoded",
53
      Authorization: "Bearer " + auth.token,
54
    },
55
    body: undefined,
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.json();
62
}
63

64
function encodeParams(o: any) {
65
  function iter(o: any, path: string) {
66
    if (Array.isArray(o)) {
67
      o.forEach(function (a) {
68
        iter(a, path + "[]");
69
      });
70
      return;
71
    }
72
    if (o !== null && typeof o === "object") {
73
      Object.keys(o).forEach(function (k) {
74
        iter(o[k], path + "[" + k + "]");
75
      });
76
      return;
77
    }
78
    data.push(path + "=" + o);
79
  }
80
  const data: string[] = [];
81
  Object.keys(o).forEach(function (k) {
82
    if (o[k] !== undefined) {
83
      iter(o[k], k);
84
    }
85
  });
86
  return new URLSearchParams(data.join("&"));
87
}
88