-
Notifications
You must be signed in to change notification settings - Fork 11
/
mailerlite.js
40 lines (33 loc) · 1.11 KB
/
mailerlite.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
const fetch = require('node-fetch');
const conf = require('ocore/conf.js');
const mailerliteController = async (ctx) => {
const email = String(ctx.request.body.email || '');
try {
if (!email || !email.includes('@')) {
ctx.status = 400;
ctx.body = 'Invalid email';
return;
} else {
const res = await fetch.default(`https://api.mailerlite.com/api/v2/subscribers`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-MailerLite-ApiKey': conf.mailerlite_api_key,
},
body: JSON.stringify({
email: String(email)
}),
});
const resData = await res.json();
if (resData.error) {
ctx.status = resData.error.code;
ctx.body = resData.error_details;
}
ctx.status = 200;
}
} catch (e) {
ctx.body = "Unknown error";
ctx.status = 400;
}
}
exports.mailerliteController = mailerliteController;