Switch newsletter form to SvelteKit Form Actions
- remove api endpoint and use latest SK form actions - create reusable server page function for form handling - handling the errors in the server function
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
import { error } from '@sveltejs/kit'
|
||||
import type { RequestHandler } from './$types'
|
||||
import { NEWSLETTER_API_TOKEN, NEWSLETTER_LIST_ID } from '$env/static/private'
|
||||
|
||||
|
||||
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/${NEWSLETTER_LIST_ID}/contacts`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
api_key: NEWSLETTER_API_TOKEN,
|
||||
email_address: body,
|
||||
})
|
||||
})
|
||||
const res = await req.json()
|
||||
|
||||
if (res) {
|
||||
return new Response(JSON.stringify({
|
||||
code: res.error ? res.error.code : res.status
|
||||
}))
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
throw error(403, err.message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user