Skip to content

Commit

Permalink
400 for invalid email addresses (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Mar 23, 2024
1 parent e18328f commit ac25e35
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import { params } from './params';

const postApiNotify = async ({ env, request }) => {
const { errors = [], ...rest } = await params(request);
await mail({
env,
...rest,
});
try {
await mail({
env,
...rest,
});
} catch (e) {
return new Response(JSON.stringify({ error: e.message }), {
headers: {
'Content-Type': 'application/json',
},
status: 400,
});
}

const response = {
...rest,
...(errors.length && { errors }),
Expand Down
35 changes: 33 additions & 2 deletions src/handlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { payload } from './mail';

describe('handlers', () => {
beforeAll(() => {
global.fetch = vi.fn();
global.fetch = vi.fn().mockImplementation(() => new Response());
});

it('400', async () => {
it('400 if missing required parameters', async () => {
const request = new Request('http://example.com/api/notify', {
method: 'POST',
headers: {
Expand All @@ -23,6 +23,37 @@ describe('handlers', () => {
);
});

it('400 if invalid to: address', async () => {
const request = new Request('http://example.com/api/notify', {
method: 'POST',
body: JSON.stringify({ to: 'invalid email', subject: 'valid subject' }),
headers: {
'Content-Type': 'application/json',
},
});

global.fetch.mockImplementationOnce(() => {
return new Response(
JSON.stringify({
errors: [
{
message: 'Does not contain a valid address.',
field: 'personalizations.0.to.0.email',
help: 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to',
},
],
}),
{ status: 400 },
);
});

const response = await handler({ request });
expect(await response.status).toEqual(400);
expect(await response.json()).toEqual(
expect.objectContaining({ error: 'Does not contain a valid address.' }),
);
});

it('404', async () => {
const request = new Request('http://example.com/undefined-route');
const response = await handler({ request });
Expand Down
5 changes: 4 additions & 1 deletion src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export const payload = ({ subject, to, type, body, attachments }) => ({
* @returns Promise<void>
*/
export const mail = async ({ env = {}, ...rest }) => {
await fetch('https://api.sendgrid.com/v3/mail/send', {
const response = await fetch('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${env.SENDGRID_API_KEY}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(payload({ ...rest })),
});
if (response.status > 299) {
throw new Error((await response.json()).errors[0].message);
}
};

0 comments on commit ac25e35

Please sign in to comment.