Skip to content

Commit

Permalink
support multiple recipients (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Mar 20, 2024
1 parent 9cfe8c0 commit 8e96a45
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
13 changes: 9 additions & 4 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { mail } from './mail';
import { params } from './params';

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

const response = {
...rest,
...(errors.length && { errors }),
Expand Down
62 changes: 50 additions & 12 deletions src/handlers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, beforeAll, vi } from 'vitest';
import { handler } from './handlers';
import { input } from './test';
import { payload } from './mail';

describe('handlers', () => {
beforeAll(() => {
Expand Down Expand Up @@ -28,18 +29,55 @@ describe('handlers', () => {
expect(response.status).toEqual(404);
});

it('postNotify', async () => {
const request = new Request('http://example.com/api/notify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
describe('postNotify', () => {
it('calls the sendgrid api', async () => {
const request = new Request('http://example.com/api/notify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
});
await handler({ request });
expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
expect.anything(),
);
});
it('supports multiple recipients', async () => {
global.fetch = vi.fn();
const request = new Request('http://example.com/api/notify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...input,
to: '[email protected],[email protected]',
}),
});
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]'),
);
});
await handler({ request });
expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
expect.anything(),
);
});
});
2 changes: 1 addition & 1 deletion src/mail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const payload = ({ subject, to, body, attachments }) => ({
export const payload = ({ subject, to, body, attachments }) => ({
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
attachments,
content: [
Expand Down

0 comments on commit 8e96a45

Please sign in to comment.