-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail2xrbase.js
116 lines (104 loc) · 3.01 KB
/
mail2xrbase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 })
}
const url = new URL(request.url)
if (url.pathname !== '/subscribe/post') {
return new Response('Not Found', { status: 404 })
}
try {
const formData = await request.formData()
const emailValue = formData.get('MERGE0')
if (!emailValue) {
return createErrorResponse('Email is required')
}
const domainValue = 'xrworldweekly.substack.com'
const substackResponse = await fetch('https://substackapi.com/api/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'domain': domainValue,
'email': emailValue
})
})
if (!substackResponse.ok) {
const errorData = await substackResponse.json()
throw new Error(errorData.errors[0] || 'Subscription failed')
}
return createSuccessResponse(emailValue)
} catch (error) {
console.error('Error in subscription process:', error)
return createErrorResponse(error.message || "An unexpected error occurred")
}
}
function createSuccessResponse(email) {
const response = {
"id": "subscriber_" + Math.random().toString(36).substr(2, 9),
"email_address": email,
"unique_email_id": "unique_" + Math.random().toString(36).substr(2, 9),
"email_type": "html",
"status": "subscribed",
"merge_fields": {
"FNAME": "",
"LNAME": "",
},
"stats": {
"avg_open_rate": 0,
"avg_click_rate": 0
},
"ip_signup": "",
"timestamp_signup": new Date().toISOString(),
"ip_opt": "0.0.0.0",
"timestamp_opt": new Date().toISOString(),
"member_rating": 2,
"last_changed": new Date().toISOString(),
"language": "en",
"vip": false,
"email_client": "",
"location": {
"latitude": 0,
"longitude": 0,
"gmtoff": 0,
"dstoff": 0,
"country_code": "",
"timezone": ""
},
"list_id": "your_list_id",
"_links": [
{
"rel": "self",
"href": "https://us20.api.mailchimp.com/3.0/lists/list_id/members/subscriber_id",
"method": "GET",
"targetSchema": "https://us20.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json"
}
]
}
return new Response(JSON.stringify(response), {
status: 200,
headers: { 'Content-Type': 'application/json' }
})
}
function createErrorResponse(errorMessage) {
const response = {
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Invalid Resource",
"status": 400,
"detail": errorMessage,
"instance": "",
"errors": [
{
"field": "email_address",
"message": "Please provide a valid email address."
}
]
}
return new Response(JSON.stringify(response), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}