1 | |
2 | type Buttondown = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Test Webhook |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Buttondown, |
11 | id: string, |
12 | event_type: |
13 | | "subscriber.created" |
14 | | "subscriber.unsubscribed" |
15 | | "subscriber.changed_email" |
16 | | "subscriber.confirmed" |
17 | | "subscriber.trial_started" |
18 | | "subscriber.trial_ended" |
19 | | "subscriber.type.changed" |
20 | | "subscriber.tags.changed" |
21 | | "subscriber.clicked" |
22 | | "subscriber.opened" |
23 | | "subscriber.paid" |
24 | | "subscriber.churned" |
25 | | "subscriber.updated" |
26 | | "subscriber.deleted" |
27 | | "subscriber.viewed_checkout_page" |
28 | | "subscriber.replied" |
29 | | "subscriber.paused" |
30 | | "subscriber.resumed" |
31 | | "subscriber.responded_to_survey" |
32 | | "subscriber.referred" |
33 | | "subscriber.referred.paid" |
34 | | "subscriber.commented" |
35 | | "email.created" |
36 | | "email.sent" |
37 | | "email.updated" |
38 | | "email.deleted" |
39 | | "email.status.changed" |
40 | | "scheduled_email.converted" |
41 | | "mention.created" |
42 | | "advertising_slot.purchased" |
43 | | "social_mention.created" |
44 | | "export.created" |
45 | | "export.completed" |
46 | | "export.failed" |
47 | | "automation.invoked" |
48 | | "stripe.checkout.session.completed" |
49 | | "stripe.subscription.activated" |
50 | | "stripe.subscription.churning" |
51 | | "stripe.subscription.deactivated" |
52 | | "stripe.customer.updated" |
53 | | "memberful.subscription.created" |
54 | | "memberful.subscription.deleted" |
55 | | "memberful.member.updated" |
56 | | undefined, |
57 | ) { |
58 | const url = new URL(`https://api.buttondown.com/v1/webhooks/${id}/test`); |
59 | for (const [k, v] of [["event_type", event_type]]) { |
60 | if (v !== undefined && v !== "" && k !== undefined) { |
61 | url.searchParams.append(k, v); |
62 | } |
63 | } |
64 | const response = await fetch(url, { |
65 | method: "POST", |
66 | headers: { |
67 | Authorization: "Token " + auth.token, |
68 | }, |
69 | body: undefined, |
70 | }); |
71 | if (!response.ok) { |
72 | const text = await response.text(); |
73 | throw new Error(`${response.status} ${text}`); |
74 | } |
75 | return await response.text(); |
76 | } |
77 |
|