mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-04-24 23:34:37 +00:00
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m11s
28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
const LISTMONK_URL = process.env.LISTMONK_URL ?? ''
|
|
const LISTMONK_LIST_ID = process.env.LISTMONK_LIST_ID ?? ''
|
|
const LISTMONK_USERNAME = process.env.LISTMONK_USERNAME ?? ''
|
|
const LISTMONK_TOKEN = process.env.LISTMONK_TOKEN ?? ''
|
|
|
|
export async function addSubscriber(email: FormDataEntryValue | null) {
|
|
if (email !== null && LISTMONK_URL !== '') {
|
|
const headers = new Headers()
|
|
headers.append('Content-Type', 'application/json')
|
|
headers.append('Authorization', 'Basic ' + Buffer.from(LISTMONK_USERNAME + ':' + LISTMONK_TOKEN).toString('base64'))
|
|
|
|
const response = await fetch(LISTMONK_URL, {
|
|
method: 'POST',
|
|
headers: headers,
|
|
body: JSON.stringify({
|
|
email: email,
|
|
lists: [parseInt(LISTMONK_LIST_ID)],
|
|
preconfirm_subscriptions: true
|
|
})
|
|
})
|
|
|
|
if (response.status === 409) {
|
|
throw new Error('Already subscribed')
|
|
} else if (response.status !== 200) {
|
|
throw new Error('Server error')
|
|
}
|
|
}
|
|
} |