mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
Remove multi-recipient support from Plunk driver
This commit is contained in:
@@ -52,41 +52,4 @@ describe.skipIf(ALL_TESTS)("plunk", () => {
|
|||||||
expect(response).toBeDefined();
|
expect(response).toBeDefined();
|
||||||
expect(response.success).toBe(true);
|
expect(response.success).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should send to multiple recipients", async () => {
|
|
||||||
const driver = plunkEmail({
|
|
||||||
apiKey: process.env.PLUNK_API_KEY!,
|
|
||||||
from: undefined,
|
|
||||||
});
|
|
||||||
const response = await driver.send(
|
|
||||||
"help@bknd.io",
|
|
||||||
"Multi-recipient Test",
|
|
||||||
"Test email to multiple recipients",
|
|
||||||
{
|
|
||||||
to: ["help@bknd.io", "cameronandrewpak@gmail.com"],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
expect(response).toBeDefined();
|
|
||||||
expect(response.success).toBe(true);
|
|
||||||
expect(response.emails).toHaveLength(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should throw error for more than 5 recipients", async () => {
|
|
||||||
const driver = plunkEmail({
|
|
||||||
apiKey: process.env.PLUNK_API_KEY!,
|
|
||||||
from: undefined,
|
|
||||||
});
|
|
||||||
expect(
|
|
||||||
driver.send("help@bknd.io", "Test", "Test", {
|
|
||||||
to: [
|
|
||||||
"help@bknd.io",
|
|
||||||
"test2@example.com",
|
|
||||||
"test3@example.com",
|
|
||||||
"test4@example.com",
|
|
||||||
"test5@example.com",
|
|
||||||
"test6@example.com",
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
).rejects.toThrow("Plunk supports a maximum of 5 recipients per email");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ export type PlunkEmailOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type PlunkEmailSendOptions = {
|
export type PlunkEmailSendOptions = {
|
||||||
to?: string | string[];
|
|
||||||
subscribed?: boolean;
|
subscribed?: boolean;
|
||||||
name?: string;
|
name?: string;
|
||||||
from?: string;
|
from?: string;
|
||||||
@@ -40,48 +39,25 @@ export const plunkEmail = (
|
|||||||
body: string | { text: string; html: string },
|
body: string | { text: string; html: string },
|
||||||
options?: PlunkEmailSendOptions,
|
options?: PlunkEmailSendOptions,
|
||||||
) => {
|
) => {
|
||||||
// Determine recipients - options.to takes precedence if provided
|
|
||||||
const recipients = options?.to ?? to;
|
|
||||||
|
|
||||||
// Validate recipient count (Plunk max is 5)
|
|
||||||
const recipientArray = Array.isArray(recipients)
|
|
||||||
? recipients
|
|
||||||
: [recipients];
|
|
||||||
if (recipientArray.length > 5) {
|
|
||||||
throw new Error(
|
|
||||||
"Plunk supports a maximum of 5 recipients per email",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build base payload
|
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
to: recipients,
|
from,
|
||||||
|
to,
|
||||||
subject,
|
subject,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle body - Plunk only accepts a single body field
|
|
||||||
if (typeof body === "string") {
|
if (typeof body === "string") {
|
||||||
payload.body = body;
|
payload.body = body;
|
||||||
} else {
|
} else {
|
||||||
// When both text and html are provided, send html (degrades gracefully)
|
|
||||||
payload.body = body.html;
|
payload.body = body.html;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add optional fields from config
|
|
||||||
if (from) {
|
|
||||||
payload.from = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge with additional options (excluding 'to' since we already handled it)
|
|
||||||
const { to: _, ...restOptions } = options ?? {};
|
|
||||||
|
|
||||||
const res = await fetch(host, {
|
const res = await fetch(host, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${config.apiKey}`,
|
Authorization: `Bearer ${config.apiKey}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ ...payload, ...restOptions }),
|
body: JSON.stringify({ ...payload, ...options }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|||||||
Reference in New Issue
Block a user