44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
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,
|
|
}
|
|
}
|
|
} |