From 96f4d81bab0a341f1ca5d935d18c12c2169494a5 Mon Sep 17 00:00:00 2001 From: Jesse Shawl Date: Thu, 21 Mar 2024 06:52:46 -0500 Subject: [PATCH] fix multiple recipients --- src/handlers.js | 13 ++++--------- src/handlers.spec.js | 27 ++++++++++----------------- src/mail.js | 2 +- src/mail.test.js | 13 ++++++++++++- wrangler.toml | 4 ++-- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/handlers.js b/src/handlers.js index b58eaa0..9b3484c 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -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 }), diff --git a/src/handlers.spec.js b/src/handlers.spec.js index 7c75b1d..49c71ad 100644 --- a/src/handlers.spec.js +++ b/src/handlers.spec.js @@ -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('first@example.com'), - ); - expect(global.fetch).toHaveBeenCalledWith( - 'https://api.sendgrid.com/v3/mail/send', - requestBody('second@example.com'), + { + body: JSON.stringify( + payload({ + ...input, + to: 'first@example.com,second@example.com', + }), + ), + headers: expect.any(Object), + method: 'POST', + }, ); }); }); diff --git a/src/mail.js b/src/mail.js index 1019a49..e84fd4a 100644 --- a/src/mail.js +++ b/src/mail.js @@ -13,7 +13,7 @@ export const payload = ({ subject, to, body, attachments }) => ({ personalizations: [ { subject, - to: [{ email: to }], + to: to?.split(',').map((recipient) => ({ email: recipient })), }, ], }); diff --git a/src/mail.test.js b/src/mail.test.js index d04af0e..3bf0290 100644 --- a/src/mail.test.js +++ b/src/mail.test.js @@ -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 = { @@ -46,3 +46,14 @@ describe('mail', () => { ); }); }); + +describe('payload', () => { + it('supports multiple recipients', () => { + expect( + payload({ to: 'one@example.com,two@example.com' }).personalizations[0].to, + ).toStrictEqual([ + { email: 'one@example.com' }, + { email: 'two@example.com' }, + ]); + }); +}); diff --git a/wrangler.toml b/wrangler.toml index 134101c..2cf3e7e 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -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" } +] \ No newline at end of file