Skip to content

Commit

Permalink
refactor (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Mar 19, 2024
1 parent b30fc55 commit 49a0c43
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 120,
"tabWidth": 2,
"useTabs": false
}
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { mail } from './mail';

const validate = async (request) => {
const contentType = request.headers.get('content-type');
const required = ['subject', 'to'];
let data = {};
try {
if (request.headers.get('content-type') === 'application/json') {
if (contentType === 'application/json') {
data = (await request.json()) ?? {};
}

if (request.headers.get('content-type') === 'application/x-www-form-urlencoded') {
data = Object.fromEntries(new URLSearchParams((await request.text()) ?? ''));
if (contentType === 'application/x-www-form-urlencoded') {
data = Object.fromEntries(
new URLSearchParams((await request.text()) ?? ''),
);
}
} catch (e) {
data = {};
}

data.errors = required.reduce((acc, el) => (data?.[el] ? acc : [...acc, `missing required parameter '${el}'`]), []);
data.errors = required.reduce(
(acc, el) =>
data?.[el] ? acc : [...acc, `missing required parameter '${el}'`],
[],
);

return data;
};
Expand Down
12 changes: 10 additions & 2 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
import {
env,
createExecutionContext,
waitOnExecutionContext,
SELF,
} from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import worker from '../src';

Expand All @@ -19,7 +24,10 @@ describe('API', () => {
});
const response = await send(request);
expect(await response.json()).toEqual({
errors: ["missing required parameter 'subject'", "missing required parameter 'to'"],
errors: [
"missing required parameter 'subject'",
"missing required parameter 'to'",
],
});
expect(response.status).toEqual(400);
});
Expand Down
38 changes: 20 additions & 18 deletions src/mail.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
const payload = ({ subject, to }) => ({
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
content: [
{
type: 'text/plain',
value: ' ',
},
],
from: {
email: '[email protected]',
},
personalizations: [
{
subject,
to: [{ email: to }],
},
],
});

export const mail = async ({ subject, to, env = {} }) => {
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({
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
content: [
{
type: 'text/plain',
value: ' ',
},
],
from: {
email: '[email protected]',
},
personalizations: [
{
subject,
to: [{ email: to }],
},
],
}),
body: JSON.stringify(payload({ subject, to })),
});
let data;
try {
Expand Down
17 changes: 10 additions & 7 deletions src/mail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ describe('mail', () => {
const to = '[email protected]';
const subject = 'hello';
await mail({ to, subject });
expect(global.fetch).toHaveBeenCalledWith('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: expect.any(String),
'Content-Type': 'application/json',
expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
{
headers: {
Authorization: expect.any(String),
'Content-Type': 'application/json',
},
method: 'POST',
body: expect.stringMatching(/hello(.*)ex@mple.com/),
},
method: 'POST',
body: expect.stringMatching(/hello(.*)ex@mple.com/),
});
);
});
});

0 comments on commit 49a0c43

Please sign in to comment.