Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

400 for invalid email addresses #14

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
};
Loading