Skip to content

Commit

Permalink
fix multiple recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Mar 21, 2024
1 parent ea922b5 commit 96f4d81
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
13 changes: 4 additions & 9 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { mail } from './mail';
import { params } from './params';

const postApiNotify = async ({ env, request }) => {
const { to, errors = [], ...rest } = await params(request);
const recipients = to?.split(',') ?? [];
recipients.map(async (recipient) => {
await mail({
env,
to: recipient,
...rest,
});
const { errors = [], ...rest } = await params(request);
await mail({
env,
...rest,
});

const response = {
...rest,
...(errors.length && { errors }),
Expand Down
27 changes: 10 additions & 17 deletions src/handlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,19 @@ describe('handlers', () => {
}),
});
await handler({ request });
expect(global.fetch).toHaveBeenCalledTimes(2);

const requestBody = (email) => ({
body: JSON.stringify(
payload({
...input,
to: email,
}),
),
headers: expect.any(Object),
method: 'POST',
});

expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
requestBody('[email protected]'),
);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
requestBody('[email protected]'),
{
body: JSON.stringify(
payload({
...input,
to: '[email protected],[email protected]',
}),
),
headers: expect.any(Object),
method: 'POST',
},
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const payload = ({ subject, to, body, attachments }) => ({
personalizations: [
{
subject,
to: [{ email: to }],
to: to?.split(',').map((recipient) => ({ email: recipient })),
},
],
});
Expand Down
13 changes: 12 additions & 1 deletion src/mail.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeAll } from 'vitest';
import { input } from './test';
import { mail } from './mail';
import { mail, payload } from './mail';

// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
const fixture = {
Expand Down Expand Up @@ -46,3 +46,14 @@ describe('mail', () => {
);
});
});

describe('payload', () => {
it('supports multiple recipients', () => {
expect(
payload({ to: '[email protected],[email protected]' }).personalizations[0].to,
).toStrictEqual([
{ email: '[email protected]' },
{ email: '[email protected]' },
]);
});
});
4 changes: 2 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ main = "src/index.js"
compatibility_date = "2024-03-04"
compatibility_flags = ["nodejs_compat"]
routes = [
{ pattern = "www.cinotify.cc/api/*", zone_id = "8b83258726802e1e77310bd3b08dfefa" }
]
{ pattern = "www.cinotify.cc/api/*", zone_id = "8b83258726802e1e77310bd3b08dfefa" }
]

0 comments on commit 96f4d81

Please sign in to comment.