bam/lib/listmonk.ts

38 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-10-18 13:12:19 +02:00
import "server-only";
2024-10-18 13:36:15 +02:00
export type listmonkData = {
email: string;
name: string;
status: "enabled" | "blocklisted";
lists: number[];
attribs: {};
};
2024-10-18 14:20:58 +02:00
async function listmonk(data: listmonkData): Promise<string> {
2024-10-18 13:36:15 +02:00
const listmonkUrl = process.env.LISTMONK_URL ?? "http://localhost:9000/api/";
const listmonkUser = process.env.LISTMONK_USER ?? "nouser";
const listmonkPass = process.env.LISTMONK_PASS ?? "nopass";
// Encode the username and password in base64
const credentials = Buffer.from(`${listmonkUser}:${listmonkPass}`).toString("base64");
const options: RequestInit = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${credentials}`,
},
body: JSON.stringify(data),
};
try {
const response = await fetch(`${listmonkUrl}subscribers`, options);
if (!response.ok) {
2024-10-18 14:20:58 +02:00
return "Error. please try again soon.";
2024-10-18 13:36:15 +02:00
}
2024-10-18 14:20:58 +02:00
return "Thanks for signing up!";
2024-10-18 13:36:15 +02:00
} catch (error) {
2024-10-18 14:20:58 +02:00
return "Error. please try again soon.";
2024-10-18 13:36:15 +02:00
}
2024-10-18 13:12:19 +02:00
}
export default listmonk;