🚧 Migrate to new SvelteKit routing system
A bit annoying but for the best I guess?
This commit is contained in:
29
src/routes/api/newsletter/+server.ts
Normal file
29
src/routes/api/newsletter/+server.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { error } from '@sveltejs/kit'
|
||||
import type { RequestHandler } from './$types'
|
||||
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
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 new Response(JSON.stringify({
|
||||
code: 'PENDING'
|
||||
}))
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
throw error(403, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user