✨ 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:
44
src/routes/api/newsletter.ts
Normal file
44
src/routes/api/newsletter.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user