Implement newsletter subscription form using API

Switch from SendInBlue to EmailOctopus for newsletter. Now uses their API through the form to handle subscription.
This commit is contained in:
2022-06-23 20:10:55 +02:00
parent dc4eaca312
commit a9869b72be
8 changed files with 179 additions and 46 deletions

View File

@@ -0,0 +1,44 @@
import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit'
// Block GET requests
export async function get({}: RequestEvent): Promise<RequestHandlerOutput> {
return {
status: 403,
body: 'nope!'
}
}
/**
* POST request
*/
export async function post ({ request }: RequestEvent): Promise<RequestHandlerOutput> {
const body = await request.text()
if (body) {
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${import.meta.env.VITE_NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: import.meta.env.VITE_NEWSLETTER_API_TOKEN,
email_address: body,
})
})
const res = await req.json()
if (res && res.email_address && res.status === 'PENDING') {
return {
status: 200,
body: {
code: 'PENDING'
},
}
}
return {
status: 403,
body: res.error,
}
}
}