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

By default, returns a list of subscriptions that have not been canceled. In order to list canceled subscriptions, specify status=canceled.

Created by hugo697 419 days ago Viewed 9202 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 419 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get subscriptions
6
 * By default, returns a list of subscriptions that have not been canceled. In order to list canceled subscriptions, specify status=canceled.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  automatic_tax: any,
11
  collection_method: "charge_automatically" | "send_invoice" | undefined,
12
  created: any,
13
  current_period_end: any,
14
  current_period_start: any,
15
  customer: string | undefined,
16
  ending_before: string | undefined,
17
  expand: any,
18
  limit: string | undefined,
19
  price: string | undefined,
20
  starting_after: string | undefined,
21
  status:
22
    | "active"
23
    | "all"
24
    | "canceled"
25
    | "ended"
26
    | "incomplete"
27
    | "incomplete_expired"
28
    | "past_due"
29
    | "paused"
30
    | "trialing"
31
    | "unpaid"
32
    | undefined,
33
  test_clock: string | undefined
34
) {
35
  const url = new URL(`https://api.stripe.com/v1/subscriptions`);
36
  for (const [k, v] of [
37
    ["collection_method", collection_method],
38
    ["customer", customer],
39
    ["ending_before", ending_before],
40
    ["limit", limit],
41
    ["price", price],
42
    ["starting_after", starting_after],
43
    ["status", status],
44
    ["test_clock", test_clock],
45
  ]) {
46
    if (v !== undefined && v !== "") {
47
      url.searchParams.append(k, v);
48
    }
49
  }
50
  encodeParams({
51
    automatic_tax,
52
    created,
53
    current_period_end,
54
    current_period_start,
55
    expand,
56
  }).forEach((v, k) => {
57
    if (v !== undefined && v !== "") {
58
      url.searchParams.append(k, v);
59
    }
60
  });
61
  const response = await fetch(url, {
62
    method: "GET",
63
    headers: {
64
      "Content-Type": "application/x-www-form-urlencoded",
65
      Authorization: "Bearer " + auth.token,
66
    },
67
    body: undefined,
68
  });
69
  if (!response.ok) {
70
    const text = await response.text();
71
    throw new Error(`${response.status} ${text}`);
72
  }
73
  return await response.json();
74
}
75

76
function encodeParams(o: any) {
77
  function iter(o: any, path: string) {
78
    if (Array.isArray(o)) {
79
      o.forEach(function (a) {
80
        iter(a, path + "[]");
81
      });
82
      return;
83
    }
84
    if (o !== null && typeof o === "object") {
85
      Object.keys(o).forEach(function (k) {
86
        iter(o[k], path + "[" + k + "]");
87
      });
88
      return;
89
    }
90
    data.push(path + "=" + o);
91
  }
92
  const data: string[] = [];
93
  Object.keys(o).forEach(function (k) {
94
    if (o[k] !== undefined) {
95
      iter(o[k], k);
96
    }
97
  });
98
  return new URLSearchParams(data.join("&"));
99
}
100